-
Notifications
You must be signed in to change notification settings - Fork 1
/
vm.clj
253 lines (219 loc) · 9.01 KB
/
vm.clj
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
(ns exoscale.compute.api.vm
"Higher order functions to manipulate VMs"
(:refer-clojure :exclude [update list])
(:require [exoscale.compute.api.client :as client]
[exoscale.compute.api.zone :as zone]
[exoscale.compute.api.offering :as offering]
[exoscale.compute.api.template :as template]
[exoscale.compute.api.keypair :as keypair]
[exoscale.compute.api.meta :as meta]
[exoscale.compute.ssh :as ssh]
[manifold.deferred :as d]
[clojure.string :as str]
[clojure.spec.alpha :as spec]
[exoscale.compute.api.spec]))
(defn sanitize-sg
"Keep relevant information for security groups"
[{:keys [id name]}]
{:id (client/parse-uuid id)
:name name})
(defn sanitize-template
"Rewrite template information to contain UUIDs"
[{:keys [templatename templateid]}]
{:id (client/parse-uuid templateid)
:name templatename})
(defn sanitize-nic
"Keep relevant information for public NICs.
We do not keep network IDs and name since this isn't
actionable information."
[{:keys [macaddress ipaddress ip6address] :as nic}]
(cond-> {:macaddress macaddress}
(some? ipaddress) (assoc :ipv4 {:ip (:ipaddress nic)
:gateway (:gateway nic)
:netmask (:netmask nic)})
(some? ip6address) (assoc :ipv6 {:ip (:ip6address nic)
:cidr (:ip6cidr nic)})))
(defn sanitize-nics
"Keep relevant information for private network NICs"
[nics]
(vec
(for [nic nics :when (= "Isolated" (:type nic))]
{:network {:id (client/parse-uuid (:networkid nic))
:name (:networkname nic)}
:macaddress (:macaddress nic)
:id (client/parse-uuid (:id nic))})))
(defn sanitize-public
"Filter out public NIC and show relevant information"
[nics]
(let [nic (first (filter #(= "Shared" (:type %)) nics))]
(sanitize-nic nic)))
(defn sanitize-ag
"Parse affinitygroup UUID"
[group]
(update-in group [:id] client/parse-uuid))
(defn sanitize-vm
"Coerce API response into something a bit more useful"
[resp]
(cond
(and (contains? resp :state) (not (nil? (:state resp))))
(let [tag-acc #(assoc %1 (keyword (:key %2)) (:value %2))]
(-> (select-keys resp [:id :name :displayname :keypair :memory
:cpunumber :group :password])
(assoc :security-groups (mapv sanitize-sg (:securitygroup resp)))
(assoc :template (sanitize-template resp))
(assoc :zone (:zonename resp))
(assoc :service-offering (-> resp :serviceofferingname str/lower-case keyword))
(assoc :state (-> resp :state str/lower-case keyword))
(assoc :tags (reduce tag-acc {} (:tags resp)))
(update-in [:id] client/parse-uuid)
(assoc :affinity-groups (mapv sanitize-ag (:affinitygroup resp)))
(assoc :public (sanitize-public (:nic resp)))
(assoc :nics (sanitize-nics (:nic resp)))
(assoc :created (:created resp))
(meta/describe :exsocale.compute/vm resp)))
(contains? resp :id)
(-> resp
(update-in [:id] client/parse-uuid)
(update-in [:jobid] client/parse-uuid))
:else
(-> resp
(update-in [:jobid] client/parse-uuid))))
(defn by-name
"The infamous name resolver, using listVirtualMachines since
Cloudstack does not allow singular get calls"
[config vm]
(d/chain (client/api-call config :list-virtual-machines {:name (name vm)})
(fn [resp]
(when-not (= 1 (count resp))
(throw (IllegalArgumentException.
"cannot resolve virtual machine name")))
(sanitize-vm (first resp)))))
(defn vmdef?
"Predicate to test against a valid VM definition"
[x]
(= :exoscale.compute/vm (meta/type x)))
(defn resolve-id
"Yield a target VM id from a target definition (name, ID, or vm return)"
[config target]
(cond
(uuid? target) (str target)
(keyword? target) (d/chain (by-name config target) :id str)
(string? target) (d/chain (by-name config target) :id str)
(vmdef? target) (str (:id target))
:else (throw (IllegalArgumentException.
"invalid argument to resolve-id"))))
(defn ^{:no-doc true} target-action
[action config target params]
(let [opcode (str (name action) "VirtualMachine")
call-fn #(client/api-call config opcode (merge params {:id %}))]
(d/chain (resolve-id config target)
call-fn
sanitize-vm)))
(defmacro ^{:no-doc true} defvmaction
"Generate a function to act on a VM"
[sym & [action]]
`(defn ~sym
~(format "%s virtual machine." (str/capitalize (name sym)))
[~'config ~'target & [~'params]]
{:pre [(spec/valid? :exoscale.compute/vmparams ~'params)]}
(target-action ~(or action (str sym)) ~'config ~'target ~'params)))
(defvmaction start)
(defvmaction stop)
(defvmaction reboot)
(defvmaction update)
(defvmaction reset-password "resetPasswordFor")
(defn get-password
"Fetch encrypted password"
[config target]
(d/chain (resolve-id config target)
#(client/api-call config :get-vm-password {:id %})
:password
:encryptedpassword))
(defn list
"List virtual machines"
[config & [params]]
(d/chain (client/api-call config :list-virtual-machines params)
(fn [vms] (mapv sanitize-vm vms))))
(defn resolve-indirect-params
""
[config {:keys [zoneid zone serviceoffering template]}]
(d/let-flow [zid (or zoneid (d/chain (zone/by-name config zone) :id))
sid (when (some? serviceoffering)
(d/chain (offering/by-name config serviceoffering) :id))
tid (when (some? template)
(d/chain (template/by-zone-name config zid template) :id))]
(cond-> {}
(some? zone) (assoc :zoneid zid)
(some? serviceoffering) (assoc :serviceofferingid sid)
(some? template) (assoc :templateid tid))))
(defn deploy
"Deploy virtual machine"
[config params]
{:pre [(spec/valid? :exoscale.compute/vm params)]}
(d/chain (resolve-indirect-params config params)
(fn [resolved] (merge resolved (dissoc params :zone :template :serviceoffering)))
(fn [params] (client/api-call config :deploy-virtual-machine params))
sanitize-vm))
(defn destroy
"Destroy virtual machine"
[config target]
(d/chain (resolve-id config target)
#(client/api-call config :destroy-virtual-machine {:id %})
(constantly nil)))
(defn ssh
"Asynchronously reach out to a machine to execute an ssh command"
[config target command]
(d/let-flow [vm (if (vmdef? target) target (by-name config target))
template (template/by-zone-id config
(meta/zone vm)
(get-in vm [:template :id]))]
(let [pkey (keypair/private-key (:keypair vm))
user (or (get-in template [:details :username]) "root")
host (get-in vm [:public :ipv4 :ip])]
(when (nil? pkey)
(throw (ex-info (str "cannot find private key named: " (:keypair vm))
{:vm vm})))
(when (nil? host)
(throw (ex-info "cannot find host address" {:vm vm})))
(d/future
(ssh/exec pkey host command {:user user})))))
(defn ensure-up
"Wait for machine to be up by probbing the SSH port. This assumes that
inbound SSH is authorized"
[config target timeout]
(d/let-flow [vm (if (vmdef? target) target (by-name config target))
template (template/by-zone-id config
(meta/zone vm)
(get-in vm [:template :id]))]
(let [pkey (keypair/private-key (:keypair vm))
user (or (get-in template [:details :username]) "root")
host (get-in vm [:public :ipv4 :ip])]
(when (nil? pkey)
(throw (ex-info (str "cannot find private key named: " (:keypair vm))
{:vm vm})))
(when (nil? host)
(throw (ex-info "cannot find host address" {:vm vm})))
(d/chain
(d/future (ssh/ensure-up pkey host timeout {:user user}))
(constantly vm)))))
(comment
@(list config)
(do
@(keypair/create-and-store config :auto)
@(d/chain (deploy config
{:name "clojure01"
:displayname "Clojure Test Box"
:zone "at-vie-1"
:template "Linux Debian 9 64-bit"
:keypair "auto"
:serviceoffering "micro"})
#(ensure-up config % 40)
#(ssh config % "echo hello")))
@(ensure-up :clojure01)
@(ssh config :clojure01 "echo hello")
(keypair/private-key :auto)
@(by-name config :clojure01)
@(ssh config :clojure01 "echo hello")
@(stop config :clojure01)
@(destroy config :clojure01)
)