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 | |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 61 | docker.image(toxDockerImage).inside { |
| 62 | result = sh(script: "cd ${repoDir} && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim() |
| 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 | * Get release metadata value for given key |
Ruslan Gustomiasov | cef1bb4 | 2019-06-25 17:00:13 +0200 | [diff] [blame] | 73 | * |
| 74 | * @param key metadata key |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 75 | * @param metadataDir metadata directory |
| 76 | * @param dirdepth the level at which YAML file should be created |
| 77 | */ |
| 78 | |
| 79 | def precreateKeyReleaseMetadataFile(String key, String metadataDir, Integer dirdepth = 0) { |
| 80 | def keySize = key.split(':').size() - 1 |
| 81 | if (dirdepth > 0 && dirdepth - 1 <= keySize) { |
| 82 | def dirPath = metadataDir + '/' + key.split(':')[0..dirdepth - 1].join('/') |
| 83 | sh "if ! test -d \"${dirPath}\" ; then mkdir -p \"${dirPath}\"; fi" |
| 84 | if (dirdepth - 1 != keySize) { |
| 85 | def pathToDummyFile = dirPath + '/' + key.split(':')[dirdepth] + '.yml' |
| 86 | sh "if ! test -f \"${pathToDummyFile}\" ; then touch \"${pathToDummyFile}\"; fi" |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Update release metadata value and upload CR to release metadata repository |
| 93 | * |
| 94 | * @param key metadata key (Several keys could be passed joined by ';' character) |
| 95 | * @param value metadata value (Several values could be passed joined by ';' character) |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 96 | * @param params map with expected parameters: |
| 97 | * - metadataCredentialsId |
| 98 | * - metadataGitRepoUrl |
| 99 | * - metadataGitRepoBranch |
| 100 | * - repoDir |
| 101 | * - comment |
| 102 | * - crTopic |
| 103 | * - crAuthorName |
| 104 | * - crAuthorEmail |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 105 | * @param dirdepth level of creation dirs from key param |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 106 | */ |
| 107 | |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 108 | def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) { |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 109 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
Anton Khlyst | fd6eb7c | 2019-09-25 13:32:17 +0300 | [diff] [blame] | 110 | 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] | 111 | String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master') |
Anton Khlyst | 8ebb9fe | 2019-09-25 16:39:54 +0300 | [diff] [blame] | 112 | String repoDir = params.get('repoDir', 'artifact-metadata') |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 113 | String comment = params.get('comment', '') |
| 114 | String crTopic = params.get('crTopic', '') |
| 115 | String changeAuthorName = params.get('crAuthorName', 'MCP-CI') |
| 116 | String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net') |
| 117 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 118 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 119 | def python = new com.mirantis.mk.Python() |
| 120 | def gerrit = new com.mirantis.mk.Gerrit() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 121 | def git = new com.mirantis.mk.Git() |
| 122 | |
| 123 | def cred = common.getCredentials(gitCredentialsId, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 124 | String gerritUser = cred.username |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 125 | String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 126 | String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 127 | String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
| 128 | String workspace = common.getWorkspace() |
| 129 | String venvDir = "${workspace}/gitreview-venv" |
| 130 | String metadataDir = "${repoDir}/metadata" |
| 131 | String ChangeId |
| 132 | String commitMessage |
| 133 | String gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 134 | stage("Installing virtualenv") { |
| 135 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 136 | } |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 137 | checkoutReleaseMetadataRepo(params) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 138 | dir(repoDir) { |
| 139 | gitRemote = sh( |
| 140 | script: |
| 141 | 'git remote -v | head -n1 | cut -f1', |
| 142 | returnStdout: true, |
| 143 | ).trim() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 144 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 145 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 146 | stage('Creating CR') { |
| 147 | def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost] |
| 148 | def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic] |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 149 | def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 150 | git.changeGitBranch(repoDir, metadataGerritBranch) |
| 151 | if (gerritChange) { |
| 152 | def jsonChange = readJSON text: gerritChange |
| 153 | changeNum = jsonChange['number'] |
| 154 | ChangeId = 'Change-Id: ' |
| 155 | ChangeId += jsonChange['id'] |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 156 | } else { |
| 157 | ChangeId = '' |
| 158 | git.createGitBranch(repoDir, crTopic) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 159 | } |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 160 | |
Ivan Udovichenko | f13b806 | 2019-11-15 16:17:54 +0300 | [diff] [blame] | 161 | def keyArr = key.split(';') |
| 162 | def valueArr = value.split(';') |
| 163 | if (keyArr.size() == valueArr.size()) { |
| 164 | for (i in 0..keyArr.size()-1) { |
| 165 | precreateKeyReleaseMetadataFile(keyArr[i], metadataDir, dirdepth) |
| 166 | |
| 167 | cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${keyArr[i]}' --value '${valueArr[i]}'" |
| 168 | python.runVirtualenvCommand(venvDir, cmdText) |
Ivan Udovichenko | 01c9941 | 2019-10-28 13:33:23 +0300 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 172 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 173 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 174 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 175 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 176 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 177 | //commit change |
Mykyta Karpin | 000d364 | 2019-08-08 15:37:45 +0300 | [diff] [blame] | 178 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 179 | //post change |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 180 | gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 181 | } |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 182 | } |