blob: 96627f1b04138042e44b07be50159276d2288064 [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
chnyda51b03142017-05-10 17:15:27 +020032def testMinion(minion, saltOpts)
33{
34 sh("reclass-salt -p ${minion} > /tmp/${minion}.pillar_verify")
chnydaa9c88702017-05-09 16:51:07 +020035}
36
chnyda51b03142017-05-10 17:15:27 +020037def setupandtest(master) {
38 def img = docker.image("ubuntu:trusty")
chnydaa9c88702017-05-09 16:51:07 +020039 def saltOpts = "--retcode-passthrough --force-color"
40
chnyda51b03142017-05-10 17:15:27 +020041 img.inside("-u root:root") {
42sh("apt-get update; apt-get install software-properties-common python-software-properties -y")
43 sh("add-apt-repository ppa:saltstack/salt -y")
44 sh("apt-get update; apt-get install -y curl subversion git python-pip sudo")
45 sh("sudo apt-get install -y salt-master salt-minion salt-ssh salt-cloud salt-doc")
46 sh("svn export --force https://github.com/chnyda/salt-formulas/trunk/deploy/scripts /srv/salt/scripts")
47 //configure git
48 sh("git config --global user.email || git config --global user.email 'ci@ci.local'")
49 sh("git config --global user.name || git config --global user.name 'CI'")
50 sh("mkdir -p /srv/salt/reclass; cp -r * /srv/salt/reclass")
51 //
52// sh("cd /srv/salt/reclass; test ! -e .gitmodules || git submodule update --init --recursive")
53// sh("cd /srv/salt/reclass; git commit -am 'Fake branch update' || true")
chnydaa9c88702017-05-09 16:51:07 +020054
chnyda51b03142017-05-10 17:15:27 +020055 // setup iniot and verify salt master and minions
56 sh(""". /srv/salt/scripts/salt-master-init.sh
57 export SUDO=sudo
58 export DEBUG=1
59 export MASTER_HOSTNAME=${master}
60 system_config;
61 saltmaster_bootstrap &&\
62 saltmaster_init > /tmp/${master}.init &&\
63 verify_salt_master
64 """)
chnydaa9c88702017-05-09 16:51:07 +020065
66 testSteps = [:]
chnyda51b03142017-05-10 17:15:27 +020067 nodes = sh script:"ls /srv/salt/reclass/nodes/_generated"
68 for (minion in nodes.tokenize()) {
69 def basename = sh script: "basename ${minion} .yml", returnStdout: true
70 testSteps = { testMinion(basename)}
chnydaa9c88702017-05-09 16:51:07 +020071 }
72 parallel testSteps
73
74 }
75
76}
77
chnydafa4b0ec2017-05-05 15:49:27 +020078node("python&&docker") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010079 try{
80 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010081 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020082 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020083 // test if change aren't already merged
84 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
85 merged = gerritChange.status == "MERGED"
86 if(!merged){
87 checkouted = gerrit.gerritPatchsetCheckout ([
88 credentialsId : CREDENTIALS_ID
89 ])
chnydaa9c88702017-05-09 16:51:07 +020090 } else{
91 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Joseffcb615e2017-04-10 14:34:40 +020092 }
Jakub Josef83379312017-03-29 18:12:34 +020093 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020094 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010095 }
Jakub Josef83379312017-03-29 18:12:34 +020096 if(checkouted){
97 if (fileExists('classes/system')) {
98 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010099 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +0200100 remoteUrl = git.getGitRemote()
101 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +0100102 }
Jakub Josef83379312017-03-29 18:12:34 +0200103 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +0100104 }
Jakub Joseffcb615e2017-04-10 14:34:40 +0200105 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +0200106 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +0100107 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100108 }
chnydaa9c88702017-05-09 16:51:07 +0200109
110 def nodes
111 dir ('nodes') {
chnyda51b03142017-05-10 17:15:27 +0200112 nodes = sh script: "find -type f -name cfg*.yml", returnStdout: true
chnydaa9c88702017-05-09 16:51:07 +0200113 }
114
Jakub Josef62218bb2017-05-09 18:31:00 +0200115 stage("test") {
chnyda51b03142017-05-10 17:15:27 +0200116 for (masterNode in nodes.tokenize()) {
117 basename = sh script: "basename ${masterNode} .yml", returnStdout: true
118 setupandtest(basename)
Jakub Josefc5a223a2017-03-01 14:40:08 +0100119 }
chnydaa9c88702017-05-09 16:51:07 +0200120
chnyda51b03142017-05-10 17:15:27 +0200121 }
Jakub Josefc5a223a2017-03-01 14:40:08 +0100122 } catch (Throwable e) {
123 // If there was an error or exception thrown, the build failed
124 currentBuild.result = "FAILURE"
125 throw e
126 } finally {
127 common.sendNotification(currentBuild.result,"",["slack"])
128 }
chnyda51b03142017-05-10 17:15:27 +0200129}