forked from bitcoin-software/nubectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (127 loc) · 3.04 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"reflect"
"strconv"
"time"
)
// return true of dir/file exist
func fileExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return true
}
func applyConfig(pubkey string) {
envir := readCloudConfig()
for nr, vm := range envir.Vm {
fmt.Println(nr)
fmt.Println("creating " + vm.Name)
cpucount, _ := strconv.Atoi(vm.Cpu)
createVM(vm.Image, cpucount, vm.Ram, vm.Disksize, pubkey, vm.Name)
}
for nr, jail := range envir.Container {
fmt.Println(nr)
fmt.Println("creating " + jail.Name)
v := reflect.ValueOf(jail)
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
fmt.Printf("Field: %s\tValue: %v\n", typeOfS.Field(i).Name, v.Field(i).Interface())
}
createJail(jail.Disksize, pubkey, jail.Name)
}
}
func divertConfig(token string) {
envir := readCloudConfig()
for nr, vm := range envir.Vm {
fmt.Println(nr)
fmt.Println("deleting " + vm.Name)
destroyResource(vm.Name, token)
}
for nr, jail := range envir.Container {
fmt.Println(nr)
fmt.Println("deleting " + jail.Name)
destroyResource(jail.Name, token)
}
}
func main() {
var keypath string
if len(os.Args) < 2 {
fmt.Println("no arguments supplied! run 'nubectl --help' to get list of args")
os.Exit(1)
}
flag.Parse()
// keypath: get from args
if len(*cloudkey) > 1 {
keypath = *cloudkey
//fmt.Println("hello world")
} else {
// keyppath: get from env(1)
keypath = os.Getenv("CLOUDKEY")
}
if !fileExists(keypath) {
fmt.Printf("no such CLOUDKEY env or --cloudkey: %s\n", keypath)
os.Exit(1)
}
// cloudUrl: get from args
if len(*cloudurl) > 1 {
cloudUrl = *cloudurl
} else {
// keyppath: get from env(1)
cloudUrl = os.Getenv("CLOUDURL")
}
// sshKey: get from args
if len(*sshkey) > 1 {
sshKey = *sshkey
} else {
// sshKey: get from env(1)
sshKey = os.Getenv("SSHKEY")
}
if len(cloudUrl) < 2 {
fmt.Printf("no such CLOUDURL env or --cloudurl\n")
os.Exit(1)
}
apitoken := getToken(keypath)
pubkey := string(readKey(keypath))
time.Sleep(1 * time.Second)
command := os.Args[1]
if command == "create" {
resourceType := os.Args[2]
if resourceType == "vm" {
createVmDialogue(pubkey, os.Args[3])
} else if resourceType == "container" {
createJailDialogue(pubkey, os.Args[3])
} else {
fmt.Println("Usage: nubectl create [vm|container]")
}
} else if command == "status" {
if len(os.Args) == 3 {
getStatus(os.Args[2], apitoken)
} else {
// empty or wrong arg num: show cluster status only
getStatus("", apitoken)
}
} else if command == "destroy" {
destroyResource(os.Args[2], apitoken)
} else if command == "list" {
listCluster(apitoken)
} else if command == "apply" {
applyConfig(pubkey)
} else if command == "divert" {
divertConfig(apitoken)
} else if command == "ssh" {
if len(cloudUrl) < 2 {
fmt.Printf("no such SSHKEY env or --sshkey\n")
os.Exit(1)
}
if len(os.Args) == 3 {
sshResource(os.Args[2], apitoken)
} else {
sshSelectResource(apitoken)
}
}
//fmt.Println(pubkey)
}