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) |
chnyda | f4e5eb4 | 2017-05-26 15:34:38 +0200 | [diff] [blame] | 10 | * @param testDir directory of model |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 11 | * @param formulasSource Salt formulas source type (optional, default pkg) |
Jakub Josef | fa6ad8d | 2017-06-26 18:29:55 +0200 | [diff] [blame] | 12 | * @param formulasRevision APT revision for formulas (optional default stable) |
Petr Michalec | 6414aa5 | 2017-08-17 14:32:52 +0200 | [diff] [blame] | 13 | * @param ignoreClassNotfound Ignore missing classes for reclass model |
Jakub Josef | 90d8e57 | 2017-10-03 18:44:44 +0200 | [diff] [blame] | 14 | * @param dockerMaxCpus max cpus passed to docker (default 0, disabled) |
| 15 | * @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] | 16 | * @param aptRepoUrl package repository with salt formulas |
| 17 | * @param aptRepoGPG GPG key for apt repository with formulas |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 18 | */ |
| 19 | |
Dmitry Ukov | 630b9e1 | 2017-10-23 12:00:19 +0400 | [diff] [blame] | 20 | def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg', formulasRevision = 'stable', dockerMaxCpus = 0, ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl='', aptRepoGPG='') { |
Jakub Josef | 2fffaba | 2017-12-12 16:33:26 +0100 | [diff] [blame] | 21 | // timeout for test execution |
chnyda | 7c86d8e | 2017-12-18 11:30:09 +0100 | [diff] [blame] | 22 | def testTimeout = 20 * 60 |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 23 | def saltOpts = "--retcode-passthrough --force-color" |
| 24 | def common = new com.mirantis.mk.Common() |
| 25 | def workspace = common.getWorkspace() |
| 26 | def imageFound = true |
| 27 | def img |
| 28 | try { |
chnyda | 0a1fe02 | 2017-11-10 15:18:42 +0100 | [diff] [blame] | 29 | img = docker.image("tcpcloud/salt-models-testing:${formulasRevision}") |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 30 | img.pull() |
| 31 | } catch (Throwable e) { |
| 32 | img = docker.image("ubuntu:latest") |
| 33 | imageFound = false |
| 34 | } |
| 35 | |
chnyda | f6a0c95 | 2017-05-29 10:34:45 +0200 | [diff] [blame] | 36 | if (!extraFormulas || extraFormulas == "") { |
chnyda | 77e4697 | 2017-05-26 16:14:27 +0200 | [diff] [blame] | 37 | extraFormulas = "linux" |
chnyda | 66bfc58 | 2017-05-26 16:24:21 +0200 | [diff] [blame] | 38 | } |
chnyda | 77e4697 | 2017-05-26 16:14:27 +0200 | [diff] [blame] | 39 | |
Dennis Dmitriev | 3c3ee6d | 2017-07-25 20:02:44 +0300 | [diff] [blame] | 40 | def dockerMaxCpusOption = "" |
| 41 | if (dockerMaxCpus > 0) { |
| 42 | dockerMaxCpusOption = "--cpus=${dockerMaxCpus}" |
| 43 | } |
| 44 | |
Jakub Josef | b7d1014 | 2017-07-31 18:32:37 +0200 | [diff] [blame] | 45 | img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOption}") { |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 46 | if (!imageFound) { |
Jakub Josef | 0603a23 | 2017-08-17 16:45:25 +0200 | [diff] [blame] | 47 | sh("apt-get update && apt-get install -y curl git python-pip sudo python-pip python-dev zlib1g-dev git") |
Cedric Hnyda | 69d39ad | 2017-10-31 15:19:32 +0000 | [diff] [blame] | 48 | sh("pip install git+https://github.com/salt-formulas/reclass.git --upgrade") |
chnyda | f0bc2e6 | 2017-11-13 10:21:53 +0100 | [diff] [blame] | 49 | sh("mkdir -p /srv/salt/scripts/ || true") |
| 50 | sh("cp -r ${testDir} /srv/salt/reclass") |
| 51 | sh("git config --global user.email || git config --global user.email 'ci@ci.local'") |
| 52 | sh("git config --global user.name || git config --global user.name 'CI'") |
| 53 | sh("git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts") |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 54 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 55 | |
chnyda | eebab58 | 2017-11-13 15:33:48 +0100 | [diff] [blame] | 56 | withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}", |
| 57 | "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", |
| 58 | "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "APT_REPOSITORY=${aptRepoUrl}", |
| 59 | "APT_REPOSITORY_GPG=${aptRepoGPG}"]){ |
| 60 | |
chnyda | 0a1fe02 | 2017-11-10 15:18:42 +0100 | [diff] [blame] | 61 | if (!imageFound) { |
chnyda | 2a902f0 | 2017-12-05 14:10:17 +0100 | [diff] [blame] | 62 | sh("cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;") |
| 63 | sh("cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;") |
chnyda | 0a1fe02 | 2017-11-10 15:18:42 +0100 | [diff] [blame] | 64 | sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && system_config_master'") |
| 65 | sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && saltmaster_bootstrap'") |
chnyda | a686663 | 2017-12-14 17:22:37 +0100 | [diff] [blame] | 66 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
| 67 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
| 68 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git; |
| 69 | done""") |
chnyda | 0a1fe02 | 2017-11-10 15:18:42 +0100 | [diff] [blame] | 70 | } else { |
| 71 | sh("cp -r ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts") |
chnyda | 2a902f0 | 2017-12-05 14:10:17 +0100 | [diff] [blame] | 72 | sh("cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;") |
| 73 | sh("cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;") |
chnyda | a686663 | 2017-12-14 17:22:37 +0100 | [diff] [blame] | 74 | sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do |
| 75 | sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \ |
| 76 | -t \"\$s\" git+https://github.com/salt-formulas/reclass.git; |
| 77 | done""") |
Jakub Josef | 2fffaba | 2017-12-12 16:33:26 +0100 | [diff] [blame] | 78 | sh("timeout ${testTimeout} bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && configure_salt_master && configure_salt_minion && install_salt_formula_pkg'") |
chnyda | 2a902f0 | 2017-12-05 14:10:17 +0100 | [diff] [blame] | 79 | sh("(pgrep salt-master | sed /\$\$/d | xargs --no-run-if-empty -I {} kill -9 {} || true) && pkill -9 salt-minion || true") |
chnyda | d9fe02f | 2017-11-13 10:44:15 +0100 | [diff] [blame] | 80 | sh("service salt-master restart && service salt-minion restart && sleep 15") |
chnyda | 0a1fe02 | 2017-11-10 15:18:42 +0100 | [diff] [blame] | 81 | } |
chnyda | bae95dd | 2017-12-19 15:09:19 +0100 | [diff] [blame] | 82 | def lockFile = "/var/lock/salt_master_running" |
| 83 | // Dirty hack to make sure salt-master service will run permanently |
| 84 | sh("timeout ${testTimeout} bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && \ |
| 85 | source_local_envs && touch ${lockFile} && (((saltmaster_init || exit 1); rm ${lockFile}) & \ |
| 86 | (while [ -f ${lockFile} ] ; do service salt-master start && sleep 60 ; done))'") |
chnyda | 3c6680a | 2017-05-29 16:55:34 +0200 | [diff] [blame] | 87 | |
Jakub Josef | 90d8e57 | 2017-10-03 18:44:44 +0200 | [diff] [blame] | 88 | if (!legacyTestingMode) { |
chnyda | e41c61c | 2017-08-09 12:49:07 +0200 | [diff] [blame] | 89 | sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minions'") |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 90 | } |
| 91 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 92 | |
Jakub Josef | 90d8e57 | 2017-10-03 18:44:44 +0200 | [diff] [blame] | 93 | if (legacyTestingMode) { |
| 94 | common.infoMsg("Running legacy mode test for master hostname ${masterName}") |
Ruslan Kamaldinov | 00c1ea4 | 2017-08-07 18:45:27 +0400 | [diff] [blame] | 95 | def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 96 | for (minion in nodes.tokenize()) { |
| 97 | def basename = sh script: "basename ${minion} .yml", returnStdout: true |
| 98 | if (!basename.trim().contains(masterName)) { |
| 99 | testMinion(basename.trim()) |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 100 | } |
| 101 | } |
chnyda | f14ea2a | 2017-05-26 15:07:47 +0200 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Test salt-minion |
| 108 | * |
| 109 | * @param minion salt minion |
| 110 | */ |
| 111 | |
| 112 | def testMinion(minionName) |
| 113 | { |
chnyda | 532239d | 2017-10-05 13:36:01 +0200 | [diff] [blame] | 114 | 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] | 115 | } |