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 | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 35 | // connection objects |
| 36 | def openstackCloud |
| 37 | def saltMaster |
| 38 | |
| 39 | // value defaults |
| 40 | def openstackVersion = OPENSTACK_API_CLIENT ? OPENSTACK_API_CLIENT : 'liberty' |
| 41 | def openstackEnv = "${env.WORKSPACE}/venv" |
| 42 | |
| 43 | if (HEAT_STACK_NAME == '') { |
| 44 | HEAT_STACK_NAME = BUILD_TAG |
| 45 | } |
| 46 | |
| 47 | // |
| 48 | // Bootstrap |
| 49 | // |
| 50 | |
| 51 | stage ('Download Heat templates') { |
| 52 | git.checkoutGitRepository('template', HEAT_TEMPLATE_URL, HEAT_TEMPLATE_BRANCH, HEAT_TEMPLATE_CREDENTIALS) |
| 53 | } |
| 54 | |
| 55 | stage('Install OpenStack CLI') { |
| 56 | openstack.setupOpenstackVirtualenv(openstackEnv, openstackVersion) |
| 57 | } |
| 58 | |
| 59 | stage('Connect to OpenStack cloud') { |
| 60 | openstackCloud = openstack.createOpenstackEnv(OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS, OPENSTACK_API_PROJECT) |
| 61 | openstack.getKeystoneToken(openstackCloud, openstackEnv) |
| 62 | } |
| 63 | |
| 64 | if (HEAT_STACK_REUSE == 'false') { |
| 65 | stage('Launch new Heat stack') { |
| 66 | envParams = [ |
| 67 | 'instance_zone': HEAT_STACK_ZONE, |
| 68 | 'public_net': HEAT_STACK_PUBLIC_NET |
| 69 | ] |
| 70 | openstack.createHeatStack(openstackCloud, HEAT_STACK_NAME, HEAT_STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, openstackEnv) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | stage('Connect to Salt master') { |
Filip Pytloun | e32fda8 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 75 | def saltMasterPort |
| 76 | try { |
| 77 | saltMasterPort = SALT_MASTER_PORT |
| 78 | } catch (MissingPropertyException e) { |
| 79 | saltMasterPort = 8000 |
| 80 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 81 | saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, HEAT_STACK_NAME, 'salt_master_ip', openstackEnv) |
Filip Pytloun | e32fda8 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 82 | saltMasterUrl = "http://${saltMasterHost}:${saltMasterPort}" |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 83 | saltMaster = salt.connection(saltMasterUrl, SALT_MASTER_CREDENTIALS) |
| 84 | } |
| 85 | |
| 86 | // |
| 87 | // Install |
| 88 | // |
| 89 | |
| 90 | stage('Install core infra') { |
| 91 | // salt.master, reclass |
| 92 | // refresh_pillar |
| 93 | // sync_all |
| 94 | // linux,openssh,salt.minion.ntp |
| 95 | |
| 96 | orchestrate.installFoundationInfra(saltMaster) |
| 97 | orchestrate.validateFoundationInfra(saltMaster) |
| 98 | } |
| 99 | |
| 100 | stage("Deploy GlusterFS") { |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 101 | salt.enforceState(saltMaster, 'I@glusterfs:server', 'glusterfs.server.service', true) |
| 102 | salt.enforceState(saltMaster, 'ci01*', 'glusterfs.server.setup', true) |
| 103 | sleep(5) |
| 104 | salt.enforceState(saltMaster, 'I@glusterfs:client', 'glusterfs.client', true) |
Filip Pytloun | 2a9d78d | 2017-02-27 19:53:21 +0100 | [diff] [blame^] | 105 | print salt.cmdRun(saltMaster, 'I@glusterfs:client', 'mount|grep fuse.glusterfs || echo "Command failed"') |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | stage("Deploy GlusterFS") { |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 109 | salt.enforceState(saltMaster, 'I@haproxy:proxy', 'haproxy,keepalived') |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | stage("Setup Docker Swarm") { |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 113 | salt.enforceState(saltMaster, 'I@docker:host', 'docker.host', true) |
| 114 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.swarm', true) |
| 115 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'salt', true) |
Filip Pytloun | 67fc9a2 | 2017-02-27 16:49:04 +0100 | [diff] [blame] | 116 | salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.flush') |
| 117 | salt.runSaltProcessStep(saltMaster, 'I@docker:swarm:role:master', 'mine.update') |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 118 | salt.enforceState(saltMaster, 'I@docker:swarm', 'docker.swarm', true) |
Filip Pytloun | d3186c1 | 2017-02-27 19:44:26 +0100 | [diff] [blame] | 119 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', 'docker node ls') |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | stage("Deploy Docker services") { |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 123 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client') |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 124 | |
| 125 | // XXX: Hack to fix dependency of gerrit on mysql |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 126 | print salt.cmdRun(saltMaster, 'I@docker:swarm:role:master', "docker service rm gerrit; rm -rf /srv/volumes/gerrit/*") |
Filip Pytloun | 2a9d78d | 2017-02-27 19:53:21 +0100 | [diff] [blame^] | 127 | sleep(10) |
Filip Pytloun | 4f739a2 | 2017-02-27 18:33:09 +0100 | [diff] [blame] | 128 | 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;'") |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 129 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'docker.client') |
| 130 | // ---- cut here (end of hack) ---- |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 131 | |
| 132 | retry(30) { |
Filip Pytloun | 2a9d78d | 2017-02-27 19:53:21 +0100 | [diff] [blame^] | 133 | 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'""") |
| 134 | for (entry in out['return']) { |
| 135 | for (node in entry) { |
| 136 | if (node.value =~ /Some services are not running/) { |
| 137 | throw new Exception("$node.key: $node.value) |
| 138 | } else { |
| 139 | print out |
| 140 | } |
| 141 | } |
| 142 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 143 | sleep(10) |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | stage("Configure CI/CD services") { |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 148 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'aptly', true) |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 149 | retry(2) { |
| 150 | // Needs to run twice to pass __virtual__ method of gerrit module |
| 151 | // after installation of dependencies |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 152 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'gerrit', true) |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 153 | } |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 154 | retry(2) { |
| 155 | // Same for jenkins |
Filip Pytloun | 89ae83a | 2017-02-27 18:31:55 +0100 | [diff] [blame] | 156 | salt.enforceState(saltMaster, 'I@docker:swarm:role:master', 'jenkins', true) |
Filip Pytloun | 2374198 | 2017-02-27 17:43:00 +0100 | [diff] [blame] | 157 | } |
Filip Pytloun | 0a07f70 | 2017-02-24 18:26:18 +0100 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // |
| 161 | // Cleanup |
| 162 | // |
| 163 | |
| 164 | if (HEAT_STACK_DELETE == 'true') { |
| 165 | stage('Trigger cleanup job') { |
| 166 | build job: 'deploy_heat_cleanup', parameters: [[$class: 'StringParameterValue', name: 'HEAT_STACK_NAME', value: HEAT_STACK_NAME]] |
| 167 | } |
| 168 | } |
| 169 | } |