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) |
| 129 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', "apt-get install -y mysql-client; mysql -ppassword -h172.16.10.254 -e'drop database gerrit;create database gerrit;'") |
| 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 | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 150 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 151 | |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame^] | 152 | stage("Configure CI/CD services") { |
| 153 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'aptly', true) |
| 154 | retry(2) { |
| 155 | // Needs to run twice to pass __virtual__ method of gerrit module |
| 156 | // after installation of dependencies |
| 157 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'gerrit', true) |
| 158 | } |
| 159 | retry(2) { |
| 160 | // Same for jenkins |
| 161 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'jenkins', true) |
| 162 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 163 | } |
Filip Pytloun | fd6726a | 2017-02-28 19:31:16 +0100 | [diff] [blame^] | 164 | } catch (Throwable e) { |
| 165 | // If there was an error or exception thrown, the build failed |
| 166 | currentBuild.result = "FAILURE" |
| 167 | throw e |
| 168 | } finally { |
| 169 | // Cleanup |
| 170 | if (HEAT_STACK_DELETE == 'true') { |
| 171 | stage('Trigger cleanup job') { |
| 172 | build job: 'deploy_heat_cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]] |
| 173 | } |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 174 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 175 | } |