blob: 429e05f220378632b2aa1115ac5a7162716c818e [file] [log] [blame]
Jakub Josefad56efe2018-02-01 13:56:44 +01001/**
2 * Helper pipeline for AWS deployments from kqueen
3 *
4 * Expected parameters:
5 * STACK_NAME Infrastructure stack name
6 * STACK_TEMPLATE File with stack template
7 *
8 * STACK_TEMPLATE_URL URL to git repo with stack templates
9 * STACK_TEMPLATE_CREDENTIALS Credentials to the templates repo
10 * STACK_TEMPLATE_BRANCH Stack templates repo branch
11 * STACK_COMPUTE_COUNT Number of compute nodes to launch
12 *
13 * HEAT_STACK_ENVIRONMENT Heat stack environmental parameters
14 * HEAT_STACK_ZONE Heat stack availability zone
15 * HEAT_STACK_PUBLIC_NET Heat stack floating IP pool
16 * OPENSTACK_API_URL OpenStack API address
17 * OPENSTACK_API_CREDENTIALS Credentials to the OpenStack API
18 * OPENSTACK_API_PROJECT OpenStack project to connect to
19 * OPENSTACK_API_CLIENT Versions of OpenStack python clients
20 * OPENSTACK_API_VERSION Version of the OpenStack API (2/3)
21 *
22 * SALT_MASTER_CREDENTIALS Credentials to the Salt API
23 * SALT_MASTER_URL URL of Salt master
24 */
25
26common = new com.mirantis.mk.Common()
27git = new com.mirantis.mk.Git()
28openstack = new com.mirantis.mk.Openstack()
29orchestrate = new com.mirantis.mk.Orchestrate()
30python = new com.mirantis.mk.Python()
31salt = new com.mirantis.mk.Salt()
32
33// Define global variables
34def venv
35def venvPepper
36def outputs = [:]
37
38def ipRegex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"
39def envParams
40timeout(time: 12, unit: 'HOURS') {
41 node("python") {
42 try {
43 // Set build-specific variables
44 venv = "${env.WORKSPACE}/venv"
45 venvPepper = "${env.WORKSPACE}/venvPepper"
46
47 //
48 // Prepare machines
49 //
50 stage ('Create infrastructure') {
51 // value defaults
52 envParams = [
53 'cluster_zone': HEAT_STACK_ZONE,
54 'cluster_public_net': HEAT_STACK_PUBLIC_NET
55 ]
56
57 // no underscore in STACK_NAME
58 STACK_NAME = STACK_NAME.replaceAll('_', '-')
59 outputs.put('stack_name', STACK_NAME)
60
61 // set description
62 currentBuild.description = STACK_NAME
63
64 // get templates
65 git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS)
66
67 // create openstack env
68 openstack.setupOpenstackVirtualenv(venv, OPENSTACK_API_CLIENT)
69 openstackCloud = openstack.createOpenstackEnv(venv,
70 OPENSTACK_API_URL, OPENSTACK_API_CREDENTIALS,
71 OPENSTACK_API_PROJECT, OPENSTACK_API_PROJECT_DOMAIN,
72 OPENSTACK_API_PROJECT_ID, OPENSTACK_API_USER_DOMAIN,
73 OPENSTACK_API_VERSION)
74 openstack.getKeystoneToken(openstackCloud, venv)
75
76 // set reclass repo in heat env
77 try {
78 envParams.put('cfg_reclass_branch', STACK_RECLASS_BRANCH)
79 envParams.put('cfg_reclass_address', STACK_RECLASS_ADDRESS)
80 } catch (MissingPropertyException e) {
81 common.infoMsg("Property STACK_RECLASS_BRANCH or STACK_RECLASS_ADDRESS not found! Using default values from template.")
82 }
83
84 // launch stack
85 openstack.createHeatStack(openstackCloud, STACK_NAME, STACK_TEMPLATE, envParams, HEAT_STACK_ENVIRONMENT, venv)
86
87 // get SALT_MASTER_URL
88 saltMasterHost = openstack.getHeatStackOutputParam(openstackCloud, STACK_NAME, 'salt_master_ip', venv)
89 // check that saltMasterHost is valid
90 if (!saltMasterHost || !saltMasterHost.matches(ipRegex)) {
91 common.errorMsg("saltMasterHost is not a valid ip, value is: ${saltMasterHost}")
92 throw new Exception("saltMasterHost is not a valid ip")
93 }
94
95 currentBuild.description = "${STACK_NAME} ${saltMasterHost}"
96
97 SALT_MASTER_URL = "http://${saltMasterHost}:6969"
98
99 // Setup virtualenv for pepper
100 python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS)
101 }
102
103 stage('Install core infrastructure') {
104 def staticMgmtNetwork = false
105 if (common.validInputParam('STATIC_MGMT_NETWORK')) {
106 staticMgmtNetwork = STATIC_MGMT_NETWORK.toBoolean()
107 }
108 orchestrate.installFoundationInfra(venvPepper, staticMgmtNetwork)
109
110 if (common.checkContains('STACK_INSTALL', 'kvm')) {
111 orchestrate.installInfraKvm(venvPepper)
112 orchestrate.installFoundationInfra(venvPepper, staticMgmtNetwork)
113 }
114
115 orchestrate.validateFoundationInfra(venvPepper)
116 }
117
118 stage('Install Kubernetes infra') {
119 // configure kubernetes_control_address - save loadbalancer
120 def awsOutputs = aws.getOutputs(venv, aws_env_vars, STACK_NAME)
121 common.prettyPrint(awsOutputs)
122 if (awsOutputs.containsKey('ControlLoadBalancer')) {
123 salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'reclass.cluster_meta_set', ['kubernetes_control_address', awsOutputs['ControlLoadBalancer']], null, true)
124 outputs.put('kubernetes_apiserver', 'https://' + awsOutputs['ControlLoadBalancer'])
125 }
126
127 // ensure certificates are generated properly
128 salt.runSaltProcessStep(venvPepper, '*', 'saltutil.refresh_pillar', [], null, true)
129 salt.enforceState(venvPepper, '*', ['salt.minion.cert'], true)
130
131 orchestrate.installKubernetesInfra(venvPepper)
132 }
133
134 stage('Install Kubernetes control') {
135 orchestrate.installKubernetesControl(venvPepper)
136
137 // collect artifacts (kubeconfig)
138 writeFile(file: 'kubeconfig', text: salt.getFileContent(venvPepper, 'I@kubernetes:master and *01*', '/etc/kubernetes/admin-kube-config'))
139 archiveArtifacts(artifacts: 'kubeconfig')
140 }
141
142 stage('Install Kubernetes computes') {
143 if (common.validInputParam('STACK_COMPUTE_COUNT')) {
144 if (STACK_COMPUTE_COUNT > 0) {
145 // get stack info
146 def scaling_group = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'ComputesScalingGroup')
147
148 //update autoscaling group
149 aws.updateAutoscalingGroup(venv, aws_env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT])
150
151 // wait for computes to boot up
152 aws.waitForAutoscalingInstances(venv, aws_env_vars, scaling_group)
153 sleep(60)
154 }
155 }
156
157 orchestrate.installKubernetesCompute(venvPepper)
158 }
159
160 stage('Finalize') {
161 outputsPretty = common.prettify(outputs)
162 print(outputsPretty)
163 writeFile(file: 'outputs.json', text: outputsPretty)
164 archiveArtifacts(artifacts: 'outputs.json')
165 }
166
167 } catch (Throwable e) {
168 currentBuild.result = 'FAILURE'
169 throw e
170 } finally {
171 if (currentBuild.result == 'FAILURE') {
172 common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.")
173
174 if (common.validInputParam('SALT_MASTER_URL')) {
175 common.errorMsg("Salt master URL: ${SALT_MASTER_URL}")
176 }
177 }
178 }
179 }
180}
181
182