chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
| 4 | * setup and test salt-master |
| 5 | * |
| 6 | * @param masterName salt master's name |
Jakub Josef | 2d01c84 | 2017-08-17 13:37:26 +0200 | [diff] [blame] | 7 | * @param clusterName model cluster name |
chnyda | 85f72e6 | 2017-05-26 15:43:27 +0200 | [diff] [blame] | 8 | * @param extraFormulas extraFormulas to install |
Mykyta Karpin | 1cb8d3f | 2017-06-23 15:42:17 +0300 | [diff] [blame] | 9 | * @param formulasSource formulas source (git or pkg) |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 10 | * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master) |
chnyda | f4e5eb4 | 2017-05-26 15:34:38 +0200 | [diff] [blame] | 11 | * @param testDir directory of model |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 12 | * @param formulasSource Salt formulas source type (optional, default pkg) |
Jakub Josef | fa6ad8d | 2017-06-26 18:29:55 +0200 | [diff] [blame] | 13 | * @param formulasRevision APT revision for formulas (optional default stable) |
Petr Michalec | 6414aa5 | 2017-08-17 14:32:52 +0200 | [diff] [blame] | 14 | * @param ignoreClassNotfound Ignore missing classes for reclass model |
Jakub Josef | 90d8e57 | 2017-10-03 18:44:44 +0200 | [diff] [blame] | 15 | * @param dockerMaxCpus max cpus passed to docker (default 0, disabled) |
| 16 | * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models) |
Dmitry Ukov | 630b9e1 | 2017-10-23 12:00:19 +0400 | [diff] [blame] | 17 | * @param aptRepoUrl package repository with salt formulas |
| 18 | * @param aptRepoGPG GPG key for apt repository with formulas |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 19 | */ |
| 20 | |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 21 | def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg', |
| 22 | formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0, |
| 23 | ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '') { |
Jakub Josef | 8fc0fd1 | 2018-01-15 18:36:48 +0100 | [diff] [blame] | 24 | // timeout for test execution (40min) |
| 25 | def testTimeout = 40 * 60 |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 26 | def dockerContainerName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}" |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 27 | def saltOpts = "--retcode-passthrough --force-color" |
| 28 | def common = new com.mirantis.mk.Common() |
| 29 | def workspace = common.getWorkspace() |
Jakub Josef | ecf8b30 | 2018-05-11 14:33:08 +0000 | [diff] [blame] | 30 | def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 31 | img.pull() |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 32 | |
chnyda | f6a0c95 | 2017-05-29 10:34:45 +0200 | [diff] [blame] | 33 | if (!extraFormulas || extraFormulas == "") { |
chnyda | 77e4697 | 2017-05-26 16:14:27 +0200 | [diff] [blame] | 34 | extraFormulas = "linux" |
chnyda | 66bfc58 | 2017-05-26 16:24:21 +0200 | [diff] [blame] | 35 | } |
chnyda | 77e4697 | 2017-05-26 16:14:27 +0200 | [diff] [blame] | 36 | |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 37 | def dockerMaxCpusOpt = "--cpus=4" |
Dennis Dmitriev | 3c3ee6d | 2017-07-25 20:02:44 +0300 | [diff] [blame] | 38 | if (dockerMaxCpus > 0) { |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 39 | dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}" |
Dennis Dmitriev | 3c3ee6d | 2017-07-25 20:02:44 +0300 | [diff] [blame] | 40 | } |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 41 | try { |
| 42 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") { |
| 43 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}", |
| 44 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 45 | "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "APT_REPOSITORY=${aptRepoUrl}", |
| 46 | "APT_REPOSITORY_GPG=${aptRepoGPG}", "SALT_STOPSTART_WAIT=10"]) { |
| 47 | // To be sure that we actually re-run |
| 48 | sh("rm -v ${env.WORKSPACE}/.test_finished || true") |
Jakub Josef | ecf8b30 | 2018-05-11 14:33:08 +0000 | [diff] [blame] | 49 | sh("git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts") |
Jakub Josef | 68f46d2 | 2018-05-11 21:34:55 +0200 | [diff] [blame] | 50 | sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 51 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 52 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;""") |
| 53 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 54 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 55 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion}; |
| 56 | done""") |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 57 | timeout(time: testTimeout, unit: 'SECONDS') { |
| 58 | sh('''#!/bin/bash |
| 59 | source /srv/salt/scripts/bootstrap.sh |
| 60 | cd /srv/salt/scripts |
| 61 | source_local_envs |
| 62 | configure_salt_master |
| 63 | configure_salt_minion |
| 64 | install_salt_formula_pkg |
| 65 | source /srv/salt/scripts/bootstrap.sh |
| 66 | cd /srv/salt/scripts |
| 67 | saltservice_restart''') |
| 68 | sh('''#!/bin/bash |
| 69 | source /srv/salt/scripts/bootstrap.sh |
| 70 | cd /srv/salt/scripts |
| 71 | source_local_envs |
| 72 | saltmaster_init''') |
chnyda | 3c6680a | 2017-05-29 16:55:34 +0200 | [diff] [blame] | 73 | |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 74 | if (!legacyTestingMode.toBoolean()) { |
| 75 | sh('''#!/bin/bash |
| 76 | source /srv/salt/scripts/bootstrap.sh |
| 77 | cd /srv/salt/scripts |
| 78 | verify_salt_minions''') |
| 79 | } |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 80 | } |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 81 | |
| 82 | // If we didn't dropped for now - test has been passed. |
| 83 | sh("touch ${env.WORKSPACE}/.test_finished") |
| 84 | } |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 85 | } |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 86 | } |
| 87 | finally { |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 88 | |
Jakub Josef | 9923de2 | 2018-05-11 20:18:22 +0200 | [diff] [blame] | 89 | if (legacyTestingMode.toBoolean()) { |
Jakub Josef | 90d8e57 | 2017-10-03 18:44:44 +0200 | [diff] [blame] | 90 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 91 | def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 92 | for (minion in nodes.tokenize()) { |
Jakub Josef | 2ccb69b | 2018-02-19 18:14:22 +0100 | [diff] [blame] | 93 | def basename = sh script: "set +x;basename ${minion} .yml", returnStdout: true |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 94 | if (!basename.trim().contains(masterName)) { |
| 95 | testMinion(basename.trim()) |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 96 | } |
| 97 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 98 | } |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 99 | |
| 100 | if (fileExists("${env.WORKSPACE}/.test_finished")) { |
| 101 | common.infoMsg("Test finished") |
| 102 | currentBuild.result = 'SUCCESS' |
| 103 | } else { |
| 104 | common.infoMsg("Test failed to finish!") |
| 105 | currentBuild.result = 'FAILURE' |
| 106 | } |
| 107 | try { |
| 108 | timeout(time: 10, unit: 'SECONDS') { |
| 109 | common.infoMsg("Cleanup slave...Ignore docker-daemon errors") |
| 110 | sh("set -x; docker kill ${dockerContainerName} || true") |
| 111 | sh("set -x; docker rm --force ${dockerContainerName} || true") |
| 112 | } |
| 113 | } |
| 114 | catch (Exception er) { |
| 115 | common.infoMsg("Timeout to delete test docker container!Continue...") |
| 116 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 117 | } |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 118 | |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Test salt-minion |
| 123 | * |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 124 | * @param minion salt minion |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 125 | */ |
| 126 | |
azvyagintsev | 130a590 | 2018-06-21 17:27:04 +0300 | [diff] [blame] | 127 | def testMinion(minionName) { |
chnyda | 532239d | 2017-10-05 13:36:01 +0200 | [diff] [blame] | 128 | sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'") |
Jakub Josef | fa6ad8d | 2017-06-26 18:29:55 +0200 | [diff] [blame] | 129 | } |