Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Update Salt environment pipeline |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | * SALT_MASTER_URL Salt API server location |
| 7 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
| 8 | * MCP_VERSION Version of MCP to upgrade to |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 9 | * UPDATE_CLUSTER_MODEL Update MCP version parameter in cluster model |
| 10 | * UPDATE_PIPELINES Update pipeline repositories on Gerrit |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 11 | * UPDATE_LOCAL_REPOS Update local repositories |
| 12 | */ |
| 13 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 14 | import hudson.model.* |
| 15 | |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 16 | // Load shared libs |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 17 | salt = new com.mirantis.mk.Salt() |
| 18 | common = new com.mirantis.mk.Common() |
| 19 | python = new com.mirantis.mk.Python() |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 20 | jenkinsUtils = new com.mirantis.mk.JenkinsUtils() |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 21 | venvPepper = "venvPepper" |
| 22 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 23 | def triggerMirrorJob(jobName){ |
| 24 | params = jenkinsUtils.getJobParameters(jobName) |
| 25 | build job: jobName, parameters: [ |
| 26 | [$class: 'StringParameterValue', name: 'BRANCHES', value: params.get("BRANCHES")], |
| 27 | [$class: 'StringParameterValue', name: 'CREDENTIALS_ID', value: params.get("CREDENTIALS_ID")], |
| 28 | [$class: 'StringParameterValue', name: 'SOURCE_URL', value: params.get("SOURCE_URL")], |
| 29 | [$class: 'StringParameterValue', name: 'TARGET_URL', value: params.get("TARGET_URL")] |
| 30 | ] |
| 31 | } |
| 32 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 33 | timeout(time: 12, unit: 'HOURS') { |
| 34 | node("python") { |
| 35 | try { |
| 36 | python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 37 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 38 | if(MCP_VERSION == ""){ |
| 39 | error("You must specify MCP version") |
| 40 | } |
| 41 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 42 | stage("Update Reclass"){ |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 43 | def cluster_name = salt.getPillar(venvPepper, 'I@salt:master', "_param:cluster_name").get("return")[0].values()[0] |
| 44 | if(UPDATE_CLUSTER_MODEL.toBoolean()){ |
| 45 | try{ |
| 46 | salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/ && git diff-index --quiet HEAD --") |
| 47 | } |
| 48 | catch(Exception ex){ |
| 49 | error("You have uncommited changes in your Reclass cluster model repository. Please commit or reset them and rerun the pipeline.") |
| 50 | } |
| 51 | salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name && grep -r --exclude-dir=aptly -l 'apt_mk_version: .*' * | xargs sed -i 's/apt_mk_version: .*/apt_mk_version: \"$MCP_VERSION\"/g'") |
| 52 | } |
| 53 | |
| 54 | try{ |
| 55 | salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/system && git diff-index --quiet HEAD --") |
| 56 | } |
| 57 | catch(Exception ex){ |
| 58 | error("You have unstaged changes in your Reclass system model repository. Please reset them and rerun the pipeline.") |
| 59 | } |
| 60 | salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/system && git checkout $MCP_VERSION") |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 61 | } |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 62 | |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 63 | if(UPDATE_LOCAL_REPOS.toBoolean()){ |
| 64 | stage("Update local repos"){ |
| 65 | common.infoMsg("Updating local repositories") |
Sam Stoelinga | aab7970 | 2018-04-09 18:49:39 -0700 | [diff] [blame] | 66 | |
| 67 | def engine = salt.getPillar(venvPepper, 'I@aptly:server', "aptly:server:source:engine") |
| 68 | runningOnDocker = engine.get("return")[0].containsValue("docker") |
| 69 | |
| 70 | if (runningOnDocker) { |
| 71 | common.infoMsg("Aptly is running as Docker container") |
| 72 | } |
| 73 | else { |
| 74 | common.infoMsg("Aptly isn't running as Docker container. Going to use aptly user for executing aptly commands") |
| 75 | } |
| 76 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 77 | salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name/cicd/aptly && git checkout $MCP_VERSION") |
| 78 | |
Sam Stoelinga | aab7970 | 2018-04-09 18:49:39 -0700 | [diff] [blame] | 79 | if(runningOnDocker){ |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 80 | salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true) |
| 81 | } |
| 82 | else{ |
| 83 | salt.cmdRun(venvPepper, 'I@aptly:server', "aptly mirror list --raw | grep -E '*' | xargs -n 1 aptly mirror drop -force", true, null, true, ['runas=aptly']) |
| 84 | } |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 85 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 86 | salt.enforceState(venvPepper, 'I@aptly:server', 'aptly', true) |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 87 | |
Sam Stoelinga | aab7970 | 2018-04-09 18:49:39 -0700 | [diff] [blame] | 88 | if(runningOnDocker){ |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 89 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv"], null, true) |
| 90 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-frv -u http://10.99.0.1:8080"], null, true) |
| 91 | } |
| 92 | else{ |
| 93 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_mirror_update.sh', "args=-sv", 'runas=aptly'], null, true) |
| 94 | salt.runSaltProcessStep(venvPepper, 'I@aptly:server', 'cmd.script', ['salt://aptly/files/aptly_publish_update.sh', "args=-afrv", 'runas=aptly'], null, true) |
| 95 | } |
Richard Felkl | 79d7df1 | 2018-01-05 16:40:11 +0100 | [diff] [blame] | 96 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 97 | salt.enforceState(venvPepper, 'I@aptly:server', 'docker.client.registry', true) |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 98 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 99 | salt.enforceState(venvPepper, 'I@aptly:server', 'debmirror', true) |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 100 | |
Richard Felkl | 5f7fdaf | 2018-02-15 15:38:31 +0100 | [diff] [blame] | 101 | salt.enforceState(venvPepper, 'I@aptly:server', 'git.server', true) |
| 102 | |
| 103 | salt.enforceState(venvPepper, 'I@aptly:server', 'linux.system.file', true) |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 104 | } |
| 105 | } |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 106 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 107 | stage("Update Drivetrain"){ |
| 108 | salt.cmdRun(venvPepper, 'I@salt:master', "sed -i -e 's/[^ ]*[^ ]/$MCP_VERSION/4' /etc/apt/sources.list.d/mcp_salt.list") |
| 109 | salt.cmdRun(venvPepper, 'I@salt:master', "apt-get -o Dir::Etc::sourcelist='/etc/apt/sources.list.d/mcp_salt.list' -o Dir::Etc::sourceparts='-' -o APT::Get::List-Cleanup='0' update") |
| 110 | salt.cmdRun(venvPepper, 'I@salt:master', "apt-get install -y --allow-downgrades salt-formula-*") |
| 111 | salt.fullRefresh(venvPepper, 'I@salt:master') |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 112 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 113 | try{ |
| 114 | salt.enforceState(venvPepper, "I@salt:master", 'reclass', true) |
| 115 | } |
| 116 | catch(Exception ex){ |
| 117 | error("Reclass fails rendering. Pay attention to your cluster model.") |
| 118 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 119 | |
Richard Felkl | 970e008 | 2018-06-12 18:00:51 +0200 | [diff] [blame^] | 120 | salt.fullRefresh(venvPepper, '*') |
| 121 | |
| 122 | try{ |
| 123 | salt.cmdRun(venvPepper, 'I@salt:master', "reclass-salt --top") |
| 124 | } |
| 125 | catch(Exception ex){ |
| 126 | error("Reclass fails rendering. Pay attention to your cluster model.") |
| 127 | } |
| 128 | |
| 129 | if(UPDATE_PIPELINES.toBoolean()){ |
| 130 | triggerMirrorJob("git-mirror-downstream-mk-pipelines") |
| 131 | triggerMirrorJob("git-mirror-downstream-pipeline-library") |
| 132 | } |
| 133 | |
| 134 | salt.enforceState(venvPepper, "I@jenkins:client", 'jenkins.client', true) |
| 135 | |
| 136 | salt.cmdRun(venvPepper, "I@salt:master", "salt -C 'I@jenkins:client and I@docker:client' state.sls docker.client --async") |
| 137 | |
| 138 | sleep(180) |
| 139 | |
| 140 | common.infoMsg("Checking if Docker containers are up") |
| 141 | |
| 142 | try{ |
| 143 | common.retry(10, 30){ |
| 144 | salt.cmdRun(venvPepper, 'I@jenkins:client and I@docker:client', "! docker service ls | tail -n +2 | grep -v -E '\\s([0-9])/\\1\\s'") |
| 145 | } |
| 146 | } |
| 147 | catch(Exception ex){ |
| 148 | error("Docker containers for CI/CD services are having troubles with starting.") |
| 149 | } |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 150 | } |
| 151 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 152 | catch (Throwable e) { |
| 153 | // If there was an error or exception thrown, the build failed |
| 154 | currentBuild.result = "FAILURE" |
| 155 | throw e |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 156 | } |
Richard Felkl | 26cae4d | 2017-12-19 00:19:16 +0100 | [diff] [blame] | 157 | } |
Jakub Josef | 2c21c6c | 2018-02-08 18:51:42 +0100 | [diff] [blame] | 158 | } |