blob: f5048b48d78ea1e0827a3d7244dacedc15827570 [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)
chnydaf14ea2a2017-05-26 15:07:47 +020016 */
17
Jakub Josef90d8e572017-10-03 18:44:44 +020018def setupAndTestNode(masterName, clusterName, extraFormulas, testDir, formulasSource = 'pkg', formulasRevision = 'stable', dockerMaxCpus = 0, ignoreClassNotfound = false, legacyTestingMode = false) {
19
chnydaf14ea2a2017-05-26 15:07:47 +020020 def saltOpts = "--retcode-passthrough --force-color"
21 def common = new com.mirantis.mk.Common()
22 def workspace = common.getWorkspace()
23 def imageFound = true
24 def img
25 try {
26 img = docker.image("tcpcloud/salt-models-testing")
27 img.pull()
28 } catch (Throwable e) {
29 img = docker.image("ubuntu:latest")
30 imageFound = false
31 }
32
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
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030037 def dockerMaxCpusOption = ""
38 if (dockerMaxCpus > 0) {
39 dockerMaxCpusOption = "--cpus=${dockerMaxCpus}"
40 }
41
Jakub Josefb7d10142017-07-31 18:32:37 +020042 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOption}") {
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040043 if (!imageFound) {
Jakub Josef0603a232017-08-17 16:45:25 +020044 sh("apt-get update && apt-get install -y curl git python-pip sudo python-pip python-dev zlib1g-dev git")
45 sh("pip install git+https://github.com/salt-formulas/reclass.git --upgrade")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040046 }
chnydae41c61c2017-08-09 12:49:07 +020047 sh("mkdir -p /srv/salt/scripts/ || true")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040048 sh("cp -r ${testDir} /srv/salt/reclass")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040049 sh("git config --global user.email || git config --global user.email 'ci@ci.local'")
50 sh("git config --global user.name || git config --global user.name 'CI'")
chnyda53f10bb2017-08-17 14:50:46 +020051 sh("git clone https://github.com/salt-formulas/salt-formulas-scripts /srv/salt/scripts")
chnydaf14ea2a2017-05-26 15:07:47 +020052
Petr Michalec6414aa52017-08-17 14:32:52 +020053 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}" ]){
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040054 sh("bash -c 'echo $MASTER_HOSTNAME'")
Petr Michalec7a94b852017-08-16 21:58:53 +020055 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && system_config_master'")
56 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && saltmaster_bootstrap'")
57 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && source_local_envs && saltmaster_init'")
chnyda3c6680a2017-05-29 16:55:34 +020058
Jakub Josef90d8e572017-10-03 18:44:44 +020059 if (!legacyTestingMode) {
chnydae41c61c2017-08-09 12:49:07 +020060 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minions'")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040061 }
62 }
chnydaf14ea2a2017-05-26 15:07:47 +020063
Jakub Josef90d8e572017-10-03 18:44:44 +020064 if (legacyTestingMode) {
65 common.infoMsg("Running legacy mode test for master hostname ${masterName}")
Ruslan Kamaldinov00c1ea42017-08-07 18:45:27 +040066 def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
67 for (minion in nodes.tokenize()) {
68 def basename = sh script: "basename ${minion} .yml", returnStdout: true
69 if (!basename.trim().contains(masterName)) {
70 testMinion(basename.trim())
chnydaf14ea2a2017-05-26 15:07:47 +020071 }
72 }
chnydaf14ea2a2017-05-26 15:07:47 +020073 }
74 }
75}
76
77/**
78 * Test salt-minion
79 *
80 * @param minion salt minion
81 */
82
83def testMinion(minionName)
84{
chnyda532239d2017-10-05 13:36:01 +020085 sh("bash -c 'source /srv/salt/scripts/bootstrap.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020086}