blob: 51227773abbad6f31df68b4db7b9c8986d4b9cee [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)
Jakub Joseff5b7e082018-03-22 14:40:57 +010010 * @param reclassVersion Version of used reclass (branch, tag, ...) (optional, default master)
chnydaf4e5eb42017-05-26 15:34:38 +020011 * @param testDir directory of model
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040012 * @param formulasSource Salt formulas source type (optional, default pkg)
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020013 * @param formulasRevision APT revision for formulas (optional default stable)
Petr Michalec6414aa52017-08-17 14:32:52 +020014 * @param ignoreClassNotfound Ignore missing classes for reclass model
Jakub Josef90d8e572017-10-03 18:44:44 +020015 * @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 Ukov630b9e12017-10-23 12:00:19 +040017 * @param aptRepoUrl package repository with salt formulas
18 * @param aptRepoGPG GPG key for apt repository with formulas
chnydaf14ea2a2017-05-26 15:07:47 +020019 */
20
azvyagintsev130a5902018-06-21 17:27:04 +030021def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg',
22 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
23 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '') {
Jakub Josef8fc0fd12018-01-15 18:36:48 +010024 // timeout for test execution (40min)
25 def testTimeout = 40 * 60
azvyagintsev130a5902018-06-21 17:27:04 +030026 def dockerContainerName = "${env.JOB_NAME.toLowerCase()}_${env.BUILD_TAG.toLowerCase()}"
chnydaf14ea2a2017-05-26 15:07:47 +020027 def saltOpts = "--retcode-passthrough --force-color"
28 def common = new com.mirantis.mk.Common()
29 def workspace = common.getWorkspace()
Jakub Josefecf8b302018-05-11 14:33:08 +000030 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
Jakub Joseff5b7e082018-03-22 14:40:57 +010031 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +020032
chnydaf6a0c952017-05-29 10:34:45 +020033 if (!extraFormulas || extraFormulas == "") {
chnyda77e46972017-05-26 16:14:27 +020034 extraFormulas = "linux"
chnyda66bfc582017-05-26 16:24:21 +020035 }
chnyda77e46972017-05-26 16:14:27 +020036
azvyagintsev130a5902018-06-21 17:27:04 +030037 def dockerMaxCpusOpt = "--cpus=4"
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030038 if (dockerMaxCpus > 0) {
azvyagintsev130a5902018-06-21 17:27:04 +030039 dockerMaxCpusOpt = "--cpus=${dockerMaxCpus}"
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030040 }
azvyagintsev130a5902018-06-21 17:27:04 +030041 try {
42 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOpt} --name=${dockerContainerName}") {
43 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}",
44 "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
45 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "APT_REPOSITORY=${aptRepoUrl}",
46 "APT_REPOSITORY_GPG=${aptRepoGPG}", "SALT_STOPSTART_WAIT=10"]) {
47 // To be sure that we actually re-run
48 sh("rm -v ${env.WORKSPACE}/.test_finished || true")
Jakub Josefecf8b302018-05-11 14:33:08 +000049 sh("git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts")
Jakub Josef68f46d22018-05-11 21:34:55 +020050 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
Jakub Joseff5b7e082018-03-22 14:40:57 +010051 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
52 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;""")
53 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
azvyagintsev130a5902018-06-21 17:27:04 +030054 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
Jakub Joseff5b7e082018-03-22 14:40:57 +010055 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
56 done""")
azvyagintsev130a5902018-06-21 17:27:04 +030057 timeout(time: testTimeout, unit: 'SECONDS') {
58 sh('''#!/bin/bash
59 source /srv/salt/scripts/bootstrap.sh
60 cd /srv/salt/scripts
61 source_local_envs
62 configure_salt_master
63 configure_salt_minion
64 install_salt_formula_pkg
65 source /srv/salt/scripts/bootstrap.sh
66 cd /srv/salt/scripts
67 saltservice_restart''')
68 sh('''#!/bin/bash
69 source /srv/salt/scripts/bootstrap.sh
70 cd /srv/salt/scripts
71 source_local_envs
72 saltmaster_init''')
chnyda3c6680a2017-05-29 16:55:34 +020073
azvyagintsev130a5902018-06-21 17:27:04 +030074 if (!legacyTestingMode.toBoolean()) {
75 sh('''#!/bin/bash
76 source /srv/salt/scripts/bootstrap.sh
77 cd /srv/salt/scripts
78 verify_salt_minions''')
79 }
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040080 }
azvyagintsev130a5902018-06-21 17:27:04 +030081
82 // If we didn't dropped for now - test has been passed.
83 sh("touch ${env.WORKSPACE}/.test_finished")
84 }
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040085 }
azvyagintsev130a5902018-06-21 17:27:04 +030086 }
87 finally {
chnydaf14ea2a2017-05-26 15:07:47 +020088
Jakub Josef9923de22018-05-11 20:18:22 +020089 if (legacyTestingMode.toBoolean()) {
Jakub Josef90d8e572017-10-03 18:44:44 +020090 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040091 def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
92 for (minion in nodes.tokenize()) {
Jakub Josef2ccb69b2018-02-19 18:14:22 +010093 def basename = sh script: "set +x;basename ${minion} .yml", returnStdout: true
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040094 if (!basename.trim().contains(masterName)) {
95 testMinion(basename.trim())
chnydaf14ea2a2017-05-26 15:07:47 +020096 }
97 }
chnydaf14ea2a2017-05-26 15:07:47 +020098 }
azvyagintsev130a5902018-06-21 17:27:04 +030099
100 if (fileExists("${env.WORKSPACE}/.test_finished")) {
101 common.infoMsg("Test finished")
102 currentBuild.result = 'SUCCESS'
103 } else {
104 common.infoMsg("Test failed to finish!")
105 currentBuild.result = 'FAILURE'
106 }
107 try {
108 timeout(time: 10, unit: 'SECONDS') {
109 common.infoMsg("Cleanup slave...Ignore docker-daemon errors")
110 sh("set -x; docker kill ${dockerContainerName} || true")
111 sh("set -x; docker rm --force ${dockerContainerName} || true")
112 }
113 }
114 catch (Exception er) {
115 common.infoMsg("Timeout to delete test docker container!Continue...")
116 }
chnydaf14ea2a2017-05-26 15:07:47 +0200117 }
azvyagintsev130a5902018-06-21 17:27:04 +0300118
chnydaf14ea2a2017-05-26 15:07:47 +0200119}
120
121/**
122 * Test salt-minion
123 *
azvyagintsev130a5902018-06-21 17:27:04 +0300124 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200125 */
126
azvyagintsev130a5902018-06-21 17:27:04 +0300127def testMinion(minionName) {
chnyda532239d2017-10-05 13:36:01 +0200128 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Joseffa6ad8d2017-06-26 18:29:55 +0200129}