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 | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 32 | def 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 | |
| 38 | def 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 |
| 55 | pillar_opts: False |
| 56 | open_mode: True |
| 57 | reclass: &reclass |
| 58 | storage_type: yaml_fs |
| 59 | inventory_base_uri: /srv/salt/reclass |
| 60 | ext_pillar: |
| 61 | - reclass: *reclass |
| 62 | master_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 |
| 75 | pretty_print: True |
| 76 | output: yaml |
| 77 | inventory_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 | |
chnyda | fa4b0ec | 2017-05-05 15:49:27 +0200 | [diff] [blame] | 97 | node("python&&docker") { |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 98 | try{ |
| 99 | stage("checkout") { |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 100 | if (gerritRef) { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 101 | // job is triggered by Gerrit |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 102 | // 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 | ]) |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 109 | } else{ |
| 110 | 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] | 111 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 112 | } else if(defaultGitRef && defaultGitUrl) { |
Jakub Josef | e1407ac | 2017-03-30 14:10:19 +0200 | [diff] [blame] | 113 | checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 114 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 115 | if(checkouted){ |
| 116 | if (fileExists('classes/system')) { |
| 117 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 118 | dir('classes/system') { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 119 | remoteUrl = git.getGitRemote() |
| 120 | ssh.ensureKnownHosts(remoteUrl) |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 121 | } |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 122 | ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
Filip Pytloun | 19376a8 | 2017-03-07 12:29:00 +0100 | [diff] [blame] | 123 | } |
Jakub Josef | fcb615e | 2017-04-10 14:34:40 +0200 | [diff] [blame] | 124 | }else if(!merged){ |
Jakub Josef | 5ce6a36 | 2017-03-31 13:41:17 +0200 | [diff] [blame] | 125 | 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] | 126 | } |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 127 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 128 | |
| 129 | def nodes |
| 130 | dir ('nodes') { |
| 131 | nodes = findFiles(glob: "cfg*.yml") |
| 132 | } |
| 133 | |
Jakub Josef | 62218bb | 2017-05-09 18:31:00 +0200 | [diff] [blame] | 134 | stage("test") { |
| 135 | for (int i = 0; i < nodes.size(); i++) { |
| 136 | testMaster(nodes[i]) |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 137 | } |
| 138 | } |
chnyda | a9c8870 | 2017-05-09 16:51:07 +0200 | [diff] [blame] | 139 | |
Jakub Josef | c5a223a | 2017-03-01 14:40:08 +0100 | [diff] [blame] | 140 | } 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 Pytloun | 38005aa | 2017-03-06 10:26:38 +0100 | [diff] [blame] | 147 | } |