blob: a81f0d15f6d1b15aa46f76bb5d3b8ffe90507c0f [file] [log] [blame]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02001package com.mirantis.mk
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02002/**
3 * ReleaseWorkflow functions
4 *
5 */
6
7/**
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +05008 * 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 */
18def 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 Gustomiasovda4edd62019-06-25 14:26:28 +020044 * Update release metadata after image build
Ruslan Gustomiasovcef1bb42019-06-25 17:00:13 +020045 *
46 * @param key metadata key
47 * @param value metadata value
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020048 * @param params string map with credentialsID, metadataRepoUrl, metadataGerritBranch and crTopic
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020049 */
50
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050051def updateReleaseMetadata(String key, String value, Map params) {
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020052 credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit"
Mykyta Karpin28457922019-07-05 15:14:58 +030053 metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020054 metadataGerritBranch = params['metadataGerritBranch'] ?: "master"
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +020055 comment = params['comment'] ?: ""
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020056 crTopic = params['crTopic'] ?: ""
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050057 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020058 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 Gustomiasov37a17282019-06-27 13:17:07 +020063 def cred = common.getCredentials(credentialsID, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020064 String gerritUser = cred.username
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020065 def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
66 def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
67 def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020068 def workspace = common.getWorkspace()
69 def venvDir = "${workspace}/gitreview-venv"
70 def repoDir = "${venvDir}/repo"
71 def metadataDir = "${repoDir}/metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020072 def ChangeId
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020073 def commitMessage
74 def gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020075 stage("Installing virtualenv") {
76 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
77 }
78 stage('Cleanup repo dir') {
79 dir(repoDir) {
80 deleteDir()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020081 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020082 }
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 Gustomiasovda4edd62019-06-25 14:26:28 +020091 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020092 }
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 Gustomiasovda4edd62019-06-25 14:26:28 +0200108 }
Alexandr Lovtsovc1cc7502019-07-02 12:41:53 +0500109 cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200110 python.runVirtualenvCommand(venvDir, cmdText)
111 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200112 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200113
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200114 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200115 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200116 //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 Gustomiasovda4edd62019-06-25 14:26:28 +0200121 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200122 //post change
123 gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200124 }
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +0500125}