blob: 664e558c5f8e127b0e4fe9de32e04ab9ca11a97d [file] [log] [blame]
Sergey Kolekonovba203982016-12-21 18:32:17 +04001package com.mirantis.mk
2
3/**
4 *
5 * Openstack functions
6 *
7 */
8
9/**
10 * Install OpenStack service clients in isolated environment
11 *
12 * @param path Path where virtualenv is created
13 * @param version Version of the OpenStack clients
14 */
15
16def setupOpenstackVirtualenv(path, version = 'kilo'){
iberezovskiyd4240b52017-02-20 17:18:28 +040017 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +040018
19 def openstack_kilo_packages = [
20 'python-cinderclient>=1.3.1,<1.4.0',
21 'python-glanceclient>=0.19.0,<0.20.0',
22 'python-heatclient>=0.6.0,<0.7.0',
23 'python-keystoneclient>=1.6.0,<1.7.0',
24 'python-neutronclient>=2.2.6,<2.3.0',
25 'python-novaclient>=2.19.0,<2.20.0',
26 'python-swiftclient>=2.5.0,<2.6.0',
27 'oslo.config>=2.2.0,<2.3.0',
28 'oslo.i18n>=2.3.0,<2.4.0',
29 'oslo.serialization>=1.8.0,<1.9.0',
30 'oslo.utils>=1.4.0,<1.5.0',
Ales Komarekd874d482016-12-26 10:33:29 +010031 'docutils>=0.12'
Sergey Kolekonovba203982016-12-21 18:32:17 +040032 ]
33
34 def openstack_latest_packages = openstack_kilo_packages
35
36 if(version == 'kilo') {
37 requirements = openstack_kilo_packages
38 }
39 else if(version == 'liberty') {
40 requirements = openstack_kilo_packages
41 }
42 else if(version == 'mitaka') {
43 requirements = openstack_kilo_packages
44 }
45 else {
46 requirements = openstack_latest_packages
47 }
48 python.setupVirtualenv(path, 'python2', requirements)
49}
50
51/**
52 * create connection to OpenStack API endpoint
53 *
54 * @param url OpenStack API endpoint address
55 * @param credentialsId Credentials to the OpenStack API
56 * @param project OpenStack project to connect to
57 */
Sergey Kolekonovba203982016-12-21 18:32:17 +040058def createOpenstackEnv(url, credentialsId, project) {
iberezovskiyd4240b52017-02-20 17:18:28 +040059 def common = new com.mirantis.mk.Common()
Ales Komarek0e558ee2016-12-23 13:02:55 +010060 rcFile = "${env.WORKSPACE}/keystonerc"
Sergey Kolekonovba203982016-12-21 18:32:17 +040061 creds = common.getPasswordCredentials(credentialsId)
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030062 rc = """set +x
63export OS_USERNAME=${creds.username}
Ales Komarek0e558ee2016-12-23 13:02:55 +010064export OS_PASSWORD=${creds.password.toString()}
65export OS_TENANT_NAME=${project}
66export OS_AUTH_URL=${url}
67export OS_AUTH_STRATEGY=keystone
Alexander Tivelkovf89a1882017-01-11 13:29:35 +030068set -x
Ales Komarek0e558ee2016-12-23 13:02:55 +010069"""
70 writeFile file: rcFile, text: rc
71 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +040072}
73
74/**
75 * Run command with OpenStack env params and optional python env
76 *
77 * @param cmd Command to be executed
78 * @param env Environmental parameters with endpoint credentials
79 * @param path Optional path to virtualenv with specific clients
80 */
81def runOpenstackCommand(cmd, venv, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +040082 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +040083 openstackCmd = ". ${venv}; ${cmd}"
84 if (path) {
85 output = python.runVirtualenvCommand(path, openstackCmd)
86 }
87 else {
88 echo("[Command]: ${openstackCmd}")
89 output = sh (
90 script: openstackCmd,
91 returnStdout: true
92 ).trim()
93 }
94 return output
95}
96
97/**
98 * Get OpenStack Keystone token for current credentials
99 *
100 * @param env Connection parameters for OpenStack API endpoint
101 * @param path Optional path to the custom virtualenv
102 */
103def getKeystoneToken(client, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400104 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400105 cmd = "keystone token-get"
106 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100107 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400108 return output
109}
110
111/**
112 * Get OpenStack Keystone token for current credentials
113 *
114 * @param env Connection parameters for OpenStack API endpoint
115 * @param path Optional path to the custom virtualenv
116 */
117def createHeatEnv(file, environment = [], original_file = null) {
118 if (original_file) {
119 envString = readFile file: original_file
Tomáš Kukrál03029442017-02-21 17:14:29 +0100120 } else {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400121 envString = "parameters:\n"
122 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100123
124 for (item in environment) {
125 envString = "${envString} ${item.key}: ${item.value}\n"
Sergey Kolekonovba203982016-12-21 18:32:17 +0400126 }
Tomáš Kukrál03029442017-02-21 17:14:29 +0100127
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100128 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400129 writeFile file: file, text: envString
130}
131
132/**
133 * Create new OpenStack Heat stack
134 *
135 * @param env Connection parameters for OpenStack API endpoint
136 * @param template HOT template for the new Heat stack
137 * @param environment Environmentale parameters of the new Heat stack
138 * @param name Name of the new Heat stack
139 * @param path Optional path to the custom virtualenv
140 */
141def createHeatStack(client, name, template, params = [], environment = null, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400142 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400143 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
144 if (environment) {
145 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
146 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
147 createHeatEnv(envFile, params, envSource)
148 }
149 else {
150 envFile = "${env.WORKSPACE}/template/${name}.env"
151 createHeatEnv(envFile, params)
152 }
153 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
154 dir("${env.WORKSPACE}/template/template") {
155 outputTable = runOpenstackCommand(cmd, client, path)
156 }
Ales Komareke11e8792016-12-28 09:42:25 +0100157 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400158
159 i = 1
Ales Komarekdce8b472016-12-30 16:56:07 +0100160
Sergey Kolekonovba203982016-12-21 18:32:17 +0400161 while (true) {
162 status = getHeatStackStatus(client, name, path)
163 echo("[Heat Stack] Status: ${status}, Check: ${i}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100164
165 if (status.indexOf('CREATE_FAILED') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400166 info = getHeatStackInfo(client, name, path)
167 throw new Exception(info.stack_status_reason)
168 }
Ales Komarekdce8b472016-12-30 16:56:07 +0100169 else if (status.indexOf('CREATE_COMPLETE') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400170 info = getHeatStackInfo(client, name, path)
171 echo(info.stack_status_reason)
172 break
173 }
174 sh('sleep 5s')
175 i++
176 }
177 echo("[Heat Stack] Status: ${status}")
178}
179
180/**
181 * Get life cycle status for existing OpenStack Heat stack
182 *
183 * @param env Connection parameters for OpenStack API endpoint
184 * @param name Name of the managed Heat stack instance
185 * @param path Optional path to the custom virtualenv
186 */
187def getHeatStackStatus(client, name, path = null) {
188 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
189 return runOpenstackCommand(cmd, client, path)
190}
191
192/**
193 * Get info about existing OpenStack Heat stack
194 *
195 * @param env Connection parameters for OpenStack API endpoint
196 * @param name Name of the managed Heat stack instance
197 * @param path Optional path to the custom virtualenv
198 */
199def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400200 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400201 cmd = "heat stack-show ${name}"
202 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100203 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400204 return output
205}
206
207/**
208 * Get existing OpenStack Heat stack output parameter
209 *
210 * @param env Connection parameters for OpenStack API endpoint
211 * @param name Name of the managed Heat stack
212 * @param parameter Name of the output parameter
213 * @param path Optional path to the custom virtualenv
214 */
215def getHeatStackOutputParam(env, name, outputParam, path = null) {
216 cmd = "heat output-show ${name} ${outputParam}"
217 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100218 echo("${cmd}: ${output}")
Ales Komarek7ebd0062017-01-03 10:59:29 +0100219 return "${output}".substring(1, output.length()-1)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400220}
221
222/**
223 * List all resources from existing OpenStack Heat stack
224 *
225 * @param env Connection parameters for OpenStack API endpoint
226 * @param name Name of the managed Heat stack instance
227 * @param path Optional path to the custom virtualenv
228 */
229def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400230 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400231 cmd = "heat resource-list ${name}"
232 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100233 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400234 return output
235}
236
237/**
238 * Get info about resource from existing OpenStack Heat stack
239 *
240 * @param env Connection parameters for OpenStack API endpoint
241 * @param name Name of the managed Heat stack instance
242 * @param path Optional path to the custom virtualenv
243 */
244def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400245 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400246 cmd = "heat resource-show ${name} ${resource}"
247 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100248 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400249 return output
250}
251
252/**
253 * Update existing OpenStack Heat stack
254 *
255 * @param env Connection parameters for OpenStack API endpoint
256 * @param name Name of the managed Heat stack instance
257 * @param path Optional path to the custom virtualenv
258 */
259def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400260 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400261 cmd = "heat stack-update ${name}"
262 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100263 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400264 return output
265}
266
267/**
268 * Delete existing OpenStack Heat stack
269 *
270 * @param env Connection parameters for OpenStack API endpoint
271 * @param name Name of the managed Heat stack instance
272 * @param path Optional path to the custom virtualenv
273 */
274def deleteHeatStack(env, name, path = null) {
275 cmd = "heat stack-delete ${name}"
276 outputTable = runOpenstackCommand(cmd, env, path)
277}
278
279/**
280 * Return list of servers from OpenStack Heat stack
281 *
282 * @param env Connection parameters for OpenStack API endpoint
283 * @param name Name of the managed Heat stack instance
284 * @param path Optional path to the custom virtualenv
285 */
286def getHeatStackServers(env, name, path = null) {
287 resources = heatGetStackResources(env, name, path)
288 servers = []
289 for (resource in resources) {
290 if (resource.resource_type == 'OS::Nova::Server') {
291 resourceName = resource.resource_name
292 server = heatGetStackResourceInfo(env, name, resourceName, path)
293 servers.add(server.attributes.name)
294 }
295 }
296 echo("[Stack ${name}] Servers: ${servers}")
297 return servers
298}