blob: 75be7f42f6c3ab28259f323dc0d1e4c739c1ee4a [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
120 }
121 else {
122 envString = "parameters:\n"
123 }
124 for ( int i = 0; i < environment.size; i++ ) {
125 envString = "${envString} ${environment.get(i).get(0)}: ${environment.get(i).get(1)}\n"
126 }
Tomáš Kukrále19ddea2017-02-21 11:09:40 +0100127 echo("writing to env file:\n${envString}")
Sergey Kolekonovba203982016-12-21 18:32:17 +0400128 writeFile file: file, text: envString
129}
130
131/**
132 * Create new OpenStack Heat stack
133 *
134 * @param env Connection parameters for OpenStack API endpoint
135 * @param template HOT template for the new Heat stack
136 * @param environment Environmentale parameters of the new Heat stack
137 * @param name Name of the new Heat stack
138 * @param path Optional path to the custom virtualenv
139 */
140def createHeatStack(client, name, template, params = [], environment = null, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400141 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400142 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
143 if (environment) {
144 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
145 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
146 createHeatEnv(envFile, params, envSource)
147 }
148 else {
149 envFile = "${env.WORKSPACE}/template/${name}.env"
150 createHeatEnv(envFile, params)
151 }
152 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
153 dir("${env.WORKSPACE}/template/template") {
154 outputTable = runOpenstackCommand(cmd, client, path)
155 }
Ales Komareke11e8792016-12-28 09:42:25 +0100156 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400157
158 i = 1
Ales Komarekdce8b472016-12-30 16:56:07 +0100159
Sergey Kolekonovba203982016-12-21 18:32:17 +0400160 while (true) {
161 status = getHeatStackStatus(client, name, path)
162 echo("[Heat Stack] Status: ${status}, Check: ${i}")
Ales Komarekdce8b472016-12-30 16:56:07 +0100163
164 if (status.indexOf('CREATE_FAILED') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400165 info = getHeatStackInfo(client, name, path)
166 throw new Exception(info.stack_status_reason)
167 }
Ales Komarekdce8b472016-12-30 16:56:07 +0100168 else if (status.indexOf('CREATE_COMPLETE') != -1) {
Sergey Kolekonovba203982016-12-21 18:32:17 +0400169 info = getHeatStackInfo(client, name, path)
170 echo(info.stack_status_reason)
171 break
172 }
173 sh('sleep 5s')
174 i++
175 }
176 echo("[Heat Stack] Status: ${status}")
177}
178
179/**
180 * Get life cycle status for existing OpenStack Heat stack
181 *
182 * @param env Connection parameters for OpenStack API endpoint
183 * @param name Name of the managed Heat stack instance
184 * @param path Optional path to the custom virtualenv
185 */
186def getHeatStackStatus(client, name, path = null) {
187 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
188 return runOpenstackCommand(cmd, client, path)
189}
190
191/**
192 * Get info about existing OpenStack Heat stack
193 *
194 * @param env Connection parameters for OpenStack API endpoint
195 * @param name Name of the managed Heat stack instance
196 * @param path Optional path to the custom virtualenv
197 */
198def getHeatStackInfo(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400199 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400200 cmd = "heat stack-show ${name}"
201 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100202 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400203 return output
204}
205
206/**
207 * Get existing OpenStack Heat stack output parameter
208 *
209 * @param env Connection parameters for OpenStack API endpoint
210 * @param name Name of the managed Heat stack
211 * @param parameter Name of the output parameter
212 * @param path Optional path to the custom virtualenv
213 */
214def getHeatStackOutputParam(env, name, outputParam, path = null) {
215 cmd = "heat output-show ${name} ${outputParam}"
216 output = runOpenstackCommand(cmd, env, path)
Ales Komarekeedc2222017-01-03 10:10:03 +0100217 echo("${cmd}: ${output}")
Ales Komarek7ebd0062017-01-03 10:59:29 +0100218 return "${output}".substring(1, output.length()-1)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400219}
220
221/**
222 * List all resources from existing OpenStack Heat stack
223 *
224 * @param env Connection parameters for OpenStack API endpoint
225 * @param name Name of the managed Heat stack instance
226 * @param path Optional path to the custom virtualenv
227 */
228def getHeatStackResources(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400229 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400230 cmd = "heat resource-list ${name}"
231 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100232 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400233 return output
234}
235
236/**
237 * Get info about resource from existing OpenStack Heat stack
238 *
239 * @param env Connection parameters for OpenStack API endpoint
240 * @param name Name of the managed Heat stack instance
241 * @param path Optional path to the custom virtualenv
242 */
243def getHeatStackResourceInfo(env, name, resource, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400244 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400245 cmd = "heat resource-show ${name} ${resource}"
246 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100247 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400248 return output
249}
250
251/**
252 * Update existing OpenStack Heat stack
253 *
254 * @param env Connection parameters for OpenStack API endpoint
255 * @param name Name of the managed Heat stack instance
256 * @param path Optional path to the custom virtualenv
257 */
258def updateHeatStack(env, name, path = null) {
iberezovskiyd4240b52017-02-20 17:18:28 +0400259 def python = new com.mirantis.mk.Python()
Sergey Kolekonovba203982016-12-21 18:32:17 +0400260 cmd = "heat stack-update ${name}"
261 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100262 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400263 return output
264}
265
266/**
267 * Delete existing OpenStack Heat stack
268 *
269 * @param env Connection parameters for OpenStack API endpoint
270 * @param name Name of the managed Heat stack instance
271 * @param path Optional path to the custom virtualenv
272 */
273def deleteHeatStack(env, name, path = null) {
274 cmd = "heat stack-delete ${name}"
275 outputTable = runOpenstackCommand(cmd, env, path)
276}
277
278/**
279 * Return list of servers from OpenStack Heat stack
280 *
281 * @param env Connection parameters for OpenStack API endpoint
282 * @param name Name of the managed Heat stack instance
283 * @param path Optional path to the custom virtualenv
284 */
285def getHeatStackServers(env, name, path = null) {
286 resources = heatGetStackResources(env, name, path)
287 servers = []
288 for (resource in resources) {
289 if (resource.resource_type == 'OS::Nova::Server') {
290 resourceName = resource.resource_name
291 server = heatGetStackResourceInfo(env, name, resourceName, path)
292 servers.add(server.attributes.name)
293 }
294 }
295 echo("[Stack ${name}] Servers: ${servers}")
296 return servers
297}