blob: a7688f0f0b0d6523724e51278fde6ae2db79c977 [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
azvyagintsev28fa9d92018-06-26 14:31:49 +030019 * Return true | false
chnydaf14ea2a2017-05-26 15:07:47 +020020 */
21
azvyagintsev28fa9d92018-06-26 14:31:49 +030022def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg',
Vasyl Saienko33e22952018-07-23 13:39:29 +030023 formulasRevision = 'stable', reclassVersion = "1.5.4", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +030024 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
Jakub Josef8fc0fd12018-01-15 18:36:48 +010025 // timeout for test execution (40min)
26 def testTimeout = 40 * 60
azvyagintsev28fa9d92018-06-26 14:31:49 +030027 def TestMarkerResult = false
chnydaf14ea2a2017-05-26 15:07:47 +020028 def saltOpts = "--retcode-passthrough --force-color"
29 def common = new com.mirantis.mk.Common()
30 def workspace = common.getWorkspace()
Jakub Josefecf8b302018-05-11 14:33:08 +000031 def img = docker.image("mirantis/salt:saltstack-ubuntu-xenial-salt-2017.7")
Jakub Joseff5b7e082018-03-22 14:40:57 +010032 img.pull()
chnydaf14ea2a2017-05-26 15:07:47 +020033
chnydaf6a0c952017-05-29 10:34:45 +020034 if (!extraFormulas || extraFormulas == "") {
chnyda77e46972017-05-26 16:14:27 +020035 extraFormulas = "linux"
chnyda66bfc582017-05-26 16:24:21 +020036 }
azvyagintsev28fa9d92018-06-26 14:31:49 +030037 if (!dockerContainerName) {
38 dockerContainerName = 'setupAndTestNode' + UUID.randomUUID().toString()
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030039 }
azvyagintsev28fa9d92018-06-26 14:31:49 +030040 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 Josef68f46d22018-05-11 21:34:55 +020051 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
Jakub Joseff5b7e082018-03-22 14:40:57 +010052 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
azvyagintsev28fa9d92018-06-26 14:31:49 +030055 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
Jakub Joseff5b7e082018-03-22 14:40:57 +010056 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
57 done""")
azvyagintsev28fa9d92018-06-26 14:31:49 +030058 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''')
chnyda3c6680a2017-05-29 16:55:34 +020074
azvyagintsev28fa9d92018-06-26 14:31:49 +030075 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 Kamaldinov00c1ea42017-08-07 18:45:27 +040081 }
azvyagintsev28fa9d92018-06-26 14:31:49 +030082 // If we didn't dropped for now - test has been passed.
83 TestMarkerResult = true
chnydaf14ea2a2017-05-26 15:07:47 +020084 }
chnydaf14ea2a2017-05-26 15:07:47 +020085 }
86 }
azvyagintsev28fa9d92018-06-26 14:31:49 +030087 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
chnydaf14ea2a2017-05-26 15:07:47 +0200122}
123
124/**
125 * Test salt-minion
126 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300127 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200128 */
129
azvyagintsev28fa9d92018-06-26 14:31:49 +0300130def testMinion(minionName) {
131 sh(script: "bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'", returnStdout: true)
Jakub Joseffa6ad8d2017-06-26 18:29:55 +0200132}