/
container.go
189 lines (152 loc) · 4.81 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package containerenv
import (
"fmt"
"log"
"os/exec"
"os"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/client"
"golang.org/x/net/context"
"k8s.io/kubernetes/pkg/kubectl/util/term"
)
// CreateContainer creates a docker container and returns the id of the container
func CreateContainer(e *Environment) (string, error) {
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
return "", fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
}
config := &container.Config{
Image: e.Image,
Labels: map[string]string{
"jaredallard.containerenv/environment": "true",
"jaredallard.containerenv/environment-name": e.Name,
},
Env: []string{fmt.Sprintf("USERNAME_CONFIG=%s", e.Username)},
}
hostconfig := &container.HostConfig{
Tmpfs: make(map[string]string),
CapAdd: make([]string, 0),
}
if e.SystemD {
log.Println("enabling systemd")
systemdTmpfsMounts := []string{
"/run",
"/run/lock",
"/tmp",
"/sys/fs/cgroup/systemd",
"/var/lib/journal",
}
for _, mount := range systemdTmpfsMounts {
hostconfig.Tmpfs[mount] = ""
}
hostconfig.CapAdd = append(hostconfig.CapAdd, "SYS_ADMIN")
hostconfig.Binds = append(hostconfig.Binds, "/sys/fs/cgroup:/sys/fs/cgroup:ro")
// systemd expects this signal for graceful shutdown
config.StopSignal = "SIGRTMIN+3"
config.Env = append(config.Env, "SYSTEMD_CONFIG=enabled")
}
if e.PulseAudio.Host {
log.Println("using pulseaudio from host")
uid := os.Getuid()
hostconfig.Binds = append(hostconfig.Binds, fmt.Sprintf("/run/user/%d/pulse:/run/user/1000/pulse", uid))
config.Env = append(config.Env, "PULSEAUDIO_CONFIG=HOST")
} else if e.PulseAudio.Containerized {
log.Println("setting up pulseaudio in the container")
hostconfig.Devices = append(hostconfig.Devices, container.DeviceMapping{
PathOnHost: "/dev/snd",
PathInContainer: "/dev/snd",
CgroupPermissions: "rwm",
})
config.Env = append(config.Env, "PULSEAUDIO_CONFIG=CONTAINER")
}
if e.X11.Host {
log.Println("using X11 from the host")
hostconfig.Binds = append(hostconfig.Binds, "/tmp/.X11-unix:/tmp/.X11-unix:ro")
hostconfig.Devices = append(hostconfig.Devices, container.DeviceMapping{
PathOnHost: "/dev/dri",
PathInContainer: "/dev/dri",
CgroupPermissions: "rwm",
})
config.Env = append(config.Env, []string{"X11_CONFIG=HOST", "DISPLAY=:0", "PULSE_SERVER"}...)
} else if e.X11.Containerized {
log.Println("running xorg in the container")
hostconfig.Privileged = true
// TODO: collapse w/ the above definition
hostconfig.Devices = append(hostconfig.Devices, container.DeviceMapping{
PathOnHost: "/dev/dri",
PathInContainer: "/dev/dri",
CgroupPermissions: "rwm",
})
config.Env = append(config.Env, "X11_CONFIG=CONTAINER")
}
resp, err := docker.ContainerCreate(ctx, config, hostconfig, &network.NetworkingConfig{}, e.Name)
if client.IsErrImageNotFound(err) {
} else if err != nil {
return "", err
}
return resp.ID, err
}
// StartContainer starts a container
func StartContainer(id string) error {
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
}
return docker.ContainerStart(ctx, id, types.ContainerStartOptions{})
}
// StopContainer stops a container
func StopContainer(id string) error {
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
return fmt.Errorf("ERROR: couldn't create docker client\n %+v", err)
}
dur := time.Minute * 5
return docker.ContainerStop(ctx, id, &dur)
}
// ListContainers returns a list of environment containers running
func ListContainers() (*[]types.Container, error) {
ctx := context.Background()
docker, err := client.NewEnvClient()
if err != nil {
return nil, fmt.Errorf("ERROR: couldn't create docker client\n%+v", err)
}
f := filters.NewArgs()
f.Add("label", "jaredallard.containerenv/environment=true")
cnts, err := docker.ContainerList(ctx, types.ContainerListOptions{
Filters: f,
})
if err != nil {
return nil, fmt.Errorf("failed to search for environments: %s", err)
}
names := make([]types.Container, len(cnts))
for i, cnt := range cnts {
names[i] = cnt
}
return &names, nil
}
// Exec opens a shell into an environment
// This just wraps docker exec due to not wanting to reimplement that.
func Exec(name string) error {
com := exec.Command("/usr/bin/docker", []string{
"exec",
"-it",
"--user",
"1000",
name,
"bash",
"--login",
}...)
com.Env = os.Environ()
com.Stderr = os.Stderr
com.Stdin = os.Stdin
com.Stdout = os.Stdout
err := (term.TTY{In: os.Stdin, TryDev: true}).Safe(com.Run)
return err
}