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