Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 2 | |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 3 | /** |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 4 | * Checkout release metadata repo with clone or without, if cloneRepo parameter is set |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 5 | * |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 6 | * @param params map with expected parameters: |
| 7 | * - metadataCredentialsId |
| 8 | * - metadataGitRepoUrl |
| 9 | * - metadataGitRepoBranch |
| 10 | * - repoDir |
| 11 | * - cloneRepo |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 12 | */ |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 13 | def checkoutReleaseMetadataRepo(Map params = [:]) { |
| 14 | def git = new com.mirantis.mk.Git() |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 15 | def common = new com.mirantis.mk.Common() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 16 | |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 17 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
Anton Khlyst | fd6eb7c | 2019-09-25 13:32:17 +0300 | [diff] [blame] | 18 | String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata") |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 19 | String gitBranch = params.get('metadataGitRepoBranch', 'master') |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 20 | String gitRef = params.get('metadataGitRepoRef', '') |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 21 | String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata')) |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 22 | Boolean cloneRepo = params.get('cloneRepo', true) |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 23 | |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 24 | if (cloneRepo) { |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 25 | stage('Cleanup repo dir') { |
| 26 | dir(repoDir) { |
| 27 | deleteDir() |
| 28 | } |
| 29 | } |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 30 | stage('Cloning artifact-metadata repository') { |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 31 | git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0, gitRef) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 32 | } |
| 33 | } else { |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 34 | git.changeGitBranch(repoDir, gitRef ?: gitBranch) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 35 | } |
| 36 | } |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 37 | |
| 38 | /** |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 39 | * Get release metadata value for given key |
| 40 | * |
| 41 | * @param key metadata key |
| 42 | * @param params map with expected parameters: |
| 43 | * - toxDockerImage |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 44 | * - outputFormat |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 45 | * - repoDir |
| 46 | */ |
| 47 | def getReleaseMetadataValue(String key, Map params = [:]) { |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 48 | // Libs |
| 49 | def common = new com.mirantis.mk.Common() |
| 50 | |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 51 | String result |
| 52 | // Get params |
| 53 | String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox') |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 54 | String outputFormat = params.get('outputFormat', 'json') |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 55 | String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata')) |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 56 | |
| 57 | String opts = '' |
| 58 | if (outputFormat && !outputFormat.isEmpty()) { |
| 59 | opts += " --${outputFormat}" |
| 60 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 61 | |
| 62 | checkoutReleaseMetadataRepo(params) |
| 63 | |
Dmitry Teselkin | 187d3a1 | 2020-02-13 14:58:32 +0300 | [diff] [blame] | 64 | docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") { |
| 65 | result = sh(script: "cd /workspace && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim() |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 66 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 67 | common.infoMsg(""" |
| 68 | Release metadata key ${key} has value: |
| 69 | ${result} |
| 70 | """) |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 71 | return result |
| 72 | } |
| 73 | |
| 74 | /** |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 75 | * Update release metadata value and upload CR to release metadata repository |
| 76 | * |
| 77 | * @param key metadata key (Several keys could be passed joined by ';' character) |
| 78 | * @param value metadata value (Several values could be passed joined by ';' character) |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 79 | * @param params map with expected parameters: |
| 80 | * - metadataCredentialsId |
| 81 | * - metadataGitRepoUrl |
| 82 | * - metadataGitRepoBranch |
| 83 | * - repoDir |
| 84 | * - comment |
| 85 | * - crTopic |
| 86 | * - crAuthorName |
| 87 | * - crAuthorEmail |
Mykyta Karpin | 2da46c6 | 2020-05-18 18:22:50 +0300 | [diff] [blame^] | 88 | * - valuesFromFile (allows to pass json strings, write them to temp files and pass files to |
| 89 | * app) |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 90 | * @param dirdepth level of creation dirs from key param |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 91 | */ |
| 92 | |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 93 | def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) { |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 94 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 95 | def python = new com.mirantis.mk.Python() |
| 96 | def gerrit = new com.mirantis.mk.Gerrit() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 97 | def git = new com.mirantis.mk.Git() |
| 98 | |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 99 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
| 100 | String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata") |
| 101 | String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master') |
| 102 | String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox') |
| 103 | String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata')) |
| 104 | String comment = params.get('comment', '') |
| 105 | String crTopic = params.get('crTopic', '') |
| 106 | String changeAuthorName = params.get('crAuthorName', 'MCP-CI') |
| 107 | String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net') |
Mykyta Karpin | 2da46c6 | 2020-05-18 18:22:50 +0300 | [diff] [blame^] | 108 | Boolean valuesFromFile = params.get('valuesFromFile', false) |
Dmitry Teselkin | d4adf97 | 2020-02-13 18:24:59 +0300 | [diff] [blame] | 109 | |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 110 | def cred = common.getCredentials(gitCredentialsId, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 111 | String gerritUser = cred.username |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 112 | String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 113 | String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 114 | String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
| 115 | String workspace = common.getWorkspace() |
| 116 | String venvDir = "${workspace}/gitreview-venv" |
| 117 | String metadataDir = "${repoDir}/metadata" |
| 118 | String ChangeId |
| 119 | String commitMessage |
| 120 | String gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 121 | stage("Installing virtualenv") { |
| 122 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 123 | } |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 124 | checkoutReleaseMetadataRepo(params) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 125 | dir(repoDir) { |
| 126 | gitRemote = sh( |
| 127 | script: |
| 128 | 'git remote -v | head -n1 | cut -f1', |
| 129 | returnStdout: true, |
| 130 | ).trim() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 131 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 132 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 133 | stage('Creating CR') { |
| 134 | def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost] |
| 135 | def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic] |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 136 | def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 137 | git.changeGitBranch(repoDir, metadataGerritBranch) |
| 138 | if (gerritChange) { |
| 139 | def jsonChange = readJSON text: gerritChange |
| 140 | changeNum = jsonChange['number'] |
| 141 | ChangeId = 'Change-Id: ' |
| 142 | ChangeId += jsonChange['id'] |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 143 | } else { |
| 144 | ChangeId = '' |
| 145 | git.createGitBranch(repoDir, crTopic) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 146 | } |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 147 | |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 148 | def keyArr = key.split(';') |
| 149 | def valueArr = value.split(';') |
| 150 | if (keyArr.size() == valueArr.size()) { |
Dmitry Teselkin | 187d3a1 | 2020-02-13 14:58:32 +0300 | [diff] [blame] | 151 | docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") { |
Alexandr Lovtsov | f874305 | 2020-01-21 14:32:14 +0200 | [diff] [blame] | 152 | for (i in 0..keyArr.size()-1) { |
Mykyta Karpin | 2da46c6 | 2020-05-18 18:22:50 +0300 | [diff] [blame^] | 153 | def valueExpression = "--value '${valueArr[i]}'" |
| 154 | def tmpFile |
| 155 | if (valuesFromFile){ |
| 156 | def data = readJSON text: valueArr[i] |
| 157 | // just print temp file name, so writeyaml can write it |
| 158 | tmpFile = sh(script: "mktemp -u -p ${workspace} meta_key_file.XXXXXX", returnStdout: true).trim() |
| 159 | // yaml is native format for meta app for loading values |
| 160 | writeYaml data: data, file: tmpFile |
| 161 | valueExpression = "--file ${tmpFile}" |
| 162 | } |
| 163 | try { |
| 164 | sh "cd /workspace && tox -qq -e metadata -- update --create --key '${keyArr[i]}' ${valueExpression}" |
| 165 | } finally { |
| 166 | if (valuesFromFile){ |
| 167 | sh "rm ${tmpFile}" |
| 168 | } |
| 169 | } |
Alexandr Lovtsov | f874305 | 2020-01-21 14:32:14 +0200 | [diff] [blame] | 170 | } |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Mykyta Karpin | ca423a7 | 2020-04-08 16:32:46 +0300 | [diff] [blame] | 174 | String status = sh(script: "git -C ${repoDir} status -s", returnStdout: true).trim() |
| 175 | if (!status){ |
| 176 | common.warningMsg('All values seem up to date, nothing to update') |
| 177 | return |
| 178 | } |
| 179 | common.infoMsg("""Next files will be updated: |
| 180 | ${status} |
| 181 | """) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 182 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 183 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 184 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 185 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 186 | """.stripMargin() |
Alexandr Lovtsov | 78c0e21 | 2020-04-02 15:35:29 +0300 | [diff] [blame] | 187 | |
| 188 | // Add some useful info (if it present) to commit message |
| 189 | if (env.BUILD_URL) { |
| 190 | commitMessage += "Build-Url: ${env.BUILD_URL}\n" |
| 191 | } |
| 192 | if (env.GERRIT_CHANGE_COMMIT_MESSAGE) { |
| 193 | def jira = new com.mirantis.mk.Atlassian() |
| 194 | jira.extractJIRA(env.GERRIT_CHANGE_COMMIT_MESSAGE).each { |
| 195 | commitMessage += "Related-To: ${it}\n" |
| 196 | } |
| 197 | } |
| 198 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 199 | //commit change |
Mykyta Karpin | 000d364 | 2019-08-08 15:37:45 +0300 | [diff] [blame] | 200 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 201 | //post change |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 202 | gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 203 | } |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 204 | } |