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', '') |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 19 | String repoDir = params.get('repoDir', 'release-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 | } |
| 27 | stage('Cloning release-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') |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 49 | String repoDir = params.get('repoDir', 'release-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 | /** |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 72 | * Update release metadata value and upload CR to release metadata repository |
Ruslan Gustomiasov | cef1bb4 | 2019-06-25 17:00:13 +0200 | [diff] [blame] | 73 | * |
| 74 | * @param key metadata key |
| 75 | * @param value metadata value |
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 |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 85 | */ |
| 86 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 87 | def updateReleaseMetadata(String key, String value, Map params) { |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 88 | String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit') |
Anton Khlyst | fd6eb7c | 2019-09-25 13:32:17 +0300 | [diff] [blame^] | 89 | 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] | 90 | String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master') |
| 91 | String repoDir = params.get('repoDir', 'release-metadata') |
| 92 | String comment = params.get('comment', '') |
| 93 | String crTopic = params.get('crTopic', '') |
| 94 | String changeAuthorName = params.get('crAuthorName', 'MCP-CI') |
| 95 | String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net') |
| 96 | |
Alexandr Lovtsov | 35cdebc | 2019-07-01 21:32:03 +0500 | [diff] [blame] | 97 | def common = new com.mirantis.mk.Common() |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 98 | def python = new com.mirantis.mk.Python() |
| 99 | def gerrit = new com.mirantis.mk.Gerrit() |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 100 | def git = new com.mirantis.mk.Git() |
| 101 | |
| 102 | def cred = common.getCredentials(gitCredentialsId, 'key') |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 103 | String gerritUser = cred.username |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 104 | String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0] |
| 105 | String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/') |
| 106 | String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0] |
| 107 | String workspace = common.getWorkspace() |
| 108 | String venvDir = "${workspace}/gitreview-venv" |
| 109 | String metadataDir = "${repoDir}/metadata" |
| 110 | String ChangeId |
| 111 | String commitMessage |
| 112 | String gitRemote |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 113 | stage("Installing virtualenv") { |
| 114 | python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml']) |
| 115 | } |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 116 | checkoutReleaseMetadataRepo(params) |
Mykyta Karpin | a102930 | 2019-07-25 18:16:31 +0300 | [diff] [blame] | 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] |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 128 | def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 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'] |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 135 | } else { |
| 136 | ChangeId = '' |
| 137 | git.createGitBranch(repoDir, crTopic) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 138 | } |
Alexandr Lovtsov | c1cc750 | 2019-07-02 12:41:53 +0500 | [diff] [blame] | 139 | 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] | 140 | python.runVirtualenvCommand(venvDir, cmdText) |
| 141 | commitMessage = |
Ruslan Gustomiasov | 2e9e345 | 2019-06-27 14:29:31 +0200 | [diff] [blame] | 142 | """${comment} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 143 | |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 144 | |${ChangeId} |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 145 | """.stripMargin() |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 146 | //commit change |
Mykyta Karpin | 000d364 | 2019-08-08 15:37:45 +0300 | [diff] [blame] | 147 | git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false) |
Ruslan Gustomiasov | 37a1728 | 2019-06-27 13:17:07 +0200 | [diff] [blame] | 148 | //post change |
Alexandr Lovtsov | 071fadb | 2019-07-29 17:01:22 +0300 | [diff] [blame] | 149 | gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch) |
Ruslan Gustomiasov | da4edd6 | 2019-06-25 14:26:28 +0200 | [diff] [blame] | 150 | } |
Alexandr Lovtsov | cefc3d1 | 2019-07-29 14:48:33 +0300 | [diff] [blame] | 151 | } |