blob: 3eb003443d1197b3414d682384d58bd2648a8dbc [file] [log] [blame]
Petr Lomakin47fee0a2017-08-01 10:46:05 -07001package com.mirantis.mcp
2
3/**
4 *
5 * Tests providing functions
6 *
7 */
8
9/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060010 * Run docker container with basic (keystone) parameters
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050011 * For backward compatibility. Deprecated.
12 * Will be removed soon.
Petr Lomakin47fee0a2017-08-01 10:46:05 -070013 *
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060014 * @param target Host to run container
15 * @param dockerImageLink Docker image link. May be custom or default rally image
Petr Lomakin47fee0a2017-08-01 10:46:05 -070016 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050017def runBasicContainer(master, target, dockerImageLink="xrally/xrally-openstack:0.10.1"){
Petr Lomakin47fee0a2017-08-01 10:46:05 -070018 def salt = new com.mirantis.mk.Salt()
19 def common = new com.mirantis.mk.Common()
Sam Stoelinga28bdb722017-09-25 18:29:59 -070020 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
21 def keystone = _pillar['return'][0].values()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -060022 if ( salt.cmdRun(master, target, "docker ps -f name=cvp -q", false, null, false)['return'][0].values()[0] ) {
23 salt.cmdRun(master, target, "docker rm -f cvp")
24 }
25 salt.cmdRun(master, target, "docker run -tid --net=host --name=cvp " +
26 "-u root -e OS_USERNAME=${keystone.admin_name} " +
Petr Lomakin47fee0a2017-08-01 10:46:05 -070027 "-e OS_PASSWORD=${keystone.admin_password} -e OS_TENANT_NAME=${keystone.admin_tenant} " +
28 "-e OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0 " +
Oleksii Zhurba1bf9be12018-01-17 15:20:00 -060029 "-e OS_REGION_NAME=${keystone.region} -e OS_ENDPOINT_TYPE=admin --entrypoint /bin/bash ${dockerImageLink}")
Petr Lomakin47fee0a2017-08-01 10:46:05 -070030}
31
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -050032
33/**
34 * Run docker container with parameters
35 *
36 * @param target Host to run container
37 * @param dockerImageLink Docker image link. May be custom or default rally image
38 * @param name Name for container
39 * @param env_var Environment variables to set in container
40 * @param entrypoint Set entrypoint to /bin/bash or leave default
41**/
42
43
44def runContainer(master, target, dockerImageLink, name='cvp', env_var=[], entrypoint=true){
45 def salt = new com.mirantis.mk.Salt()
46 def common = new com.mirantis.mk.Common()
47 def variables = ''
48 def entry_point = ''
49 if ( salt.cmdRun(master, target, "docker ps -f name=${name} -q", false, null, false)['return'][0].values()[0] ) {
50 salt.cmdRun(master, target, "docker rm -f ${name}")
51 }
52 if (env_var.size() > 0) {
53 variables = ' -e ' + env_var.join(' -e ')
54 }
55 if (entrypoint) {
56 entry_point = '--entrypoint /bin/bash'
57 }
58 salt.cmdRun(master, target, "docker run -tid --net=host --name=${name} " +
59 "-u root ${entry_point} ${variables} ${dockerImageLink}")
60}
61
62
Petr Lomakin47fee0a2017-08-01 10:46:05 -070063/**
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070064 * Get file content (encoded). The content encoded by Base64.
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070065 *
66 * @param target Compound target (should target only one host)
67 * @param file File path to read
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070068 * @return The encoded content of the file
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070069 */
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070070def getFileContentEncoded(master, target, file) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -070071 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -070072 def file_content = ''
73 def cmd = "base64 -w0 ${file} > ${file}_encoded; " +
74 "split -b 1MB -d ${file}_encoded ${file}__; " +
75 "rm ${file}_encoded"
76 salt.cmdRun(master, target, cmd, false, null, false)
77 def filename = file.tokenize('/').last()
78 def folder = file - filename
79 def parts = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*"])
80 for ( part in parts['return'][0].values()[0]) {
81 def _result = salt.cmdRun(master, target, "cat ${part}", false, null, false)
82 file_content = file_content + _result['return'][0].values()[0].replaceAll('Salt command execution success','')
83 }
84 salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f", "name=${filename}__*", "delete"])
85 return file_content
86}
87
88/**
89 * Copy files from remote to local directory. The content of files will be
90 * decoded by Base64.
91 *
92 * @param target Compound target (should target only one host)
93 * @param folder The path to remote folder.
94 * @param output_dir The path to local folder.
95 */
96def addFiles(master, target, folder, output_dir) {
97 def salt = new com.mirantis.mk.Salt()
98 def _result = salt.runSaltProcessStep(master, target, 'file.find', ["${folder}", "type=f"])
99 def files = _result['return'][0].values()[0]
100 for (file in files) {
101 def file_content = getFileContentEncoded(master, target, "${file}")
102 def fileName = file.tokenize('/').last()
103 writeFile file: "${output_dir}${fileName}_encoded", text: file_content
104 def cmd = "base64 -d ${output_dir}${fileName}_encoded > ${output_dir}${fileName}; " +
105 "rm ${output_dir}${fileName}_encoded"
106 sh(script: cmd)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700107 }
108}
109
110/**
111 * Get reclass value
112 *
113 * @param target The host for which the values will be provided
114 * @param filter Parameters divided by dots
115 * @return The pillar data
116 */
117def getReclassValue(master, target, filter) {
118 def common = new com.mirantis.mk.Common()
119 def salt = new com.mirantis.mk.Salt()
120 def items = filter.tokenize('.')
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700121 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -p ${target}", false, null, false)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700122 _result = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700123 for (int k = 0; k < items.size(); k++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700124 if ( _result ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700125 _result = _result["${items[k]}"]
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700126 }
127 }
128 return _result
129}
130
131/**
132 * Create list of nodes in JSON format.
133 *
134 * @param filter The Salt's matcher
135 * @return JSON list of nodes
136 */
137def getNodeList(master, filter = null) {
138 def salt = new com.mirantis.mk.Salt()
139 def common = new com.mirantis.mk.Common()
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700140 def nodes = []
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700141 def filtered_list = null
142 def controllers = salt.getMinions(master, 'I@nova:controller')
143 def hw_nodes = salt.getMinions(master, 'G@virtual:physical')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700144 if ( filter ) {
145 filtered_list = salt.getMinions(master, filter)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700146 }
147 def _result = salt.cmdRun(master, 'I@salt:master', "reclass-salt -o json -t", false, null, false)
148 def reclass_top = common.parseJSON(_result['return'][0].values()[0])
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700149 def nodesList = reclass_top['base'].keySet()
150 for (int i = 0; i < nodesList.size(); i++) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700151 if ( filtered_list ) {
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700152 if ( ! filtered_list.contains(nodesList[i]) ) {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700153 continue
154 }
155 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700156 def ip = getReclassValue(master, nodesList[i], '_param.linux_single_interface.address')
157 def network_data = [ip: ip, name: 'management']
158 def roles = [nodesList[i].tokenize('.')[0]]
159 if ( controllers.contains(nodesList[i]) ) {
160 roles.add('controller')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700161 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700162 if ( hw_nodes.contains(nodesList[i]) ) {
163 roles.add('hw_node')
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700164 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700165 nodes.add([id: i+1, ip: ip, roles: roles, network_data: [network_data]])
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700166 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700167 return common.prettify(nodes)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700168}
169
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500170/**
171 * Execute mcp sanity tests
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500172 * Deprecated. Will be removed soon
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500173 *
174 * @param salt_url Salt master url
175 * @param salt_credentials Salt credentials
176 * @param test_set Test set for mcp sanity framework
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600177 * @param env_vars Additional environment variables for cvp-sanity-checks
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500178 * @param output_dir Directory for results
179 */
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600180def runSanityTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500181 def common = new com.mirantis.mk.Common()
Oleksii Zhurba0a7b0702017-11-10 16:02:16 -0600182 def creds = common.getCredentials(salt_credentials)
183 def username = creds.username
184 def password = creds.password
185 def settings = ""
186 if ( env_vars != "" ) {
187 for (var in env_vars.tokenize(";")) {
188 settings += "export ${var}; "
189 }
190 }
191 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
Oleksii Zhurba5250a9c2018-03-21 15:47:03 -0500192 "pytest --junitxml ${output_dir}cvp_sanity.xml --tb=short -sv ${env.WORKSPACE}/cvp-sanity-checks/cvp_checks/tests/${test_set}"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500193 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
194 def statusCode = sh script:script, returnStatus:true
195 }
196}
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700197
198/**
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600199 * Execute pytest framework tests
200 *
201 * @param salt_url Salt master url
202 * @param salt_credentials Salt credentials
203 * @param test_set Test set to run
204 * @param env_vars Additional environment variables for cvp-sanity-checks
205 * @param output_dir Directory for results
206 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500207def runPyTests(salt_url, salt_credentials, test_set="", env_vars="", name='cvp', container_node="", remote_dir='/root/qa_results/', artifacts_dir='validation_artifacts/') {
208 def xml_file = "${name}_report.xml"
209 def common = new com.mirantis.mk.Common()
210 def salt = new com.mirantis.mk.Salt()
211 def creds = common.getCredentials(salt_credentials)
212 def username = creds.username
213 def password = creds.password
214 if (container_node != "") {
215 def saltMaster
216 saltMaster = salt.connection(salt_url, salt_credentials)
217 def script = "pytest --junitxml ${xml_file} --tb=short -sv ${test_set}"
218 env_vars.addAll("SALT_USERNAME=${username}", "SALT_PASSWORD=${password}",
219 "SALT_URL=${salt_url}")
220 variables = ' -e ' + env_vars.join(' -e ')
221 salt.cmdRun(saltMaster, container_node, "docker exec ${variables} ${name} bash -c '${script}'", false)
222 salt.cmdRun(saltMaster, container_node, "docker cp ${name}:/var/lib/${xml_file} ${remote_dir}${xml_file}")
223 addFiles(saltMaster, container_node, remote_dir+xml_file, artifacts_dir)
224 }
225 else {
226 if (env_vars.size() > 0) {
227 variables = 'export ' + env_vars.join(';export ')
228 }
229 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${variables}; " +
230 "pytest --junitxml ${artifacts_dir}${xml_file} --tb=short -sv ${env.WORKSPACE}/${test_set}"
231 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
232 def statusCode = sh script:script, returnStatus:true
233 }
234 }
235}
236
237/**
238 * Execute pytest framework tests
239 * For backward compatibility
240 * Will be removed soon
241 *
242 * @param salt_url Salt master url
243 * @param salt_credentials Salt credentials
244 * @param test_set Test set to run
245 * @param env_vars Additional environment variables for cvp-sanity-checks
246 * @param output_dir Directory for results
247 */
Oleksii Zhurba4e366ff2018-02-16 20:06:52 -0600248def runTests(salt_url, salt_credentials, test_set="", output_dir="validation_artifacts/", env_vars="") {
249 def common = new com.mirantis.mk.Common()
250 def creds = common.getCredentials(salt_credentials)
251 def username = creds.username
252 def password = creds.password
253 def settings = ""
254 if ( env_vars != "" ) {
255 for (var in env_vars.tokenize(";")) {
256 settings += "export ${var}; "
257 }
258 }
259 def script = ". ${env.WORKSPACE}/venv/bin/activate; ${settings}" +
260 "pytest --junitxml ${output_dir}report.xml --tb=short -sv ${env.WORKSPACE}/${test_set}"
261 withEnv(["SALT_USERNAME=${username}", "SALT_PASSWORD=${password}", "SALT_URL=${salt_url}"]) {
262 def statusCode = sh script:script, returnStatus:true
263 }
264}
265
266/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700267 * Execute tempest tests
268 *
269 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700270 * @param dockerImageLink Docker image link
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700271 * @param pattern If not false, will run only tests matched the pattern
272 * @param output_dir Directory for results
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800273 * @param confRepository Git repository with configuration files for Tempest
274 * @param confBranch Git branch which will be used during the checkout
275 * @param repository Git repository with Tempest
276 * @param version Version of Tempest (tag, branch or commit)
Sergey Galkind1068e22018-02-13 13:59:32 +0400277 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700278 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400279def runTempestTests(master, target, dockerImageLink, output_dir, confRepository, confBranch, repository, version, pattern = "false", results = '/root/qa_results') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700280 def salt = new com.mirantis.mk.Salt()
281 def output_file = 'docker-tempest.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700282 def dest_folder = '/home/rally/qa_results'
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800283 def skip_list = '--skip-list /opt/devops-qa-tools/deployment/skip_contrail.list'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700284 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
285 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
286 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
287 def keystone = _pillar['return'][0].values()[0]
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000288 def env_vars = ['tempest_version=15.0.0',
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700289 "OS_USERNAME=${keystone.admin_name}",
290 "OS_PASSWORD=${keystone.admin_password}",
291 "OS_TENANT_NAME=${keystone.admin_tenant}",
292 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
293 "OS_REGION_NAME=${keystone.region}",
294 'OS_ENDPOINT_TYPE=admin'].join(' -e ')
295 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; '
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800296 if (confRepository != '' ) {
297 cmd = "git clone -b ${confBranch ?: 'master'} ${confRepository} test_config; " +
298 'rally deployment create --fromenv --name=tempest; rally deployment config; ' +
299 'rally verify create-verifier --name tempest_verifier --type tempest ' +
Dmitry Tsapikovb6911922018-07-24 15:21:23 +0000300 "--source ${repository ?: '/tmp/tempest/'} --version ${version: '15.0.0'}; " +
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800301 'rally verify configure-verifier --extend test_config/tempest/tempest.conf --show; '
302 skip_list = '--skip-list test_config/tempest/skip-list.yaml'
303 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700304 if (pattern == 'false') {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800305 cmd += "rally verify start --pattern set=full ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700306 }
307 else {
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800308 cmd += "rally verify start --pattern set=${pattern} ${skip_list} --detailed; "
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700309 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700310 cmd += "rally verify report --type json --to ${dest_folder}/report-tempest.json; " +
311 "rally verify report --type html --to ${dest_folder}/report-tempest.html"
mkraynovd49daf52018-07-12 16:11:14 +0400312 salt.cmdRun(master, target, "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin193ef872017-11-29 14:20:35 +0400313 "-v ${results}:${dest_folder} --entrypoint /bin/bash ${dockerImageLink} " +
314 "-c \"${cmd}\" > ${results}/${output_file}")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700315 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700316}
317
318/**
Oleg Basov40e502c2018-09-04 20:42:21 +0200319 * Make all-in-one scenario cmd for rally tests
320 *
321 * @param scenarios_path Path to scenarios folder/file
322 * @param skip_scenarios Comma-delimited list of scenarios names to skip
323 * @param bundle_file Bundle name to create
324*/
325def bundle_up_scenarios(scenarios_path, skip_scenarios, bundle_file) {
326 def skip_names = ''
327 def skip_dirs = ''
328 def result = ''
329 if (skip_scenarios != ''){
330 for ( scen in skip_scenarios.split(',') ) {
331 if ( scen.contains('yaml')) {
332 skip_names += "! -name ${scen} "
333 }
334 else {
335 skip_dirs += "-path ${scenarios_path}/${scen} -prune -o "
336 }
337 }
338 }
339 result = "if [ -f ${scenarios_path} ]; then cp ${scenarios_path} ${bundle_file}; " +
340 "else " +
341 "find -L ${scenarios_path} " + skip_dirs +
342 " -name '*.yaml' " + skip_names +
343 "-exec cat {} >> ${bundle_file} \\; ; " +
344 "sed -i '/---/d' ${bundle_file}; fi; "
345
346 return result
347}
348
349/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700350 * Execute rally tests
351 *
352 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700353 * @param dockerImageLink Docker image link
Oleg Basov20afb152018-06-10 03:09:25 +0200354 * @param platform What do we have underneath (openstack/k8s)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700355 * @param output_dir Directory for results
Oleg Basov40e502c2018-09-04 20:42:21 +0200356 * @param config_repo Git repository with with files for Rally
357 * @param config_branch Git config repo branch which will be used during the checkout
358 * @param plugins_repo Git repository with Rally plugins
359 * @param plugins_branch Git plugins repo branch which will be used during the checkout
Oleg Basov20afb152018-06-10 03:09:25 +0200360 * @param scenarios Directory inside repo with specific scenarios
Oleg Basov40e502c2018-09-04 20:42:21 +0200361 * @param sl_scenarios Directory inside repo with specific scenarios for stacklight
Oleg Basov20afb152018-06-10 03:09:25 +0200362 * @param tasks_args_file Argument file that is used for throttling settings
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700363 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400364 * @param results The reports directory
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700365 */
Oleg Basov40e502c2018-09-04 20:42:21 +0200366def runRallyTests(master, target, dockerImageLink, platform, output_dir, config_repo, config_branch, plugins_repo, plugins_branch, scenarios, sl_scenarios = '', tasks_args_file = '', ext_variables = [], results = '/root/qa_results', skip_list = '') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700367 def salt = new com.mirantis.mk.Salt()
368 def output_file = 'docker-rally.log'
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700369 def dest_folder = '/home/rally/qa_results'
Oleg Basov20afb152018-06-10 03:09:25 +0200370 def env_vars = []
371 def rally_extra_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200372 def cmd_rally_plugins =
373 "git clone -b ${plugins_branch ?: 'master'} ${plugins_repo} /tmp/plugins; " +
374 "sudo pip install --upgrade /tmp/plugins; "
Oleg Basov20afb152018-06-10 03:09:25 +0200375 def cmd_rally_init = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200376 def cmd_rally_checkout = "git clone -b ${config_branch ?: 'master'} ${config_repo} test_config; "
Oleg Basov20afb152018-06-10 03:09:25 +0200377 def cmd_rally_start = ''
378 def cmd_rally_task_args = ''
Oleg Basov40e502c2018-09-04 20:42:21 +0200379 def cmd_rally_stacklight = ''
380 def cmd_rally_report = ''
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700381 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
382 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
Oleg Basov40e502c2018-09-04 20:42:21 +0200383 if (platform['type'] == 'openstack') {
Oleg Basov20afb152018-06-10 03:09:25 +0200384 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
385 def keystone = _pillar['return'][0].values()[0]
386 env_vars = ( ['tempest_version=15.0.0',
387 "OS_USERNAME=${keystone.admin_name}",
388 "OS_PASSWORD=${keystone.admin_password}",
389 "OS_TENANT_NAME=${keystone.admin_tenant}",
390 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
391 "OS_REGION_NAME=${keystone.region}",
392 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
Oleg Basov40e502c2018-09-04 20:42:21 +0200393 cmd_rally_init = 'rally db create; ' +
394 'rally deployment create --fromenv --name=existing; ' +
395 'rally deployment config; '
396 if (platform['stacklight_enabled'] == true) {
397 cmd_rally_stacklight = bundle_up_scenarios(sl_scenarios, skip_list, "scenarios_${platform.type}_stacklight.yaml")
398 cmd_rally_stacklight += "rally $rally_extra_args task start scenarios_${platform.type}_stacklight.yaml " +
399 "--task-args-file test_config/job-params-stacklight.yaml; "
Oleg Basov20afb152018-06-10 03:09:25 +0200400 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200401 } else if (platform['type'] == 'k8s') {
Oleg Basov20afb152018-06-10 03:09:25 +0200402 rally_extra_args = "--debug --log-file ${dest_folder}/task.log"
Oleg Basov3e050d32018-07-03 22:10:56 +0200403 def _pillar = salt.getPillar(master, 'I@kubernetes:master and *01*', 'kubernetes:master')
404 def kubernetes = _pillar['return'][0].values()[0]
405 env_vars = [
406 "KUBERNETES_HOST=${kubernetes.apiserver.vip_address}" +
407 ":${kubernetes.apiserver.insecure_port}",
408 "KUBERNETES_CERT_AUTH=${dest_folder}/k8s-ca.crt",
409 "KUBERNETES_CLIENT_KEY=${dest_folder}/k8s-client.key",
410 "KUBERNETES_CLIENT_CERT=${dest_folder}/k8s-client.crt"].join(' -e ')
411 def k8s_ca = salt.getReturnValues(salt.runSaltProcessStep(master,
Oleg Basov20afb152018-06-10 03:09:25 +0200412 'I@kubernetes:master and *01*', 'cmd.run',
Oleg Basov3e050d32018-07-03 22:10:56 +0200413 ["cat /etc/kubernetes/ssl/ca-kubernetes.crt"]))
414 def k8s_client_key = salt.getReturnValues(salt.runSaltProcessStep(master,
415 'I@kubernetes:master and *01*', 'cmd.run',
416 ["cat /etc/kubernetes/ssl/kubelet-client.key"]))
417 def k8s_client_crt = salt.getReturnValues(salt.runSaltProcessStep(master,
418 'I@kubernetes:master and *01*', 'cmd.run',
419 ["cat /etc/kubernetes/ssl/kubelet-client.crt"]))
Oleg Basov20afb152018-06-10 03:09:25 +0200420 def tmp_dir = '/tmp/kube'
421 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${tmp_dir}", "mode=777"])
Oleg Basov3e050d32018-07-03 22:10:56 +0200422 writeFile file: "${tmp_dir}/k8s-ca.crt", text: k8s_ca
423 writeFile file: "${tmp_dir}/k8s-client.key", text: k8s_client_key
424 writeFile file: "${tmp_dir}/k8s-client.crt", text: k8s_client_crt
Oleg Basov20afb152018-06-10 03:09:25 +0200425 salt.cmdRun(master, target, "mv ${tmp_dir}/* ${results}/")
426 salt.runSaltProcessStep(master, target, 'file.rmdir', ["${tmp_dir}"])
Oleg Basov40e502c2018-09-04 20:42:21 +0200427 cmd_rally_init = "rally db recreate; " +
Oleg Basov3e050d32018-07-03 22:10:56 +0200428 "rally env create --name k8s --from-sysenv; " +
Oleg Basov20afb152018-06-10 03:09:25 +0200429 "rally env check k8s; "
Oleg Basov20afb152018-06-10 03:09:25 +0200430 } else {
431 throw new Exception("Platform ${platform} is not supported yet")
Dmitrii Kabanov999fda92017-11-10 00:18:30 -0800432 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200433 cmd_rally_checkout += bundle_up_scenarios(scenarios, skip_list, "scenarios_${platform.type}.yaml")
434 cmd_rally_start = "rally $rally_extra_args task start scenarios_${platform.type}.yaml "
435 if (config_repo != '' ) {
Oleg Basov20afb152018-06-10 03:09:25 +0200436 switch(tasks_args_file) {
437 case 'none':
438 cmd_rally_task_args = '; '
439 break
440 case '':
441 cmd_rally_task_args = '--task-args-file test_config/job-params-light.yaml; '
442 break
443 default:
444 cmd_rally_task_args = "--task-args-file ${tasks_args_file}; "
445 break
446 }
447 }
Oleg Basov40e502c2018-09-04 20:42:21 +0200448 cmd_rally_report= "rally task export --type junit-xml --to ${dest_folder}/report-rally.xml; " +
449 "rally task report --out ${dest_folder}/report-rally.html"
450 full_cmd = 'set -xe; ' + cmd_rally_plugins +
451 cmd_rally_init + cmd_rally_checkout +
452 'set +e; ' + cmd_rally_start +
453 cmd_rally_task_args + cmd_rally_stacklight +
454 cmd_rally_report
Sergey Galkinf89509d2018-03-19 15:24:17 +0400455 salt.runSaltProcessStep(master, target, 'file.touch', ["${results}/rally.db"])
456 salt.cmdRun(master, target, "chmod 666 ${results}/rally.db")
mkraynovd49daf52018-07-12 16:11:14 +0400457 salt.cmdRun(master, target, "docker run -w /home/rally -i --rm --net=host -e ${env_vars} " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400458 "-v ${results}:${dest_folder} " +
mkraynovd49daf52018-07-12 16:11:14 +0400459 "-v ${results}/rally.db:/home/rally/data/rally.db " +
Sergey Galkin3c1e9e22018-01-12 16:31:53 +0400460 "--entrypoint /bin/bash ${dockerImageLink} " +
461 "-c \"${full_cmd}\" > ${results}/${output_file}")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700462 addFiles(master, target, results, output_dir)
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700463}
464
465/**
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700466 * Generate test report
467 *
468 * @param target Host to run script from
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700469 * @param dockerImageLink Docker image link
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700470 * @param output_dir Directory for results
Sergey Galkind1068e22018-02-13 13:59:32 +0400471 * @param results The reports directory
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700472 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400473def generateTestReport(master, target, dockerImageLink, output_dir, results = '/root/qa_results') {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700474 def report_file = 'jenkins_test_report.html'
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700475 def salt = new com.mirantis.mk.Salt()
476 def common = new com.mirantis.mk.Common()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700477 def dest_folder = '/opt/devops-qa-tools/generate_test_report/test_results'
478 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
479 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
480 def reports = ['report-tempest.json',
481 'report-rally.xml',
482 'report-k8s-e2e-tests.txt',
483 'report-ha.json',
484 'report-spt.txt']
485 for ( report in reports ) {
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700486 if ( fileExists("${output_dir}${report}") ) {
487 common.infoMsg("Copying ${report} to docker container")
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700488 def items = sh(script: "base64 -w0 ${output_dir}${report} > ${output_dir}${report}_encoded; " +
489 "split -b 100KB -d -a 4 ${output_dir}${report}_encoded ${output_dir}${report}__; " +
490 "rm ${output_dir}${report}_encoded; " +
491 "find ${output_dir} -type f -name ${report}__* -printf \'%f\\n\' | sort", returnStdout: true)
492 for ( item in items.tokenize() ) {
493 def content = sh(script: "cat ${output_dir}${item}", returnStdout: true)
494 salt.cmdRun(master, target, "echo \"${content}\" >> ${results}/${report}_encoded", false, null, false)
495 sh(script: "rm ${output_dir}${item}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700496 }
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700497 salt.cmdRun(master, target, "base64 -d ${results}/${report}_encoded > ${results}/${report}; " +
498 "rm ${results}/${report}_encoded", false, null, false)
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700499 }
500 }
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700501
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700502 def cmd = "jenkins_report.py --path /opt/devops-qa-tools/generate_test_report/; " +
503 "cp ${report_file} ${dest_folder}/${report_file}"
504 salt.cmdRun(master, target, "docker run -i --rm --net=host " +
505 "-v ${results}:${dest_folder} ${dockerImageLink} " +
506 "/bin/bash -c \"${cmd}\"")
507 def report_content = salt.getFileContent(master, target, "${results}/${report_file}")
Tetiana Korchak3383cc92017-08-25 09:36:19 -0700508 writeFile file: "${output_dir}${report_file}", text: report_content
509}
510
511/**
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700512 * Execute SPT tests
513 *
514 * @param target Host to run tests
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700515 * @param dockerImageLink Docker image link
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700516 * @param output_dir Directory for results
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700517 * @param ext_variables The list of external variables
Sergey Galkind1068e22018-02-13 13:59:32 +0400518 * @param results The reports directory
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700519 */
Sergey Galkind1068e22018-02-13 13:59:32 +0400520def runSptTests(master, target, dockerImageLink, output_dir, ext_variables = [], results = '/root/qa_results') {
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700521 def salt = new com.mirantis.mk.Salt()
Dmitrii Kabanov23901c22017-10-20 10:25:36 -0700522 def dest_folder = '/home/rally/qa_results'
523 salt.runSaltProcessStep(master, target, 'file.remove', ["${results}"])
524 salt.runSaltProcessStep(master, target, 'file.mkdir', ["${results}", "mode=777"])
525 def nodes = getNodeList(master)
526 def nodes_hw = getNodeList(master, 'G@virtual:physical')
527 def _pillar = salt.getPillar(master, 'I@keystone:server', 'keystone:server')
528 def keystone = _pillar['return'][0].values()[0]
529 def ssh_key = salt.getFileContent(master, 'I@salt:master', '/root/.ssh/id_rsa')
530 def env_vars = ( ['tempest_version=15.0.0',
531 "OS_USERNAME=${keystone.admin_name}",
532 "OS_PASSWORD=${keystone.admin_password}",
533 "OS_TENANT_NAME=${keystone.admin_tenant}",
534 "OS_AUTH_URL=http://${keystone.bind.private_address}:${keystone.bind.private_port}/v2.0",
535 "OS_REGION_NAME=${keystone.region}",
536 'OS_ENDPOINT_TYPE=admin'] + ext_variables ).join(' -e ')
537 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes.json", nodes])
538 salt.runSaltProcessStep(master, target, 'file.write', ["${results}/nodes_hw.json", nodes_hw])
539 def cmd = '/opt/devops-qa-tools/deployment/configure.sh; ' +
540 'sudo mkdir -p /root/.ssh; sudo chmod 700 /root/.ssh; ' +
541 "echo \\\"${ssh_key}\\\" | sudo tee /root/.ssh/id_rsa > /dev/null; " +
542 'sudo chmod 600 /root/.ssh/id_rsa; ' +
543 "sudo timmy -c simplified-performance-testing/config.yaml " +
544 "--nodes-json ${dest_folder}/nodes.json --log-file ${dest_folder}/docker-spt2.log; " +
545 "./simplified-performance-testing/SPT_parser.sh > ${dest_folder}/report-spt.txt; " +
546 "custom_spt_parser.sh ${dest_folder}/nodes_hw.json > ${dest_folder}/report-spt-hw.txt; " +
547 "cp /tmp/timmy/archives/general.tar.gz ${dest_folder}/results-spt.tar.gz"
548 salt.cmdRun(master, target, "docker run -i --rm --net=host -e ${env_vars} " +
549 "-v ${results}:${dest_folder} ${dockerImageLink} /bin/bash -c " +
550 "\"${cmd}\" > ${results}/docker-spt.log")
551 addFiles(master, target, results, output_dir)
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700552}
553
Dmitrii Kabanovd5f1c5f2017-08-30 14:51:41 -0700554/**
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600555 * Configure docker container
556 *
557 * @param target Host to run container
558 * @param proxy Proxy for accessing github and pip
559 * @param testing_tools_repo Repo with testing tools: configuration script, skip-list, etc.
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600560 * @param tempest_repo Tempest repo to clone. Can be upstream tempest (default, recommended), your customized tempest in local/remote repo or path inside container. If not specified, tempest will not be configured.
561 * @param tempest_endpoint_type internalURL or adminURL or publicURL to use in tests
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500562 * @param tempest_version Version of tempest to use. This value will be just passed to configure.sh script (cvp-configuration repo).
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600563 * @param conf_script_path Path to configuration script.
564 * @param ext_variables Some custom extra variables to add into container
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600565 */
566def configureContainer(master, target, proxy, testing_tools_repo, tempest_repo,
Oleksii Zhurba198dd682018-09-07 18:16:59 -0500567 tempest_endpoint_type="internalURL", tempest_version="",
Oleksii Zhurba1579b972017-12-14 15:21:56 -0600568 conf_script_path="", ext_variables = []) {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600569 def salt = new com.mirantis.mk.Salt()
570 if (testing_tools_repo != "" ) {
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500571 if (testing_tools_repo.contains('http://') || testing_tools_repo.contains('https://')) {
572 salt.cmdRun(master, target, "docker exec cvp git clone ${testing_tools_repo} cvp-configuration")
573 configure_script = conf_script_path != "" ? conf_script_path : "cvp-configuration/configure.sh"
574 }
575 else {
576 configure_script = testing_tools_repo
577 }
578 ext_variables.addAll("PROXY=${proxy}", "TEMPEST_REPO=${tempest_repo}",
579 "TEMPEST_ENDPOINT_TYPE=${tempest_endpoint_type}",
580 "tempest_version=${tempest_version}")
581 salt.cmdRun(master, target, "docker exec -e " + ext_variables.join(' -e ') + " cvp bash -c ${configure_script}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600582 }
Oleksii Zhurba0cda6e02018-06-20 14:53:19 -0500583 else {
584 common.infoMsg("TOOLS_REPO is empty, no confguration is needed for container")
585 }
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600586}
587
588/**
589 * Run Tempest
590 *
591 * @param target Host to run container
592 * @param test_pattern Test pattern to run
593 * @param skip_list Path to skip-list
594 * @param output_dir Directory on target host for storing results (containers is not a good place)
595 */
596def runCVPtempest(master, target, test_pattern="set=smoke", skip_list="", output_dir, output_filename="docker-tempest") {
597 def salt = new com.mirantis.mk.Salt()
598 def xml_file = "${output_filename}.xml"
Oleksii Zhurba44045312017-12-12 15:38:26 -0600599 def html_file = "${output_filename}.html"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600600 skip_list_cmd = ''
601 if (skip_list != '') {
602 skip_list_cmd = "--skip-list ${skip_list}"
603 }
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500604 salt.cmdRun(master, target, "docker exec cvp rally verify start --pattern ${test_pattern} ${skip_list_cmd} --detailed")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600605 salt.cmdRun(master, target, "docker exec cvp rally verify report --type junit-xml --to /home/rally/${xml_file}")
Oleksii Zhurba44045312017-12-12 15:38:26 -0600606 salt.cmdRun(master, target, "docker exec cvp rally verify report --type html --to /home/rally/${html_file}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600607 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
Oleksii Zhurba44045312017-12-12 15:38:26 -0600608 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500609 return salt.cmdRun(master, target, "docker exec cvp rally verify show | head -5 | tail -1 | " +
610 "awk '{print \$4}'")['return'][0].values()[0].split()[0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600611}
612
613/**
614 * Run Rally
615 *
616 * @param target Host to run container
617 * @param test_pattern Test pattern to run
618 * @param scenarios_path Path to Rally scenarios
619 * @param output_dir Directory on target host for storing results (containers is not a good place)
620 */
621def runCVPrally(master, target, scenarios_path, output_dir, output_filename="docker-rally") {
622 def salt = new com.mirantis.mk.Salt()
623 def xml_file = "${output_filename}.xml"
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600624 def html_file = "${output_filename}.html"
Oleksii Zhurba77896d42018-05-25 18:11:30 -0500625 salt.cmdRun(master, target, "docker exec cvp rally task start ${scenarios_path}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600626 salt.cmdRun(master, target, "docker exec cvp rally task report --out ${html_file}")
Oleksii Zhurba1bf9be12018-01-17 15:20:00 -0600627 salt.cmdRun(master, target, "docker exec cvp rally task report --junit --out ${xml_file}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600628 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${xml_file} ${output_dir}")
629 salt.cmdRun(master, target, "docker cp cvp:/home/rally/${html_file} ${output_dir}")
630}
631
632
633/**
634 * Shutdown node
635 *
636 * @param target Host to run command
637 * @param mode How to shutdown node
638 * @param retries # of retries to make to check node status
639 */
640def shutdown_vm_node(master, target, mode, retries=200) {
641 def salt = new com.mirantis.mk.Salt()
642 def common = new com.mirantis.mk.Common()
643 if (mode == 'reboot') {
644 try {
645 def out = salt.runSaltCommand(master, 'local', ['expression': target, 'type': 'compound'], 'cmd.run', null, ['reboot'], null, 3, 3)
646 } catch (Exception e) {
647 common.warningMsg('Timeout from minion: node must be rebooting now')
648 }
649 common.warningMsg("Checking that minion is down")
650 status = "True"
651 for (i = 0; i < retries; i++) {
652 status = salt.minionsReachable(master, 'I@salt:master', target, null, 5, 1)
653 if (status != "True") {
654 break
655 }
656 }
657 if (status == "True") {
658 throw new Exception("Tired to wait for minion ${target} to stop responding")
659 }
660 }
661 if (mode == 'hard_shutdown' || mode == 'soft_shutdown') {
662 kvm = locate_node_on_kvm(master, target)
663 if (mode == 'soft_shutdown') {
664 salt.cmdRun(master, target, "shutdown -h 0")
665 }
666 if (mode == 'hard_shutdown') {
667 salt.cmdRun(master, kvm, "virsh destroy ${target}")
668 }
669 common.warningMsg("Checking that vm on kvm is in power off state")
670 status = 'running'
671 for (i = 0; i < retries; i++) {
672 status = check_vm_status(master, target, kvm)
673 echo "Current status - ${status}"
674 if (status != 'running') {
675 break
676 }
677 sleep (1)
678 }
679 if (status == 'running') {
680 throw new Exception("Tired to wait for node ${target} to shutdown")
681 }
682 }
683}
684
685
686/**
687 * Locate kvm where target host is located
688 *
689 * @param target Host to check
690 */
691def locate_node_on_kvm(master, target) {
692 def salt = new com.mirantis.mk.Salt()
693 def list = salt.runSaltProcessStep(master, "I@salt:control", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
694 for (item in list.keySet()) {
695 if (list[item]) {
696 return item
697 }
698 }
699}
700
701/**
702 * Check target host status
703 *
704 * @param target Host to check
705 * @param kvm KVM node where target host is located
706 */
707def check_vm_status(master, target, kvm) {
708 def salt = new com.mirantis.mk.Salt()
709 def list = salt.runSaltProcessStep(master, "${kvm}", 'cmd.run', ["virsh list --all | grep ' ${target}'"])['return'][0]
710 for (item in list.keySet()) {
711 if (list[item]) {
712 return list[item].split()[2]
713 }
714 }
715}
716
717/**
718 * Find vip on nodes
719 *
720 * @param target Pattern, e.g. ctl*
721 */
722def get_vip_node(master, target) {
723 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba5f73cf62018-08-03 16:11:10 -0500724 def list = salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["ip a | grep '/32'"])['return'][0]
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600725 for (item in list.keySet()) {
726 if (list[item]) {
727 return item
728 }
729 }
730}
731
732/**
733 * Find vip on nodes
734 *
735 * @param target Host with cvp container
736 */
Oleksii Zhurba5250a9c2018-03-21 15:47:03 -0500737def openstack_cleanup(master, target, script_path="/home/rally/cvp-configuration/cleanup.sh") {
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600738 def salt = new com.mirantis.mk.Salt()
739 salt.runSaltProcessStep(master, "${target}", 'cmd.run', ["docker exec cvp bash -c ${script_path}"])
740}
741
742
743/**
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700744 * Cleanup
745 *
746 * @param target Host to run commands
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500747 * @param name Name of container to remove
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700748 */
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500749def runCleanup(master, target, name='cvp') {
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700750 def salt = new com.mirantis.mk.Salt()
Oleksii Zhurba1f4a6ff2018-06-27 16:45:17 -0500751 if ( salt.cmdRun(master, target, "docker ps -f name=${name} -q", false, null, false)['return'][0].values()[0] ) {
752 salt.cmdRun(master, target, "docker rm -f ${name}")
Oleksii Zhurba7b44ef12017-11-13 17:50:16 -0600753 }
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700754}
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500755/**
756 * Prepare venv for any python project
757 * Note: <repo_name>\/requirements.txt content will be used
758 * for this venv
759 *
760 * @param repo_url Repository url to clone
761 * @param proxy Proxy address to use
762 */
763def prepareVenv(repo_url, proxy) {
764 def python = new com.mirantis.mk.Python()
765 repo_name = "${repo_url}".tokenize("/").last()
Oleksii Zhurbae711ebb2018-06-15 16:36:38 -0500766 if (repo_url.tokenize().size() > 1){
767 if (repo_url.tokenize()[1] == '-b'){
768 repo_name = repo_url.tokenize()[0].tokenize("/").last()
769 }
770 }
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500771 path_venv = "${env.WORKSPACE}/venv"
772 path_req = "${env.WORKSPACE}/${repo_name}/requirements.txt"
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500773 sh "rm -rf ${repo_name}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500774 // this is temporary W/A for offline deployments
775 // Jenkins slave image has /opt/pip-mirror/ folder
776 // where pip wheels for cvp projects are located
777 if (proxy != 'offline') {
778 withEnv(["HTTPS_PROXY=${proxy}", "HTTP_PROXY=${proxy}", "https_proxy=${proxy}", "http_proxy=${proxy}"]) {
779 sh "git clone ${repo_url}"
780 python.setupVirtualenv(path_venv, "python2", [], path_req, true)
781 }
782 }
783 else {
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500784 sh "git clone ${repo_url}"
Oleksii Zhurba83e3e5c2018-06-27 16:59:29 -0500785 sh "virtualenv ${path_venv} --python python2"
786 python.runVirtualenvCommand(path_venv, "pip install --no-index --find-links=/opt/pip-mirror/ -r ${path_req}", true)
Oleksii Zhurbabcb97e22017-10-05 14:10:39 -0500787 }
788}
789
Petr Lomakin47fee0a2017-08-01 10:46:05 -0700790/** Install docker if needed
791 *
792 * @param target Target node to install docker pkg
793 */
794def installDocker(master, target) {
795 def salt = new com.mirantis.mk.Salt()
796 if ( ! salt.runSaltProcessStep(master, target, 'pkg.version', ["docker-engine"]) ) {
797 salt.runSaltProcessStep(master, target, 'pkg.install', ["docker.io"])
798 }
799}