blob: 0c5c97170e893bada128ec3a36ad85212df90d43 [file] [log] [blame]
Richard Felkl26cae4d2017-12-19 00:19:16 +01001/**
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 Felkl970e0082018-06-12 18:00:51 +02009 * UPDATE_CLUSTER_MODEL Update MCP version parameter in cluster model
10 * UPDATE_PIPELINES Update pipeline repositories on Gerrit
Richard Felkl26cae4d2017-12-19 00:19:16 +010011 * UPDATE_LOCAL_REPOS Update local repositories
12 */
13
Richard Felkl970e0082018-06-12 18:00:51 +020014import hudson.model.*
15
Richard Felkl26cae4d2017-12-19 00:19:16 +010016// Load shared libs
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010017salt = new com.mirantis.mk.Salt()
18common = new com.mirantis.mk.Common()
19python = new com.mirantis.mk.Python()
Richard Felkl970e0082018-06-12 18:00:51 +020020jenkinsUtils = new com.mirantis.mk.JenkinsUtils()
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010021venvPepper = "venvPepper"
22
Richard Felkl970e0082018-06-12 18:00:51 +020023def 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 Josefa63f9862018-01-11 17:58:38 +010033timeout(time: 12, unit: 'HOURS') {
34 node("python") {
35 try {
36 python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
Richard Felkl26cae4d2017-12-19 00:19:16 +010037
Richard Felkl970e0082018-06-12 18:00:51 +020038 if(MCP_VERSION == ""){
39 error("You must specify MCP version")
40 }
41
Jakub Josefa63f9862018-01-11 17:58:38 +010042 stage("Update Reclass"){
Richard Felkl970e0082018-06-12 18:00:51 +020043 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 Josefa63f9862018-01-11 17:58:38 +010061 }
Richard Felkl26cae4d2017-12-19 00:19:16 +010062
Jakub Josefa63f9862018-01-11 17:58:38 +010063 if(UPDATE_LOCAL_REPOS.toBoolean()){
64 stage("Update local repos"){
65 common.infoMsg("Updating local repositories")
Sam Stoelingaaab79702018-04-09 18:49:39 -070066
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 Felkl970e0082018-06-12 18:00:51 +020077 salt.cmdRun(venvPepper, 'I@salt:master', "cd /srv/salt/reclass/classes/cluster/$cluster_name/cicd/aptly && git checkout $MCP_VERSION")
78
Sam Stoelingaaab79702018-04-09 18:49:39 -070079 if(runningOnDocker){
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010080 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 Felkl26cae4d2017-12-19 00:19:16 +010085
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010086 salt.enforceState(venvPepper, 'I@aptly:server', 'aptly', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +010087
Sam Stoelingaaab79702018-04-09 18:49:39 -070088 if(runningOnDocker){
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010089 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 Felkl79d7df12018-01-05 16:40:11 +010096
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010097 salt.enforceState(venvPepper, 'I@aptly:server', 'docker.client.registry', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +010098
Richard Felkl5f7fdaf2018-02-15 15:38:31 +010099 salt.enforceState(venvPepper, 'I@aptly:server', 'debmirror', true)
Richard Felkl26cae4d2017-12-19 00:19:16 +0100100
Richard Felkl5f7fdaf2018-02-15 15:38:31 +0100101 salt.enforceState(venvPepper, 'I@aptly:server', 'git.server', true)
102
103 salt.enforceState(venvPepper, 'I@aptly:server', 'linux.system.file', true)
Jakub Josefa63f9862018-01-11 17:58:38 +0100104 }
105 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100106
Richard Felkl970e0082018-06-12 18:00:51 +0200107 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 Josefa63f9862018-01-11 17:58:38 +0100112
Richard Felkl970e0082018-06-12 18:00:51 +0200113 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 Josefa63f9862018-01-11 17:58:38 +0100119
Richard Felkl970e0082018-06-12 18:00:51 +0200120 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 Felkl26cae4d2017-12-19 00:19:16 +0100150 }
151 }
Jakub Josefa63f9862018-01-11 17:58:38 +0100152 catch (Throwable e) {
153 // If there was an error or exception thrown, the build failed
154 currentBuild.result = "FAILURE"
155 throw e
Richard Felkl26cae4d2017-12-19 00:19:16 +0100156 }
Richard Felkl26cae4d2017-12-19 00:19:16 +0100157 }
Jakub Josef2c21c6c2018-02-08 18:51:42 +0100158}