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 |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 31 | |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 32 | def testMinion(minion, saltOpts) |
| 33 | { |
| 34 | sh("reclass-salt -p ${minion} > /tmp/${minion}.pillar_verify") |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 35 | } |
| 36 | |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 37 | def setupandtest(master) { |
| 38 | def img = docker.image("ubuntu:trusty") |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 39 | def saltOpts = "--retcode-passthrough --force-color" |
| 40 | |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 41 | img.inside("-u root:root") { |
| 42 | sh("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") |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 54 | |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 55 | // 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 | """) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 65 | |
| 66 | testSteps = [:] |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 67 | 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)} |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 71 | } |
| 72 | parallel testSteps |
| 73 | |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
chnyda | fa4b0ec | 2017-05-05 15:49:27 +0200 | [diff] [blame] | 78 | node("python&&docker") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 79 | try{ |
| 80 | stage("checkout") { |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 81 | if (gerritRef) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 82 | // job is triggered by Gerrit |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 83 | // 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 | ]) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 90 | } else{ |
| 91 | 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] | 92 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 93 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 94 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 95 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 96 | if(checkouted){ |
| 97 | if (fileExists('classes/system')) { |
| 98 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 99 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 100 | remoteUrl = git.getGitRemote() |
| 101 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 102 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 103 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 104 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 105 | }else if(!merged){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 106 | 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] | 107 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 108 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 109 | |
| 110 | def nodes |
| 111 | dir ('nodes') { |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 112 | nodes = sh script: "find -type f -name cfg*.yml", returnStdout: true |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 113 | } |
| 114 | |
Jakub Josef | 62218bb | 2017-05-09 18:31:00 +0200 | [diff] [blame] | 115 | stage("test") { |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 116 | for (masterNode in nodes.tokenize()) { |
| 117 | basename = sh script: "basename ${masterNode} .yml", returnStdout: true |
| 118 | setupandtest(basename) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 119 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 120 | |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 121 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 122 | } 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 | } |
chnyda | 51b0314 | 2017-05-10 17:15:27 +0200 | [diff] [blame^] | 129 | } |