-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathvm.go
125 lines (116 loc) · 2.74 KB
/
vm.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
package main
import (
"fmt"
"log"
"os"
)
var cmdVm = &Command{
Run: runVm,
UsageLine: "vm",
Short: "work with vms from vmpooler",
Long: `
vm grab <pool name>:
- request a vm from pool
vm delete <vm>:
- delete a vm
vm info <vm>:
- retreive info about a specific vm
vm lifetime <vm> <TTL in hours>
`,
}
func vmTag(hostname string, tags string) {
debug("Funciton vmTag")
debug(" Hostname: " + hostname)
debug(" Tags: " + tags)
token := retrieveToken()
params := token + "|" + hostname
_, output_json := RequestWrapper("vm", params, "PUT", tags)
if output_json["ok"] == true {
debug(fmt.Sprintf("Tags changed to " + tags + " on " + hostname))
} else {
fmt.Println("Something went wrong. Unable to adjust Tags.")
os.Exit(255)
}
}
func vmLifetime(hostname string, ttl string) {
debug("Function: vmLifetime")
debug(" Hostname: " + hostname)
debug(" TTL: " + ttl)
token := retrieveToken()
params := token + "|" + hostname
lifetime := "{\"lifetime\":" + ttl + "}"
input_json := lifetime
_, output_json := RequestWrapper("vm", params, "PUT", input_json)
if output_json["ok"] == true {
fmt.Println("Lifetime changed to " + ttl + " on " + hostname)
} else {
fmt.Println("Something went wrong. Unable to adjust TTL.")
os.Exit(255)
}
}
func appendTags(vm string) {
debug("Function: appendTags")
user := ""
if os.Getenv("LDAP_USERNAME") != "" {
user = os.Getenv("LDAP_USERNAME")
} else {
user = os.Getenv("USER")
}
tags := `{"tags": {"user":"` + user + `", "client":"vmpool-cli-` + version + `"}}`
debug("Tags:" + tags)
vmTag(vm, tags)
}
func runVm(cmd *Command, args []string) {
debug("Function: runVM")
if len(args) < 1 {
log.Fatal("You need arguments.")
}
debug(" ARGS for runVM are" + fmt.Sprintf("%v", args))
var subcmd string
if len(args) > 0 {
subcmd = args[0]
} else {
subcmd = ""
}
var params string
var http_action string
var vm string
if len(args) > 1 {
params = shortname(args[1])
}
if len(args) < 2 {
log.Fatal("info requires a vm name argument.")
} else {
vm = shortname(args[1])
}
switch {
case (subcmd == "info"):
http_action = "GET"
// how do I know args is correct?
contents, output_json := RequestWrapper("vm", params, http_action, "{}")
if output_json["ok"] == false {
fmt.Println("VM not found.")
os.Exit(255)
}
pjson(contents)
os.Exit(0)
case (subcmd == "delete"):
host := []string{vm}
runDelete(cmdDelete, host)
os.Exit(0)
case (subcmd == "grab"):
host := []string{vm}
runGrab(cmdGrab, host)
os.Exit(0)
case (subcmd == "lifetime"):
ttl := ""
if len(args) < 3 {
log.Fatal("lifetime action requires a TTL.")
} else {
ttl = args[2]
}
vmLifetime(vm, ttl)
default:
log.Fatal("Unkonwn argument to vm.")
}
}