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 |
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 | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 22 | def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg', |
Vasyl Saienko | 33e2295 | 2018-07-23 13:39:29 +0300 | [diff] [blame^] | 23 | formulasRevision = 'stable', reclassVersion = "1.5.4", dockerMaxCpus = 0, |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 24 | ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) { |
Jakub Josef | 8fc0fd1 | 2018-01-15 18:36:48 +0100 | [diff] [blame] | 25 | // timeout for test execution (40min) |
| 26 | def testTimeout = 40 * 60 |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 27 | def TestMarkerResult = false |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 28 | def saltOpts = "--retcode-passthrough --force-color" |
| 29 | def common = new com.mirantis.mk.Common() |
| 30 | def workspace = common.getWorkspace() |
Jakub Josef | ecf8b30 | 2018-05-11 14:33:08 +0000 | [diff] [blame] | 31 | def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7") |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 32 | img.pull() |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 33 | |
chnyda | f6a0c95 | 2017-05-29 10:34:45 +0200 | [diff] [blame] | 34 | if (!extraFormulas || extraFormulas == "") { |
chnyda | 77e4697 | 2017-05-26 16:14:27 +0200 | [diff] [blame] | 35 | extraFormulas = "linux" |
chnyda | 66bfc58 | 2017-05-26 16:24:21 +0200 | [diff] [blame] | 36 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 37 | if (!dockerContainerName) { |
| 38 | dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString() |
Dennis Dmitriev | 3c3ee6d | 2017-07-25 20:02:44 +0300 | [diff] [blame] | 39 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 40 | def dockerMaxCpusOpt = "--cpus=4" |
| 41 | if (dockerMaxCpus > 0) { |
| 42 | dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}" |
| 43 | } |
| 44 | try { |
| 45 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") { |
| 46 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}", |
| 47 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 48 | "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "APT_REPOSITORY=${aptRepoUrl}", |
| 49 | "APT_REPOSITORY_GPG=${aptRepoGPG}", "SALT_STOPSTART_WAIT=10"]) { |
| 50 | sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true) |
Jakub Josef | 68f46d2 | 2018-05-11 21:34:55 +0200 | [diff] [blame] | 51 | 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] | 52 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\; |
| 53 | cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;""") |
| 54 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 55 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
Jakub Josef | f5b7e08 | 2018-03-22 14:40:57 +0100 | [diff] [blame] | 56 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion}; |
| 57 | done""") |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 58 | timeout(time: testTimeout, unit: 'SECONDS') { |
| 59 | sh('''#!/bin/bash |
| 60 | source /srv/salt/scripts/bootstrap.sh |
| 61 | cd /srv/salt/scripts |
| 62 | source_local_envs |
| 63 | configure_salt_master |
| 64 | configure_salt_minion |
| 65 | install_salt_formula_pkg |
| 66 | source /srv/salt/scripts/bootstrap.sh |
| 67 | cd /srv/salt/scripts |
| 68 | saltservice_restart''') |
| 69 | sh('''#!/bin/bash |
| 70 | source /srv/salt/scripts/bootstrap.sh |
| 71 | cd /srv/salt/scripts |
| 72 | source_local_envs |
| 73 | saltmaster_init''') |
chnyda | 3c6680a | 2017-05-29 16:55:34 +0200 | [diff] [blame] | 74 | |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 75 | if (!legacyTestingMode.toBoolean()) { |
| 76 | sh('''#!/bin/bash |
| 77 | source /srv/salt/scripts/bootstrap.sh |
| 78 | cd /srv/salt/scripts |
| 79 | verify_salt_minions''') |
| 80 | } |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 81 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 82 | // If we didn't dropped for now - test has been passed. |
| 83 | TestMarkerResult = true |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 84 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 85 | } |
| 86 | } |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 87 | catch (Exception er) { |
| 88 | common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString()) |
| 89 | } |
| 90 | |
| 91 | if (legacyTestingMode.toBoolean()) { |
| 92 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
| 93 | def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true) |
| 94 | for (minion in nodes.tokenize()) { |
| 95 | def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true) |
| 96 | if (!basename.trim().contains(masterName)) { |
| 97 | testMinion(basename.trim()) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | try { |
| 103 | common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors") |
| 104 | timeout(time: 10, unit: 'SECONDS') { |
| 105 | sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true) |
| 106 | } |
| 107 | timeout(time: 10, unit: 'SECONDS') { |
| 108 | sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true) |
| 109 | } |
| 110 | } |
| 111 | catch (Exception er) { |
| 112 | common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString()) |
| 113 | } |
| 114 | |
| 115 | if (TestMarkerResult) { |
| 116 | common.infoMsg("Test finished: SUCCESS") |
| 117 | } else { |
| 118 | common.warningMsg("Test finished: FAILURE") |
| 119 | } |
| 120 | return TestMarkerResult |
| 121 | |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Test salt-minion |
| 126 | * |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 127 | * @param minion salt minion |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 128 | */ |
| 129 | |
azvyagintsev | 28fa9d9 | 2018-06-26 14:31:49 +0300 | [diff] [blame] | 130 | def testMinion(minionName) { |
| 131 | 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] | 132 | } |