chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
| 3 | /** |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 4 | * Setup Docker to run some tests. Returns true/false based on |
| 5 | were tests successful or not. |
| 6 | * @param config - LinkedHashMap with configuration params: |
| 7 | * dockerHostname - (required) Hostname to use for Docker container. |
azvyagintsev | abcf42e | 2018-10-05 20:40:27 +0300 | [diff] [blame] | 8 | * distribRevision - (optional) Revision of packages to use (default proposed). |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 9 | * runCommands - (optional) Dict with closure structure of body required tests. For example: |
| 10 | * [ '001_Test': { sh("./run-some-test") }, '002_Test': { sh("./run-another-test") } ] |
| 11 | * Before execution runCommands will be sorted by key names. Alpabetical order is preferred. |
| 12 | * runFinally - (optional) Dict with closure structure of body required commands, which should be |
| 13 | * executed in any case of test results. Same format as for runCommands |
| 14 | * updateRepo - (optional) Whether to run common repo update step. |
| 15 | * dockerContainerName - (optional) Docker container name. |
| 16 | * dockerImageName - (optional) Docker image name |
| 17 | * dockerMaxCpus - (optional) Number of CPUS to use in Docker. |
| 18 | * dockerExtraOpts - (optional) Array of Docker extra opts for container |
| 19 | * envOpts - (optional) Array of variables that should be passed as ENV vars to Docker container. |
| 20 | * Return true | false |
| 21 | */ |
| 22 | |
| 23 | def setupDockerAndTest(LinkedHashMap config) { |
| 24 | def common = new com.mirantis.mk.Common() |
| 25 | def TestMarkerResult = false |
| 26 | // setup options |
| 27 | def defaultContainerName = 'test-' + UUID.randomUUID().toString() |
| 28 | def dockerHostname = config.get('dockerHostname', defaultContainerName) |
azvyagintsev | abcf42e | 2018-10-05 20:40:27 +0300 | [diff] [blame] | 29 | def distribRevision = config.get('distribRevision', 'proposed') |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 30 | def runCommands = config.get('runCommands', [:]) |
| 31 | def runFinally = config.get('runFinally', [:]) |
| 32 | def baseRepoPreConfig = config.get('baseRepoPreConfig', true) |
| 33 | def dockerContainerName = config.get('dockerContainerName', defaultContainerName) |
| 34 | def dockerImageName = config.get('image', "mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
| 35 | def dockerMaxCpus = config.get('dockerMaxCpus', 4) |
| 36 | def dockerExtraOpts = config.get('dockerExtraOpts', []) |
| 37 | def envOpts = config.get('envOpts', []) |
azvyagintsev | abcf42e | 2018-10-05 20:40:27 +0300 | [diff] [blame] | 38 | envOpts.add("DISTRIB_REVISION=${distribRevision}") |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 39 | def dockerBaseOpts = [ |
| 40 | '-u root:root', |
| 41 | "--hostname=${dockerHostname}", |
| 42 | '--ulimit nofile=4096:8192', |
| 43 | "--name=${dockerContainerName}", |
| 44 | "--cpus=${dockerMaxCpus}" |
| 45 | ] |
Denis Egorenko | 649cf7d | 2018-10-18 16:36:33 +0400 | [diff] [blame] | 46 | def dockerOptsFinal = (dockerBaseOpts + dockerExtraOpts).join(' ') |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame^] | 47 | def extraReposConfig = null |
| 48 | if (baseRepoPreConfig) { |
| 49 | // extra repo on mirror.mirantis.net, which is not supported before 2018.11.0 release |
| 50 | def extraRepoSource = "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/extra/xenial xenial main" |
| 51 | try { |
| 52 | def releaseNaming = 'yyyy.MM.dd' |
| 53 | def repoDateUsed = new Date().parse(releaseNaming, distribRevision) |
| 54 | def extraAvailableFrom = new Date().parse(releaseNaming, '2018.11.0') |
| 55 | if (repoDateUsed < extraAvailableFrom) { |
| 56 | extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra" |
| 57 | } |
| 58 | } catch (Exception e) { |
| 59 | common.warningMsg(e) |
| 60 | if ( !(distribRevision in [ 'nightly', 'proposed', 'testing' ] )) { |
| 61 | extraRepoSource = "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} extra" |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | def defaultExtraReposYaml = """ |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 66 | --- |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 67 | aprConfD: |- |
| 68 | APT::Get::AllowUnauthenticated 'true'; |
| 69 | APT::Get::Install-Suggests 'false'; |
| 70 | APT::Get::Install-Recommends 'false'; |
| 71 | repo: |
| 72 | mcp_saltstack: |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 73 | source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/saltstack-2017.7/xenial xenial main" |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 74 | pin: |
| 75 | - package: "libsodium18" |
| 76 | pin: "release o=SaltStack" |
| 77 | priority: 50 |
| 78 | - package: "*" |
| 79 | pin: "release o=SaltStack" |
| 80 | priority: "1100" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 81 | mcp_extra: |
Denis Egorenko | 3c752a5 | 2018-10-12 12:21:29 +0400 | [diff] [blame] | 82 | source: "${extraRepoSource}" |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 83 | mcp_saltformulas: |
| 84 | source: "deb http://apt.mcp.mirantis.net:8085/xenial ${distribRevision} salt salt-latest" |
| 85 | repo_key: "http://apt.mcp.mirantis.net:8085/public.gpg" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 86 | ubuntu: |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 87 | source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial main restricted universe" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 88 | ubuntu-upd: |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 89 | source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-updates main restricted universe" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 90 | ubuntu-sec: |
Denis Egorenko | 395aa21 | 2018-10-11 15:11:28 +0400 | [diff] [blame] | 91 | source: "deb [arch=amd64] http://mirror.mirantis.com/${distribRevision}/ubuntu xenial-security main restricted universe" |
| 92 | """ |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame^] | 93 | // override for now |
| 94 | def extraRepoMergeStrategy = config.get('extraRepoMergeStrategy', 'override') |
| 95 | def extraRepos = config.get('extraRepos', [:]) |
| 96 | def defaultRepos = readYaml text: defaultExtraReposYaml |
| 97 | if (extraRepoMergeStrategy == 'merge') { |
| 98 | extraReposConfig = common.mergeMaps(defaultRepos, extraRepos) |
| 99 | } else { |
| 100 | extraReposConfig = extraRepos ? extraRepos : defaultRepos |
| 101 | } |
| 102 | } |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 103 | def img = docker.image(dockerImageName) |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 104 | |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 105 | img.pull() |
| 106 | |
| 107 | try { |
| 108 | img.inside(dockerOptsFinal) { |
| 109 | withEnv(envOpts) { |
| 110 | try { |
| 111 | // Currently, we don't have any other point to install |
| 112 | // runtime dependencies for tests. |
| 113 | if (baseRepoPreConfig) { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 114 | // Warning! POssible point of 'allow-downgrades' issue |
| 115 | // Probably, need to add such flag into apt.prefs |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 116 | sh("""#!/bin/bash -xe |
| 117 | echo "Installing extra-deb dependencies inside docker:" |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 118 | echo > /etc/apt/sources.list |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 119 | rm -vf /etc/apt/sources.list.d/* || true |
Denis Egorenko | e02a1b2 | 2018-10-19 17:47:53 +0400 | [diff] [blame] | 120 | rm -vf /etc/apt/preferences.d/* || true |
Aleksey Zvyagintsev | c545334 | 2018-10-05 15:03:59 +0000 | [diff] [blame] | 121 | """) |
Denis Egorenko | 5cea141 | 2018-10-18 16:40:11 +0400 | [diff] [blame^] | 122 | common.debianExtraRepos(extraReposConfig) |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 123 | sh('''#!/bin/bash -xe |
| 124 | apt-get update |
Denis Egorenko | c6b24be | 2018-10-10 17:36:04 +0400 | [diff] [blame] | 125 | apt-get install -y python-netaddr |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 126 | ''') |
| 127 | |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 128 | } |
| 129 | runCommands.sort().each { command, body -> |
| 130 | common.warningMsg("Running command: ${command}") |
| 131 | // doCall is the closure implementation in groovy, allow to pass arguments to closure |
| 132 | body.call() |
| 133 | } |
| 134 | // If we didn't dropped for now - test has been passed. |
| 135 | TestMarkerResult = true |
| 136 | } |
| 137 | finally { |
| 138 | runFinally.sort().each { command, body -> |
| 139 | common.warningMsg("Running ${command} command.") |
| 140 | // doCall is the closure implementation in groovy, allow to pass arguments to closure |
| 141 | body.call() |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | catch (Exception er) { |
| 148 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 149 | } |
| 150 | |
| 151 | try { |
| 152 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 153 | timeout(time: 10, unit: 'SECONDS') { |
| 154 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 155 | } |
| 156 | timeout(time: 10, unit: 'SECONDS') { |
| 157 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 158 | } |
| 159 | } |
| 160 | catch (Exception er) { |
| 161 | common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString()) |
| 162 | } |
| 163 | |
| 164 | if (TestMarkerResult) { |
| 165 | common.infoMsg("Test finished: SUCCESS") |
| 166 | } else { |
| 167 | common.warningMsg("Test finished: FAILURE") |
| 168 | } |
| 169 | return TestMarkerResult |
| 170 | } |
| 171 | |
| 172 | /** |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 173 | * Wrapper around setupDockerAndTest, to run checks against new Reclass version |
| 174 | * that current model is compatible with new Reclass. |
| 175 | * |
| 176 | * @param config - LinkedHashMap with configuration params: |
| 177 | * dockerHostname - (required) Hostname to use for Docker container. |
| 178 | * distribRevision - (optional) Revision of packages to use (default proposed). |
| 179 | * extraRepo - (optional) Extra repo to use to install new Reclass version. Has |
| 180 | * high priority on distribRevision |
| 181 | * targetNodes - (required) List nodes to check pillar data. |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 182 | */ |
| 183 | def compareReclassVersions(config) { |
| 184 | def common = new com.mirantis.mk.Common() |
| 185 | def salt = new com.mirantis.mk.Salt() |
| 186 | common.infoMsg("Going to test new reclass for CFG node") |
| 187 | def distribRevision = config.get('distribRevision', 'proposed') |
| 188 | def venv = config.get('venv') |
| 189 | def extraRepo = config.get('extraRepo', '') |
| 190 | def extraRepoKey = config.get('extraRepoKey', '') |
| 191 | def targetNodes = config.get('targetNodes') |
| 192 | sh "rm -rf ${env.WORKSPACE}/old ${env.WORKSPACE}/new" |
| 193 | sh "mkdir -p ${env.WORKSPACE}/old ${env.WORKSPACE}/new" |
| 194 | def configRun = [ |
azvyagintsev | abcf42e | 2018-10-05 20:40:27 +0300 | [diff] [blame] | 195 | 'distribRevision': distribRevision, |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 196 | 'dockerExtraOpts' : [ |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 197 | "-v /srv/salt/reclass:/srv/salt/reclass:ro", |
| 198 | "-v /etc/salt:/etc/salt:ro", |
| 199 | "-v /usr/share/salt-formulas/:/usr/share/salt-formulas/:ro" |
| 200 | ], |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 201 | 'envOpts' : [ |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 202 | "WORKSPACE=${env.WORKSPACE}", |
| 203 | "NODES_LIST=${targetNodes.join(' ')}" |
| 204 | ], |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 205 | 'runCommands' : [ |
| 206 | '001_Update_Reclass_package' : { |
| 207 | sh('apt-get update && apt-get install -y reclass') |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 208 | }, |
| 209 | '002_Test_Reclass_Compatibility': { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 210 | sh(''' |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 211 | reclass-salt -b /srv/salt/reclass -t > ${WORKSPACE}/new/inventory || exit 1 |
| 212 | for node in $NODES_LIST; do |
| 213 | reclass-salt -b /srv/salt/reclass -p $node > ${WORKSPACE}/new/$node || exit 1 |
| 214 | done |
| 215 | ''') |
| 216 | } |
| 217 | ] |
| 218 | ] |
| 219 | if (extraRepo) { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 220 | // FIXME |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 221 | configRun['runCommands']['0001_Additional_Extra_Repo_Passed'] = { |
| 222 | sh(""" |
| 223 | echo "${extraRepo}" > /etc/apt/sources.list.d/mcp_extra.list |
| 224 | [ "${extraRepoKey}" ] && wget -O - ${extraRepoKey} | apt-key add - |
| 225 | """) |
| 226 | } |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 227 | } |
| 228 | if (setupDockerAndTest(configRun)) { |
| 229 | common.infoMsg("New reclass version is compatible with current model: SUCCESS") |
| 230 | def inventoryOld = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -t", true, null, true).get("return")[0].values()[0] |
| 231 | // [0..-31] to exclude 'echo Salt command execution success' from output |
| 232 | writeFile(file: "${env.WORKSPACE}/old/inventory", text: inventoryOld[0..-31]) |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 233 | for (String node in targetNodes) { |
Denis Egorenko | b090a76 | 2018-09-12 19:25:41 +0400 | [diff] [blame] | 234 | def nodeOut = salt.cmdRun(venv, "I@salt:master", "reclass-salt -b /srv/salt/reclass -p ${node}", true, null, true).get("return")[0].values()[0] |
| 235 | writeFile(file: "${env.WORKSPACE}/old/${node}", text: nodeOut[0..-31]) |
| 236 | } |
| 237 | def reclassDiff = common.comparePillars(env.WORKSPACE, env.BUILD_URL, '') |
| 238 | currentBuild.description = reclassDiff |
| 239 | if (reclassDiff != '<b>No job changes</b>') { |
| 240 | throw new RuntimeException("Pillars with new reclass version has been changed: FAILED") |
| 241 | } else { |
| 242 | common.infoMsg("Pillars not changed with new reclass version: SUCCESS") |
| 243 | } |
| 244 | } else { |
| 245 | throw new RuntimeException("New reclass version is not compatible with current model: FAILED") |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | /** |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 250 | * Wrapper over setupDockerAndTest, to test CC model. |
| 251 | * |
| 252 | * @param config - dict with params: |
| 253 | * dockerHostname - (required) salt master's name |
| 254 | * clusterName - (optional) model cluster name |
| 255 | * extraFormulas - (optional) extraFormulas to install. DEPRECATED |
| 256 | * formulasSource - (optional) formulas source (git or pkg, default pkg) |
| 257 | * reclassVersion - (optional) Version of used reclass (branch, tag, ...) (optional, default master) |
| 258 | * reclassEnv - (require) directory of model |
| 259 | * ignoreClassNotfound - (optional) Ignore missing classes for reclass model (default false) |
| 260 | * aptRepoUrl - (optional) package repository with salt formulas |
| 261 | * aptRepoGPG - (optional) GPG key for apt repository with formulas |
| 262 | * testContext - (optional) Description of test |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 263 | Return: true\exception |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 264 | */ |
| 265 | |
| 266 | def testNode(LinkedHashMap config) { |
| 267 | def common = new com.mirantis.mk.Common() |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 268 | def dockerHostname = config.get('dockerHostname') |
| 269 | def reclassEnv = config.get('reclassEnv') |
| 270 | def clusterName = config.get('clusterName', "") |
| 271 | def formulasSource = config.get('formulasSource', 'pkg') |
| 272 | def extraFormulas = config.get('extraFormulas', 'linux') |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 273 | def ignoreClassNotfound = config.get('ignoreClassNotfound', false) |
| 274 | def aptRepoUrl = config.get('aptRepoUrl', "") |
| 275 | def aptRepoGPG = config.get('aptRepoGPG', "") |
| 276 | def testContext = config.get('testContext', 'test') |
| 277 | config['envOpts'] = [ |
| 278 | "RECLASS_ENV=${reclassEnv}", "SALT_STOPSTART_WAIT=5", |
| 279 | "MASTER_HOSTNAME=${dockerHostname}", "CLUSTER_NAME=${clusterName}", |
| 280 | "MINION_ID=${dockerHostname}", "FORMULAS_SOURCE=${formulasSource}", |
Denis Egorenko | d54f60f | 2018-10-10 19:38:03 +0400 | [diff] [blame] | 281 | "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true", |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 282 | "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "DEBUG=1", |
Denis Egorenko | d54f60f | 2018-10-10 19:38:03 +0400 | [diff] [blame] | 283 | "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}" |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 284 | ] |
| 285 | |
| 286 | config['runCommands'] = [ |
| 287 | '001_Clone_salt_formulas_scripts': { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 288 | sh(script: 'git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts', returnStdout: true) |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 289 | }, |
| 290 | |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 291 | '002_Prepare_something' : { |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 292 | sh('''rsync -ah ${RECLASS_ENV}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts |
| 293 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 294 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 295 | ''') |
| 296 | }, |
| 297 | |
Denis Egorenko | c6b24be | 2018-10-10 17:36:04 +0400 | [diff] [blame] | 298 | '003_Install_Reclass_package' : { |
| 299 | sh('apt-get install -y reclass') |
| 300 | }, |
| 301 | |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 302 | '004_Run_tests' : { |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 303 | def testTimeout = 40 * 60 |
| 304 | timeout(time: testTimeout, unit: 'SECONDS') { |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 305 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 306 | source /srv/salt/scripts/bootstrap.sh |
| 307 | cd /srv/salt/scripts |
| 308 | source_local_envs |
| 309 | configure_salt_master |
| 310 | configure_salt_minion |
| 311 | install_salt_formula_pkg |
| 312 | source /srv/salt/scripts/bootstrap.sh |
| 313 | cd /srv/salt/scripts |
| 314 | saltservice_restart''') |
| 315 | |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 316 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 317 | source /srv/salt/scripts/bootstrap.sh |
| 318 | cd /srv/salt/scripts |
| 319 | source_local_envs |
| 320 | saltmaster_init''') |
| 321 | |
Aleksey Zvyagintsev | b20bd26 | 2018-10-05 15:09:06 +0000 | [diff] [blame] | 322 | sh('''#!/bin/bash |
Denis Egorenko | 6fd79ac | 2018-09-12 13:28:21 +0400 | [diff] [blame] | 323 | source /srv/salt/scripts/bootstrap.sh |
| 324 | cd /srv/salt/scripts |
| 325 | verify_salt_minions''') |
| 326 | } |
| 327 | } |
| 328 | ] |
| 329 | config['runFinally'] = [ |
| 330 | '001_Archive_artefacts': { |
| 331 | sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true) |
| 332 | archiveArtifacts artifacts: "nodesinfo.tar.gz" |
| 333 | } |
| 334 | ] |
| 335 | testResult = setupDockerAndTest(config) |
| 336 | if (testResult) { |
| 337 | common.infoMsg("Node test for context: ${testContext} model: ${reclassEnv} finished: SUCCESS") |
| 338 | } else { |
| 339 | throw new RuntimeException("Node test for context: ${testContext} model: ${reclassEnv} finished: FAILURE") |
| 340 | } |
| 341 | return testResult |
| 342 | } |
| 343 | |
| 344 | /** |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 345 | * setup and test salt-master |
| 346 | * |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 347 | * @param masterName salt master's name |
| 348 | * @param clusterName model cluster name |
| 349 | * @param extraFormulas extraFormulas to install. DEPRECATED |
| 350 | * @param formulasSource formulas source (git or pkg) |
| 351 | * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master) |
| 352 | * @param testDir directory of model |
| 353 | * @param formulasSource Salt formulas source type (optional, default pkg) |
| 354 | * @param formulasRevision APT revision for formulas (optional default stable) |
Petr Michalec | 6414aa5 | 2017-08-17 14:32:52 +0200 | [diff] [blame] | 355 | * @param ignoreClassNotfound Ignore missing classes for reclass model |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 356 | * @param dockerMaxCpus max cpus passed to docker (default 0, disabled) |
| 357 | * @param legacyTestingMode do you want to enable legacy testing mode (iterating through the nodes directory definitions instead of reading cluster models) |
| 358 | * @param aptRepoUrl package repository with salt formulas |
| 359 | * @param aptRepoGPG GPG key for apt repository with formulas |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 360 | * Return true | false |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 361 | */ |
| 362 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 363 | def setupAndTestNode(masterName, clusterName, extraFormulas = '*', testDir, formulasSource = 'pkg', |
Vasyl Saienko | 369ed90 | 2018-07-23 11:49:32 +0000 | [diff] [blame] | 364 | formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0, |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 365 | ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 366 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 1cecc09 | 2018-09-14 13:19:16 +0300 | [diff] [blame] | 367 | // TODO |
| 368 | common.errorMsg('You are using deprecated function!Please migrate to "setupDockerAndTest".' + |
| 369 | 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..') |
| 370 | sh('sleep 60') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 371 | // timeout for test execution (40min) |
| 372 | def testTimeout = 40 * 60 |
| 373 | def TestMarkerResult = false |
| 374 | def saltOpts = "--retcode-passthrough --force-color" |
| 375 | def workspace = common.getWorkspace() |
| 376 | def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
| 377 | img.pull() |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 378 | |
azvyagintsev | 635affb | 2018-09-13 13:02:54 +0300 | [diff] [blame] | 379 | if (formulasSource == 'pkg') { |
| 380 | if (extraFormulas) { |
| 381 | common.warningMsg("You have passed deprecated variable:extraFormulas=${extraFormulas}. " + |
| 382 | "\n It would be ignored, and all formulas would be installed anyway") |
| 383 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 384 | } |
| 385 | if (!dockerContainerName) { |
| 386 | dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString() |
| 387 | } |
| 388 | def dockerMaxCpusOpt = "--cpus=4" |
| 389 | if (dockerMaxCpus > 0) { |
| 390 | dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}" |
| 391 | } |
| 392 | try { |
| 393 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") { |
azvyagintsev | 635affb | 2018-09-13 13:02:54 +0300 | [diff] [blame] | 394 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "EXTRA_FORMULAS_PKG_ALL=true", |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 395 | "DISTRIB_REVISION=${formulasRevision}", |
| 396 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", |
| 397 | "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 398 | "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", |
| 399 | "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5", |
| 400 | "APT_REPOSITORY_GPG=${aptRepoGPG}"]) { |
| 401 | try { |
| 402 | // Currently, we don't have any other point to install |
| 403 | // runtime dependencies for tests. |
| 404 | sh("""#!/bin/bash -xe |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 405 | echo "Installing extra-deb dependencies inside docker:" |
| 406 | echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode |
| 407 | echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 408 | echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode |
| 409 | rm -vf /etc/apt/sources.list.d/* || true |
| 410 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list |
| 411 | echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list |
| 412 | apt-get update |
| 413 | apt-get install -y python-netaddr |
| 414 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 415 | sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true) |
| 416 | 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] | 417 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 418 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 419 | """) |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 420 | // FIXME: should be changed to use reclass from mcp_extra_nigtly? |
| 421 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 422 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
| 423 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion}; |
| 424 | done""") |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 425 | timeout(time: testTimeout, unit: 'SECONDS') { |
| 426 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 427 | source /srv/salt/scripts/bootstrap.sh |
| 428 | cd /srv/salt/scripts |
| 429 | source_local_envs |
| 430 | configure_salt_master |
| 431 | configure_salt_minion |
| 432 | install_salt_formula_pkg |
| 433 | source /srv/salt/scripts/bootstrap.sh |
| 434 | cd /srv/salt/scripts |
| 435 | saltservice_restart''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 436 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 437 | source /srv/salt/scripts/bootstrap.sh |
| 438 | cd /srv/salt/scripts |
| 439 | source_local_envs |
| 440 | saltmaster_init''') |
| 441 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 442 | if (!legacyTestingMode.toBoolean()) { |
| 443 | sh('''#!/bin/bash |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 444 | source /srv/salt/scripts/bootstrap.sh |
| 445 | cd /srv/salt/scripts |
| 446 | verify_salt_minions |
| 447 | ''') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | // If we didn't dropped for now - test has been passed. |
| 451 | TestMarkerResult = true |
| 452 | } |
| 453 | |
| 454 | finally { |
| 455 | // Collect rendered per-node data.Those info could be simply used |
| 456 | // for diff processing. Data was generated via reclass.cli --nodeinfo, |
| 457 | /// during verify_salt_minions. |
| 458 | sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true) |
| 459 | archiveArtifacts artifacts: "nodesinfo.tar.gz" |
| 460 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 461 | } |
azvyagintsev | 1bfe684 | 2018-08-09 18:40:17 +0200 | [diff] [blame] | 462 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 463 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 464 | catch (Exception er) { |
| 465 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 466 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 467 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 468 | if (legacyTestingMode.toBoolean()) { |
| 469 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
| 470 | def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true) |
| 471 | for (minion in nodes.tokenize()) { |
| 472 | def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true) |
| 473 | if (!basename.trim().contains(masterName)) { |
| 474 | testMinion(basename.trim()) |
| 475 | } |
| 476 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 477 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 478 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 479 | try { |
| 480 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 481 | timeout(time: 10, unit: 'SECONDS') { |
| 482 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 483 | } |
| 484 | timeout(time: 10, unit: 'SECONDS') { |
| 485 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 486 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 487 | } |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 488 | catch (Exception er) { |
| 489 | 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] | 490 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 491 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 492 | if (TestMarkerResult) { |
| 493 | common.infoMsg("Test finished: SUCCESS") |
| 494 | } else { |
| 495 | common.warningMsg("Test finished: FAILURE") |
| 496 | } |
| 497 | return TestMarkerResult |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 498 | |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Test salt-minion |
| 503 | * |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 504 | * @param minion salt minion |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 505 | */ |
| 506 | |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 507 | def testMinion(minionName) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 508 | 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] | 509 | } |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 510 | |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 511 | /** |
| 512 | * Wrapper over setupAndTestNode, to test exactly one CC model. |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 513 | Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ |
| 514 | Flow: grab all data, and pass to setupAndTestNode function |
| 515 | under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*` |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 516 | * |
| 517 | * @param cfg - dict with params: |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 518 | MODELS_TARGZ http link to arch with (models|contexts|global_reclass) |
| 519 | modelFile |
| 520 | DockerCName directly passed to setupAndTestNode |
| 521 | EXTRA_FORMULAS directly passed to setupAndTestNode |
| 522 | DISTRIB_REVISION directly passed to setupAndTestNode |
| 523 | reclassVersion directly passed to setupAndTestNode |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 524 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 525 | Return: true\exception |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 526 | */ |
| 527 | |
| 528 | def testCCModel(cfg) { |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 529 | def common = new com.mirantis.mk.Common() |
azvyagintsev | 1cecc09 | 2018-09-14 13:19:16 +0300 | [diff] [blame] | 530 | common.errorMsg('You are using deprecated function!Please migrate to "testNode".' + |
| 531 | 'It would be removed after 2018.q4 release!Pushing forced 60s sleep..') |
| 532 | sh('sleep 60') |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 533 | sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true) |
| 534 | sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}") |
| 535 | // unpack data |
| 536 | sh(script: "tar -xzf models.tar.gz ") |
| 537 | common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}") |
| 538 | content = readFile(file: cfg.modelFile) |
| 539 | templateContext = readYaml text: content |
| 540 | clusterName = templateContext.default_context.cluster_name |
| 541 | clusterDomain = templateContext.default_context.cluster_domain |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 542 | |
azvyagintsev | b4e0c44 | 2018-09-12 17:00:04 +0300 | [diff] [blame] | 543 | def testResult = false |
| 544 | testResult = setupAndTestNode( |
| 545 | "cfg01.${clusterDomain}", |
| 546 | clusterName, |
| 547 | '', |
| 548 | cfg.testReclassEnv, // Sync into image exactly one env |
| 549 | 'pkg', |
| 550 | cfg.DISTRIB_REVISION, |
| 551 | cfg.reclassVersion, |
| 552 | 0, |
| 553 | false, |
| 554 | false, |
| 555 | '', |
| 556 | '', |
| 557 | cfg.DockerCName) |
| 558 | if (testResult) { |
| 559 | common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS") |
| 560 | } else { |
| 561 | throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE") |
| 562 | } |
| 563 | return testResult |
azvyagintsev | 2b279d8 | 2018-08-07 17:22:54 +0200 | [diff] [blame] | 564 | } |