Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 1 | /** |
| 2 | * |
| 3 | * Launch heat stack with CI/CD lab infrastructure |
| 4 | * |
| 5 | * Expected parameters: |
| 6 | * HEAT_TEMPLATE_URL URL to git repo with Heat templates |
| 7 | * HEAT_TEMPLATE_CREDENTIALS Credentials to the Heat templates repo |
| 8 | * HEAT_TEMPLATE_BRANCH Heat templates repo branch |
| 9 | * HEAT_STACK_NAME Heat stack name |
| 10 | * HEAT_STACK_TEMPLATE Heat stack HOT template |
| 11 | * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters |
| 12 | * HEAT_STACK_ZONE Heat stack availability zone |
| 13 | * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool |
| 14 | * HEAT_STACK_DELETE Delete Heat stack when finished (bool) |
| 15 | * HEAT_STACK_CLEANUP_JOB Name of job for deleting Heat stack |
| 16 | * HEAT_STACK_REUSE Reuse Heat stack (don't create one) |
| 17 | * |
| 18 | * SALT_MASTER_CREDENTIALS Credentials to the Salt API |
Filip Pytloun | e32fda8 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 19 | * SALT_MASTER_PORT Port of salt-api, defaults to 8000 |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 20 | * |
| 21 | * OPENSTACK_API_URL OpenStack API address |
| 22 | * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API |
| 23 | * OPENSTACK_API_PROJECT OpenStack project to connect to |
| 24 | * OPENSTACK_API_CLIENT Versions of OpenStack python clients |
| 25 | * OPENSTACK_API_VERSION Version of the OpenStack API (2/3) |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | git = new com.mirantis.mk.Git() |
| 30 | openstack = new com.mirantis.mk.Openstack() |
| 31 | salt = new com.mirantis.mk.Salt() |
| 32 | orchestrate = new com.mirantis.mk.Orchestrate() |
| 33 | |
| 34 | node { |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 35 | try { |
| 36 | // connection objects |
| 37 | def openstackCloud |
| 38 | def saltMaster |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 39 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 40 | // value defaults |
| 41 | def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty' |
| 42 | def openstackEnv = "${env.WORKSPACE}/venv" |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 43 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 44 | if (HEAT_STACK_NAME == '') { |
| 45 | HEAT_STACK_NAME = BUILD_TAG |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 46 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 47 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 48 | // |
| 49 | // Bootstrap |
| 50 | // |
| 51 | |
| 52 | stage ('Download Heat templates') { |
| 53 | git.checkoutGitRepository('template', HEAT_TEMPLATE_URL, HEAT_TEMPLATE_BRANCH, HEAT_TEMPLATE_CREDENTIALS) |
Filip Pytloun | e32fda8 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 54 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 55 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 56 | stage('Install OpenStack CLI') { |
| 57 | openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion) |
| 58 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 59 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 60 | stage('Connect to OpenStack cloud') { |
| 61 | openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT) |
| 62 | openstack.getKeystoneToken(openstackCloud, openstackEnv) |
| 63 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 64 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 65 | if (HEAT_STACK_REUSE == 'false') { |
| 66 | stage('Launch new Heat stack') { |
| 67 | envParams = [ |
| 68 | 'instance_zone': HEAT_STACK_ZONE, |
| 69 | 'public_net': HEAT_STACK_PUBLIC_NET |
| 70 | ] |
| 71 | openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv) |
| 72 | } |
| 73 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 74 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 75 | stage('Connect to Salt master') { |
| 76 | def saltMasterPort |
| 77 | try { |
| 78 | saltMasterPort = SALT_MASTER_PORT |
| 79 | } catch (MissingPropertyException e) { |
| 80 | saltMasterPort = 8000 |
| 81 | } |
| 82 | saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv) |
| 83 | saltMasterUrl = "http://${saltMasterHost}:${saltMasterPort}" |
| 84 | saltMaster = salt.connection(saltMasterUrl, SALT_MASTER_CREDENTIALS) |
| 85 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 86 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 87 | // |
| 88 | // Install |
| 89 | // |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 90 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 91 | stage('Install core infra') { |
| 92 | // salt.master, reclass |
| 93 | // refresh_pillar |
| 94 | // sync_all |
| 95 | // linux,openssh,salt.minion.ntp |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 96 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 97 | orchestrate.installFoundationInfra(saltMaster) |
| 98 | orchestrate.validateFoundationInfra(saltMaster) |
| 99 | } |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 100 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 101 | stage("Deploy GlusterFS") { |
| 102 | salt.enforceState(saltMaster, 'I@glusterfs:server', 'glusterfs.server.service', true) |
| 103 | salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true) |
| 104 | sleep(5) |
| 105 | salt.enforceState(saltMaster, 'I@glusterfs:client', 'glusterfs.client', true) |
| 106 | print salt.cmdRun(saltMaster, 'I@glusterfs:client', 'mount|grep fuse.glusterfs || echo "Command failed"') |
| 107 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 108 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 109 | stage("Deploy GlusterFS") { |
| 110 | salt.enforceState(saltMaster, 'I@haproxy:proxy', 'haproxy,keepalived') |
| 111 | } |
| 112 | |
| 113 | stage("Setup Docker Swarm") { |
| 114 | salt.enforceState(saltMaster, 'I@docker:host', 'docker.host', true) |
| 115 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.swarm', true) |
| 116 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'salt', true) |
| 117 | salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.flush') |
| 118 | salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.update') |
| 119 | salt.enforceState(saltMaster, 'I@docker:swarm', 'docker.swarm', true) |
| 120 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'docker node ls') |
| 121 | } |
| 122 | |
| 123 | stage("Deploy Docker services") { |
| 124 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client') |
| 125 | |
| 126 | // XXX: Hack to fix dependency of gerrit on mysql |
| 127 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', "docker service rm gerrit; sleep 5; rm -rf /srv/volumes/gerrit/*") |
| 128 | sleep(10) |
Filip Pytloun | c6d3bbf | 2017-03-01 08:48:02 +0100 | [diff] [blame] | 129 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', "apt-get install -y mysql-client; mysql -ppassword -h172.16.10.11 -P13306 -e'drop database gerrit;create database gerrit;'") |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 130 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client') |
| 131 | // ---- cut here (end of hack) ---- |
| 132 | |
| 133 | retry(30) { |
| 134 | out = salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', """/bin/bash -c 'docker service ls | grep -E "0/[0-9]+"' && echo 'Some services are not running'""") |
| 135 | for (int a = 0; a < out['return'].size(); a++) { |
| 136 | def entry = out['return'].get(a) |
| 137 | for (int i = 0; i < entry.size(); i++) { |
| 138 | def node = entry.get(i) |
| 139 | if (node) { |
| 140 | if (node.value =~ /Some services are not running/) { |
| 141 | sleep(10) |
| 142 | throw new Exception("$node.key: $node.value") |
| 143 | } else { |
| 144 | print out |
| 145 | } |
Filip Pytloun | 7e2a8af | 2017-02-28 15:36:29 +0100 | [diff] [blame] | 146 | } |
Filip Pytloun | 2a9d78d | 2017-02-27 19:53:21 +0100 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | } |
Filip Pytloun | e8657fc | 2017-02-28 21:46:13 +0100 | [diff] [blame] | 150 | // Give services some time to settle |
| 151 | sleep(60) |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 152 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 153 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 154 | stage("Configure CI/CD services") { |
Filip Pytloun | 5b0954b | 2017-03-01 10:10:18 +0100 | [diff] [blame^] | 155 | // Aptly |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 156 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'aptly', true) |
Filip Pytloun | 5b0954b | 2017-03-01 10:10:18 +0100 | [diff] [blame^] | 157 | |
| 158 | // Gerrit |
| 159 | timeout(600) { |
| 160 | println "Waiting for Gerrit to come up.." |
| 161 | salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'while true; do curl -svf 172.16.10.254:8080 >/dev/null && break; done') |
| 162 | } |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 163 | retry(2) { |
| 164 | // Needs to run twice to pass __virtual__ method of gerrit module |
| 165 | // after installation of dependencies |
| 166 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'gerrit', true) |
| 167 | } |
Filip Pytloun | 5b0954b | 2017-03-01 10:10:18 +0100 | [diff] [blame^] | 168 | |
| 169 | // Jenkins |
| 170 | timeout(600) { |
| 171 | println "Waiting for Jenkins to come up.." |
| 172 | salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'while true; do curl -svf 172.16.10.254:8081 >/dev/null && break; done') |
| 173 | } |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 174 | retry(2) { |
| 175 | // Same for jenkins |
| 176 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'jenkins', true) |
| 177 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 178 | } |
Filip Pytloun | 3d045f8 | 2017-03-01 09:44:52 +0100 | [diff] [blame] | 179 | |
| 180 | stage("Finalize") { |
| 181 | // |
| 182 | // Generate docs |
| 183 | // |
| 184 | try { |
| 185 | retry(3) { |
| 186 | salt.runSaltProcessStep(saltMaster, '*', 'state.orchestrate', ['sphinx.orch.generate_doc']) |
| 187 | } |
| 188 | } catch (Throwable e) { |
| 189 | // We don't want sphinx docs to ruin whole build, so possible |
| 190 | // errors are just ignored here |
| 191 | true |
| 192 | } |
| 193 | salt.enforceState(saltMaster, 'I@nginx:server', 'nginx') |
| 194 | |
| 195 | if (HEAT_STACK_DELETE != 'true') { |
| 196 | println "============================================================" |
| 197 | println "Your CI/CD lab has been deployed and you can enjoy it:" |
| 198 | println " Use sshuttle -r ubuntu@${saltMasterHost} 172.16.10.0/24" |
Filip Pytloun | ee0b8c6 | 2017-03-01 09:48:19 +0100 | [diff] [blame] | 199 | println " to connect to your private subnet and visit services" |
| 200 | println " running at 172.16.10.254 (vip address):" |
Filip Pytloun | 3d045f8 | 2017-03-01 09:44:52 +0100 | [diff] [blame] | 201 | println " 9600 haproxy stats" |
| 202 | println " 8080 gerrit" |
| 203 | println " 8081 jenkins" |
| 204 | println " 8091 Docker swarm visualizer" |
| 205 | println " 8090 Reclass-generated documentation" |
| 206 | println "" |
| 207 | println "Don't forget to terminate your stack when you don't needed!" |
| 208 | println "============================================================" |
| 209 | } |
| 210 | } |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame] | 211 | } catch (Throwable e) { |
| 212 | // If there was an error or exception thrown, the build failed |
| 213 | currentBuild.result = "FAILURE" |
| 214 | throw e |
| 215 | } finally { |
| 216 | // Cleanup |
| 217 | if (HEAT_STACK_DELETE == 'true') { |
| 218 | stage('Trigger cleanup job') { |
| 219 | build job: 'deploy_heat_cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]] |
| 220 | } |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 221 | } |
Filip Pytloun | f6e877f | 2017-02-28 19:38:16 +0100 | [diff] [blame] | 222 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 223 | } |