| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 1 | /** | 
|  | 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 | *   AWS_STACK_REGION           CloudFormation AWS region | 
|  | 14 | *   AWS_API_CREDENTIALS        AWS Access key ID with  AWS secret access key | 
|  | 15 | *   AWS_SSH_KEY                AWS key pair name (used for SSH access) | 
|  | 16 | * | 
|  | 17 | *   SALT_MASTER_CREDENTIALS    Credentials to the Salt API | 
|  | 18 | *   SALT_MASTER_URL            URL of Salt master | 
|  | 19 | */ | 
|  | 20 |  | 
|  | 21 | common = new com.mirantis.mk.Common() | 
|  | 22 | git = new com.mirantis.mk.Git() | 
|  | 23 | aws = new com.mirantis.mk.Aws() | 
|  | 24 | orchestrate = new com.mirantis.mk.Orchestrate() | 
|  | 25 | python = new com.mirantis.mk.Python() | 
|  | 26 | salt = new com.mirantis.mk.Salt() | 
|  | 27 |  | 
|  | 28 | // Define global variables | 
|  | 29 | def venv | 
|  | 30 | def venvPepper | 
|  | 31 | def outputs = [:] | 
|  | 32 |  | 
|  | 33 | def ipRegex = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}" | 
|  | 34 | def aws_env_vars | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 35 | timeout(time: 12, unit: 'HOURS') { | 
|  | 36 | node("python") { | 
|  | 37 | try { | 
|  | 38 | // Set build-specific variables | 
|  | 39 | venv = "${env.WORKSPACE}/venv" | 
|  | 40 | venvPepper = "${env.WORKSPACE}/venvPepper" | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 41 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 42 | // | 
|  | 43 | // Prepare machines | 
|  | 44 | // | 
|  | 45 | stage ('Create infrastructure') { | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 46 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 47 | outputs.put('stack_type', "aws") | 
|  | 48 | // setup environment | 
|  | 49 | aws.setupVirtualEnv(venv) | 
|  | 50 | // set aws_env_vars | 
|  | 51 | aws_env_vars = aws.getEnvVars(AWS_API_CREDENTIALS, AWS_STACK_REGION) | 
|  | 52 | // We just use STACK_NAME from param | 
|  | 53 | currentBuild.description = STACK_NAME | 
|  | 54 | outputs.put('stack_name', STACK_NAME) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 55 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 56 | // get templates | 
|  | 57 | git.checkoutGitRepository('template', STACK_TEMPLATE_URL, STACK_TEMPLATE_BRANCH, STACK_TEMPLATE_CREDENTIALS) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 58 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 59 | // start stack | 
|  | 60 | def stack_params = [ | 
|  | 61 | "ParameterKey=KeyName,ParameterValue=" + AWS_SSH_KEY, | 
|  | 62 | "ParameterKey=CmpNodeCount,ParameterValue=" + STACK_COMPUTE_COUNT | 
|  | 63 | ] | 
|  | 64 | def template_file = 'cfn/' + STACK_TEMPLATE + '.yml' | 
|  | 65 | aws.createStack(venv, aws_env_vars, template_file, STACK_NAME, stack_params) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 66 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 67 | // wait for stack to be ready | 
|  | 68 | aws.waitForStatus(venv, aws_env_vars, STACK_NAME, 'CREATE_COMPLETE') | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 69 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 70 | // get outputs | 
|  | 71 | saltMasterHost = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'SaltMasterIP') | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 72 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 73 | // check that saltMasterHost is valid | 
|  | 74 | if (!saltMasterHost || !saltMasterHost.matches(ipRegex)) { | 
|  | 75 | common.errorMsg("saltMasterHost is not a valid ip, value is: ${saltMasterHost}") | 
|  | 76 | throw new Exception("saltMasterHost is not a valid ip") | 
|  | 77 | } | 
|  | 78 |  | 
|  | 79 | currentBuild.description = "${STACK_NAME} ${saltMasterHost}" | 
|  | 80 | SALT_MASTER_URL = "http://${saltMasterHost}:6969" | 
|  | 81 |  | 
|  | 82 | outputs.put('salt_api', SALT_MASTER_URL) | 
|  | 83 |  | 
|  | 84 | // Setup virtualenv for pepper | 
|  | 85 | python.setupPepperVirtualenv(venvPepper, SALT_MASTER_URL, SALT_MASTER_CREDENTIALS) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 88 | stage('Install core infrastructure') { | 
|  | 89 | def staticMgmtNetwork = false | 
|  | 90 | if (common.validInputParam('STATIC_MGMT_NETWORK')) { | 
|  | 91 | staticMgmtNetwork = STATIC_MGMT_NETWORK.toBoolean() | 
|  | 92 | } | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 93 | orchestrate.installFoundationInfra(venvPepper, staticMgmtNetwork) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 94 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 95 | if (common.checkContains('STACK_INSTALL', 'kvm')) { | 
|  | 96 | orchestrate.installInfraKvm(venvPepper) | 
|  | 97 | orchestrate.installFoundationInfra(venvPepper, staticMgmtNetwork) | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | orchestrate.validateFoundationInfra(venvPepper) | 
|  | 101 | } | 
|  | 102 | stage('Install Kubernetes infra') { | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 103 | // configure kubernetes_control_address - save loadbalancer | 
|  | 104 | def awsOutputs = aws.getOutputs(venv, aws_env_vars, STACK_NAME) | 
|  | 105 | common.prettyPrint(awsOutputs) | 
|  | 106 | if (awsOutputs.containsKey('ControlLoadBalancer')) { | 
|  | 107 | salt.runSaltProcessStep(venvPepper, 'I@salt:master', 'reclass.cluster_meta_set', ['kubernetes_control_address', awsOutputs['ControlLoadBalancer']], null, true) | 
|  | 108 | outputs.put('kubernetes_apiserver', 'https://' + awsOutputs['ControlLoadBalancer']) | 
|  | 109 | } | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 110 |  | 
|  | 111 | // ensure certificates are generated properly | 
|  | 112 | salt.runSaltProcessStep(venvPepper, '*', 'saltutil.refresh_pillar', [], null, true) | 
|  | 113 | salt.enforceState(venvPepper, '*', ['salt.minion.cert'], true) | 
|  | 114 |  | 
|  | 115 | orchestrate.installKubernetesInfra(venvPepper) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 118 | stage('Install Kubernetes control') { | 
|  | 119 | orchestrate.installKubernetesControl(venvPepper) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 120 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 121 | // collect artifacts (kubeconfig) | 
|  | 122 | writeFile(file: 'kubeconfig', text: salt.getFileContent(venvPepper, 'I@kubernetes:master and *01*', '/etc/kubernetes/admin-kube-config')) | 
|  | 123 | archiveArtifacts(artifacts: 'kubeconfig') | 
|  | 124 | } | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 125 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 126 | stage('Install Kubernetes computes') { | 
|  | 127 | if (common.validInputParam('STACK_COMPUTE_COUNT')) { | 
|  | 128 | if (STACK_COMPUTE_COUNT > 0) { | 
|  | 129 | // get stack info | 
|  | 130 | def scaling_group = aws.getOutputs(venv, aws_env_vars, STACK_NAME, 'ComputesScalingGroup') | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 131 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 132 | //update autoscaling group | 
|  | 133 | aws.updateAutoscalingGroup(venv, aws_env_vars, scaling_group, ["--desired-capacity " + STACK_COMPUTE_COUNT]) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 134 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 135 | // wait for computes to boot up | 
|  | 136 | aws.waitForAutoscalingInstances(venv, aws_env_vars, scaling_group) | 
|  | 137 | sleep(60) | 
|  | 138 | } | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 139 | } | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 140 |  | 
|  | 141 | orchestrate.installKubernetesCompute(venvPepper) | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 144 | stage('Finalize') { | 
|  | 145 | outputsPretty = common.prettify(outputs) | 
|  | 146 | print(outputsPretty) | 
|  | 147 | writeFile(file: 'outputs.json', text: outputsPretty) | 
|  | 148 | archiveArtifacts(artifacts: 'outputs.json') | 
|  | 149 | } | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 150 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 151 | } catch (Throwable e) { | 
|  | 152 | currentBuild.result = 'FAILURE' | 
|  | 153 | throw e | 
|  | 154 | } finally { | 
|  | 155 | if (currentBuild.result == 'FAILURE') { | 
|  | 156 | common.errorMsg("Deploy job FAILED and was not deleted. Please fix the problem and delete stack on you own.") | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 157 |  | 
| Jakub Josef | fe0720e | 2018-01-11 17:43:53 +0100 | [diff] [blame^] | 158 | if (common.validInputParam('SALT_MASTER_URL')) { | 
|  | 159 | common.errorMsg("Salt master URL: ${SALT_MASTER_URL}") | 
|  | 160 | } | 
| Jakub Josef | 3947b06 | 2018-01-10 13:04:13 +0100 | [diff] [blame] | 161 | } | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 |  |