-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
90 lines (76 loc) · 2.11 KB
/
list.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
package cmd
import (
"fmt"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(listCmd)
}
var listCmd = &cobra.Command{
Use: "list",
Short: "List all your running clusters",
Long: `All software has versions. This is Eezhee's`,
Run: func(cmd *cobra.Command, args []string) {
listVMs()
},
}
func listVMs() {
clouds := []string{"digitalocean", "linode", "vultr"}
// // see which cloud we have an api token for
// cloud := AppConfig.GetDefaultCloud()
// if len(cloud) == 0 {
// // opps, no api keys specified so can't proceed until resolved
// log.Error("no cloud provider configured. User 'eezhee auth add'")
// return false
// }
numClusters := 0
for _, cloud := range clouds {
manager, err := GetManager(cloud)
if err != nil {
// don't have api key for this cloud
continue
}
// get all VMs in our account
vmInfo, err := manager.ListVMs()
if err != nil {
log.Error(err)
}
// go through all VMs and look for VMs that are tagged with 'eezhee'
if len(vmInfo) > 0 {
fmt.Println(cloud, ":")
for i := range vmInfo {
if len(vmInfo[i].Tags) > 0 {
for _, tag := range vmInfo[i].Tags {
if strings.Compare(tag, "eezhee") == 0 {
// we created this VM
// TODO: this conversion should probalby be done in the provider
var createdTimestamp time.Time
switch cloud {
case "digitalocean":
createdTimestamp, err = time.Parse(time.RFC3339, vmInfo[i].CreatedAt)
case "linode":
createdTimestamp, err = time.Parse("2006-01-02 15:04:05 +0000 UTC", vmInfo[i].CreatedAt)
case "vultr":
createdTimestamp, err = time.Parse(time.RFC3339, vmInfo[i].CreatedAt)
default:
}
if err != nil {
fmt.Println("error: ", err)
continue
}
fmt.Printf(" %s (%s) status: %s created at: %s\n", vmInfo[i].Name, vmInfo[i].ID, vmInfo[i].Status,
createdTimestamp.Format("2006-01-02 15:04"))
numClusters = numClusters + 1
}
}
}
}
}
}
if numClusters == 0 {
fmt.Println("no clusters currently running")
}
}