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 | * |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 6 | * @param masterName salt master's name |
| 7 | * @param clusterName model cluster name |
| 8 | * @param extraFormulas extraFormulas to install. DEPRECATED |
| 9 | * @param formulasSource formulas source (git or pkg) |
| 10 | * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master) |
| 11 | * @param testDir directory of model |
| 12 | * @param formulasSource Salt formulas source type (optional, default pkg) |
| 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 |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [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) |
| 17 | * @param aptRepoUrl package repository with salt formulas |
| 18 | * @param aptRepoGPG GPG key for apt repository with formulas |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 19 | * Return true | false |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 20 | */ |
| 21 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 22 | def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg', |
Vasyl Saienko | 369ed90 | 2018-07-23 11:49:32 +0000 | [diff] [blame] | 23 | formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0, |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 24 | ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 25 | def common = new com.mirantis.mk.Common() |
| 26 | // timeout for test execution (40min) |
| 27 | def testTimeout = 40 * 60 |
| 28 | def TestMarkerResult = false |
| 29 | def saltOpts = "--retcode-passthrough --force-color" |
| 30 | def workspace = common.getWorkspace() |
| 31 | def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
| 32 | img.pull() |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 33 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 34 | if (!extraFormulas.contains('*') && formulasSource == 'pkg') { |
| 35 | common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " + |
| 36 | "\n It would be ignored, and all formulas would be installed anyway") |
| 37 | } |
| 38 | if (!dockerContainerName) { |
| 39 | dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString() |
| 40 | } |
| 41 | def dockerMaxCpusOpt = "--cpus=4" |
| 42 | if (dockerMaxCpus > 0) { |
| 43 | dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}" |
| 44 | } |
| 45 | try { |
| 46 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") { |
| 47 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", |
| 48 | "DISTRIB_REVISION=${formulasRevision}", |
| 49 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", |
| 50 | "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 51 | "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", |
| 52 | "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5", |
| 53 | "APT_REPOSITORY_GPG=${aptRepoGPG}"]) { |
| 54 | try { |
| 55 | // Currently, we don't have any other point to install |
| 56 | // runtime dependencies for tests. |
| 57 | sh("""#!/bin/bash -xe |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 58 | echo "Installing extra-deb dependencies inside docker:" |
| 59 | echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode |
| 60 | echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 61 | echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 62 | rm -vf /etc/apt/sources.list.d/* || true |
| 63 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list |
| 64 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list |
| 65 | apt-get update |
| 66 | apt-get install -y python-netaddr |
| 67 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 68 | sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true) |
| 69 | sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 70 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 71 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 72 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 73 | // FIXME: should be changed to use reclass from mcp_extra_nigtly? |
| 74 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 75 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
| 76 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion}; |
| 77 | done""") |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 78 | timeout(time: testTimeout, unit: 'SECONDS') { |
| 79 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 80 | source /srv/salt/scripts/bootstrap.sh |
| 81 | cd /srv/salt/scripts |
| 82 | source_local_envs |
| 83 | configure_salt_master |
| 84 | configure_salt_minion |
| 85 | install_salt_formula_pkg |
| 86 | source /srv/salt/scripts/bootstrap.sh |
| 87 | cd /srv/salt/scripts |
| 88 | saltservice_restart''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 89 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 90 | source /srv/salt/scripts/bootstrap.sh |
| 91 | cd /srv/salt/scripts |
| 92 | source_local_envs |
| 93 | saltmaster_init''') |
| 94 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 95 | if (!legacyTestingMode.toBoolean()) { |
| 96 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 97 | source /srv/salt/scripts/bootstrap.sh |
| 98 | cd /srv/salt/scripts |
| 99 | verify_salt_minions |
| 100 | ''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 101 | } |
| 102 | } |
| 103 | // If we didn't dropped for now - test has been passed. |
| 104 | TestMarkerResult = true |
| 105 | } |
| 106 | |
| 107 | finally { |
| 108 | // Collect rendered per-node data.Those info could be simply used |
| 109 | // for diff processing. Data was generated via reclass.cli --nodeinfo, |
| 110 | /// during verify_salt_minions. |
| 111 | sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true) |
| 112 | archiveArtifacts artifacts: "nodesinfo.tar.gz" |
| 113 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 114 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 115 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 116 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 117 | catch (Exception er) { |
| 118 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 119 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 120 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 121 | if (legacyTestingMode.toBoolean()) { |
| 122 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
| 123 | def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true) |
| 124 | for (minion in nodes.tokenize()) { |
| 125 | def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true) |
| 126 | if (!basename.trim().contains(masterName)) { |
| 127 | testMinion(basename.trim()) |
| 128 | } |
| 129 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 130 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 131 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 132 | try { |
| 133 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 134 | timeout(time: 10, unit: 'SECONDS') { |
| 135 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 136 | } |
| 137 | timeout(time: 10, unit: 'SECONDS') { |
| 138 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 139 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 140 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 141 | catch (Exception er) { |
| 142 | common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString()) |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 143 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 144 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 145 | if (TestMarkerResult) { |
| 146 | common.infoMsg("Test finished: SUCCESS") |
| 147 | } else { |
| 148 | common.warningMsg("Test finished: FAILURE") |
| 149 | } |
| 150 | return TestMarkerResult |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 151 | |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Test salt-minion |
| 156 | * |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 157 | * @param minion salt minion |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 158 | */ |
| 159 | |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 160 | def testMinion(minionName) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 161 | sh(script: "bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'", returnStdout: true) |
Jakub Josef | fa6ad8d | 2017-06-26 18:29:55 +0200 | [diff] [blame] | 162 | } |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 163 | |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 164 | /** |
| 165 | * Wrapper over setupAndTestNode, to test exactly one CC model. |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 166 | Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ |
| 167 | Flow: grab all data, and pass to setupAndTestNode function |
| 168 | under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*` |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 169 | * |
| 170 | * @param cfg - dict with params: |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 171 | MODELS_TARGZ http link to arch with (models|contexts|global_reclass) |
| 172 | modelFile |
| 173 | DockerCName directly passed to setupAndTestNode |
| 174 | EXTRA_FORMULAS directly passed to setupAndTestNode |
| 175 | DISTRIB_REVISION directly passed to setupAndTestNode |
| 176 | reclassVersion directly passed to setupAndTestNode |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 177 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 178 | Return: true\exception |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 179 | */ |
| 180 | |
| 181 | def testCCModel(cfg) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 182 | def common = new com.mirantis.mk.Common() |
| 183 | sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true) |
| 184 | sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}") |
| 185 | // unpack data |
| 186 | sh(script: "tar -xzf models.tar.gz ") |
| 187 | common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}") |
| 188 | content = readFile(file: cfg.modelFile) |
| 189 | templateContext = readYaml text: content |
| 190 | clusterName = templateContext.default_context.cluster_name |
| 191 | clusterDomain = templateContext.default_context.cluster_domain |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 192 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame^] | 193 | def testResult = false |
| 194 | testResult = setupAndTestNode( |
| 195 | "cfg01.${clusterDomain}", |
| 196 | clusterName, |
| 197 | '', |
| 198 | cfg.testReclassEnv, // Sync into image exactly one env |
| 199 | 'pkg', |
| 200 | cfg.DISTRIB_REVISION, |
| 201 | cfg.reclassVersion, |
| 202 | 0, |
| 203 | false, |
| 204 | false, |
| 205 | '', |
| 206 | '', |
| 207 | cfg.DockerCName) |
| 208 | if (testResult) { |
| 209 | common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS") |
| 210 | } else { |
| 211 | throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE") |
| 212 | } |
| 213 | return testResult |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 214 | } |