Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 1 | package com.mirantis.mk |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 2 | /** |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 3 | * 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] | 4 | * |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 5 | * @param params map with expected parameters: |
| 6 | * - metadataCredentialsId |
| 7 | * - metadataGitRepoUrl |
| 8 | * - metadataGitRepoBranch |
| 9 | * - repoDir |
| 10 | * - cloneRepo |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 11 | */ |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 12 | def checkoutReleaseMetadataRepo(Map params = [:]) { |
| 13 | def git = new com.mirantis.mk.Git() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 14 | |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 15 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
Anton Khlyst | fd6eb7c | 2019-09-25 13:32:17 +0300 | [diff] [blame] | 16 | 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] | 17 | String gitBranch = params.get('metadataGitRepoBranch', 'master') |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 18 | String gitRef = params.get('metadataGitRepoRef', '') |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 19 | String repoDir = params.get('repoDir', 'artifact-metadata') |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 20 | Boolean cloneRepo = params.get('cloneRepo', true) |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 21 | if (cloneRepo) { |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 22 | stage('Cleanup repo dir') { |
| 23 | dir(repoDir) { |
| 24 | deleteDir() |
| 25 | } |
| 26 | } |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 27 | stage('Cloning artifact-metadata repository') { |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 28 | git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0, gitRef) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 29 | } |
| 30 | } else { |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 31 | git.changeGitBranch(repoDir, gitRef ?: gitBranch) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 32 | } |
| 33 | } |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 34 | |
| 35 | /** |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 36 | * Get release metadata value for given key |
| 37 | * |
| 38 | * @param key metadata key |
| 39 | * @param params map with expected parameters: |
| 40 | * - toxDockerImage |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 41 | * - outputFormat |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 42 | * - repoDir |
| 43 | */ |
| 44 | def getReleaseMetadataValue(String key, Map params = [:]) { |
| 45 | String result |
| 46 | // Get params |
| 47 | 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] | 48 | String outputFormat = params.get('outputFormat', 'json') |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 49 | String repoDir = params.get('repoDir', 'artifact-metadata') |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 50 | |
| 51 | // Libs |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 52 | def common = new com.mirantis.mk.Common() |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 53 | |
| 54 | String opts = '' |
| 55 | if (outputFormat && !outputFormat.isEmpty()) { |
| 56 | opts += " --${outputFormat}" |
| 57 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 58 | |
| 59 | checkoutReleaseMetadataRepo(params) |
| 60 | |
Dmitry Teselkin | 187d3a1 | 2020-02-13 14:58:32 +0300 | [diff] [blame] | 61 | docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") { |
| 62 | 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] | 63 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 64 | common.infoMsg(""" |
| 65 | Release metadata key ${key} has value: |
| 66 | ${result} |
| 67 | """) |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 68 | return result |
| 69 | } |
| 70 | |
| 71 | /** |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 72 | * Update release metadata value and upload CR to release metadata repository |
| 73 | * |
| 74 | * @param key metadata key (Several keys could be passed joined by ';' character) |
| 75 | * @param value metadata value (Several values could be passed joined by ';' character) |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 76 | * @param params map with expected parameters: |
| 77 | * - metadataCredentialsId |
| 78 | * - metadataGitRepoUrl |
| 79 | * - metadataGitRepoBranch |
| 80 | * - repoDir |
| 81 | * - comment |
| 82 | * - crTopic |
| 83 | * - crAuthorName |
| 84 | * - crAuthorEmail |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 85 | * @param dirdepth level of creation dirs from key param |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 86 | */ |
| 87 | |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 88 | def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) { |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 89 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
Anton Khlyst | fd6eb7c | 2019-09-25 13:32:17 +0300 | [diff] [blame] | 90 | String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata") |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 91 | String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master') |
Alexandr Lovtsov | f874305 | 2020-01-21 14:32:14 +0200 | [diff] [blame] | 92 | String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox') |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 93 | String repoDir = params.get('repoDir', 'artifact-metadata') |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 94 | String comment = params.get('comment', '') |
| 95 | String crTopic = params.get('crTopic', '') |
| 96 | String changeAuthorName = params.get('crAuthorName', 'MCP-CI') |
| 97 | String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net') |
| 98 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 99 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 100 | def python = new com.mirantis.mk.Python() |
| 101 | def gerrit = new com.mirantis.mk.Gerrit() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 102 | def git = new com.mirantis.mk.Git() |
| 103 | |
| 104 | def cred = common.getCredentials(gitCredentialsId, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 105 | String gerritUser = cred.username |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 106 | String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 107 | String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 108 | String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
| 109 | String workspace = common.getWorkspace() |
| 110 | String venvDir = "${workspace}/gitreview-venv" |
| 111 | String metadataDir = "${repoDir}/metadata" |
| 112 | String ChangeId |
| 113 | String commitMessage |
| 114 | String gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 115 | stage("Installing virtualenv") { |
| 116 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 117 | } |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 118 | checkoutReleaseMetadataRepo(params) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 119 | dir(repoDir) { |
| 120 | gitRemote = sh( |
| 121 | script: |
| 122 | 'git remote -v | head -n1 | cut -f1', |
| 123 | returnStdout: true, |
| 124 | ).trim() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 125 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 126 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 127 | stage('Creating CR') { |
| 128 | def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost] |
| 129 | def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic] |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 130 | def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 131 | git.changeGitBranch(repoDir, metadataGerritBranch) |
| 132 | if (gerritChange) { |
| 133 | def jsonChange = readJSON text: gerritChange |
| 134 | changeNum = jsonChange['number'] |
| 135 | ChangeId = 'Change-Id: ' |
| 136 | ChangeId += jsonChange['id'] |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 137 | } else { |
| 138 | ChangeId = '' |
| 139 | git.createGitBranch(repoDir, crTopic) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 140 | } |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 141 | |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 142 | def keyArr = key.split(';') |
| 143 | def valueArr = value.split(';') |
| 144 | if (keyArr.size() == valueArr.size()) { |
Dmitry Teselkin | 187d3a1 | 2020-02-13 14:58:32 +0300 | [diff] [blame] | 145 | docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") { |
Alexandr Lovtsov | f874305 | 2020-01-21 14:32:14 +0200 | [diff] [blame] | 146 | for (i in 0..keyArr.size()-1) { |
Oleksii Grudev | 4515054 | 2020-02-13 16:27:58 +0200 | [diff] [blame^] | 147 | sh "cd /workspace && tox -qq -e metadata -- update --create --key '${keyArr[i]}' --value '${valueArr[i]}'" |
Alexandr Lovtsov | f874305 | 2020-01-21 14:32:14 +0200 | [diff] [blame] | 148 | } |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 152 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 153 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 154 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 155 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 156 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 157 | //commit change |
Mykyta Karpin | 000d364 | 2019-08-08 15:37:45 +0300 | [diff] [blame] | 158 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 159 | //post change |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 160 | gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 161 | } |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 162 | } |