blob: 824e1fac773ae95d7182c0750ea49c2a9c302b9e [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 Josef90d8e572017-10-03 18:44:44 +020021
chnydaf14ea2a2017-05-26 15:07:47 +020022 def saltOpts = "--retcode-passthrough --force-color"
23 def common = new com.mirantis.mk.Common()
24 def workspace = common.getWorkspace()
25 def imageFound = true
26 def img
27 try {
chnyda0a1fe022017-11-10 15:18:42 +010028 img = docker.image("tcpcloud/salt-models-testing:${formulasRevision}")
chnydaf14ea2a2017-05-26 15:07:47 +020029 img.pull()
30 } catch (Throwable e) {
31 img = docker.image("ubuntu:latest")
32 imageFound = false
33 }
34
chnydaf6a0c952017-05-29 10:34:45 +020035 if (!extraFormulas || extraFormulas == "") {
chnyda77e46972017-05-26 16:14:27 +020036 extraFormulas = "linux"
chnyda66bfc582017-05-26 16:24:21 +020037 }
chnyda77e46972017-05-26 16:14:27 +020038
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030039 def dockerMaxCpusOption = ""
40 if (dockerMaxCpus > 0) {
41 dockerMaxCpusOption = "--cpus=${dockerMaxCpus}"
42 }
43
Jakub Josefb7d10142017-07-31 18:32:37 +020044 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOption}") {
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040045 if (!imageFound) {
Jakub Josef0603a232017-08-17 16:45:25 +020046 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 +000047 sh("pip install git+https://github.com/salt-formulas/reclass.git --upgrade")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040048 }
chnydae41c61c2017-08-09 12:49:07 +020049 sh("mkdir -p /srv/salt/scripts/ || true")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040050 sh("cp -r ${testDir} /srv/salt/reclass")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040051 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'")
chnyda53f10bb2017-08-17 14:50:46 +020053 sh("git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts")
chnydaf14ea2a2017-05-26 15:07:47 +020054
Dmitry Ukov630b9e12017-10-23 12:00:19 +040055 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "CLUSTER_NAME=${clusterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local", "RECLASS_IGNORE_CLASS_NOTFOUND=${ignoreClassNotfound}", "APT_REPOSITORY=${aptRepoUrl}", "APT_REPOSITORY_GPG=${aptRepoGPG}"]){
chnyda0a1fe022017-11-10 15:18:42 +010056 if (!imageFound) {
57 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && system_config_master'")
58 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && saltmaster_bootstrap'")
59 } else {
60 sh("cp -r ${testDir}/* /srv/salt/reclass && echo '127.0.1.2 salt' >> /etc/hosts")
61 sh("service salt-master restart && service salt-minion restart && sleep 2")
62 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && configure_salt_master && configure_salt_minion'")
63 sh("service salt-master restart && service salt-minion restart && sleep 2")
64 }
65
Petr Michalec7a94b852017-08-16 21:58:53 +020066 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && saltmaster_init'")
chnyda3c6680a2017-05-29 16:55:34 +020067
Jakub Josef90d8e572017-10-03 18:44:44 +020068 if (!legacyTestingMode) {
chnydae41c61c2017-08-09 12:49:07 +020069 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minions'")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040070 }
71 }
chnydaf14ea2a2017-05-26 15:07:47 +020072
Jakub Josef90d8e572017-10-03 18:44:44 +020073 if (legacyTestingMode) {
74 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040075 def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
76 for (minion in nodes.tokenize()) {
77 def basename = sh script: "basename ${minion} .yml", returnStdout: true
78 if (!basename.trim().contains(masterName)) {
79 testMinion(basename.trim())
chnydaf14ea2a2017-05-26 15:07:47 +020080 }
81 }
chnydaf14ea2a2017-05-26 15:07:47 +020082 }
83 }
84}
85
86/**
87 * Test salt-minion
88 *
89 * @param minion salt minion
90 */
91
92def testMinion(minionName)
93{
chnyda532239d2017-10-05 13:36:01 +020094 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020095}