blob: 3218917ed19eea7c097f275a77d828b67dda40e4 [file] [log] [blame]
chnydaf14ea2a2017-05-26 15:07:47 +02001package com.mirantis.mk
2
3/**
4 * setup and test salt-master
5 *
6 * @param masterName salt master's name
Jakub Josef2d01c842017-08-17 13:37:26 +02007 * @param clusterName model cluster name
chnyda85f72e62017-05-26 15:43:27 +02008 * @param extraFormulas extraFormulas to install
Mykyta Karpin1cb8d3f2017-06-23 15:42:17 +03009 * @param formulasSource formulas source (git or pkg)
chnydaf4e5eb42017-05-26 15:34:38 +020010 * @param testDir directory of model
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040011 * @param formulasSource Salt formulas source type (optional, default pkg)
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020012 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +020013 * @param ignoreClassNotfound Ignore missing classes for reclass model
Jakub Josef90d8e572017-10-03 18:44:44 +020014 * @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 Ukov630b9e12017-10-23 12:00:19 +040016 * @param aptRepoUrl package repository with salt formulas
17 * @param aptRepoGPG GPG key for apt repository with formulas
chnydaf14ea2a2017-05-26 15:07:47 +020018 */
19
Dmitry Ukov630b9e12017-10-23 12:00:19 +040020def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg', formulasRevision = 'stable', dockerMaxCpus = 0, ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl='', aptRepoGPG='') {
Jakub Josef2fffaba2017-12-12 16:33:26 +010021 // timeout for test execution
chnyda7c86d8e2017-12-18 11:30:09 +010022 def testTimeout = 20 * 60
chnydaf14ea2a2017-05-26 15:07:47 +020023 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 {
chnyda0a1fe022017-11-10 15:18:42 +010029 img = docker.image("tcpcloud/salt-models-testing:${formulasRevision}")
chnydaf14ea2a2017-05-26 15:07:47 +020030 img.pull()
31 } catch (Throwable e) {
32 img = docker.image("ubuntu:latest")
33 imageFound = false
34 }
35
chnydaf6a0c952017-05-29 10:34:45 +020036 if (!extraFormulas || extraFormulas == "") {
chnyda77e46972017-05-26 16:14:27 +020037 extraFormulas = "linux"
chnyda66bfc582017-05-26 16:24:21 +020038 }
chnyda77e46972017-05-26 16:14:27 +020039
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030040 def dockerMaxCpusOption = ""
41 if (dockerMaxCpus > 0) {
42 dockerMaxCpusOption = "--cpus=${dockerMaxCpus}"
43 }
44
Jakub Josefb7d10142017-07-31 18:32:37 +020045 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOption}") {
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040046 if (!imageFound) {
Jakub Josef0603a232017-08-17 16:45:25 +020047 sh("apt-get update && apt-get install -y curl git python-pip sudo python-pip python-dev zlib1g-dev git")
Cedric Hnyda69d39ad2017-10-31 15:19:32 +000048 sh("pip install git+https://github.com/salt-formulas/reclass.git --upgrade")
chnydaf0bc2e62017-11-13 10:21:53 +010049 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 Kamaldinov00c1ea42017-08-07 18:45:27 +040054 }
chnydaf14ea2a2017-05-26 15:07:47 +020055
chnydaeebab582017-11-13 15:33:48 +010056 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
chnyda0a1fe022017-11-10 15:18:42 +010061 if (!imageFound) {
chnyda2a902f02017-12-05 14:10:17 +010062 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' {} \\;")
chnyda0a1fe022017-11-10 15:18:42 +010064 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'")
chnydaa6866632017-12-14 17:22:37 +010066 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""")
chnyda0a1fe022017-11-10 15:18:42 +010070 } else {
71 sh("cp -r ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts")
chnyda2a902f02017-12-05 14:10:17 +010072 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' {} \\;")
chnydaa6866632017-12-14 17:22:37 +010074 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 Josef2fffaba2017-12-12 16:33:26 +010078 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'")
chnyda2a902f02017-12-05 14:10:17 +010079 sh("(pgrep salt-master | sed /\$\$/d | xargs --no-run-if-empty -I {} kill -9 {} || true) && pkill -9 salt-minion || true")
chnydad9fe02f2017-11-13 10:44:15 +010080 sh("service salt-master restart && service salt-minion restart && sleep 15")
chnyda0a1fe022017-11-10 15:18:42 +010081 }
chnydabae95dd2017-12-19 15:09:19 +010082 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))'")
chnyda3c6680a2017-05-29 16:55:34 +020087
Jakub Josef90d8e572017-10-03 18:44:44 +020088 if (!legacyTestingMode) {
chnydae41c61c2017-08-09 12:49:07 +020089 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minions'")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040090 }
91 }
chnydaf14ea2a2017-05-26 15:07:47 +020092
Jakub Josef90d8e572017-10-03 18:44:44 +020093 if (legacyTestingMode) {
94 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040095 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())
chnydaf14ea2a2017-05-26 15:07:47 +0200100 }
101 }
chnydaf14ea2a2017-05-26 15:07:47 +0200102 }
103 }
104}
105
106/**
107 * Test salt-minion
108 *
109 * @param minion salt minion
110 */
111
112def testMinion(minionName)
113{
chnyda532239d2017-10-05 13:36:01 +0200114 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Joseffa6ad8d2017-06-26 18:29:55 +0200115}