blob: 704b6226c64ffed38939d02007565ab946685724 [file] [log] [blame]
Jakub Josef83379312017-03-29 18:12:34 +02001
2/**
3 * Test salt models pipeline
4 * DEFAULT_GIT_REF
5 * DEFAULT_GIT_URL
6 * CREDENTIALS_ID
7 */
8
Jakub Josefc5a223a2017-03-01 14:40:08 +01009def common = new com.mirantis.mk.Common()
10def gerrit = new com.mirantis.mk.Gerrit()
Filip Pytloun38005aa2017-03-06 10:26:38 +010011def ssh = new com.mirantis.mk.Ssh()
12def git = new com.mirantis.mk.Git()
Jakub Josefc5a223a2017-03-01 14:40:08 +010013
Filip Pytloun19376a82017-03-07 12:29:00 +010014def gerritRef
15try {
16 gerritRef = GERRIT_REFSPEC
17} catch (MissingPropertyException e) {
18 gerritRef = null
19}
20
Jakub Josef83379312017-03-29 18:12:34 +020021def defaultGitRef, defaultGitUrl
Filip Pytloun19376a82017-03-07 12:29:00 +010022try {
Jakub Josef83379312017-03-29 18:12:34 +020023 defaultGitRef = DEFAULT_GIT_REF
24 defaultGitUrl = DEFAULT_GIT_URL
Filip Pytloun19376a82017-03-07 12:29:00 +010025} catch (MissingPropertyException e) {
Jakub Josef83379312017-03-29 18:12:34 +020026 defaultGitRef = null
27 defaultGitUrl = null
Filip Pytloun19376a82017-03-07 12:29:00 +010028}
Jakub Josef83379312017-03-29 18:12:34 +020029def checkouted = false
Jakub Joseffcb615e2017-04-10 14:34:40 +020030def merged = false
Filip Pytloun19376a82017-03-07 12:29:00 +010031
chnydaa9c88702017-05-09 16:51:07 +020032def testNode(node, basename, saltOpts) {
33 sh("reclass --nodeinfo ${node} >/dev/null")
34 sh("salt-call ${saltOpts} --id=${basename} state.show_top")
35 sh("salt-call ${saltOpts} --id=${basename} state.show_lowstate >/dev/null")
36}
37
38def testMaster(masterHostname) {
39
40 def img = docker.image("ubuntu:xenial")
41 def saltOpts = "--retcode-passthrough --force-color"
42
43 img.inside {
44 sh("apt-get update; apt-get install -y wget")
45 sh("echo 'deb [arch=amd64] http://apt-mk.mirantis.com/${DIST}/ nightly salt salt-latest' > /etc/apt/sources.list.d/apt-mk.list")
46 sh("wget -O - http://apt-mk.mirantis.com/public.gpg | apt-key add -")
47 sh("apt-get update; apt-get install -y salt-master python-psutil iproute2 curl python-dev python-pip salt-formula-* python-sphinx")
48 sh("pip install -U https://github.com/madduck/reclass/archive/master.zip")
49 sh("mkdir -p /etc/salt/grains.d && touch /etc/salt/grains.d/dummy")
50 sh("[ ! -d /etc/salt/pki/minion ] && mkdir -p /etc/salt/pki/minion")
51 sh("[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d || true")
52 def masterConf = """file_roots
53: base:
54 - /usr/share/salt-formulas/env
55pillar_opts: False
56open_mode: True
57reclass: &reclass
58 storage_type: yaml_fs
59 inventory_base_uri: /srv/salt/reclass
60ext_pillar:
61 - reclass: *reclass
62master_tops:
63 reclass: *reclass"""
64 writeFile file: "/etc/salt/master.d/master.conf", text: masterConf
65
66 sh("[ -d /srv/salt/reclass/classes/service ] || mkdir -p /srv/salt/reclass/classes/service || true")
67 sh("""for i in /usr/share/salt-formulas/reclass/service/*; do
68 [ -e /srv/salt/reclass/classes/service/\$(basename \$i) ] || ln -s \$i /srv/salt/reclass/classes/service/\$(basename \$i)
69 done""")
70 def jenkinsUID = common.getJenkinsUid()
71 def jenkinsGID = common.getJenkinsGid()
72 sh("chown -R ${jenkinsUID}:${jenkinsGID} /srv/salt/reclass/classes/service")
73 sh("[ ! -d /etc/reclass ] && mkdir /etc/reclass || true")
74 def reclassConfig = """storage_type: yaml_fs
75pretty_print: True
76output: yaml
77inventory_base_uri: /srv/salt/reclass"""
78 writeFile file: "/etc/reclass/reclass-config.yml", text: reclassConfig
79 sh("usr/bin/salt-master; sleep 3")
80 sh("salt-call saltutil.sync_all")
81 sh("reclass --nodeinfo ${masterHostname} >/dev/null")
82 sh("salt-call ${saltOpts} state.show_top")
83
84
85 testSteps = [:]
86 def nodes = sh script: "find /srv/salt/reclass/nodes -type f -name *.yml ! -name cfg*", returnStdout: true
87 for (reclassNode in nodes.tokenize()) {
88 basename = sh script: "\$(basename $node .yml)", returnStdout: true
89 testSteps[basename] = { testNode(reclassNode, basename, saltOpts) }
90 }
91 parallel testSteps
92
93 }
94
95}
96
chnydafa4b0ec2017-05-05 15:49:27 +020097node("python&&docker") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010098 try{
99 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +0100100 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +0200101 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +0200102 // test if change aren't already merged
103 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
104 merged = gerritChange.status == "MERGED"
105 if(!merged){
106 checkouted = gerrit.gerritPatchsetCheckout ([
107 credentialsId : CREDENTIALS_ID
108 ])
chnydaa9c88702017-05-09 16:51:07 +0200109 } else{
110 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Joseffcb615e2017-04-10 14:34:40 +0200111 }
Jakub Josef83379312017-03-29 18:12:34 +0200112 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +0200113 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +0100114 }
Jakub Josef83379312017-03-29 18:12:34 +0200115 if(checkouted){
116 if (fileExists('classes/system')) {
117 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +0100118 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +0200119 remoteUrl = git.getGitRemote()
120 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +0100121 }
Jakub Josef83379312017-03-29 18:12:34 +0200122 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +0100123 }
Jakub Joseffcb615e2017-04-10 14:34:40 +0200124 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +0200125 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +0100126 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100127 }
chnydaa9c88702017-05-09 16:51:07 +0200128
129 def nodes
130 dir ('nodes') {
131 nodes = findFiles(glob: "cfg*.yml")
132 }
133
Jakub Josef62218bb2017-05-09 18:31:00 +0200134 stage("test") {
135 for (int i = 0; i < nodes.size(); i++) {
136 testMaster(nodes[i])
Jakub Josefc5a223a2017-03-01 14:40:08 +0100137 }
138 }
chnydaa9c88702017-05-09 16:51:07 +0200139
Jakub Josefc5a223a2017-03-01 14:40:08 +0100140 } catch (Throwable e) {
141 // If there was an error or exception thrown, the build failed
142 currentBuild.result = "FAILURE"
143 throw e
144 } finally {
145 common.sendNotification(currentBuild.result,"",["slack"])
146 }
Filip Pytloun38005aa2017-03-06 10:26:38 +0100147}