forked from tsuru/tsuru-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockermachine.go
207 lines (190 loc) · 6.65 KB
/
dockermachine.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright 2016 tsuru-client authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package dm
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync/atomic"
"github.com/docker/machine/libmachine/cert"
"github.com/docker/machine/libmachine/mcnutils"
"github.com/tsuru/tsuru/cmd"
"github.com/tsuru/tsuru/iaas/dockermachine"
)
var storeBasePath = cmd.JoinWithUserDir(".tsuru", "installs")
type DockerMachine struct {
name string
storePath string
certsPath string
API dockermachine.DockerMachineAPI
machinesCount uint64
config DockerMachineConfig
}
type DockerMachineConfig struct {
CAPath string `yaml:"ca-path,omitempty"`
DriverOpts *DriverOpts `yaml:"driver,omitempty"`
DockerHubMirror string `yaml:"docker-hub-mirror,omitempty"`
DockerFlags []string `yaml:"docker-flags,omitempty"`
DockerStorageDriver string `yaml:"docker-storage-driver,omitempty"`
DockerInstallURL string `yaml:"docker-install-url,omitempty"`
}
type DriverOpts struct {
Name string
Options map[string]interface{} `yaml:",omitempty"`
}
type MachineProvisioner interface {
ProvisionMachine(map[string]interface{}) (*dockermachine.Machine, error)
}
func NewDockerMachine(config DockerMachineConfig, name string) (*DockerMachine, error) {
storePath := filepath.Join(storeBasePath, name)
certsPath := filepath.Join(storePath, "certs")
dm, err := dockermachine.NewDockerMachine(dockermachine.DockerMachineConfig{
CaPath: config.CAPath,
OutWriter: os.Stdout,
ErrWriter: os.Stderr,
StorePath: storePath,
})
if err != nil {
return nil, err
}
return &DockerMachine{
API: dm,
name: name,
certsPath: certsPath,
storePath: storePath,
config: config,
}, nil
}
func (d *DockerMachine) ProvisionMachine(driverOpts map[string]interface{}) (*dockermachine.Machine, error) {
m, err := d.CreateMachine(driverOpts)
if err != nil {
return nil, fmt.Errorf("error creating machine %s", err)
}
err = d.uploadRegistryCertificate(GetPrivateIP(m), m.Host.Driver.GetSSHUsername(), m.Host)
if err != nil {
return nil, fmt.Errorf("error uploading registry certificates to %s: %s", m.Base.Address, err)
}
return m, nil
}
func (d *DockerMachine) CreateMachine(driverOpts map[string]interface{}) (*dockermachine.Machine, error) {
driverOpts["swarm-master"] = false
driverOpts["swarm-host"] = ""
driverOpts["engine-install-url"] = ""
driverOpts["swarm-discovery"] = ""
mergedOpts := make(map[string]interface{})
for k, v := range d.config.DriverOpts.Options {
mergedOpts[k] = v
}
for k, v := range driverOpts {
mergedOpts[k] = v
}
m, err := d.API.CreateMachine(dockermachine.CreateMachineOpts{
Name: d.generateMachineName(),
DriverName: d.config.DriverOpts.Name,
Params: mergedOpts,
RegistryMirror: d.config.DockerHubMirror,
ArbitraryFlags: d.config.DockerFlags,
DockerEngineStorageDriver: d.config.DockerStorageDriver,
DockerEngineInstallURL: d.config.DockerInstallURL,
})
if err != nil {
return nil, err
}
if m.Host.AuthOptions() != nil {
m.Host.AuthOptions().ServerCertSANs = append(m.Host.AuthOptions().ServerCertSANs, GetPrivateIP(m))
err = m.Host.ConfigureAuth()
if err != nil {
return nil, err
}
}
return m, nil
}
func (d *DockerMachine) generateMachineName() string {
atomic.AddUint64(&d.machinesCount, 1)
return fmt.Sprintf("%s-%d", d.name, atomic.LoadUint64(&d.machinesCount))
}
func nixPathJoin(elem ...string) string {
return strings.Join(elem, "/")
}
func (d *DockerMachine) uploadRegistryCertificate(ip, user string, target sshTarget) error {
registryCertPath := filepath.Join(d.certsPath, "registry-cert.pem")
registryKeyPath := filepath.Join(d.certsPath, "registry-key.pem")
var registryIP string
if _, errReg := os.Stat(registryCertPath); os.IsNotExist(errReg) {
errCreate := d.createRegistryCertificate(ip)
if errCreate != nil {
return errCreate
}
registryIP = ip
} else {
certData, errRead := ioutil.ReadFile(registryCertPath)
if errRead != nil {
return fmt.Errorf("failed to read registry-cert.pem: %s", errRead)
}
block, _ := pem.Decode(certData)
cert, errRead := x509.ParseCertificate(block.Bytes)
if errRead != nil {
return fmt.Errorf("failed to parse registry certificate: %s", errRead)
}
registryIP = cert.IPAddresses[0].String()
}
fmt.Printf("Uploading registry certificate...\n")
certsBasePath := fmt.Sprintf("/home/%s/certs/%s:5000", user, registryIP)
if _, err := target.RunSSHCommand(fmt.Sprintf("mkdir -p %s", certsBasePath)); err != nil {
return err
}
dockerCertsPath := "/etc/docker/certs.d"
if _, err := target.RunSSHCommand(fmt.Sprintf("sudo mkdir -p %s", dockerCertsPath)); err != nil {
return err
}
fileCopies := map[string]string{
registryCertPath: nixPathJoin(certsBasePath, "registry-cert.pem"),
registryKeyPath: nixPathJoin(certsBasePath, "registry-key.pem"),
filepath.Join(d.certsPath, "ca-key.pem"): nixPathJoin(dockerCertsPath, "ca-key.pem"),
filepath.Join(d.certsPath, "ca.pem"): nixPathJoin(dockerCertsPath, "ca.pem"),
filepath.Join(d.certsPath, "cert.pem"): nixPathJoin(dockerCertsPath, "cert.pem"),
filepath.Join(d.certsPath, "key.pem"): nixPathJoin(dockerCertsPath, "key.pem"),
}
for src, dst := range fileCopies {
errWrite := writeRemoteFile(target, src, dst)
if errWrite != nil {
return errWrite
}
}
if _, err := target.RunSSHCommand(fmt.Sprintf("sudo cp -r /home/%s/certs/* %s/", user, dockerCertsPath)); err != nil {
return err
}
if _, err := target.RunSSHCommand(fmt.Sprintf("sudo cat %s/ca.pem | sudo tee -a /etc/ssl/certs/ca-certificates.crt", dockerCertsPath)); err != nil {
return err
}
_, err := target.RunSSHCommand("sudo mkdir -p /var/lib/registry/")
return err
}
func (d *DockerMachine) createRegistryCertificate(hosts ...string) error {
fmt.Printf("Creating registry certificate...\n")
caOrg := mcnutils.GetUsername()
org := caOrg + ".<bootstrap>"
generator := &cert.X509CertGenerator{}
certOpts := &cert.Options{
Hosts: hosts,
CertFile: filepath.Join(d.certsPath, "registry-cert.pem"),
KeyFile: filepath.Join(d.certsPath, "registry-key.pem"),
CAFile: filepath.Join(d.certsPath, "ca.pem"),
CAKeyFile: filepath.Join(d.certsPath, "ca-key.pem"),
Org: org,
Bits: 2048,
SwarmMaster: false,
}
return generator.GenerateCert(certOpts)
}
func (d *DockerMachine) DeleteAll() error {
return d.API.DeleteAll()
}
func (d *DockerMachine) Close() error {
return d.API.Close()
}