forked from docker/machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filestore.go
186 lines (151 loc) · 4.6 KB
/
filestore.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
package persist
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/docker/machine/libmachine/auth"
"github.com/docker/machine/libmachine/drivers"
"github.com/docker/machine/libmachine/drivers/rpc"
"github.com/docker/machine/libmachine/engine"
"github.com/docker/machine/libmachine/host"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/mcnerror"
"github.com/docker/machine/libmachine/swarm"
"github.com/docker/machine/libmachine/version"
)
type Filestore struct {
Path string
CaCertPath string
CaPrivateKeyPath string
}
func (s Filestore) getMachinesDir() string {
return filepath.Join(s.Path, "machines")
}
func (s Filestore) saveToFile(data []byte, file string) error {
return ioutil.WriteFile(file, data, 0600)
}
func (s Filestore) Save(host *host.Host) error {
// TODO: Does this belong here?
if rpcClientDriver, ok := host.Driver.(*rpcdriver.RpcClientDriver); ok {
data, err := rpcClientDriver.GetConfigRaw()
if err != nil {
return fmt.Errorf("Error getting raw config for driver: %s", err)
}
host.RawDriver = data
}
data, err := json.MarshalIndent(host, "", " ")
if err != nil {
return err
}
hostPath := filepath.Join(s.getMachinesDir(), host.Name)
// Ensure that the directory we want to save to exists.
if err := os.MkdirAll(hostPath, 0700); err != nil {
return err
}
return s.saveToFile(data, filepath.Join(hostPath, "config.json"))
}
func (s Filestore) Remove(name string) error {
hostPath := filepath.Join(s.getMachinesDir(), name)
return os.RemoveAll(hostPath)
}
func (s Filestore) List() ([]*host.Host, error) {
dir, err := ioutil.ReadDir(s.getMachinesDir())
if err != nil && !os.IsNotExist(err) {
return nil, err
}
hosts := []*host.Host{}
for _, file := range dir {
if file.IsDir() && !strings.HasPrefix(file.Name(), ".") {
host, err := s.Load(file.Name())
if err != nil {
log.Errorf("error loading host %q: %s", file.Name(), err)
continue
}
hosts = append(hosts, host)
}
}
return hosts, nil
}
func (s Filestore) Exists(name string) (bool, error) {
_, err := os.Stat(filepath.Join(s.getMachinesDir(), name))
if os.IsNotExist(err) {
return false, nil
} else if err == nil {
return true, nil
}
return false, err
}
func (s Filestore) loadConfig(h *host.Host) error {
data, err := ioutil.ReadFile(filepath.Join(s.getMachinesDir(), h.Name, "config.json"))
if err != nil {
return err
}
// Remember the machine name so we don't have to pass it through each
// struct in the migration.
name := h.Name
migratedHost, migrationPerformed, err := host.MigrateHost(h, data)
if err != nil {
return fmt.Errorf("Error getting migrated host: %s", err)
}
*h = *migratedHost
h.Name = name
// If we end up performing a migration, we should save afterwards so we don't have to do it again on subsequent invocations.
if migrationPerformed {
if err := s.saveToFile(data, filepath.Join(s.getMachinesDir(), h.Name, "config.json.bak")); err != nil {
return fmt.Errorf("Error attempting to save backup after migration: %s", err)
}
if err := s.Save(h); err != nil {
return fmt.Errorf("Error saving config after migration was performed: %s", err)
}
}
return nil
}
func (s Filestore) Load(name string) (*host.Host, error) {
hostPath := filepath.Join(s.getMachinesDir(), name)
if _, err := os.Stat(hostPath); os.IsNotExist(err) {
return nil, mcnerror.ErrHostDoesNotExist{
Name: name,
}
}
host := &host.Host{
Name: name,
}
if err := s.loadConfig(host); err != nil {
return nil, err
}
return host, nil
}
func (s Filestore) NewHost(driver drivers.Driver) (*host.Host, error) {
certDir := filepath.Join(s.Path, "certs")
hostOptions := &host.HostOptions{
AuthOptions: &auth.AuthOptions{
CertDir: certDir,
CaCertPath: filepath.Join(certDir, "ca.pem"),
CaPrivateKeyPath: filepath.Join(certDir, "ca-key.pem"),
ClientCertPath: filepath.Join(certDir, "cert.pem"),
ClientKeyPath: filepath.Join(certDir, "key.pem"),
ServerCertPath: filepath.Join(s.getMachinesDir(), "server.pem"),
ServerKeyPath: filepath.Join(s.getMachinesDir(), "server-key.pem"),
},
EngineOptions: &engine.EngineOptions{
InstallURL: "https://get.docker.com",
StorageDriver: "aufs",
TlsVerify: true,
},
SwarmOptions: &swarm.SwarmOptions{
Host: "tcp://0.0.0.0:3376",
Image: "swarm:latest",
Strategy: "spread",
},
}
return &host.Host{
ConfigVersion: version.ConfigVersion,
Name: driver.GetMachineName(),
Driver: driver,
DriverName: driver.DriverName(),
HostOptions: hostOptions,
}, nil
}