<commit_before>package engine

import (
	"bufio"
	"fmt"
	"io"
	"strings"
	"sync"
	"time"

	"golang.org/x/net/context"

	"github.com/pottava/docker-webui/app/config"
	"github.com/pottava/docker-webui/app/logs"
	"github.com/pottava/docker-webui/app/misc"
	"github.com/pottava/docker-webui/app/models"

	api "github.com/fsouza/go-dockerclient"
)

// Client represents wrapped api.Client
type Client struct {
	*api.Client
	Conf *models.DockerClient
}

// ContainerMetadata represents docker response
type ContainerMetadata struct {
	Container *api.Container
	Error     error
}

// ImageMetadata represents docker response
type ImageMetadata struct {
	Image *api.Image
	Error error
}

var current *models.DockerClient

var cfg *config.Config
var containerID string
var pullLock sync.Mutex

func init() {
	cfg = config.NewConfig()

	if cfg.PreventSelfStop {
		if candidate, err := misc.ShellExec([]string{"bash", "-c",
			"cat /proc/self/cgroup | grep -o -e 'docker-.*.scope' | head -n 1"}); err == nil {
			candidate = strings.Replace(candidate, "docker-", "", -1)
			candidate = strings.Replace(candidate, ".scope", "", -1)
			containerID = candidate[:64]
			logs.Debug.Printf("Docker container ID: %s", containerID)
		}
	}
	for index, endpoint := range cfg.DockerEndpoints {
		if len(cfg.DockerCertPath) > index {
			Configure(endpoint, cfg.DockerCertPath[index], true)
		} else {
			Configure(endpoint, "", true)
		}
	}
}

// Configure set client's configuration to current
func Configure(endpoint, certPath string, def bool) {
	current = &models.DockerClient{
		Endpoint:  endpoint,
		CertPath:  certPath,
		IsActive:  true,
		IsDefault: def,
	}
	current.Save()<commit_msg>fix a cgroup bug closes #4<commit_after>package engine

import (
	"bufio"
	"fmt"
	"io"
	"regexp"
	"strings"
	"sync"
	"time"

	"golang.org/x/net/context"

	"github.com/pottava/docker-webui/app/config"
	"github.com/pottava/docker-webui/app/logs"
	"github.com/pottava/docker-webui/app/misc"
	"github.com/pottava/docker-webui/app/models"

	api "github.com/fsouza/go-dockerclient"
)

// Client represents wrapped api.Client
type Client struct {
	*api.Client
	Conf *models.DockerClient
}

// ContainerMetadata represents docker response
type ContainerMetadata struct {
	Container *api.Container
	Error     error
}

// ImageMetadata represents docker response
type ImageMetadata struct {
	Image *api.Image
	Error error
}

var current *models.DockerClient

var cfg *config.Config
var containerID string
var pullLock sync.Mutex

func init() {
	cfg = config.NewConfig()

	if cfg.PreventSelfStop {
		if candidate, err := misc.ShellExec([]string{"bash", "-c",
			"cat /proc/self/cgroup | grep -o -e 'docker.*' | head -n 1"}); err == nil {
			pattern := regexp.MustCompile(`^docker[^0-9a-zA-Z]*`)
			candidate = pattern.ReplaceAllString(candidate, "")
			if len(candidate) >= 64 {
				containerID = candidate[:64]
			}
		}
		logs.Debug.Printf("Docker container ID: %s", containerID)
	}
	for index, endpoint := range cfg.DockerEndpoints {
		if len(cfg.DockerCertPath) > index {
			Configure(endpoint, cfg.DockerCertPath[index], true)
		} else {
			Configure(endpoint, "", true)
		}
	}
}

// Configure set client's configuration to current
func Configure(endpoint, certPath string, def bool) {
	current = &models.DockerClient{
		Endpoint:  endpoint,
		CertPath:  certPath,
		IsActive:  true,
		IsDefault: def,
	}
	current.Save()
{'fraction_non_alphanumeric': 0.1344165435745938, 'fraction_numerical': 0.0038404726735598227, 'mean_word_length': 4.023738872403561, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5069545', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1170, 'n_words': 272}