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() |
| 14 | Boolean cloneRepo = params.get('cloneRepo', true) |
| 15 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
| 16 | String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/release-metadata") |
| 17 | String gitBranch = params.get('metadataGitRepoBranch', 'master') |
| 18 | String repoDir = params.get('repoDir', 'release-metadata') |
| 19 | if (cloneRepo){ |
| 20 | stage('Cleanup repo dir') { |
| 21 | dir(repoDir) { |
| 22 | deleteDir() |
| 23 | } |
| 24 | } |
| 25 | stage('Cloning release-metadata repository') { |
| 26 | git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0) |
| 27 | } |
| 28 | } else { |
| 29 | git.changeGitBranch(repoDir, gitBranch) |
| 30 | } |
| 31 | } |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 32 | |
| 33 | /** |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 34 | * Get release metadata value for given key |
| 35 | * |
| 36 | * @param key metadata key |
| 37 | * @param params map with expected parameters: |
| 38 | * - toxDockerImage |
| 39 | * - metadataCredentialsId |
| 40 | * - metadataGitRepoUrl |
| 41 | * - metadataGitRepoBranch |
| 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') |
| 48 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
| 49 | String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/release-metadata") |
| 50 | String gitBranch = params.get('metadataGitRepoBranch', 'master') |
| 51 | String repoDir = params.get('repoDir', 'release-metadata') |
| 52 | String outputFormat = params.get('outputFormat', 'json') |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 53 | Boolean cloneRepo = params.get('cloneRepo', true) |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 54 | |
| 55 | // Libs |
| 56 | def git = new com.mirantis.mk.Git() |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 57 | def common = new com.mirantis.mk.Common() |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 58 | |
| 59 | String opts = '' |
| 60 | if (outputFormat && !outputFormat.isEmpty()) { |
| 61 | opts += " --${outputFormat}" |
| 62 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 63 | |
| 64 | checkoutReleaseMetadataRepo(params) |
| 65 | |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 66 | docker.image(toxDockerImage).inside { |
| 67 | result = sh(script: "cd ${repoDir} && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim() |
| 68 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 69 | common.infoMsg(""" |
| 70 | Release metadata key ${key} has value: |
| 71 | ${result} |
| 72 | """) |
Alexandr Lovtsov | 3121fc4 | 2019-07-19 19:20:39 +0500 | [diff] [blame] | 73 | return result |
| 74 | } |
| 75 | |
| 76 | /** |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 77 | * Update release metadata value and upload CR to release metadata repository |
Ruslan Gustomiasov | cef1bb4 | 2019-06-25 17:00:13 +0200 | [diff] [blame] | 78 | * |
| 79 | * @param key metadata key |
| 80 | * @param value metadata value |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 81 | * @param params string map with credentialsID, metadataRepoUrl, metadataGerritBranch and crTopic |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 82 | */ |
| 83 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 84 | def updateReleaseMetadata(String key, String value, Map params) { |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 85 | credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit" |
Mykyta Karpin | 2845792 | 2019-07-05 15:14:58 +0300 | [diff] [blame] | 86 | metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-metadata" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 87 | metadataGerritBranch = params['metadataGerritBranch'] ?: "master" |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 88 | comment = params['comment'] ?: "" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 89 | crTopic = params['crTopic'] ?: "" |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 90 | Boolean cloneRepo = params.get('cloneRepo', true) |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 91 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 92 | def python = new com.mirantis.mk.Python() |
| 93 | def gerrit = new com.mirantis.mk.Gerrit() |
| 94 | def git = new com.mirantis.mk.Git() |
| 95 | def changeAuthorName = "MCP-CI" |
| 96 | def changeAuthorEmail = "mcp-ci-jenkins@ci.mcp.mirantis.net" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 97 | def cred = common.getCredentials(credentialsID, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 98 | String gerritUser = cred.username |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 99 | def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 100 | def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 101 | def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 102 | def workspace = common.getWorkspace() |
| 103 | def venvDir = "${workspace}/gitreview-venv" |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 104 | def repoDir = params.get('repoDir', "${venvDir}/repo") |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 105 | def metadataDir = "${repoDir}/metadata" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 106 | def ChangeId |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 107 | def commitMessage |
| 108 | def gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 109 | stage("Installing virtualenv") { |
| 110 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 111 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 112 | checkoutReleaseMetadataRepo(['metadataCredentialsId': credentialsID, |
| 113 | 'metadataGitRepoBranch': metadataGerritBranch, |
| 114 | 'metadataGitRepoUrl': metadataRepoUrl, |
| 115 | 'repoDir': repoDir, |
| 116 | 'cloneRepo': cloneRepo]) |
| 117 | dir(repoDir) { |
| 118 | gitRemote = sh( |
| 119 | script: |
| 120 | 'git remote -v | head -n1 | cut -f1', |
| 121 | returnStdout: true, |
| 122 | ).trim() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 123 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 124 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 125 | stage('Creating CR') { |
| 126 | def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost] |
| 127 | def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic] |
| 128 | def gerritChange = gerrit.findGerritChange(credentialsID, gerritAuth, changeParams) |
| 129 | git.changeGitBranch(repoDir, metadataGerritBranch) |
| 130 | if (gerritChange) { |
| 131 | def jsonChange = readJSON text: gerritChange |
| 132 | changeNum = jsonChange['number'] |
| 133 | ChangeId = 'Change-Id: ' |
| 134 | ChangeId += jsonChange['id'] |
| 135 | //get existent change from gerrit |
| 136 | gerrit.getGerritChangeByNum(credentialsID, venvDir, repoDir, gitRemote, changeNum) |
| 137 | } else { |
| 138 | ChangeId = '' |
| 139 | git.createGitBranch(repoDir, crTopic) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 140 | } |
Alexandr Lovtsov | c1cc750 | 2019-07-02 12:41:53 +0500 | [diff] [blame] | 141 | cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'" |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 142 | python.runVirtualenvCommand(venvDir, cmdText) |
| 143 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 144 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 145 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 146 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 147 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 148 | //commit change |
| 149 | if (gerritChange) { |
| 150 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false, true) |
| 151 | } else { |
| 152 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 153 | } |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 154 | //post change |
| 155 | gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 156 | } |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 157 | } |