/
client.go
353 lines (284 loc) · 9.62 KB
/
client.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package client
import (
"io/ioutil"
"os"
"path/filepath"
"github.com/getcarina/carina/common"
"github.com/getcarina/libcarina"
"github.com/pkg/errors"
"github.com/ryanuber/go-glob"
)
// Client is the multi-cloud Carina client, which coordinates communication with all Carina-esque clouds
type Client struct {
Cache *Cache
Error error
}
// CarinaHomeDirEnvVar is the environment variable name for carina data, config, etc.
const CarinaHomeDirEnvVar = "CARINA_HOME"
// CloudMakeSwarm is the v1 Carina (make-swarm) cloud type
const CloudMakeSwarm = "make-swarm"
// CloudMakeCOE is the v2 Carina (make-coe) cloud type
const CloudMakeCOE = "public"
// CloudMagnum is the Rackspace Private Cloud Magnum cloud type
const CloudMagnum = "private"
// NewClient builds a new Carina client
func NewClient(cacheEnabled bool) *Client {
client := &Client{}
client.initCache(cacheEnabled)
return client
}
func wrapClientError(err error) error {
if err == nil {
return nil
}
return newClientError(err)
}
func (client *Client) initCache(cacheEnabled bool) {
disableCache := func(err error) {
common.Log.WriteWarning("Unable to initialize cache. Starting fresh!")
common.Log.WriteWarning(err.Error())
client.Cache = &Cache{}
client.Error = CacheUnavailableError{cause: err}
}
if !cacheEnabled {
common.Log.WriteDebug("Cache disabled")
client.Cache = &Cache{}
return
}
bd, err := GetCredentialsDir()
if err != nil {
disableCache(err)
return
}
err = os.MkdirAll(bd, 0777)
if err != nil {
disableCache(errors.Wrap(err, "Unable to create cache directory"))
return
}
path, err := defaultCacheFilename()
if err != nil {
disableCache(err)
return
}
client.Cache = newCache(path)
err = client.Cache.load()
if err != nil {
disableCache(err)
}
}
func (client *Client) buildContainerService(account Account) (common.ClusterService, error) {
client.Cache.apply(account)
return account.NewClusterService(), nil
}
// GetQuotas retrieves the quotas set for the account
func (client *Client) GetQuotas(account Account) (common.Quotas, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
quotas, err := svc.GetQuotas()
return quotas, wrapClientError(err)
}
// CreateCluster creates a new cluster and prints the cluster information
func (client *Client) CreateCluster(account Account, name string, template string, nodes int, waitUntilActive bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.CreateCluster(name, template, nodes)
if waitUntilActive && err == nil {
cluster, err = svc.WaitUntilClusterIsActive(cluster)
}
return cluster, wrapClientError(err)
}
// DownloadClusterCredentials downloads the TLS certificates and configuration scripts for a cluster
func (client *Client) DownloadClusterCredentials(account Account, name string, customPath string) (credentialsPath string, err error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return "", err
}
creds, err := svc.GetClusterCredentials(name)
if err != nil {
return "", wrapClientError(err)
}
credentialsPath, err = buildClusterCredentialsPath(account, name, customPath)
if err != nil {
return "", errors.Wrap(err, "Unable to save downloaded cluster credentials")
}
// Ensure the credentials destination directory exists
if credentialsPath != "." {
err = os.MkdirAll(credentialsPath, 0777)
if err != nil {
return "", err
}
}
for file, fileContents := range creds.Files {
file = filepath.Join(credentialsPath, file)
err = ioutil.WriteFile(file, fileContents, 0600)
if err != nil {
return "", err
}
}
return credentialsPath, nil
}
// GetSourceCommand returns the shell command and appropriate help text to load a cluster's credentials
func (client *Client) GetSourceCommand(account Account, shell string, name string, customPath string) (sourceText string, err error) {
// We are ignoring errors here, and checking lower down if the creds are missing
credentialsPath, _ := buildClusterCredentialsPath(account, name, customPath)
creds := libcarina.LoadCredentialsBundle(credentialsPath)
// Re-download the credentials bundle, if the credentials are invalid
err = creds.Verify()
if err != nil {
common.Log.Debug(err)
common.Log.Debugln("Re-downloading credentials due to missing or invalid credentials bundle.")
credentialsPath, err = client.DownloadClusterCredentials(account, name, customPath)
if err != nil {
return "", err
}
}
shellScriptPath, err := getCredentialScriptPath(credentialsPath, shell)
if err != nil {
return "", err
}
sourceText = sourceHelpString(shellScriptPath, name, shell)
return sourceText, nil
}
// ListClusters retrieves all clusters
func (client *Client) ListClusters(account Account) ([]common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
clusters, err := svc.ListClusters()
return clusters, wrapClientError(err)
}
// ListClusterTemplates retrieves available templates for creating a new cluster
func (client *Client) ListClusterTemplates(account Account, nameFilter string) ([]common.ClusterTemplate, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
templates, err := svc.ListClusterTemplates()
// Filter the templates by name, e.g. Kubernetes*
if err == nil && nameFilter != "" {
common.Log.WriteDebug("Filtering templates by pattern '%s'", nameFilter)
var filteredTemplates []common.ClusterTemplate
for _, template := range templates {
if glob.GlobI(nameFilter, template.GetName()) {
filteredTemplates = append(filteredTemplates, template)
}
}
templates = filteredTemplates
}
return templates, wrapClientError(err)
}
// GetCluster retrieves a cluster
func (client *Client) GetCluster(account Account, name string, waitUntilActive bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.GetCluster(name)
if waitUntilActive && err == nil {
cluster, err = svc.WaitUntilClusterIsActive(cluster)
}
return cluster, wrapClientError(err)
}
// GrowCluster adds nodes to a cluster
func (client *Client) GrowCluster(account Account, name string, nodes int, waitUntilActive bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.GrowCluster(name, nodes)
if waitUntilActive && err == nil {
cluster, err = svc.WaitUntilClusterIsActive(cluster)
}
return cluster, wrapClientError(err)
}
// ResizeCluster resizes the cluster to the specified number of nodes
func (client *Client) ResizeCluster(account Account, name string, nodes int, waitUntilActive bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.ResizeCluster(name, nodes)
if waitUntilActive && err == nil {
cluster, err = svc.WaitUntilClusterIsActive(cluster)
}
return cluster, wrapClientError(err)
}
// RebuildCluster destroys and recreates the cluster
func (client *Client) RebuildCluster(account Account, name string, waitUntilActive bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.RebuildCluster(name)
if waitUntilActive && err == nil {
cluster, err = svc.WaitUntilClusterIsActive(cluster)
}
return cluster, wrapClientError(err)
}
// SetAutoScale adds nodes to a cluster
func (client *Client) SetAutoScale(account Account, name string, value bool) (common.Cluster, error) {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return nil, err
}
cluster, err := svc.SetAutoScale(name, value)
return cluster, wrapClientError(err)
}
// DeleteCluster deletes a cluster
func (client *Client) DeleteCluster(account Account, name string, waitUntilDeleted bool) error {
defer client.Cache.SaveAccount(account)
svc, err := client.buildContainerService(account)
if err != nil {
return err
}
cluster, err := svc.DeleteCluster(name)
if waitUntilDeleted && err == nil {
err = svc.WaitUntilClusterIsDeleted(cluster)
}
if err == nil {
err = client.DeleteClusterCredentials(account, name, "")
}
return wrapClientError(err)
}
// DeleteClusterCredentials removes a cluster's downloaded credentials
func (client *Client) DeleteClusterCredentials(account Account, name string, customPath string) error {
p, err := buildClusterCredentialsPath(account, name, customPath)
if err != nil {
common.Log.WriteWarning("Unable to locate carina config path, not deleteing credentials on disk.")
return err
}
p = filepath.Clean(p)
if p == "" || p == "." || p == "/" {
return errors.New("Path to cluster is empty, the current directory, or a root path, not deleting")
}
_, statErr := os.Stat(p)
if os.IsNotExist(statErr) {
// Assume credentials were never on disk
return nil
}
// If the path exists but not the actual credentials, inform user
_, statErr = os.Stat(filepath.Join(p, "ca.pem"))
if os.IsNotExist(statErr) {
return errors.New("Path to cluster credentials exists but not the ca.pem, not deleting")
}
err = os.RemoveAll(p)
if err != nil {
return errors.Wrap(err, "Unable to delete the credentials on disk")
}
return nil
}