forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemd.go
96 lines (79 loc) · 2.74 KB
/
systemd.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
package provision
import (
"bytes"
"fmt"
"text/template"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/provision/serviceaction"
)
type SystemdProvisioner struct {
GenericProvisioner
}
func (p *SystemdProvisioner) String() string {
return "redhat"
}
func NewSystemdProvisioner(osReleaseID string, d drivers.Driver) SystemdProvisioner {
return SystemdProvisioner{
GenericProvisioner{
SSHCommander: GenericSSHCommander{Driver: d},
DockerOptionsDir: "/etc/docker",
DaemonOptionsFile: "/etc/systemd/system/docker.service",
OsReleaseID: osReleaseID,
Packages: []string{
"curl",
},
Driver: d,
},
}
}
func (p *SystemdProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
var (
engineCfg bytes.Buffer
)
driverNameLabel := fmt.Sprintf("provider=%s", p.Driver.DriverName())
p.EngineOptions.Labels = append(p.EngineOptions.Labels, driverNameLabel)
engineConfigTmpl := `[Service]
ExecStart=/usr/bin/docker daemon -H tcp://0.0.0.0:{{.DockerPort}} -H unix:///var/run/docker.sock --storage-driver {{.EngineOptions.StorageDriver}} --tlsverify --tlscacert {{.AuthOptions.CaCertRemotePath}} --tlscert {{.AuthOptions.ServerCertRemotePath}} --tlskey {{.AuthOptions.ServerKeyRemotePath}} {{ range .EngineOptions.Labels }}--label {{.}} {{ end }}{{ range .EngineOptions.InsecureRegistry }}--insecure-registry {{.}} {{ end }}{{ range .EngineOptions.RegistryMirror }}--registry-mirror {{.}} {{ end }}{{ range .EngineOptions.ArbitraryFlags }}--{{.}} {{ end }}
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity
Environment={{range .EngineOptions.Env}}{{ printf "%q" . }} {{end}}
[Install]
WantedBy=multi-user.target
`
t, err := template.New("engineConfig").Parse(engineConfigTmpl)
if err != nil {
return nil, err
}
engineConfigContext := EngineConfigContext{
DockerPort: dockerPort,
AuthOptions: p.AuthOptions,
EngineOptions: p.EngineOptions,
}
t.Execute(&engineCfg, engineConfigContext)
return &DockerOptions{
EngineOptions: engineCfg.String(),
EngineOptionsPath: p.DaemonOptionsFile,
}, nil
}
func (p *SystemdProvisioner) Service(name string, action serviceaction.ServiceAction) error {
reloadDaemon := false
switch action {
case serviceaction.Start, serviceaction.Restart:
reloadDaemon = true
}
// systemd needs reloaded when config changes on disk; we cannot
// be sure exactly when it changes from the provisioner so
// we call a reload on every restart to be safe
if reloadDaemon {
if _, err := p.SSHCommand("sudo systemctl daemon-reload"); err != nil {
return err
}
}
command := fmt.Sprintf("sudo systemctl -f %s %s", action.String(), name)
if _, err := p.SSHCommand(command); err != nil {
return err
}
return nil
}