blob: 239ba69d06ccae597106550b1bbd8031320f14f5 [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 Saienko369ed902018-07-23 11:49:32 +000023 formulasRevision = 'stable', reclassVersion = "master", dockerMaxCpus = 0,
azvyagintsev28fa9d92018-06-26 14:31:49 +030024 ignoreClassNotfound = false, legacyTestingMode = false, aptRepoUrl = '', aptRepoGPG = '', dockerContainerName = false) {
azvyagintsev2b279d82018-08-07 17:22:54 +020025 def common = new com.mirantis.mk.Common()
Jakub Josef8fc0fd12018-01-15 18:36:48 +010026 // timeout for test execution (40min)
27 def testTimeout = 40 * 60
azvyagintsev28fa9d92018-06-26 14:31:49 +030028 def TestMarkerResult = false
chnydaf14ea2a2017-05-26 15:07:47 +020029 def saltOpts = "--retcode-passthrough --force-color"
chnydaf14ea2a2017-05-26 15:07:47 +020030 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}") {
azvyagintsev1bfe6842018-08-09 18:40:17 +020046 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}",
47 "DISTRIB_REVISION=${formulasRevision}",
48 "DEBUG=1", "MASTER_HOSTNAME=${masterName}",
49 "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}",
50 "RECLASS_VERSION=${reclassVersion}", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}",
51 "APT_REPOSITORY=${aptRepoUrl}", "SALT_STOPSTART_WAIT=5",
52 "APT_REPOSITORY_GPG=${aptRepoGPG}"]) {
53 try {
54 // Currently, we don't have any other point to install
55 // runtime dependencies for tests.
56 sh("""#!/bin/bash -xe
57 echo "Installing extra-deb dependencies inside docker:"
58 echo "APT::Get::AllowUnauthenticated 'true';" > /etc/apt/apt.conf.d/99setupAndTestNode
59 echo "APT::Get::Install-Suggests 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
60 echo "APT::Get::Install-Recommends 'false';" >> /etc/apt/apt.conf.d/99setupAndTestNode
61 rm -vf /etc/apt/sources.list.d/* || true
62 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial main restricted universe' > /etc/apt/sources.list
63 echo 'deb [arch=amd64] http://mirror.mirantis.com/$DISTRIB_REVISION/ubuntu xenial-updates main restricted universe' >> /etc/apt/sources.list
64 apt-get update
65 apt-get install -y python-netaddr
66 """)
67 sh(script: "git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts", returnStdout: true)
68 sh("""rsync -ah ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts
69 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt-mk.mirantis.com/apt.mirantis.net:8085/g' {} \\;
70 cd /srv/salt && find . -type f \\( -name '*.yml' -or -name '*.sh' \\) -exec sed -i 's/apt.mirantis.com/apt.mirantis.net:8085/g' {} \\;
71 """)
72 // FIXME: should be changed to use reclass from mcp_extra_nigtly?
73 sh("""for s in \$(python -c \"import site; print(' '.join(site.getsitepackages()))\"); do
74 sudo -H pip install --install-option=\"--prefix=\" --upgrade --force-reinstall -I \
75 -t \"\$s\" git+https://github.com/salt-formulas/reclass.git@${reclassVersion};
76 done""")
77 timeout(time: testTimeout, unit: 'SECONDS') {
azvyagintsev28fa9d92018-06-26 14:31:49 +030078 sh('''#!/bin/bash
azvyagintsev1bfe6842018-08-09 18:40:17 +020079 source /srv/salt/scripts/bootstrap.sh
80 cd /srv/salt/scripts
81 source_local_envs
82 configure_salt_master
83 configure_salt_minion
84 install_salt_formula_pkg
85 source /srv/salt/scripts/bootstrap.sh
86 cd /srv/salt/scripts
87 saltservice_restart''')
88 sh('''#!/bin/bash
89 source /srv/salt/scripts/bootstrap.sh
90 cd /srv/salt/scripts
91 source_local_envs
92 saltmaster_init''')
93
94 if (!legacyTestingMode.toBoolean()) {
95 sh('''#!/bin/bash
96 source /srv/salt/scripts/bootstrap.sh
97 cd /srv/salt/scripts
98 verify_salt_minions
99 ''')
100 }
101 }
102 // If we didn't dropped for now - test has been passed.
103 TestMarkerResult = true
azvyagintsev28fa9d92018-06-26 14:31:49 +0300104 }
azvyagintsev1bfe6842018-08-09 18:40:17 +0200105
106 finally {
azvyagintseva1ee5ac2018-08-08 16:09:58 +0200107 // Collect rendered per-node data.Those info could be simply used
108 // for diff processing. Data was generated via reclass.cli --nodeinfo,
109 /// during verify_salt_minions.
110 sh(script: "cd /tmp; tar -czf ${env.WORKSPACE}/nodesinfo.tar.gz *reclass*", returnStatus: true)
111 archiveArtifacts artifacts: "nodesinfo.tar.gz"
azvyagintsev1bfe6842018-08-09 18:40:17 +0200112 }
chnydaf14ea2a2017-05-26 15:07:47 +0200113 }
chnydaf14ea2a2017-05-26 15:07:47 +0200114 }
115 }
azvyagintsev28fa9d92018-06-26 14:31:49 +0300116 catch (Exception er) {
117 common.warningMsg("IgnoreMe:Something wrong with img.Message:\n" + er.toString())
118 }
119
120 if (legacyTestingMode.toBoolean()) {
121 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
122 def nodes = sh(script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true)
123 for (minion in nodes.tokenize()) {
124 def basename = sh(script: "set +x;basename ${minion} .yml", returnStdout: true)
125 if (!basename.trim().contains(masterName)) {
126 testMinion(basename.trim())
127 }
128 }
129 }
130
131 try {
132 common.warningMsg("IgnoreMe:Force cleanup slave.Ignore docker-daemon errors")
133 timeout(time: 10, unit: 'SECONDS') {
134 sh(script: "set -x; docker kill ${dockerContainerName} || true", returnStdout: true)
135 }
136 timeout(time: 10, unit: 'SECONDS') {
137 sh(script: "set -x; docker rm --force ${dockerContainerName} || true", returnStdout: true)
138 }
139 }
140 catch (Exception er) {
141 common.warningMsg("IgnoreMe:Timeout to delete test docker container with force!Message:\n" + er.toString())
142 }
143
144 if (TestMarkerResult) {
145 common.infoMsg("Test finished: SUCCESS")
146 } else {
147 common.warningMsg("Test finished: FAILURE")
148 }
149 return TestMarkerResult
150
chnydaf14ea2a2017-05-26 15:07:47 +0200151}
152
153/**
154 * Test salt-minion
155 *
azvyagintsev28fa9d92018-06-26 14:31:49 +0300156 * @param minion salt minion
chnydaf14ea2a2017-05-26 15:07:47 +0200157 */
158
azvyagintsev28fa9d92018-06-26 14:31:49 +0300159def testMinion(minionName) {
160 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 +0200161}
azvyagintsev2b279d82018-08-07 17:22:54 +0200162
163
164/**
165 * Wrapper over setupAndTestNode, to test exactly one CC model.
166 Whole workspace and model - should be pre-rendered and passed via MODELS_TARGZ
167 Flow: grab all data, and pass to setupAndTestNode function
168 under-modell will be directly mirrored to `model/{cfg.testReclassEnv}/* /srv/salt/reclass/*`
169 *
170 * @param cfg - dict with params:
171 MODELS_TARGZ http link to arch with (models|contexts|global_reclass)
172 modelFile
173 DockerCName directly passed to setupAndTestNode
174 EXTRA_FORMULAS directly passed to setupAndTestNode
175 DISTRIB_REVISION directly passed to setupAndTestNode
176 reclassVersion directly passed to setupAndTestNode
177
178 Return: true\exception
179 */
180
181def testCCModel(cfg) {
182 def common = new com.mirantis.mk.Common()
183 sh(script: 'find . -mindepth 1 -delete || true', returnStatus: true)
184 sh(script: "wget --progress=dot:mega --auth-no-challenge -O models.tar.gz ${cfg.MODELS_TARGZ}")
185 // unpack data
186 sh(script: "tar -xzf models.tar.gz ")
187 common.infoMsg("Going to test exactly one context: ${cfg.modelFile}\n, with params: ${cfg}")
188 content = readFile(file: cfg.modelFile)
189 templateContext = readYaml text: content
190 clusterName = templateContext.default_context.cluster_name
191 clusterDomain = templateContext.default_context.cluster_domain
192
193 def testResult = false
194 testResult = setupAndTestNode(
195 "cfg01.${clusterDomain}",
196 clusterName,
197 cfg.EXTRA_FORMULAS,
198 cfg.testReclassEnv, // Sync into image exactly one env
199 'pkg',
200 cfg.DISTRIB_REVISION,
201 cfg.reclassVersion,
202 0,
203 false,
204 false,
205 '',
206 '',
207 cfg.DockerCName)
208 if (testResult) {
azvyagintsev5ca0b722018-08-08 10:52:10 +0200209 common.infoMsg("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: SUCCESS")
azvyagintsev2b279d82018-08-07 17:22:54 +0200210 } else {
azvyagintsev5ca0b722018-08-08 10:52:10 +0200211 throw new RuntimeException("testCCModel for context: ${cfg.modelFile} model: ${cfg.testReclassEnv} finished: FAILURE")
azvyagintsev2b279d82018-08-07 17:22:54 +0200212 }
213 return testResult
214}