Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 1 | |
| 2 | /** |
| 3 | * Test salt models pipeline |
| 4 | * DEFAULT_GIT_REF |
| 5 | * DEFAULT_GIT_URL |
| 6 | * CREDENTIALS_ID |
| 7 | */ |
| 8 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 9 | def common = new com.mirantis.mk.Common() |
| 10 | def gerrit = new com.mirantis.mk.Gerrit() |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 11 | def ssh = new com.mirantis.mk.Ssh() |
| 12 | def git = new com.mirantis.mk.Git() |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 13 | |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 14 | def gerritRef |
| 15 | try { |
| 16 | gerritRef = GERRIT_REFSPEC |
| 17 | } catch (MissingPropertyException e) { |
| 18 | gerritRef = null |
| 19 | } |
| 20 | |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 21 | def defaultGitRef, defaultGitUrl |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 22 | try { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 23 | defaultGitRef = DEFAULT_GIT_REF |
| 24 | defaultGitUrl = DEFAULT_GIT_URL |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 25 | } catch (MissingPropertyException e) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 26 | defaultGitRef = null |
| 27 | defaultGitUrl = null |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 28 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 29 | def checkouted = false |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 30 | def merged = false |
chnyda | fa4b0ec | 2017-05-05 15:49:27 +0200 | [diff] [blame] | 31 | node("python&&docker") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 32 | try{ |
| 33 | stage("checkout") { |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 34 | if (gerritRef) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 35 | // job is triggered by Gerrit |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 36 | // test if change aren't already merged |
| 37 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| 38 | merged = gerritChange.status == "MERGED" |
| 39 | if(!merged){ |
| 40 | checkouted = gerrit.gerritPatchsetCheckout ([ |
| 41 | credentialsId : CREDENTIALS_ID |
| 42 | ]) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 43 | } else{ |
| 44 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 45 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 46 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 47 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 48 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 49 | if(checkouted){ |
| 50 | if (fileExists('classes/system')) { |
| 51 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 52 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 53 | remoteUrl = git.getGitRemote() |
| 54 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 55 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 56 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 57 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 58 | }else if(!merged){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 59 | throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
Filip Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 60 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 61 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 62 | |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 63 | stage("test-nodes") { |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame^] | 64 | def nodes = sh(script: "find ./nodes -type f -name 'cfg*.yml'", returnStdout: true).tokenize() |
chnyda | b098e56 | 2017-05-16 10:31:26 +0200 | [diff] [blame] | 65 | def buildSteps = [:] |
Jakub Josef | 35dd8c0 | 2017-05-17 14:25:42 +0200 | [diff] [blame^] | 66 | if(nodes.size() > 1){ |
| 67 | if(nodes.size() <= 3){ |
| 68 | common.infoMsg("Found <=3 cfg nodes, running parallel test") |
| 69 | for(int i=0; i < nodes.size();i++){ |
| 70 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 71 | buildSteps.put("node-${basename}", { setupAndTestNode(basename) }) |
| 72 | } |
| 73 | parallel buildSteps |
| 74 | }else{ |
| 75 | common.infoMsg("Found more than 3 cfg nodes, running parallel group test with 3 nodes") |
| 76 | def partitions = common.partitionList(nodes, 3) |
| 77 | for (int i=0; i < partitions.size();i++) { |
| 78 | def partition = partitions[i] |
| 79 | buildSteps.put("partition-${i}", new HashMap<String,org.jenkinsci.plugins.workflow.cps.CpsClosure2>()) |
| 80 | for(int k=0; k < partition.size;k++){ |
| 81 | def basename = sh(script: "basename ${partition[k]} .yml", returnStdout: true).trim() |
| 82 | buildSteps.get("partition-${i}").put(basename, { setupAndTestNode(basename) }) |
| 83 | } |
| 84 | } |
| 85 | common.serial(buildSteps) |
| 86 | } |
| 87 | }else{ |
| 88 | common.infoMsg("Found one cfg node, running single test") |
| 89 | def basename = sh(script: "basename ${nodes[0]} .yml", returnStdout: true).trim() |
| 90 | setupAndTestNode(basename) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 91 | } |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame] | 92 | } |
chnyda | 14e4429 | 2017-05-13 19:00:06 +0200 | [diff] [blame] | 93 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 94 | } catch (Throwable e) { |
| 95 | // If there was an error or exception thrown, the build failed |
| 96 | currentBuild.result = "FAILURE" |
| 97 | throw e |
| 98 | } finally { |
| 99 | common.sendNotification(currentBuild.result,"",["slack"]) |
| 100 | } |
Tomáš Kukrál | 500c018 | 2017-05-11 13:46:19 +0200 | [diff] [blame] | 101 | } |
Jakub Josef | 8d02477 | 2017-05-15 18:07:45 +0200 | [diff] [blame] | 102 | |
| 103 | def setupAndTestNode(masterName) { |
| 104 | def img = docker.image("ubuntu:latest") |
| 105 | def saltOpts = "--retcode-passthrough --force-color" |
| 106 | def common = new com.mirantis.mk.Common() |
| 107 | def workspace = common.getWorkspace() |
| 108 | |
| 109 | img.inside("-u root:root --hostname=${masterName}") { |
| 110 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 111 | sh("mkdir -p /srv/salt/ || true") |
| 112 | sh("cp -r ${workspace} /srv/salt/reclass") |
| 113 | sh("apt-get update && apt-get install -y curl subversion git python-pip sudo python-pip python-dev zlib1g-dev git") |
| 114 | sh("svn export --force https://github.com/salt-formulas/salt-formulas/trunk/deploy/scripts /srv/salt/scripts") |
| 115 | sh("git config --global user.email || git config --global user.email 'ci@ci.local'") |
| 116 | sh("git config --global user.name || git config --global user.name 'CI'") |
| 117 | sh("pip install git+https://github.com/epcim/reclass.git@pr/fix/fix_raise_UndefinedVariableError") |
| 118 | sh("ls -lRa /srv/salt/reclass") |
| 119 | |
| 120 | // setup iniot and verify salt master and minions |
| 121 | withEnv(["FORMULAS_SOURCE=pkg", "DEBUG=1", "MASTER_HOSTNAME=${masterName}", "MINION_ID=${masterName}", "HOSTNAME=cfg01", "DOMAIN=mk-ci.local"]){ |
| 122 | sh("bash -c 'echo $MASTER_HOSTNAME'") |
| 123 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && system_config'") |
| 124 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_bootstrap'") |
| 125 | sh("bash -c 'source /srv/salt/scripts/salt-master-init.sh; cd /srv/salt/scripts && saltmaster_init'") |
| 126 | } |
| 127 | sh("ls -lRa /srv/salt/reclass/classes/service/") |
| 128 | |
| 129 | def nodes |
| 130 | if (DEFAULT_GIT_URL.contains("mk-ci")) { |
| 131 | nodes = sh script: "find /srv/salt/reclass/nodes -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 132 | } else { |
| 133 | nodes = sh script:"find /srv/salt/reclass/nodes/_generated -name '*.yml' | grep -v 'cfg*.yml'", returnStdout: true |
| 134 | } |
| 135 | for (minion in nodes.tokenize()) { |
| 136 | def basename = sh script: "basename ${minion} .yml", returnStdout: true |
| 137 | if (!basename.trim().contains(masterName)) { |
| 138 | testMinion(basename.trim()) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | def testMinion(minionName) |
| 146 | { |
| 147 | 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}'") |
| 148 | } |