generic.go 3.75 KB
Newer Older
1
2
3
4
5
6
7
8
package provision

import (
	"bytes"
	"fmt"
	"text/template"

	"github.com/docker/machine/libmachine/auth"
9
	"github.com/docker/machine/libmachine/drivers"
10
11
12
13
14
	"github.com/docker/machine/libmachine/engine"
	"github.com/docker/machine/libmachine/swarm"
)

type GenericProvisioner struct {
15
	SSHCommander
Olivier Gambier's avatar
Lint    
Olivier Gambier committed
16
	OsReleaseID       string
17
18
19
20
21
	DockerOptionsDir  string
	DaemonOptionsFile string
	Packages          []string
	OsReleaseInfo     *OsRelease
	Driver            drivers.Driver
Olivier Gambier's avatar
Lint    
Olivier Gambier committed
22
23
24
	AuthOptions       auth.Options
	EngineOptions     engine.Options
	SwarmOptions      swarm.Options
25
26
}

27
28
29
30
31
32
33
34
type GenericSSHCommander struct {
	Driver drivers.Driver
}

func (sshCmder GenericSSHCommander) SSHCommand(args string) (string, error) {
	return drivers.RunSSHCommandFromDriver(sshCmder.Driver, args)
}

35
func (provisioner *GenericProvisioner) Hostname() (string, error) {
36
	return provisioner.SSHCommand("hostname")
37
38
39
40
41
42
43
44
45
46
47
48
}

func (provisioner *GenericProvisioner) SetHostname(hostname string) error {
	if _, err := provisioner.SSHCommand(fmt.Sprintf(
		"sudo hostname %s && echo %q | sudo tee /etc/hostname",
		hostname,
		hostname,
	)); err != nil {
		return err
	}

	// ubuntu/debian use 127.0.1.1 for non "localhost" loopback hostnames: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution
49
	if _, err := provisioner.SSHCommand(fmt.Sprintf(`
50
51
52
		if ! grep -xq '.*\s%s' /etc/hosts; then
			if grep -xq '127.0.1.1\s.*' /etc/hosts; then
				sudo sed -i 's/^127.0.1.1\s.*/127.0.1.1 %s/g' /etc/hosts;
53
54
55
56
57
			else 
				echo '127.0.1.1 %s' | sudo tee -a /etc/hosts; 
			fi
		fi`,
		hostname,
58
59
60
61
62
63
64
65
66
67
68
69
70
71
		hostname,
		hostname,
	)); err != nil {
		return err
	}

	return nil
}

func (provisioner *GenericProvisioner) GetDockerOptionsDir() string {
	return provisioner.DockerOptionsDir
}

func (provisioner *GenericProvisioner) CompatibleWithHost() bool {
Olivier Gambier's avatar
Lint    
Olivier Gambier committed
72
	return provisioner.OsReleaseInfo.ID == provisioner.OsReleaseID
73
74
}

Olivier Gambier's avatar
Lint    
Olivier Gambier committed
75
func (provisioner *GenericProvisioner) GetAuthOptions() auth.Options {
76
77
78
	return provisioner.AuthOptions
}

79
80
81
82
func (provisioner *GenericProvisioner) GetSwarmOptions() swarm.Options {
	return provisioner.SwarmOptions
}

83
84
85
86
func (provisioner *GenericProvisioner) SetOsReleaseInfo(info *OsRelease) {
	provisioner.OsReleaseInfo = info
}

87
88
89
90
func (provisioner *GenericProvisioner) GetOsReleaseInfo() (*OsRelease, error) {
	return provisioner.OsReleaseInfo, nil
}

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
func (provisioner *GenericProvisioner) GenerateDockerOptions(dockerPort int) (*DockerOptions, error) {
	var (
		engineCfg bytes.Buffer
	)

	driverNameLabel := fmt.Sprintf("provider=%s", provisioner.Driver.DriverName())
	provisioner.EngineOptions.Labels = append(provisioner.EngineOptions.Labels, driverNameLabel)

	engineConfigTmpl := `
DOCKER_OPTS='
-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 }}
'
114
115
{{range .EngineOptions.Env}}export \"{{ printf "%q" . }}\"
{{end}}
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
`
	t, err := template.New("engineConfig").Parse(engineConfigTmpl)
	if err != nil {
		return nil, err
	}

	engineConfigContext := EngineConfigContext{
		DockerPort:    dockerPort,
		AuthOptions:   provisioner.AuthOptions,
		EngineOptions: provisioner.EngineOptions,
	}

	t.Execute(&engineCfg, engineConfigContext)

	return &DockerOptions{
		EngineOptions:     engineCfg.String(),
		EngineOptionsPath: provisioner.DaemonOptionsFile,
	}, nil
}

func (provisioner *GenericProvisioner) GetDriver() drivers.Driver {
	return provisioner.Driver
}