<commit_before>package docker

import (
	. "atlantis/supervisor/constant"
	"atlantis/supervisor/crypto"
	"atlantis/supervisor/helper"
	"atlantis/supervisor/rpc/types"
	atypes "atlantis/types"
	"fmt"
	"github.com/fsouza/go-dockerclient"
)

func NewDockerPort(port, proto string) docker.Port {
	return docker.Port(fmt.Sprintf("%s/%s", port, proto))
}

func ContainerAppCfgs(c *types.Container) (*atypes.AppConfig, error) {
	var err error
	deps := map[string]map[string]interface{}{}
	if c.Manifest.Deps != nil {
		for name, appDep := range c.Manifest.Deps {
			deps[name], err = crypto.DecryptedAppDepData(appDep)
			if err != nil {
				return nil, err
			}
		}
	}
	return &atypes.AppConfig{
		HTTPPort:       c.PrimaryPort,
		SecondaryPorts: c.SecondaryPorts,
		Container: &atypes.ContainerConfig{
			ID:   c.ID,
			Host: c.Host,
			Env:  c.Env,
		},
		Dependencies: deps,
	}, nil
}

func ContainerDockerCfgs(c *types.Container) (*docker.Config, *docker.HostConfig) {
	// get env cfg
	envs := []string{
		"ATLANTIS=true",
		fmt.Sprintf("CONTAINER_ID=%s", c.ID),
		fmt.Sprintf("CONTAINER_HOST=%s", c.Host),
		fmt.Sprintf("CONTAINER_ENV=%s", c.Env),
		fmt.Sprintf("HTTP_PORT=%d", c.PrimaryPort),
		fmt.Sprintf("SSHD_PORT=%d", c.SSHPort),
	}

	// get port cfg
	exposedPorts := map[docker.Port]struct{}{}
	portBindings := map[docker.Port][]docker.PortBinding{}
	sPrimaryPort := fmt.Sprintf("%d", c.PrimaryPort)
	dPrimaryPort := NewDockerPort(sPrimaryPort, "tcp")
	exposedPorts[dPrimaryPort] = struct{}{}
	portBindings[dPrimaryPort] = []docker.PortBinding{docker.PortBinding{
		HostIp:   "",
		HostPort: sPrimaryPort,
	}}
	sSSHPort := fmt.Sprintf("%d", c.SSHPort)
	dSSHPort := NewDockerPort(sSSHPort, "tcp")
	exposedPorts[dSSHPort] = struct{}{}
	portBindings[dSSHPort] = []docker.PortBinding{docker.PortBinding{
		HostIp:   "",
		HostPort: sSSHPort,
	}}
	for i, port := range c.SecondaryPorts {
		sPort := fmt.Sprintf("%d", port)
		dPort := NewDockerPort(sPort, "tcp")
		exposedPorts[dPort] = struct{}{}
		portBindings[dPort] = []docker.PortBinding{docker.PortBinding{
			HostIp:   "",
			HostPort: sPort,
		}}
		envs = append(envs, fmt.Sprintf("SECONDARY_PORT%d=%d", i, port))
	}

	// setup actual cfg
	dCfg := &docker.Config{
		CpuShares:    int64(c.Manifest.CPUShares),
		Memory:       int64(c.Manifest.MemoryLimit) * int64(1024*1024), // this is in bytes
		MemorySwap:   int64(-1),                                        // -1 turns swap off
		ExposedPorts: exposedPorts,
		Env:          envs,
		Cmd:          []string{
			"/bin/sh",
			"-c",
			"runsvdir",
			"/etc/service",
		},
		Image:        fmt.Sprintf("%s/%s/%s-%s", RegistryHost, c.GetDockerRepo(), c.App, c.Sha),
		Volumes: map[string]struct{}{
			ContainerLogDir:           struct{}{},
			atypes.ContainerConfigDir: struct{}{},
		},
	}
	dHostCfg := &docker.HostConfig{<commit_msg>yay for testing on staging<commit_after>package docker

import (
	. "atlantis/supervisor/constant"
	"atlantis/supervisor/crypto"
	"atlantis/supervisor/helper"
	"atlantis/supervisor/rpc/types"
	atypes "atlantis/types"
	"fmt"
	"github.com/fsouza/go-dockerclient"
)

func NewDockerPort(port, proto string) docker.Port {
	return docker.Port(fmt.Sprintf("%s/%s", port, proto))
}

func ContainerAppCfgs(c *types.Container) (*atypes.AppConfig, error) {
	var err error
	deps := map[string]map[string]interface{}{}
	if c.Manifest.Deps != nil {
		for name, appDep := range c.Manifest.Deps {
			deps[name], err = crypto.DecryptedAppDepData(appDep)
			if err != nil {
				return nil, err
			}
		}
	}
	return &atypes.AppConfig{
		HTTPPort:       c.PrimaryPort,
		SecondaryPorts: c.SecondaryPorts,
		Container: &atypes.ContainerConfig{
			ID:   c.ID,
			Host: c.Host,
			Env:  c.Env,
		},
		Dependencies: deps,
	}, nil
}

func ContainerDockerCfgs(c *types.Container) (*docker.Config, *docker.HostConfig) {
	// get env cfg
	envs := []string{
		"ATLANTIS=true",
		fmt.Sprintf("CONTAINER_ID=%s", c.ID),
		fmt.Sprintf("CONTAINER_HOST=%s", c.Host),
		fmt.Sprintf("CONTAINER_ENV=%s", c.Env),
		fmt.Sprintf("HTTP_PORT=%d", c.PrimaryPort),
		fmt.Sprintf("SSHD_PORT=%d", c.SSHPort),
	}

	// get port cfg
	exposedPorts := map[docker.Port]struct{}{}
	portBindings := map[docker.Port][]docker.PortBinding{}
	sPrimaryPort := fmt.Sprintf("%d", c.PrimaryPort)
	dPrimaryPort := NewDockerPort(sPrimaryPort, "tcp")
	exposedPorts[dPrimaryPort] = struct{}{}
	portBindings[dPrimaryPort] = []docker.PortBinding{docker.PortBinding{
		HostIp:   "",
		HostPort: sPrimaryPort,
	}}
	sSSHPort := fmt.Sprintf("%d", c.SSHPort)
	dSSHPort := NewDockerPort(sSSHPort, "tcp")
	exposedPorts[dSSHPort] = struct{}{}
	portBindings[dSSHPort] = []docker.PortBinding{docker.PortBinding{
		HostIp:   "",
		HostPort: sSSHPort,
	}}
	for i, port := range c.SecondaryPorts {
		sPort := fmt.Sprintf("%d", port)
		dPort := NewDockerPort(sPort, "tcp")
		exposedPorts[dPort] = struct{}{}
		portBindings[dPort] = []docker.PortBinding{docker.PortBinding{
			HostIp:   "",
			HostPort: sPort,
		}}
		envs = append(envs, fmt.Sprintf("SECONDARY_PORT%d=%d", i, port))
	}

	// setup actual cfg
	dCfg := &docker.Config{
		CpuShares:    int64(c.Manifest.CPUShares),
		Memory:       int64(c.Manifest.MemoryLimit) * int64(1024*1024), // this is in bytes
		MemorySwap:   int64(-1),                                        // -1 turns swap off
		ExposedPorts: exposedPorts,
		Env:          envs,
		Cmd:          []string{
			"runsvdir",
			"/etc/service",
		},
		Image:        fmt.Sprintf("%s/%s/%s-%s", RegistryHost, c.GetDockerRepo(), c.App, c.Sha),
		Volumes: map[string]struct{}{
			ContainerLogDir:           struct{}{},
			atypes.ContainerConfigDir: struct{}{},
		},
	}
	dHostCfg := &docker.HostConfig{
{'fraction_non_alphanumeric': 0.165666901905434, 'fraction_numerical': 0.006351446718419196, 'mean_word_length': 3.8370307167235493, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '814191', 'n_tokens_mistral': 2335, 'n_tokens_neox': 2137, 'n_words': 371}