| |
| /** |
| * Test salt models pipeline |
| * DEFAULT_GIT_REF |
| * DEFAULT_GIT_URL |
| * CREDENTIALS_ID |
| */ |
| |
| def common = new com.mirantis.mk.Common() |
| def gerrit = new com.mirantis.mk.Gerrit() |
| def ssh = new com.mirantis.mk.Ssh() |
| def git = new com.mirantis.mk.Git() |
| |
| def gerritRef |
| try { |
| gerritRef = GERRIT_REFSPEC |
| } catch (MissingPropertyException e) { |
| gerritRef = null |
| } |
| |
| def defaultGitRef, defaultGitUrl |
| try { |
| defaultGitRef = DEFAULT_GIT_REF |
| defaultGitUrl = DEFAULT_GIT_URL |
| } catch (MissingPropertyException e) { |
| defaultGitRef = null |
| defaultGitUrl = null |
| } |
| def checkouted = false |
| def merged = false |
| |
| def testNode(node, basename, saltOpts) { |
| sh("reclass --nodeinfo ${node} >/dev/null") |
| sh("salt-call ${saltOpts} --id=${basename} state.show_top") |
| sh("salt-call ${saltOpts} --id=${basename} state.show_lowstate >/dev/null") |
| } |
| |
| def testMaster(masterHostname) { |
| |
| def img = docker.image("ubuntu:xenial") |
| def saltOpts = "--retcode-passthrough --force-color" |
| |
| img.inside { |
| sh("apt-get update; apt-get install -y wget") |
| sh("echo 'deb [arch=amd64] http://apt-mk.mirantis.com/${DIST}/ nightly salt salt-latest' > /etc/apt/sources.list.d/apt-mk.list") |
| sh("wget -O - http://apt-mk.mirantis.com/public.gpg | apt-key add -") |
| sh("apt-get update; apt-get install -y salt-master python-psutil iproute2 curl python-dev python-pip salt-formula-* python-sphinx") |
| sh("pip install -U https://github.com/madduck/reclass/archive/master.zip") |
| sh("mkdir -p /etc/salt/grains.d && touch /etc/salt/grains.d/dummy") |
| sh("[ ! -d /etc/salt/pki/minion ] && mkdir -p /etc/salt/pki/minion") |
| sh("[ ! -d /etc/salt/master.d ] && mkdir -p /etc/salt/master.d || true") |
| def masterConf = """file_roots |
| : base: |
| - /usr/share/salt-formulas/env |
| pillar_opts: False |
| open_mode: True |
| reclass: &reclass |
| storage_type: yaml_fs |
| inventory_base_uri: /srv/salt/reclass |
| ext_pillar: |
| - reclass: *reclass |
| master_tops: |
| reclass: *reclass""" |
| writeFile file: "/etc/salt/master.d/master.conf", text: masterConf |
| |
| sh("[ -d /srv/salt/reclass/classes/service ] || mkdir -p /srv/salt/reclass/classes/service || true") |
| sh("""for i in /usr/share/salt-formulas/reclass/service/*; do |
| [ -e /srv/salt/reclass/classes/service/\$(basename \$i) ] || ln -s \$i /srv/salt/reclass/classes/service/\$(basename \$i) |
| done""") |
| def jenkinsUID = common.getJenkinsUid() |
| def jenkinsGID = common.getJenkinsGid() |
| sh("chown -R ${jenkinsUID}:${jenkinsGID} /srv/salt/reclass/classes/service") |
| sh("[ ! -d /etc/reclass ] && mkdir /etc/reclass || true") |
| def reclassConfig = """storage_type: yaml_fs |
| pretty_print: True |
| output: yaml |
| inventory_base_uri: /srv/salt/reclass""" |
| writeFile file: "/etc/reclass/reclass-config.yml", text: reclassConfig |
| sh("usr/bin/salt-master; sleep 3") |
| sh("salt-call saltutil.sync_all") |
| sh("reclass --nodeinfo ${masterHostname} >/dev/null") |
| sh("salt-call ${saltOpts} state.show_top") |
| |
| |
| testSteps = [:] |
| def nodes = sh script: "find /srv/salt/reclass/nodes -type f -name *.yml ! -name cfg*", returnStdout: true |
| for (reclassNode in nodes.tokenize()) { |
| basename = sh script: "\$(basename $node .yml)", returnStdout: true |
| testSteps[basename] = { testNode(reclassNode, basename, saltOpts) } |
| } |
| parallel testSteps |
| |
| } |
| |
| } |
| |
| node("python&&docker") { |
| try{ |
| stage("checkout") { |
| if (gerritRef) { |
| // job is triggered by Gerrit |
| // test if change aren't already merged |
| def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID) |
| merged = gerritChange.status == "MERGED" |
| if(!merged){ |
| checkouted = gerrit.gerritPatchsetCheckout ([ |
| credentialsId : CREDENTIALS_ID |
| ]) |
| } else{ |
| common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| } |
| } else if(defaultGitRef && defaultGitUrl) { |
| checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID) |
| } |
| if(checkouted){ |
| if (fileExists('classes/system')) { |
| ssh.prepareSshAgentKey(CREDENTIALS_ID) |
| dir('classes/system') { |
| remoteUrl = git.getGitRemote() |
| ssh.ensureKnownHosts(remoteUrl) |
| } |
| ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive") |
| } |
| }else if(!merged){ |
| throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null") |
| } |
| } |
| |
| def nodes |
| dir ('nodes') { |
| nodes = findFiles(glob: "cfg*.yml") |
| } |
| |
| stage("test") { |
| for (int i = 0; i < nodes.size(); i++) { |
| testMaster(nodes[i]) |
| } |
| } |
| |
| } catch (Throwable e) { |
| // If there was an error or exception thrown, the build failed |
| currentBuild.result = "FAILURE" |
| throw e |
| } finally { |
| common.sendNotification(currentBuild.result,"",["slack"]) |
| } |
| } |