blob: efc588b18e92281aefed4fd19ed2ae2fe854236d [file] [log] [blame]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02001package com.mirantis.mk
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02002/**
Mykyta Karpina1029302019-07-25 18:16:31 +03003 * Checkout release metadata repo with clone or without, if cloneRepo parameter is set
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02004 *
Mykyta Karpina1029302019-07-25 18:16:31 +03005 * @param params map with expected parameters:
6 * - metadataCredentialsId
7 * - metadataGitRepoUrl
8 * - metadataGitRepoBranch
9 * - repoDir
10 * - cloneRepo
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020011 */
Mykyta Karpina1029302019-07-25 18:16:31 +030012def 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 Gustomiasovda4edd62019-06-25 14:26:28 +020032
33/**
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050034 * 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 */
44def 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 Karpina1029302019-07-25 18:16:31 +030053 Boolean cloneRepo = params.get('cloneRepo', true)
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050054
55 // Libs
56 def git = new com.mirantis.mk.Git()
Mykyta Karpina1029302019-07-25 18:16:31 +030057 def common = new com.mirantis.mk.Common()
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050058
59 String opts = ''
60 if (outputFormat && !outputFormat.isEmpty()) {
61 opts += " --${outputFormat}"
62 }
Mykyta Karpina1029302019-07-25 18:16:31 +030063
64 checkoutReleaseMetadataRepo(params)
65
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050066 docker.image(toxDockerImage).inside {
67 result = sh(script: "cd ${repoDir} && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim()
68 }
Mykyta Karpina1029302019-07-25 18:16:31 +030069 common.infoMsg("""
70 Release metadata key ${key} has value:
71 ${result}
72 """)
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050073 return result
74}
75
76/**
Mykyta Karpina1029302019-07-25 18:16:31 +030077 * Update release metadata value and upload CR to release metadata repository
Ruslan Gustomiasovcef1bb42019-06-25 17:00:13 +020078 *
79 * @param key metadata key
80 * @param value metadata value
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020081 * @param params string map with credentialsID, metadataRepoUrl, metadataGerritBranch and crTopic
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020082 */
83
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050084def updateReleaseMetadata(String key, String value, Map params) {
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020085 credentialsID = params['credentialsID'] ?: "mcp-ci-gerrit"
Mykyta Karpin28457922019-07-05 15:14:58 +030086 metadataRepoUrl = params['metadataRepoUrl'] ?: "ssh://mcp-ci-gerrit@gerrit.mcp.mirantis.net:29418/mcp/release-metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020087 metadataGerritBranch = params['metadataGerritBranch'] ?: "master"
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +020088 comment = params['comment'] ?: ""
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020089 crTopic = params['crTopic'] ?: ""
Mykyta Karpina1029302019-07-25 18:16:31 +030090 Boolean cloneRepo = params.get('cloneRepo', true)
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050091 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020092 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 Gustomiasov37a17282019-06-27 13:17:07 +020097 def cred = common.getCredentials(credentialsID, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020098 String gerritUser = cred.username
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +020099 def gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
100 def metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
101 def gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200102 def workspace = common.getWorkspace()
103 def venvDir = "${workspace}/gitreview-venv"
Mykyta Karpina1029302019-07-25 18:16:31 +0300104 def repoDir = params.get('repoDir', "${venvDir}/repo")
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200105 def metadataDir = "${repoDir}/metadata"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200106 def ChangeId
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200107 def commitMessage
108 def gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200109 stage("Installing virtualenv") {
110 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
111 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300112 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 Gustomiasov37a17282019-06-27 13:17:07 +0200123 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300124
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200125 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 Gustomiasovda4edd62019-06-25 14:26:28 +0200140 }
Alexandr Lovtsovc1cc7502019-07-02 12:41:53 +0500141 cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200142 python.runVirtualenvCommand(venvDir, cmdText)
143 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200144 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200145
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200146 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200147 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200148 //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 Gustomiasovda4edd62019-06-25 14:26:28 +0200153 }
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200154 //post change
155 gerrit.postGerritReview(credentialsID, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200156 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300157}