-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtools.go
462 lines (388 loc) · 11.4 KB
/
tools.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package main
import (
"archive/tar"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/user"
"path"
"path/filepath"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/rosti-cz/cli/src/parser"
"github.com/rosti-cz/cli/src/rostiapi"
"github.com/rosti-cz/cli/src/state"
"github.com/urfave/cli/v2"
)
func createArchive(source, target string, exclude []string) error {
tarfile, err := os.Create(target)
if err != nil {
return err
}
defer tarfile.Close()
tarball := tar.NewWriter(tarfile)
defer tarball.Close()
info, err := os.Stat(source)
if err != nil {
return nil
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source,
func(path string, info os.FileInfo, err error) error {
for _, excludedItem := range exclude {
if info.IsDir() && info.Name() == excludedItem {
return filepath.SkipDir
} else if info.Name() == excludedItem {
return nil
}
}
if err != nil {
return err
}
header, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if err := tarball.WriteHeader(header); err != nil {
return err
}
if info.IsDir() || !info.Mode().IsRegular() { // IsRegular is added because without it it fails on Mac
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(tarball, file)
return err
})
}
// Returns list of SSH key found in the current system.
// It looks for the keys in ~/.ssh which should be valid for Linux, Mac and possibly Windows.
// The function returns paths to the private key, public key and error if there is any
func findSSHKey() (string, string, error) {
dirname, err := os.UserHomeDir()
if err != nil {
return "", "", fmt.Errorf("getting user info error: %w", err)
}
sshKeysDirectory := path.Join(dirname, ".ssh")
keysFilenames, err := getLocalSSHKeys(sshKeysDirectory)
if err != nil {
return "", "", fmt.Errorf("loading local SSH keys error: %w", err)
}
user, err := user.Current()
if err != nil {
return "", "", fmt.Errorf("getting user info error: %w", err)
}
// Manually entered key
if len(keysFilenames) == 0 {
cRed.Println("No local SSH key found. Please enter path to your private key manually: ")
cYellow.Print("> ")
var keyPath string
_, err := fmt.Scanln(&keyPath)
if err != nil {
return "", "", fmt.Errorf("reading user input error: %w", err)
}
keyPath = strings.Replace(keyPath, "~", user.HomeDir, 1)
_, err = os.Stat(keyPath)
if os.IsNotExist(err) {
return "", "", fmt.Errorf("file %s does not exist", keyPath)
}
_, err = os.Stat(keyPath + ".pub")
if os.IsNotExist(err) {
return "", "", fmt.Errorf("file %s.pub does not exist", keyPath)
}
return keyPath, keyPath + ".pub", nil
}
// If there is only one discovered key
if len(keysFilenames) == 1 {
}
// Select one of the discovered keys
cWhite.Printf("Following keys were discovered in %s.\n", sshKeysDirectory)
cWhite.Println("Please select one: ")
fmt.Println("")
cGrey.Printf(" %6s Key name\n", "ID")
cGrey.Printf(" %6s ------------\n", "------")
var index int = 1
for _, keyFilename := range keysFilenames {
fmt.Printf(" %6s %s\n", cYellow.Sprint(strconv.Itoa(index)), cWhite.Sprint(keyFilename))
index += 1
}
fmt.Println("")
cYellow.Print("> ")
var selectionRaw string
_, err = fmt.Scanln(&selectionRaw)
if err != nil {
return "", "", fmt.Errorf("reading user input error: %w", err)
}
selection, err := strconv.Atoi(selectionRaw)
if err != nil {
return "", "", fmt.Errorf("user input error: %w", err)
}
if selection < 1 || selection > len(keysFilenames) {
cRed.Println("ERROR: Invalid key index entered")
os.Exit(1)
}
privateKeyPath := path.Join(user.HomeDir, ".ssh", keysFilenames[selection-1])
publicKeyPath := path.Join(user.HomeDir, ".ssh", keysFilenames[selection-1]+".pub")
_, err = os.Stat(privateKeyPath)
if os.IsNotExist(err) {
return "", "", fmt.Errorf("file %s does not exist", privateKeyPath)
}
_, err = os.Stat(publicKeyPath)
if os.IsNotExist(err) {
return "", "", fmt.Errorf("file %s does not exist", publicKeyPath)
}
return privateKeyPath, publicKeyPath, nil
}
func readLocalSSHPubKey(publicKeyPath string) (string, error) {
body, err := ioutil.ReadFile(publicKeyPath)
if err != nil {
return "", err
}
return string(body), nil
}
// findCompany returns company ID based the environment
func findCompany(client *rostiapi.Client, appState *state.RostiState, c *cli.Context) (uint, error) {
// When company is forced by a parameter
if c.Int("company") != 0 {
return uint(c.Int("company")), nil
}
// When company is set already
companyID := appState.CompanyID
if companyID != 0 {
return companyID, nil
}
// When company is not set yet
companies, err := client.GetCompanies()
if err != nil {
return 0, err
}
if len(companies) == 0 {
return 0, errors.New("no company found")
}
if len(companies) == 1 {
companyID = companies[0].ID
} else if len(companies) > 1 {
fmt.Println("You have access to multiple companies, pick one of the companies below:")
fmt.Println("")
fmt.Printf(" %6s Company name\n", "ID")
fmt.Printf(" %6s ------------\n", "------")
for _, company := range companies {
fmt.Printf(" %6s %s\n", strconv.Itoa(int(company.ID)), company.Name)
}
fmt.Println("")
fmt.Print("Pick one of the IDs: ")
var companyIDRaw string
_, err := fmt.Scanln(&companyIDRaw)
if err != nil {
return companyID, fmt.Errorf("scanning user's input error: %v", err)
}
companyID_, err := strconv.Atoi(companyIDRaw)
companyID = uint(companyID_)
if err != nil {
return companyID, fmt.Errorf("input error: %v", err)
}
} else {
return companyID, errors.New("no company found")
}
var found bool
for _, company := range companies {
if company.ID == companyID {
found = true
break
}
}
if !found {
return companyID, errors.New("selected company (" + strconv.Itoa(int(companyID)) + ") not found")
}
return companyID, nil
}
// Returns ID of selected application
func selectApp(client *rostiapi.Client) (uint, error) {
apps, err := client.GetApps()
if err != nil {
return 0, fmt.Errorf("listing app error: %v", err)
}
var appID uint
if len(apps) == 0 {
return appID, errors.New("no app found")
} else if len(apps) == 1 {
appID = apps[0].ID
cYellow.Println("WARNING: Only one application found, selecting that one.")
} else {
fmt.Println("Select application to import. There won't be any change to the application\n" +
"but rosti.state file will be generated for the current working directory and\n" +
"you will be able to deploy it as selected app.")
fmt.Println("")
cGrey.Printf(" %6s App name\n", "ID")
cGrey.Printf(" %6s --------\n", "------")
for _, app := range apps {
fmt.Printf(" %6s %s\n", cYellow.Sprint(strconv.Itoa(int(app.ID))), cWhite.Sprint(app.Name))
}
fmt.Println("")
cWhite.Println("Pick one of the IDs")
cGreen.Print("> ")
var appIDRaw string
_, err := fmt.Scanln(&appIDRaw)
if err != nil {
return appID, fmt.Errorf("scanning user's input error: %v", err)
}
appID_, err := strconv.Atoi(appIDRaw)
appID = uint(appID_)
if err != nil {
return appID, fmt.Errorf("input error: %v", err)
}
}
var found bool
for _, app := range apps {
if app.ID == appID {
found = true
break
}
}
if !found {
return appID, fmt.Errorf("selected app (%d) not found", appID)
}
return appID, nil
}
// Selects plan based on Rostifile or default settings
func selectPlan(client *rostiapi.Client, rostifile *parser.Rostifile) (uint, error) {
// TODO: implement something like default plan loaded from the API (needs support in the API)
if rostifile.Plan == "" {
rostifile.Plan = "start+"
}
cYellow.Println(".. loading list of available plans")
plans, err := client.GetPlans()
if err != nil {
return 0, err
}
var planID uint
for _, plan := range plans {
if strings.ToLower(plan.Name) == strings.ToLower(rostifile.Plan) {
planID = plan.ID
}
}
return planID, nil
}
// Selects runtime image based on rostifile
func selectRuntime(client *rostiapi.Client, rostifile *parser.Rostifile) (string, error) {
cYellow.Println(".. loading list of available runtimes")
runtimes, err := client.GetRuntimes()
if err != nil {
return "", err
}
var selectedRuntime string
if len(runtimes) == 0 {
return selectedRuntime, errors.New("no runtime available")
}
// Find default runtime if there is non available
if len(rostifile.Runtime) == 0 {
for _, runtime := range runtimes {
if runtime.Default {
selectedRuntime = runtime.Image
break
}
}
} else {
// Check if selected runtime exists
for _, runtime := range runtimes {
if runtime.Image == rostifile.Runtime {
selectedRuntime = rostifile.Runtime
break
}
}
}
if selectedRuntime == "" {
return selectedRuntime, errors.New("no suitable runtime found, check runtimes command to pick one")
}
return selectedRuntime, nil
}
func printAppStatus(domains []string, status rostiapi.AppStatus, app rostiapi.App, showTechs bool) {
cYellow.Println("The application is available on these domains:")
fmt.Println("")
for _, domain := range domains {
fmt.Println(" * http://" + domain)
}
if len(app.SSHAccess) > 0 {
fmt.Println("")
fmt.Println("")
cYellow.Println("SSH access:")
fmt.Println("")
fmt.Printf(" SSH command: ssh -p %d %s@%s\n", app.SSHAccess[0].Port, app.SSHAccess[0].Username, app.SSHAccess[0].Hostname)
fmt.Printf(" SSH URI: ssh://%s@%s:%d\n", app.SSHAccess[0].Username, app.SSHAccess[0].Hostname, app.SSHAccess[0].Port)
}
fmt.Println("")
fmt.Println("")
cYellow.Println("Current status:")
fmt.Println("")
if status.Running {
cGreen.Println(" Container: running")
} else {
cRed.Println(" Container: NOT running")
}
fmt.Printf(" Memory: %.2f / %.2f MB\n", status.Memory.Usage, status.Memory.Limit)
if status.Storage.Usage >= 0 {
fmt.Printf(" Storage: %.2f / %.2f GB (over limit: %.2f GB)\n", status.Storage.Usage, status.Storage.Limit, status.Storage.OverLimit)
} else {
fmt.Printf(" Storage: - / %.2f GB (over limit: %.2f GB)\n", status.Storage.Limit, status.Storage.OverLimit)
}
if status.DNSStatus {
cGreen.Println(" DNS: all good")
} else {
cYellow.Println(" DNS: records are not set properly or they haven't been propagated to the internet yet")
}
if status.HTTPStatus {
cGreen.Println(" HTTP: all good")
} else {
cRed.Println(" HTTP: application doesn't return 200-like status code")
}
if len(status.Errors) > 0 {
fmt.Println("")
fmt.Println("")
cYellow.Println("Error messages:")
for _, message := range status.Errors {
cRed.Println(" * " + message)
}
}
if len(status.Info) > 0 {
fmt.Println("")
fmt.Println("")
cYellow.Println("Info messages:")
fmt.Println("")
for _, message := range status.Info {
fmt.Println(" * " + message)
}
}
if showTechs {
fmt.Println("")
fmt.Println("")
cYellow.Println("Available technologies:")
fmt.Println("")
for _, tech := range status.Techs {
if tech.Name == status.PrimaryTech.Name && tech.Version == status.PrimaryTech.Version {
fmt.Printf(cGreen.Sprint(" %-10s %s <--\n"), tech.Name, tech.Version)
} else {
fmt.Printf(" %-10s %s\n", tech.Name, tech.Version)
}
}
}
fmt.Println("")
}
// noColor disables color output
func noColor(c *cli.Context) error {
if c.Bool("no-color") {
color.NoColor = true
}
return nil
}