blob: 2416f91739362518b41b91776b227545d8f9d80e [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * SaltStack functions
6 *
7 */
8
9/**
10 * Login to Salt API and return auth token
11 *
12 * @param url Salt API server URL
13 * @param params Salt connection params
14 */
15def getSaltToken(url, params) {
16 def http = new com.mirantis.mk.http()
17 data = [
18 'username': params.creds.username,
19 'password': params.creds.password.toString(),
20 'eauth': 'pam'
21 ]
22 authToken = http.sendHttpGetRequest("${url}/login", data, ['Accept': '*/*'])['return'][0]['token']
23 return authToken
24}
25
26/**
27 * Salt connection and context parameters
28 *
29 * @param url Salt API server URL
30 * @param credentialsID ID of credentials store entry
31 */
32def createSaltConnection(url, credentialsId) {
33 def common = new com.mirantis.mk.common()
34 params = [
35 "url": url,
36 "credentialsId": credentialsId,
37 "authToken": null,
38 "creds": common.getPasswordCredentials(credentialsId)
39 ]
40 params["authToken"] = getSaltToken(url, params)
41
42 return params
43}
44
45/**
46 * Run action using Salt API
47 *
48 * @param master Salt connection object
49 * @param client Client type
50 * @param target Target specification, eg. for compound matches by Pillar
51 * data: ['expression': 'I@openssh:server', 'type': 'compound'])
52 * @param function Function to execute (eg. "state.sls")
53 * @param args Additional arguments to function
54 * @param kwargs Additional key-value arguments to function
55 */
56def runSaltCommand(master, client, target, function, args = null, kwargs = null) {
57 data = [
58 'tgt': target.expression,
59 'fun': function,
60 'client': client,
61 'expr_form': target.type,
62 ]
63
64 if (args) {
65 data['arg'] = args
66 }
67 if (kwargs) {
68 data['kwarg'] = kwargs
69 }
70
71 headers = [
72 'X-Auth-Token': "${master.authToken}"
73 ]
74
75 return http.sendHttpPostRequest("${master.url}/", data, headers)
76}
77
78def getSaltPillar(master, target, pillar) {
79 def out = runSaltCommand(master, 'local', target, 'pillar.get', [pillar.replace('.', ':')])
80 return out
81}
82
83def enforceSaltState(master, target, state, output = false) {
84 def run_states
85 if (state instanceof String) {
86 run_states = state
87 } else {
88 run_states = state.join(',')
89 }
90
91 def out = runSaltCommand(master, 'local', target, 'state.sls', [run_states])
92 try {
93 checkSaltResult(out)
94 } finally {
95 if (output == true) {
96 printSaltResult(out)
97 }
98 }
99 return out
100}
101
102def runSaltCmd(master, target, cmd) {
103 return runSaltCommand(master, 'local', target, 'cmd.run', [cmd])
104}
105
106def syncSaltAll(master, target) {
107 return runSaltCommand(master, 'local', target, 'saltutil.sync_all')
108}
109
110def enforceSaltApply(master, target, output = false) {
111 def out = runSaltCommand(master, 'local', target, 'state.highstate')
112 try {
113 checkSaltResult(out)
114 } finally {
115 if (output == true) {
116 printSaltResult(out)
117 }
118 }
119 return out
120}
121
122def generateSaltNodeKey(master, target, host, keysize = 4096) {
123 args = [host]
124 kwargs = ['keysize': keysize]
125 return runSaltCommand(master, 'wheel', target, 'key.gen_accept', args, kwargs)
126}
127
128def generateSaltNodeMetadata(master, target, host, classes, parameters) {
129 args = [host, '_generated']
130 kwargs = ['classes': classes, 'parameters': parameters]
131 return runSaltCommand(master, 'local', target, 'reclass.node_create', args, kwargs)
132}
133
134def orchestrateSaltSystem(master, target, orchestrate) {
135 return runSaltCommand(master, 'runner', target, 'state.orchestrate', [orchestrate])
136}
137
138/**
139 * Check result for errors and throw exception if any found
140 *
141 * @param result Parsed response of Salt API
142 */
143def checkSaltResult(result) {
144 for (entry in result['return']) {
145 if (!entry) {
146 throw new Exception("Salt API returned empty response: ${result}")
147 }
148 for (node in entry) {
149 for (resource in node.value) {
150 if (resource instanceof String || resource.value.result.toString().toBoolean() != true) {
151 throw new Exception("Salt state on node ${node.key} failed: ${node.value}")
152 }
153 }
154 }
155 }
156}
157
158/**
159 * Print Salt run results in human-friendly form
160 *
161 * @param result Parsed response of Salt API
162 * @param onlyChanges If true (default), print only changed resources
163 * @param raw Simply pretty print what we have, no additional
164 * parsing
165 */
166def printSaltResult(result, onlyChanges = true, raw = false) {
167 if (raw == true) {
168 print new groovy.json.JsonBuilder(result).toPrettyString()
169 } else {
170 def out = [:]
171 for (entry in result['return']) {
172 for (node in entry) {
173 out[node.key] = [:]
174 for (resource in node.value) {
175 if (resource instanceof String) {
176 out[node.key] = node.value
177 } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
178 out[node.key][resource.key] = resource.value
179 }
180 }
181 }
182 }
183
184 for (node in out) {
185 if (node.value) {
186 println "Node ${node.key} changes:"
187 print new groovy.json.JsonBuilder(node.value).toPrettyString()
188 } else {
189 println "No changes for node ${node.key}"
190 }
191 }
192 }
193}
194
195@NonCPS
196def getSaltProcess(saltProcess) {
197
198 def process_def = [
199 'validate_foundation_infra': [
200 [tgt: 'I@salt:master', fun: 'cmd.run', arg: ['salt-key']],
201 [tgt: 'I@salt:minion', fun: 'test.version'],
202 [tgt: 'I@salt:master', fun: 'cmd.run', arg: ['reclass-salt --top']],
203 [tgt: 'I@reclass:storage', fun: 'reclass.inventory'],
204 [tgt: 'I@salt:minion', fun: 'state.show_top'],
205 ],
206 'install_foundation_infra': [
207 [tgt: 'I@salt:master', fun: 'state.sls', arg: ['salt.master,reclass']],
208 [tgt: 'I@linux:system', fun: 'saltutil.refresh_pillar'],
209 [tgt: 'I@linux:system', fun: 'saltutil.sync_all'],
210 [tgt: 'I@linux:system', fun: 'state.sls', arg: ['linux,openssh,salt.minion,ntp']],
211 ],
212 'install_openstack_mk_infra': [
213 // Install keepaliveds
214 [tgt: 'I@keepalived:cluster', fun: 'state.sls', arg: ['keepalived'], batch:1],
215 // Check the keepalived VIPs
216 [tgt: 'I@keepalived:cluster', fun: 'cmd.run', arg: ['ip a | grep 172.16.10.2']],
217 // Install glusterfs
218 [tgt: 'I@glusterfs:server', fun: 'state.sls', arg: ['glusterfs.server.service']],
219 [tgt: 'I@glusterfs:server', fun: 'state.sls', arg: ['glusterfs.server.setup'], batch:1],
220 [tgt: 'I@glusterfs:server', fun: 'cmd.run', arg: ['gluster peer status']],
221 [tgt: 'I@glusterfs:server', fun: 'cmd.run', arg: ['gluster volume status']],
222 // Install rabbitmq
223 [tgt: 'I@rabbitmq:server', fun: 'state.sls', arg: ['rabbitmq']],
224 // Check the rabbitmq status
225 [tgt: 'I@rabbitmq:server', fun: 'cmd.run', arg: ['rabbitmqctl cluster_status']],
226 // Install galera
227 [tgt: 'I@galera:master', fun: 'state.sls', arg: ['galera']],
228 [tgt: 'I@galera:slave', fun: 'state.sls', arg: ['galera']],
229 // Check galera status
230 [tgt: 'I@galera:master', fun: 'mysql.status'],
231 [tgt: 'I@galera:slave', fun: 'mysql.status'],
232 // Install haproxy
233 [tgt: 'I@haproxy:proxy', fun: 'state.sls', arg: ['haproxy']],
234 [tgt: 'I@haproxy:proxy', fun: 'service.status', arg: ['haproxy']],
235 [tgt: 'I@haproxy:proxy', fun: 'service.restart', arg: ['rsyslog']],
236 // Install memcached
237 [tgt: 'I@memcached:server', fun: 'state.sls', arg: ['memcached']],
238 ],
239 'install_openstack_mk_control': [
240 // setup keystone service
241 [tgt: 'I@keystone:server', fun: 'state.sls', arg: ['keystone.server'], batch:1],
242 // populate keystone services/tenants/roles/users
243 [tgt: 'I@keystone:client', fun: 'state.sls', arg: ['keystone.client']],
244 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; keystone service-list']],
245 // Install glance and ensure glusterfs clusters
246 [tgt: 'I@glance:server', fun: 'state.sls', arg: ['glance.server'], batch:1],
247 [tgt: 'I@glance:server', fun: 'state.sls', arg: ['glusterfs.client']],
248 // Update fernet tokens before doing request on keystone server
249 [tgt: 'I@keystone:server', fun: 'state.sls', arg: ['keystone.server']],
250 // Check glance service
251 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; glance image-list']],
252 // Install and check nova service
253 [tgt: 'I@nova:controller', fun: 'state.sls', arg: ['nova'], batch:1],
254 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; nova service-list']],
255 // Install and check cinder service
256 [tgt: 'I@cinder:controller', fun: 'state.sls', arg: ['cinder'], batch:1],
257 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; cinder list']],
258 // Install neutron service
259 [tgt: 'I@neutron:server', fun: 'state.sls', arg: ['neutron'], batch:1],
260 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; neutron agent-list']],
261 // Install heat service
262 [tgt: 'I@heat:server', fun: 'state.sls', arg: ['heat'], batch:1],
263 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; heat resource-type-list']],
264 // Install horizon dashboard
265 [tgt: 'I@horizon:server', fun: 'state.sls', arg: ['horizon']],
266 [tgt: 'I@nginx:server', fun: 'state.sls', arg: ['nginx']],
267 ],
268 'install_openstack_mk_network': [
269 // Install opencontrail database services
270 [tgt: 'I@opencontrail:database', fun: 'state.sls', arg: ['opencontrail.database'], batch:1],
271 // Install opencontrail control services
272 [tgt: 'I@opencontrail:control', fun: 'state.sls', arg: ['opencontrail'], batch:1],
273 // Provision opencontrail control services
274 [tgt: 'I@opencontrail:control:id:1', fun: 'cmd.run', arg: ['/usr/share/contrail-utils/provision_control.py --api_server_ip 172.16.10.254 --api_server_port 8082 --host_name ctl01 --host_ip 172.16.10.101 --router_asn 64512 --admin_password workshop --admin_user admin --admin_tenant_name admin --oper add']],
275 [tgt: 'I@opencontrail:control:id:1', fun: 'cmd.run', arg: ['/usr/share/contrail-utils/provision_control.py --api_server_ip 172.16.10.254 --api_server_port 8082 --host_name ctl02 --host_ip 172.16.10.102 --router_asn 64512 --admin_password workshop --admin_user admin --admin_tenant_name admin --oper add']],
276 [tgt: 'I@opencontrail:control:id:1', fun: 'cmd.run', arg: ['/usr/share/contrail-utils/provision_control.py --api_server_ip 172.16.10.254 --api_server_port 8082 --host_name ctl03 --host_ip 172.16.10.103 --router_asn 64512 --admin_password workshop --admin_user admin --admin_tenant_name admin --oper add']],
277 // Test opencontrail
278 [tgt: 'I@opencontrail:control', fun: 'cmd.run', arg: ['contrail-status']],
279 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; neutron net-list']],
280 [tgt: 'I@keystone:server', fun: 'cmd.run', arg: ['. /root/keystonerc; nova net-list']],
281 ],
282 'install_openstack_mk_compute': [
283 // Configure compute nodes
284 [tgt: 'I@nova:compute', fun: 'state.apply'],
285 [tgt: 'I@nova:compute', fun: 'state.apply'],
286 // Provision opencontrail virtual routers
287 [tgt: 'I@opencontrail:control:id:1', fun: 'cmd.run', arg: ['/usr/share/contrail-utils/provision_vrouter.py --host_name cmp01 --host_ip 172.16.10.105 --api_server_ip 172.16.10.254 --oper add --admin_user admin --admin_password workshop --admin_tenant_name admin']],
288 [tgt: 'I@nova:compute', fun: 'system.reboot'],
289 ],
290 'install_openstack_mcp_infra': [
291 // Comment nameserver
292 [tgt: 'I@kubernetes:master', fun: 'cmd.run', arg: ["sed -i 's/nameserver 10.254.0.10/#nameserver 10.254.0.10/g' /etc/resolv.conf"]],
293 // Install glusterfs
294 [tgt: 'I@glusterfs:server', fun: 'state.sls', arg: ['glusterfs.server.service']],
295 // Install keepalived
296 [tgt: 'I@keepalived:cluster', fun: 'state.sls', arg: ['keepalived'], batch:1],
297 // Check the keepalived VIPs
298 [tgt: 'I@keepalived:cluster', fun: 'cmd.run', arg: ['ip a | grep 172.16.10.2']],
299 // Setup glusterfs
300 [tgt: 'I@glusterfs:server', fun: 'state.sls', arg: ['glusterfs.server.setup'], batch:1],
301 [tgt: 'I@glusterfs:server', fun: 'cmd.run', arg: ['gluster peer status']],
302 [tgt: 'I@glusterfs:server', fun: 'cmd.run', arg: ['gluster volume status']],
303 // Install haproxy
304 [tgt: 'I@haproxy:proxy', fun: 'state.sls', arg: ['haproxy']],
305 [tgt: 'I@haproxy:proxy', fun: 'service.status', arg: ['haproxy']],
306 // Install docker
307 [tgt: 'I@docker:host', fun: 'state.sls', arg: ['docker.host']],
308 [tgt: 'I@docker:host', fun: 'cmd.run', arg: ['docker ps']],
309 // Install bird
310 [tgt: 'I@bird:server', fun: 'state.sls', arg: ['bird']],
311 // Install etcd
312 [tgt: 'I@etcd:server', fun: 'state.sls', arg: ['etcd.server.service']],
313 [tgt: 'I@etcd:server', fun: 'cmd.run', arg: ['etcdctl cluster-health']],
314 ],
315 'install_stacklight_control': [
316 [tgt: 'I@elasticsearch:server', fun: 'state.sls', arg: ['elasticsearch.server'], batch:1],
317 [tgt: 'I@influxdb:server', fun: 'state.sls', arg: ['influxdb'], batch:1],
318 [tgt: 'I@kibana:server', fun: 'state.sls', arg: ['kibana.server'], batch:1],
319 [tgt: 'I@grafana:server', fun: 'state.sls', arg: ['grafana'], batch:1],
320 [tgt: 'I@nagios:server', fun: 'state.sls', arg: ['nagios'], batch:1],
321 [tgt: 'I@elasticsearch:client', fun: 'state.sls', arg: ['elasticsearch.client'], batch:1],
322 [tgt: 'I@kibana:client', fun: 'state.sls', arg: ['kibana.client'], batch:1],
323 ],
324 'install_stacklight_client': [
325 ]
326 ]
327 return process_def[saltProcess]
328}
329
330/**
331 * Run predefined salt process
332 *
333 * @param master Salt connection object
334 * @param process Process name to be run
335 */
336def runSaltProcess(master, process) {
337
338 tasks = getSaltProcess(process)
339
340 for (i = 0; i <tasks.size(); i++) {
341 task = tasks[i]
342 infoMsg("[Salt master ${master.url}] Task ${task}")
343 if (task.containsKey('arg')) {
344 result = runSaltCommand(master, 'local', ['expression': task.tgt, 'type': 'compound'], task.fun, task.arg)
345 }
346 else {
347 result = runSaltCommand(master, 'local', ['expression': task.tgt, 'type': 'compound'], task.fun)
348 }
349 if (task.fun == 'state.sls') {
350 printSaltResult(result, false)
351 }
352 else {
353 echo("${result}")
354 }
355 }
356}
357
358/**
359 * Print Salt state run results in human-friendly form
360 *
361 * @param result Parsed response of Salt API
362 * @param onlyChanges If true (default), print only changed resources
363 * parsing
364 */
365def printSaltStateResult(result, onlyChanges = true) {
366 def out = [:]
367 for (entry in result['return']) {
368 for (node in entry) {
369 out[node.key] = [:]
370 for (resource in node.value) {
371 if (resource instanceof String) {
372 out[node.key] = node.value
373 } else if (resource.value.result.toString().toBoolean() == false || resource.value.changes || onlyChanges == false) {
374 out[node.key][resource.key] = resource.value
375 }
376 }
377 }
378 }
379
380 for (node in out) {
381 if (node.value) {
382 println "Node ${node.key} changes:"
383 print new groovy.json.JsonBuilder(node.value).toPrettyString()
384 } else {
385 println "No changes for node ${node.key}"
386 }
387 }
388}
389
390/**
391 * Print Salt state run results in human-friendly form
392 *
393 * @param result Parsed response of Salt API
394 * @param onlyChanges If true (default), print only changed resources
395 * parsing
396 */
397def printSaltCommandResult(result, onlyChanges = true) {
398 def out = [:]
399 for (entry in result['return']) {
400 for (node in entry) {
401 out[node.key] = [:]
402 for (resource in node.value) {
403 out[node.key] = node.value
404 }
405 }
406 }
407
408 for (node in out) {
409 if (node.value) {
410 println "Node ${node.key} changes:"
411 print new groovy.json.JsonBuilder(node.value).toPrettyString()
412 } else {
413 println "No changes for node ${node.key}"
414 }
415 }
416}