blob: b87dd4ffc804b10abe556d9a2163936006fb1da8 [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'){
17 def python = new com.mirantis.mk.python()
18
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) {
59 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)
Ales Komarek0e558ee2016-12-23 13:02:55 +010062 rc = """export OS_USERNAME=${creds.username}
63export OS_PASSWORD=${creds.password.toString()}
64export OS_TENANT_NAME=${project}
65export OS_AUTH_URL=${url}
66export OS_AUTH_STRATEGY=keystone
67"""
68 writeFile file: rcFile, text: rc
69 return rcFile
Sergey Kolekonovba203982016-12-21 18:32:17 +040070}
71
72/**
73 * Run command with OpenStack env params and optional python env
74 *
75 * @param cmd Command to be executed
76 * @param env Environmental parameters with endpoint credentials
77 * @param path Optional path to virtualenv with specific clients
78 */
79def runOpenstackCommand(cmd, venv, path = null) {
80 def python = new com.mirantis.mk.python()
81 openstackCmd = ". ${venv}; ${cmd}"
82 if (path) {
83 output = python.runVirtualenvCommand(path, openstackCmd)
84 }
85 else {
86 echo("[Command]: ${openstackCmd}")
87 output = sh (
88 script: openstackCmd,
89 returnStdout: true
90 ).trim()
91 }
92 return output
93}
94
95/**
96 * Get OpenStack Keystone token for current credentials
97 *
98 * @param env Connection parameters for OpenStack API endpoint
99 * @param path Optional path to the custom virtualenv
100 */
101def getKeystoneToken(client, path = null) {
102 def python = new com.mirantis.mk.python()
103 cmd = "keystone token-get"
104 outputTable = runOpenstackCommand(cmd, client, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100105 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400106 return output
107}
108
109/**
110 * Get OpenStack Keystone token for current credentials
111 *
112 * @param env Connection parameters for OpenStack API endpoint
113 * @param path Optional path to the custom virtualenv
114 */
115def createHeatEnv(file, environment = [], original_file = null) {
116 if (original_file) {
117 envString = readFile file: original_file
118 }
119 else {
120 envString = "parameters:\n"
121 }
122 for ( int i = 0; i < environment.size; i++ ) {
123 envString = "${envString} ${environment.get(i).get(0)}: ${environment.get(i).get(1)}\n"
124 }
125 writeFile file: file, text: envString
126}
127
128/**
129 * Create new OpenStack Heat stack
130 *
131 * @param env Connection parameters for OpenStack API endpoint
132 * @param template HOT template for the new Heat stack
133 * @param environment Environmentale parameters of the new Heat stack
134 * @param name Name of the new Heat stack
135 * @param path Optional path to the custom virtualenv
136 */
137def createHeatStack(client, name, template, params = [], environment = null, path = null) {
138 def python = new com.mirantis.mk.python()
139 templateFile = "${env.WORKSPACE}/template/template/${template}.hot"
140 if (environment) {
141 envFile = "${env.WORKSPACE}/template/env/${template}/${name}.env"
142 envSource = "${env.WORKSPACE}/template/env/${template}/${environment}.env"
143 createHeatEnv(envFile, params, envSource)
144 }
145 else {
146 envFile = "${env.WORKSPACE}/template/${name}.env"
147 createHeatEnv(envFile, params)
148 }
149 cmd = "heat stack-create -f ${templateFile} -e ${envFile} ${name}"
150 dir("${env.WORKSPACE}/template/template") {
151 outputTable = runOpenstackCommand(cmd, client, path)
152 }
Ales Komareke11e8792016-12-28 09:42:25 +0100153 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400154
155 i = 1
156 while (true) {
157 status = getHeatStackStatus(client, name, path)
158 echo("[Heat Stack] Status: ${status}, Check: ${i}")
159 if (status == 'CREATE_FAILED') {
160 info = getHeatStackInfo(client, name, path)
161 throw new Exception(info.stack_status_reason)
162 }
163 else if (status == 'CREATE_COMPLETE') {
164 info = getHeatStackInfo(client, name, path)
165 echo(info.stack_status_reason)
166 break
167 }
168 sh('sleep 5s')
169 i++
170 }
171 echo("[Heat Stack] Status: ${status}")
172}
173
174/**
175 * Get life cycle status for existing OpenStack Heat stack
176 *
177 * @param env Connection parameters for OpenStack API endpoint
178 * @param name Name of the managed Heat stack instance
179 * @param path Optional path to the custom virtualenv
180 */
181def getHeatStackStatus(client, name, path = null) {
182 cmd = 'heat stack-list | awk -v stack='+name+' \'{if ($4==stack) print $6}\''
183 return runOpenstackCommand(cmd, client, path)
184}
185
186/**
187 * Get info about existing OpenStack Heat stack
188 *
189 * @param env Connection parameters for OpenStack API endpoint
190 * @param name Name of the managed Heat stack instance
191 * @param path Optional path to the custom virtualenv
192 */
193def getHeatStackInfo(env, name, path = null) {
194 def python = new com.mirantis.mk.python()
195 cmd = "heat stack-show ${name}"
196 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100197 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400198 return output
199}
200
201/**
202 * Get existing OpenStack Heat stack output parameter
203 *
204 * @param env Connection parameters for OpenStack API endpoint
205 * @param name Name of the managed Heat stack
206 * @param parameter Name of the output parameter
207 * @param path Optional path to the custom virtualenv
208 */
209def getHeatStackOutputParam(env, name, outputParam, path = null) {
210 cmd = "heat output-show ${name} ${outputParam}"
211 output = runOpenstackCommand(cmd, env, path)
212 return output.substring(1, output.length()-1)
213}
214
215/**
216 * List all resources from existing OpenStack Heat stack
217 *
218 * @param env Connection parameters for OpenStack API endpoint
219 * @param name Name of the managed Heat stack instance
220 * @param path Optional path to the custom virtualenv
221 */
222def getHeatStackResources(env, name, path = null) {
223 def python = new com.mirantis.mk.python()
224 cmd = "heat resource-list ${name}"
225 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100226 output = python.parseTextTable(outputTable, 'list', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400227 return output
228}
229
230/**
231 * Get info about resource from existing OpenStack Heat stack
232 *
233 * @param env Connection parameters for OpenStack API endpoint
234 * @param name Name of the managed Heat stack instance
235 * @param path Optional path to the custom virtualenv
236 */
237def getHeatStackResourceInfo(env, name, resource, path = null) {
238 def python = new com.mirantis.mk.python()
239 cmd = "heat resource-show ${name} ${resource}"
240 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100241 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400242 return output
243}
244
245/**
246 * Update existing OpenStack Heat stack
247 *
248 * @param env Connection parameters for OpenStack API endpoint
249 * @param name Name of the managed Heat stack instance
250 * @param path Optional path to the custom virtualenv
251 */
252def updateHeatStack(env, name, path = null) {
253 def python = new com.mirantis.mk.python()
254 cmd = "heat stack-update ${name}"
255 outputTable = runOpenstackCommand(cmd, env, path)
Ales Komareke11e8792016-12-28 09:42:25 +0100256 output = python.parseTextTable(outputTable, 'item', 'prettytable', path)
Sergey Kolekonovba203982016-12-21 18:32:17 +0400257 return output
258}
259
260/**
261 * Delete existing OpenStack Heat stack
262 *
263 * @param env Connection parameters for OpenStack API endpoint
264 * @param name Name of the managed Heat stack instance
265 * @param path Optional path to the custom virtualenv
266 */
267def deleteHeatStack(env, name, path = null) {
268 cmd = "heat stack-delete ${name}"
269 outputTable = runOpenstackCommand(cmd, env, path)
270}
271
272/**
273 * Return list of servers from OpenStack Heat stack
274 *
275 * @param env Connection parameters for OpenStack API endpoint
276 * @param name Name of the managed Heat stack instance
277 * @param path Optional path to the custom virtualenv
278 */
279def getHeatStackServers(env, name, path = null) {
280 resources = heatGetStackResources(env, name, path)
281 servers = []
282 for (resource in resources) {
283 if (resource.resource_type == 'OS::Nova::Server') {
284 resourceName = resource.resource_name
285 server = heatGetStackResourceInfo(env, name, resourceName, path)
286 servers.add(server.attributes.name)
287 }
288 }
289 echo("[Stack ${name}] Servers: ${servers}")
290 return servers
291}