<commit_before>package dockerface

import (
	"fmt"
	"net/http"
	"os"

	log "github.com/Sirupsen/logrus"
	"github.com/ant0ine/go-json-rest/rest"
	"github.com/fsouza/go-dockerclient"
)

const CONTAINER_STOP_TIMEOUT = 10 // seconds

var (
	daemonPort    int = 2375
	daemonTLSPort int = 2376
	dockerClient  *docker.Client
)

func init() {
	var daemonHost string
	var client *docker.Client
	var err error
	certPath := os.Getenv("DOCKER_CERT_PATH")
	if certPath == "" {
		daemonHost = fmt.Sprintf("tcp://%s:%d", getDaemonHostIp(), daemonPort)
		client, err = docker.NewClient(daemonHost)
	} else {
		daemonHost = fmt.Sprintf("tcp://%s:%d", getDaemonHostIp(), daemonTLSPort)
		log.Infof("Docker cert path: %s", certPath)
		ca := fmt.Sprintf("%s/ca.pem", certPath)
		cert := fmt.Sprintf("%s/cert.pem", certPath)
		key := fmt.Sprintf("%s/key.pem", certPath)
		client, err = docker.NewTLSClient(daemonHost, cert, key, ca)
	}

	log.Infof("Docker daemon host: %s", daemonHost)
	if err != nil {
		log.Fatalf("Error initializing docker client: %v", err)
	}
	dockerClient = client
}

type ContainerEndpoint struct{}

func (ce *ContainerEndpoint) Filter(w rest.ResponseWriter, req *rest.Request) {
	containers, err := dockerClient.ListContainers(docker.ListContainersOptions{
		All: req.URL.Query().Get("all") == "true",
	})
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteJson(containers)
}

func (ce *ContainerEndpoint) Start(w rest.ResponseWriter, req *rest.Request) {
	cid := req.PathParam("id")<commit_msg>use DOCKER_TLS_VERIFY as flag for using TLS and add envars for certs and keys<commit_after>package dockerface

import (
	"fmt"
	"net/http"

	log "github.com/Sirupsen/logrus"
	"github.com/ant0ine/go-json-rest/rest"
	"github.com/fsouza/go-dockerclient"
)

const CONTAINER_STOP_TIMEOUT = 10 // seconds

var (
	daemonPort    int = 2375
	daemonTLSPort int = 2376
	dockerClient  *docker.Client
)

func init() {
	var (
		daemonHost string
		client     *docker.Client
		err        error
	)

	// See https://docs.docker.com/reference/commandline/cli/
	tlsVerify := GetEnv("DOCKER_TLS_VERIFY", "")
	if tlsVerify == "" {
		daemonHost = fmt.Sprintf("tcp://%s:%d", getDaemonHostIp(), daemonPort)
		client, err = docker.NewClient(daemonHost)
	} else {
		certPath := GetEnv("DOCKER_CERT_PATH", "")
		if certPath == "" {
			log.Fatal("Using TLS verify but DOCKER_CERT_PATH is empty")
		}

		log.Infof("Docker cert path: %s", certPath)
		ca := GetEnv("TLS_CA", fmt.Sprintf("%s/ca.pem", certPath))
		cert := GetEnv("TLS_CERT", fmt.Sprintf("%s/cert.pem", certPath))
		key := GetEnv("TLS_KEY", fmt.Sprintf("%s/key.pem", certPath))

		daemonHost = fmt.Sprintf("tcp://%s:%d", getDaemonHostIp(), daemonTLSPort)
		client, err = docker.NewTLSClient(daemonHost, cert, key, ca)
	}

	log.Infof("Docker daemon host: %s", daemonHost)
	if err != nil {
		log.Fatalf("Error initializing docker client: %v", err)
	}
	dockerClient = client
}

type ContainerEndpoint struct{}

func (ce *ContainerEndpoint) Filter(w rest.ResponseWriter, req *rest.Request) {
	containers, err := dockerClient.ListContainers(docker.ListContainersOptions{
		All: req.URL.Query().Get("all") == "true",
	})
	if err != nil {
		rest.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.WriteJson(containers)
}

func (ce *ContainerEndpoint) Start(w rest.ResponseWriter, req *rest.Request) {
	cid := req.PathParam("id")
{'fraction_non_alphanumeric': 0.1533508925958443, 'fraction_numerical': 0.006438396254023998, 'mean_word_length': 4.5218093699515345, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '1254727', 'n_tokens_mistral': 1332, 'n_tokens_neox': 1256, 'n_words': 263}