blob: 8cb0f51754175aad6b631e00b7b63b005d67d4a0 [file] [log] [blame]
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02001package com.mirantis.mk
Dmitry Teselkind4adf972020-02-13 18:24:59 +03002
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02003/**
Mykyta Karpina1029302019-07-25 18:16:31 +03004 * Checkout release metadata repo with clone or without, if cloneRepo parameter is set
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +02005 *
Mykyta Karpina1029302019-07-25 18:16:31 +03006 * @param params map with expected parameters:
7 * - metadataCredentialsId
8 * - metadataGitRepoUrl
9 * - metadataGitRepoBranch
10 * - repoDir
11 * - cloneRepo
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020012 */
Mykyta Karpina1029302019-07-25 18:16:31 +030013def checkoutReleaseMetadataRepo(Map params = [:]) {
14 def git = new com.mirantis.mk.Git()
Dmitry Teselkind4adf972020-02-13 18:24:59 +030015 def common = new com.mirantis.mk.Common()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030016
Mykyta Karpina1029302019-07-25 18:16:31 +030017 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +030018 String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Mykyta Karpina1029302019-07-25 18:16:31 +030019 String gitBranch = params.get('metadataGitRepoBranch', 'master')
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030020 String gitRef = params.get('metadataGitRepoRef', '')
Dmitry Teselkind4adf972020-02-13 18:24:59 +030021 String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata'))
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030022 Boolean cloneRepo = params.get('cloneRepo', true)
Dmitry Teselkind4adf972020-02-13 18:24:59 +030023
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030024 if (cloneRepo) {
Mykyta Karpina1029302019-07-25 18:16:31 +030025 stage('Cleanup repo dir') {
26 dir(repoDir) {
27 deleteDir()
28 }
29 }
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030030 stage('Cloning artifact-metadata repository') {
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030031 git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0, gitRef)
Mykyta Karpina1029302019-07-25 18:16:31 +030032 }
33 } else {
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030034 git.changeGitBranch(repoDir, gitRef ?: gitBranch)
Mykyta Karpina1029302019-07-25 18:16:31 +030035 }
36}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020037
38/**
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050039 * Get release metadata value for given key
40 *
41 * @param key metadata key
42 * @param params map with expected parameters:
43 * - toxDockerImage
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030044 * - outputFormat
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050045 * - repoDir
46 */
47def getReleaseMetadataValue(String key, Map params = [:]) {
Dmitry Teselkind4adf972020-02-13 18:24:59 +030048 // Libs
49 def common = new com.mirantis.mk.Common()
50
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050051 String result
52 // Get params
53 String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox')
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050054 String outputFormat = params.get('outputFormat', 'json')
Dmitry Teselkind4adf972020-02-13 18:24:59 +030055 String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata'))
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050056
57 String opts = ''
58 if (outputFormat && !outputFormat.isEmpty()) {
59 opts += " --${outputFormat}"
60 }
Mykyta Karpina1029302019-07-25 18:16:31 +030061
62 checkoutReleaseMetadataRepo(params)
63
Dmitry Teselkin187d3a12020-02-13 14:58:32 +030064 docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") {
65 result = sh(script: "cd /workspace && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim()
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050066 }
Mykyta Karpina1029302019-07-25 18:16:31 +030067 common.infoMsg("""
68 Release metadata key ${key} has value:
69 ${result}
70 """)
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050071 return result
72}
73
74/**
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030075 * Update release metadata value and upload CR to release metadata repository
76 *
77 * @param key metadata key (Several keys could be passed joined by ';' character)
78 * @param value metadata value (Several values could be passed joined by ';' character)
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030079 * @param params map with expected parameters:
80 * - metadataCredentialsId
81 * - metadataGitRepoUrl
82 * - metadataGitRepoBranch
83 * - repoDir
84 * - comment
85 * - crTopic
86 * - crAuthorName
87 * - crAuthorEmail
Ivan Udovichenko01c99412019-10-28 13:33:23 +030088 * @param dirdepth level of creation dirs from key param
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020089 */
90
Ivan Udovichenko01c99412019-10-28 13:33:23 +030091def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) {
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050092 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020093 def python = new com.mirantis.mk.Python()
94 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030095 def git = new com.mirantis.mk.Git()
96
Dmitry Teselkind4adf972020-02-13 18:24:59 +030097 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
98 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
99 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
100 String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox')
101 String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata'))
102 String comment = params.get('comment', '')
103 String crTopic = params.get('crTopic', '')
104 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
105 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
106
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300107 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200108 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300109 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
110 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
111 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
112 String workspace = common.getWorkspace()
113 String venvDir = "${workspace}/gitreview-venv"
114 String metadataDir = "${repoDir}/metadata"
115 String ChangeId
116 String commitMessage
117 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200118 stage("Installing virtualenv") {
119 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
120 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300121 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300122 dir(repoDir) {
123 gitRemote = sh(
124 script:
125 'git remote -v | head -n1 | cut -f1',
126 returnStdout: true,
127 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200128 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300129
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200130 stage('Creating CR') {
131 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
132 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300133 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200134 git.changeGitBranch(repoDir, metadataGerritBranch)
135 if (gerritChange) {
136 def jsonChange = readJSON text: gerritChange
137 changeNum = jsonChange['number']
138 ChangeId = 'Change-Id: '
139 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200140 } else {
141 ChangeId = ''
142 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200143 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300144
Ivan Udovichenkof13b8062019-11-15 16:17:54 +0300145 def keyArr = key.split(';')
146 def valueArr = value.split(';')
147 if (keyArr.size() == valueArr.size()) {
Dmitry Teselkin187d3a12020-02-13 14:58:32 +0300148 docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") {
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200149 for (i in 0..keyArr.size()-1) {
Oleksii Grudev45150542020-02-13 16:27:58 +0200150 sh "cd /workspace && tox -qq -e metadata -- update --create --key '${keyArr[i]}' --value '${valueArr[i]}'"
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200151 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300152 }
153 }
154
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200155 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200156 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200157
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200158 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200159 """.stripMargin()
Alexandr Lovtsov78c0e212020-04-02 15:35:29 +0300160
161 // Add some useful info (if it present) to commit message
162 if (env.BUILD_URL) {
163 commitMessage += "Build-Url: ${env.BUILD_URL}\n"
164 }
165 if (env.GERRIT_CHANGE_COMMIT_MESSAGE) {
166 def jira = new com.mirantis.mk.Atlassian()
167 jira.extractJIRA(env.GERRIT_CHANGE_COMMIT_MESSAGE).each {
168 commitMessage += "Related-To: ${it}\n"
169 }
170 }
171
172
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200173 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300174 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200175 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300176 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200177 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300178}