blob: 5bba09b05ba800ecc20118c72cbdf0d2516014ac [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
chnyda85f72e62017-05-26 15:43:27 +02007 * @param extraFormulas extraFormulas to install
Mykyta Karpin1cb8d3f2017-06-23 15:42:17 +03008 * @param formulasSource formulas source (git or pkg)
chnydaf4e5eb42017-05-26 15:34:38 +02009 * @param testDir directory of model
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020010 * @param formulasSource Salt formulas source type (optional, default pkg)
11 * @param formulasRevision APT revision for formulas (optional default stable)
chnydaf14ea2a2017-05-26 15:07:47 +020012 */
13
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030014def setupAndTestNode(masterName, extraFormulas, testDir, formulasSource = 'pkg', formulasRevision = 'stable', dockerMaxCpus = 0) {
chnydaf14ea2a2017-05-26 15:07:47 +020015 def saltOpts = "--retcode-passthrough --force-color"
16 def common = new com.mirantis.mk.Common()
17 def workspace = common.getWorkspace()
18 def imageFound = true
19 def img
20 try {
21 img = docker.image("tcpcloud/salt-models-testing")
22 img.pull()
23 } catch (Throwable e) {
24 img = docker.image("ubuntu:latest")
25 imageFound = false
26 }
27
chnydaf6a0c952017-05-29 10:34:45 +020028 if (!extraFormulas || extraFormulas == "") {
chnyda77e46972017-05-26 16:14:27 +020029 extraFormulas = "linux"
chnyda66bfc582017-05-26 16:24:21 +020030 }
chnyda77e46972017-05-26 16:14:27 +020031
Dennis Dmitriev3c3ee6d2017-07-25 20:02:44 +030032 def dockerMaxCpusOption = ""
33 if (dockerMaxCpus > 0) {
34 dockerMaxCpusOption = "--cpus=${dockerMaxCpus}"
35 }
36
Jakub Josefb7d10142017-07-31 18:32:37 +020037 img.inside("-u root:root --hostname=${masterName} --ulimit nofile=4096:8192 ${dockerMaxCpusOption}") {
chnyda3c6680a2017-05-29 16:55:34 +020038
39 def is_mk_ci
40 try {
41 is_mk_ci = DEFAULT_GIT_URL.contains("mk-ci")
42 } catch (Throwable e) {
43 is_mk_ci = false
44 }
45
chnydaf14ea2a2017-05-26 15:07:47 +020046 wrap([$class: 'AnsiColorBuildWrapper']) {
47 if (!imageFound) {
48 sh("apt-get update && apt-get install -y curl subversion git python-pip sudo python-pip python-dev zlib1g-dev git")
49 sh("pip install git+https://github.com/epcim/reclass.git@pr/fix/fix_raise_UndefinedVariableError")
50 }
51 sh("mkdir -p /srv/salt/ || true")
chnydaf4e5eb42017-05-26 15:34:38 +020052 sh("cp -r ${testDir} /srv/salt/reclass")
chnydaf14ea2a2017-05-26 15:07:47 +020053 sh("svn export --force https://github.com/salt-formulas/salt-formulas/trunk/deploy/scripts /srv/salt/scripts")
54 sh("git config --global user.email || git config --global user.email 'ci@ci.local'")
55 sh("git config --global user.name || git config --global user.name 'CI'")
56
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020057 withEnv(["FORMULAS_SOURCE=${formulasSource}", "EXTRA_FORMULAS=${extraFormulas}", "DISTRIB_REVISION=${formulasRevision}", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local"]){
chnydaf14ea2a2017-05-26 15:07:47 +020058 sh("bash -c 'echo $MASTER_HOSTNAME'")
59 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && system_config'")
60 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_bootstrap'")
61 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_init'")
chnyda3c6680a2017-05-29 16:55:34 +020062
63 if (!is_mk_ci) {
64 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && verify_salt_minions'")
65 }
chnydaf14ea2a2017-05-26 15:07:47 +020066 }
67
chnyda4967e4d2017-05-29 16:07:53 +020068 if (is_mk_ci) {
chnyda3c6680a2017-05-29 16:55:34 +020069 def nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
70 for (minion in nodes.tokenize()) {
71 def basename = sh script: "basename ${minion} .yml", returnStdout: true
72 if (!basename.trim().contains(masterName)) {
73 testMinion(basename.trim())
74 }
chnydaf14ea2a2017-05-26 15:07:47 +020075 }
76 }
chnyda3c6680a2017-05-29 16:55:34 +020077
chnydaf14ea2a2017-05-26 15:07:47 +020078 }
79 }
80}
81
82/**
83 * Test salt-minion
84 *
85 * @param minion salt minion
86 */
87
88def testMinion(minionName)
89{
90 sh("service salt-master restart && service salt-minion restart && sleep 5 && bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && verify_salt_minion ${minionName}'")
Jakub Joseffa6ad8d2017-06-26 18:29:55 +020091}