blob: fed6ed551475c0d385189b82b65090389d7ac45d [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
chnydafa4b0ec2017-05-05 15:49:27 +020032node("python&&docker") {
Jakub Josefc5a223a2017-03-01 14:40:08 +010033 try{
34 stage("checkout") {
Filip Pytloun19376a82017-03-07 12:29:00 +010035 if (gerritRef) {
Jakub Josef83379312017-03-29 18:12:34 +020036 // job is triggered by Gerrit
Jakub Joseffcb615e2017-04-10 14:34:40 +020037 // test if change aren't already merged
38 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
39 merged = gerritChange.status == "MERGED"
40 if(!merged){
41 checkouted = gerrit.gerritPatchsetCheckout ([
42 credentialsId : CREDENTIALS_ID
43 ])
chnydaa9c88702017-05-09 16:51:07 +020044 } else{
45 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Joseffcb615e2017-04-10 14:34:40 +020046 }
Jakub Josef83379312017-03-29 18:12:34 +020047 } else if(defaultGitRef && defaultGitUrl) {
Jakub Josefe1407ac2017-03-30 14:10:19 +020048 checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010049 }
Jakub Josef83379312017-03-29 18:12:34 +020050 if(checkouted){
51 if (fileExists('classes/system')) {
52 ssh.prepareSshAgentKey(CREDENTIALS_ID)
Filip Pytloun19376a82017-03-07 12:29:00 +010053 dir('classes/system') {
Jakub Josef83379312017-03-29 18:12:34 +020054 remoteUrl = git.getGitRemote()
55 ssh.ensureKnownHosts(remoteUrl)
Filip Pytloun19376a82017-03-07 12:29:00 +010056 }
Jakub Josef83379312017-03-29 18:12:34 +020057 ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
Filip Pytloun19376a82017-03-07 12:29:00 +010058 }
Jakub Joseffcb615e2017-04-10 14:34:40 +020059 }else if(!merged){
Jakub Josef5ce6a362017-03-31 13:41:17 +020060 throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
Filip Pytloun38005aa2017-03-06 10:26:38 +010061 }
Jakub Josefc5a223a2017-03-01 14:40:08 +010062 }
chnydaa9c88702017-05-09 16:51:07 +020063
chnyda14e44292017-05-13 19:00:06 +020064 stage("test-nodes") {
Jakub Josef8d024772017-05-15 18:07:45 +020065 dir ('nodes') {
66 def nodes = sh script: "find ./ -type f -name 'cfg*.yml'", returnStdout: true
67 def buildSteps = [:]
68 def partitions = common.partitionList(nodes.tokenize(), 3)
69 for (int i=0; i< partitions.size();i++) {
70 def partition = partitions[i]
Jakub Josef4d23e302017-05-15 18:57:54 +020071 buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>())
Jakub Josef8d024772017-05-15 18:07:45 +020072 for(int k=0; k < partition.size;k++){
Jakub Josef417bfde2017-05-15 19:12:30 +020073 def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim()
Jakub Josefa85e19f2017-05-15 19:14:21 +020074 buildSteps.get("partition-${i}").put(basename, { setupAndTestNode(basename) })
Jakub Josef8d024772017-05-15 18:07:45 +020075 }
76 }
77 common.serial(buildSteps)
Jakub Josefc5a223a2017-03-01 14:40:08 +010078 }
chnyda51b03142017-05-10 17:15:27 +020079 }
chnyda14e44292017-05-13 19:00:06 +020080
Jakub Josefc5a223a2017-03-01 14:40:08 +010081 } catch (Throwable e) {
82 // If there was an error or exception thrown, the build failed
83 currentBuild.result = "FAILURE"
84 throw e
85 } finally {
86 common.sendNotification(currentBuild.result,"",["slack"])
87 }
Tomáš Kukrál500c0182017-05-11 13:46:19 +020088}
Jakub Josef8d024772017-05-15 18:07:45 +020089
90def setupAndTestNode(masterName) {
91 def img = docker.image("ubuntu:latest")
92 def saltOpts = "--retcode-passthrough --force-color"
93 def common = new com.mirantis.mk.Common()
94 def workspace = common.getWorkspace()
95
96 img.inside("-u root:root --hostname=${masterName}") {
97 wrap([$class: 'AnsiColorBuildWrapper']) {
98 sh("mkdir -p /srv/salt/ || true")
99 sh("cp -r ${workspace} /srv/salt/reclass")
100 sh("apt-get update && apt-get install -y curl subversion git python-pip sudo python-pip python-dev zlib1g-dev git")
101 sh("svn export --force https://github.com/salt-formulas/salt-formulas/trunk/deploy/scripts /srv/salt/scripts")
102 sh("git config --global user.email || git config --global user.email 'ci@ci.local'")
103 sh("git config --global user.name || git config --global user.name 'CI'")
104 sh("pip install git+https://github.com/epcim/reclass.git@pr/fix/fix_raise_UndefinedVariableError")
105 sh("ls -lRa /srv/salt/reclass")
106
107 // setup iniot and verify salt master and minions
108 withEnv(["FORMULAS_SOURCE=pkg", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local"]){
109 sh("bash -c 'echo $MASTER_HOSTNAME'")
110 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && system_config'")
111 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_bootstrap'")
112 sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_init'")
113 }
114 sh("ls -lRa /srv/salt/reclass/classes/service/")
115
116 def nodes
117 if (DEFAULT_GIT_URL.contains("mk-ci")) {
118 nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
119 } else {
120 nodes = sh script:"find /srv/salt/reclass/nodes/_generated -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true
121 }
122 for (minion in nodes.tokenize()) {
123 def basename = sh script: "basename ${minion} .yml", returnStdout: true
124 if (!basename.trim().contains(masterName)) {
125 testMinion(basename.trim())
126 }
127 }
128 }
129 }
130}
131
132def testMinion(minionName)
133{
134 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}'")
135}