· 6 years ago · Mar 12, 2020, 07:20 PM
1/**
2 *
3 * Workflow for KaaS bootstrap on VMs and run KaaS UI test
4 *
5 * Expected parameters:
6 * ENV_NAME Name to create Heat stack and management KaaS cluster
7 *
8 * # Variables used to render KaaS bootstrap templates
9 * KAAS_EXTERNAL_NETWORK_ID ID of 'public' network, example for EU cloud: bf6b85a1-39db-4582-b0d1-f4291dddb9cf
10 * KAAS_MANAGEMENT_CLUSTER_DNS1 DNS to resolve internal resources, example: 172.18.224.4
11 * KAAS_MANAGEMENT_CLUSTER_FLAVOR Flavor to create KaaS management cluster instances, example: kaas.small
12 * KAAS_MANAGEMENT_CLUSTER_IMAGE Image to create KaaS management cluster instances, example: bionic-server-cloudimg-amd64-20190612
13 * KAAS_MANAGEMENT_CLUSTER_NODES Kubernetes nodes for KaaS management cluster, example: 3
14 *
15 * # Variables used to customize kaas release
16 * KAAS_RELEASE_REPO_URL URL to repo, where kaas/* cluster/* releases stored, used by kaas release-controller during upgrade procedure
17 * KAAS_RELEASES_REFSPEC Refspec from kaas/releases repo, to run deployment
18 * KAAS_CUSTOM_DEPLOYMENT_RELEASE_REPO_URL URL to custom relesases repo, which contains custom version of kaas/releases refspec, if set, KAAS_RELEASES_REFSPEC will be ignored
19 * KAAS_CUSTOM_DEPLOYMENT_BOOTSTRAP_TEMPLATES_TARBALL URL to tarball, which contains custom cluster and machine templates, si cluster defaults and clouds.yaml file, needed for bootstrap
20 *
21 * # Variables used to render clouds.yaml for OpenStack
22 * OS_AUTH_URL OpenStack auth URL, example for EU cloud: https://ic-eu.ssl.mirantis.net:5000/v3
23 * OS_IDENTITY_API_VERSION OpenStack auth API version, example: 3
24 * OS_INTERFACE OpenStack endpoints interface, example: public
25 * OS_PROJECT_ID OpenStack project ID, used for bootstrap.sh, example for 'systest-team': 7fd2d1904af849ccb2d72fcfb8469c97
26 * OS_PROJECT_NAME OpenStack project name, used for si-tests, example: systest-team
27 * OS_REGION_NAME OpenStack region name, example: RegionOne
28 * OS_USER_DOMAIN_NAME OpenStack domain name, example: default
29 *
30 * # Remote access parameters
31 * OPENSTACK_API_CREDENTIALS Jenkins credential ID with OpenStack OS_USERNAME and OS_PASSWORD, example: system-integration-team-ci
32 * GERRIT_MCP_CREDENTIALS_ID Jenkins credential ID with username and SSH private key to access gerrit.mcp.mirantis.com
33 * CLOUD_KEY_NAME OpenStack keypair name to create the 'seed' node in the OpenStack, example: kaas-vm-seed-node
34 * SEED_PRIVATE_KEY_CREDENTIAL_ID Jenkins credential ID with username and private key to access the 'seed' node created with CLOUD_KEY_NAME
35 *
36 * # Pytest test name(s)
37 * RUN_TESTS Space separated list of tests and options
38 * Example: si_tests/tests/deployment/management-cluster/test_provision_mgm_cluster.py
39 *
40 * Optional parameters:
41 * BOOTSTRAP_TIMEOUT Bootstrap timeout, default: 7800
42 * SLAVE_NODE Jenkins node label to run the job, default: 'python'
43 * SI_TESTS_REFSPEC Refspec of patchset in gerrit.mcp.mirantis.com for the kaas/si-tests repository
44 * SEND_LOGS_TO_ELK Send logs to http://logcollector.mcp.mirantis.net:5601
45 */
46
47def gerrit = new com.mirantis.mk.Gerrit()
48def python = new com.mirantis.mk.Python()
49
50def slaveNode = env.SLAVE_NODE ?: 'python'
51def artifacts_dir = "artifacts"
52def testrail_report_job_name = "si-test-testrail-report"
53def report_filename = "${artifacts_dir}/bootstrap_kaas_result.xml"
54def virtualenv = ".venv-si-tests"
55def job_timeout = env.BOOTSTRAP_TIMEOUT ?: 7800
56def kaas_bootstrap_license_credentials = "kaas-bootstrap-si-license"
57def runEnvs = []
58
59timeout(time: job_timeout.toInteger(), unit: 'SECONDS') {
60 node (slaveNode) {
61 def repo_url
62 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
63 credentialsId: env.GERRIT_MCP_CREDENTIALS_ID,
64 keyFileVariable: "GERRIT_KEY",
65 usernameVariable: "GERRIT_USERNAME",
66 passwordVariable: "GERRIT_PASSWORD",],]) {
67
68 def GERRIT_SCHEME = 'ssh'
69 def GERRIT_HOST = 'gerrit.mcp.mirantis.com'
70 def GERRIT_PORT = '29418'
71 def GERRIT_PROJECT = 'kaas/si-tests'
72 repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
73 release_repo_url = "${GERRIT_SCHEME}://${GERRIT_USERNAME}@${GERRIT_HOST}:${GERRIT_PORT}/kaas/releases"
74 }
75
76 stage('Checkout kaas/si-tests, create Python virtualenv and install requirements') {
77 deleteDir() // TODO(ddmitriev): do more selective cleanup to keep the git repo and virtual env
78 gerrit.gerritPatchsetCheckout(repo_url,
79 env.SI_TESTS_REFSPEC,
80 'FETCH_HEAD',
81 env.GERRIT_MCP_CREDENTIALS_ID)
82
83 python.runCmd("virtualenv --python=python3.5 ${virtualenv}", '', false)
84 python.runCmd("mkdir ${artifacts_dir}")
85 python.runCmd("pip install -q -r ./si_tests/requirements.txt", virtualenv)
86 }
87
88 stage('Determining kaas release') {
89 def releaseFolder = 'kaas-release'
90 sh("mkdir -p ${releaseFolder}")
91 runEnvs.add("KAAS_RELEASES_FOLDER=${releaseFolder}/")
92
93 dir(releaseFolder) {
94 // Warning: custom repo should contain 100% equal file structure as in kaas/releases repo, otherwise deployment will fail
95 if (env.KAAS_CUSTOM_DEPLOYMENT_RELEASE_REPO_URL) {
96 // count amount of subdirs in url minus initial domain-host directory like artifactory.com
97 def depth = env.KAAS_CUSTOM_DEPLOYMENT_RELEASE_REPO_URL.replaceAll('^(http|https)://', '').split('/').size() - 1
98 sh("wget -r --no-parent -nH --cut-dirs=${depth} --reject \'index.html*\' ${KAAS_RELEASE_REPO_URL}")
99 } else {
100 // Checkout in kaas/releases repo by default
101 gerrit.gerritPatchsetCheckout(release_repo_url,
102 KAAS_RELEASES_REFSPEC,
103 'FETCH_HEAD',
104 GERRIT_MCP_CREDENTIALS_ID)
105 python.runCmd("rm -rfv hack/vendor")
106 }
107 }
108
109 if (env.KAAS_CUSTOM_DEPLOYMENT_BOOTSTRAP_TEMPLATES_TARBALL) {
110 sh("wget ${env.KAAS_CUSTOM_DEPLOYMENT_BOOTSTRAP_TEMPLATES_TARBALL} -O templates.tar.gz")
111 sh("tar -C ${releaseFolder} -xvz templates.tar.gz")
112
113 // KAAS_CUSTOM_BOOTSTRAP_TEMPLATES_DIR should contain subdir called `templates_${VERSION}` in root dir, where all templates stored.
114 runEnvs.add("KAAS_BOOTSTRAP_TEMPLATES_DIR=${releaseFolder}/")
115 }
116 }
117
118 try {
119 stage('Run bootstrap kaas on vm test') {
120 withCredentials([[$class: 'SSHUserPrivateKeyBinding',
121 credentialsId: env.SEED_PRIVATE_KEY_CREDENTIAL_ID,
122 keyFileVariable: "SEED_SSH_PRIV_KEY_FILE",
123 usernameVariable: "SEED_SSH_PRIV_KEY_USERNAME",
124 passwordVariable: "SEED_SSH_PRIV_KEY_PASSWORD",],]) {
125
126 withCredentials([
127 [$class : 'UsernamePasswordMultiBinding',
128 credentialsId : env.OPENSTACK_API_CREDENTIALS,
129 passwordVariable: 'OS_PASSWORD',
130 usernameVariable: 'OS_USERNAME',],
131 file(credentialsId: kaas_bootstrap_license_credentials, variable: 'KAAS_BOOTSTRAP_LICENSE_FILE'),
132 ]) {
133 // Run bootstrap test
134 withEnv(runEnvs) {
135 python.runCmd("py.test --junit-xml=${report_filename} ${env.RUN_TESTS} 2>&1", virtualenv)
136 }
137 }
138 }
139 } // stage
140 } finally {
141 stage("Archive xml report and bootstrap artifacts") {
142 archiveArtifacts artifacts: "${artifacts_dir}/*"
143 } // stage
144 stage("Collect info for description") {
145 currentBuild.description = " ENV_NAME=${ENV_NAME}<br>"
146 SEED_NODE_IP = python.runCmd("[ -f ${artifacts_dir}/seed_node_ip ] && cat ${artifacts_dir}/seed_node_ip")['stdout'].trim().split().last()
147 if (SEED_NODE_IP) {
148 currentBuild.description += " ssh ubuntu@${SEED_NODE_IP} # Seed node, password: qalab<br>"
149 }
150 if (fileExists("${artifacts_dir}/management_kubeconfig")) {
151 currentBuild.description += " wget ${BUILD_URL}/artifact/${artifacts_dir}/management_kubeconfig -O kubeconfig; export KUBECONFIG=./kubeconfig # Get KaaS management cluster kubeconfig<br>"
152 }
153 if (fileExists("${artifacts_dir}/management_public_urls")) {
154 public_urls = readFile("${artifacts_dir}/management_public_urls").split('\n')
155 for (public_url in public_urls) {
156 currentBuild.description += " ${public_url}<br>"
157 }
158 }
159 } // stage
160
161 if (env.SEND_LOGS_TO_ELK.toBoolean()) {
162 // send logs to ELK
163 logstashSend()
164 }
165 if (env.REPORT_TO_TESTRAIL.toBoolean()) {
166 stage("Report SI tests results ${report_filename}") {
167
168 DeployedKaasVersion = sh(script: "cat ${artifacts_dir}/management_version", returnStdout: true).trim()
169
170 parameters = [
171 string(name: "ENV_NAME", value: env.ENV_NAME),
172 string(name: "KAAS_VERSION", value: DeployedKaasVersion),
173 text(name: "REPORTS_LIST", value: "REPORT_SI_TESTS: ${BUILD_URL}artifact/${report_filename}\n"),
174 ]
175 job_info = build job: testrail_report_job_name, parameters: parameters
176 build_description = job_info.getDescription()
177 if (build_description) {
178 currentBuild.description += build_description
179 }
180 }
181 } // if
182 } // try
183 } // node
184} // timeout