blob: a6ac96672a247cf6a2461a502cbc51e2eddcdf0c [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:
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +030043 * - appDockerImage
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
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +030053 String appDockerImage = params.get('appDockerImage', 'docker-dev-kaas-local.docker.mirantis.net/mirantis/cicd/artifact-metadata-app:latest')
54 String outputFormat = params.get('outputFormat', 'json')
55 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 Teselkine2ccc1a2020-07-07 16:29:39 +030064 docker.image(appDockerImage).inside("--volume ${repoDir}:/workspace") {
Sergey Otpuschennikov2538e132022-12-27 20:39:02 +040065 checkResult = sh(script: "metadata-app --path /workspace/metadata ${opts} validate --structure", returnStatus: true)
66 if (checkResult != 0) {
Sergey Otpuschennikov883f10f2023-01-03 18:44:33 +040067 throw new Exception("Invalid metadata structure, see errors in the metadata.log file")
Sergey Otpuschennikov2538e132022-12-27 20:39:02 +040068 }
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +030069 result = sh(script: "metadata-app --path /workspace/metadata ${opts} get --key ${key}", returnStdout: true).trim()
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050070 }
Mykyta Karpina1029302019-07-25 18:16:31 +030071 common.infoMsg("""
72 Release metadata key ${key} has value:
73 ${result}
74 """)
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050075 return result
76}
77
78/**
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030079 * Update release metadata value and upload CR to release metadata repository
80 *
81 * @param key metadata key (Several keys could be passed joined by ';' character)
82 * @param value metadata value (Several values could be passed joined by ';' character)
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030083 * @param params map with expected parameters:
84 * - metadataCredentialsId
85 * - metadataGitRepoUrl
86 * - metadataGitRepoBranch
87 * - repoDir
88 * - comment
89 * - crTopic
90 * - crAuthorName
91 * - crAuthorEmail
Mykyta Karpin2da46c62020-05-18 18:22:50 +030092 * - valuesFromFile (allows to pass json strings, write them to temp files and pass files to
93 * app)
Ivan Udovichenko01c99412019-10-28 13:33:23 +030094 * @param dirdepth level of creation dirs from key param
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020095 */
96
Ivan Udovichenko01c99412019-10-28 13:33:23 +030097def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) {
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050098 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020099 def python = new com.mirantis.mk.Python()
100 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300101 def git = new com.mirantis.mk.Git()
102
Dmitry Teselkind4adf972020-02-13 18:24:59 +0300103 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
104 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
105 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +0300106 String appDockerImage = params.get('appDockerImage', 'docker-dev-kaas-local.docker.mirantis.net/mirantis/cicd/artifact-metadata-app:latest')
Dmitry Teselkind4adf972020-02-13 18:24:59 +0300107 String repoDir = common.getAbsolutePath(params.get('repoDir', 'artifact-metadata'))
108 String comment = params.get('comment', '')
109 String crTopic = params.get('crTopic', '')
110 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
111 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
Mykyta Karpin2da46c62020-05-18 18:22:50 +0300112 Boolean valuesFromFile = params.get('valuesFromFile', false)
azvyagintsev7cd8eac2023-03-24 23:38:58 +0200113 Boolean catMetadataLog = params.get('catMetadataLog', false)
114 Boolean validateOnce = params.get('validateOnce', false)
Dmitry Teselkind4adf972020-02-13 18:24:59 +0300115
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300116 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200117 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300118 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
119 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
120 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
121 String workspace = common.getWorkspace()
122 String venvDir = "${workspace}/gitreview-venv"
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300123 String ChangeId
124 String commitMessage
125 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200126 stage("Installing virtualenv") {
127 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
128 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300129 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300130 dir(repoDir) {
131 gitRemote = sh(
132 script:
133 'git remote -v | head -n1 | cut -f1',
134 returnStdout: true,
135 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200136 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300137
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200138 stage('Creating CR') {
139 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
140 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300141 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200142 git.changeGitBranch(repoDir, metadataGerritBranch)
143 if (gerritChange) {
144 def jsonChange = readJSON text: gerritChange
145 changeNum = jsonChange['number']
146 ChangeId = 'Change-Id: '
147 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200148 } else {
149 ChangeId = ''
150 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200151 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300152
Ivan Udovichenkof13b8062019-11-15 16:17:54 +0300153 def keyArr = key.split(';')
154 def valueArr = value.split(';')
155 if (keyArr.size() == valueArr.size()) {
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +0300156 docker.image(appDockerImage).inside("--volume ${repoDir}:/workspace") {
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200157 for (i in 0..keyArr.size()-1) {
Mykyta Karpin2da46c62020-05-18 18:22:50 +0300158 def valueExpression = "--value '${valueArr[i]}'"
159 def tmpFile
160 if (valuesFromFile){
161 def data = readJSON text: valueArr[i]
162 // just print temp file name, so writeyaml can write it
163 tmpFile = sh(script: "mktemp -u -p ${workspace} meta_key_file.XXXXXX", returnStdout: true).trim()
164 // yaml is native format for meta app for loading values
165 writeYaml data: data, file: tmpFile
166 valueExpression = "--file ${tmpFile}"
167 }
168 try {
Dmitry Teselkine2ccc1a2020-07-07 16:29:39 +0300169 sh "metadata-app --path /workspace/metadata update --create --key '${keyArr[i]}' ${valueExpression}"
azvyagintsev7cd8eac2023-03-24 23:38:58 +0200170 if (!validateOnce) {
171 def checkResult = sh(script: "metadata-app --path /workspace/metadata validate --structure", returnStatus: true)
172 def MetadataLogFile = readFile("metadata.log")
173 if (MetadataLogFile.size() > 0) {
174 if (catMetadataLog) {
175 common.errorMsg("Invalid metadata structure, metadata.log file content:\n${MetadataLogFile}")
176 }
azvyagintseva4d484a2023-01-04 22:41:56 +0200177 }
azvyagintsev7cd8eac2023-03-24 23:38:58 +0200178 if (checkResult != 0) {
179 throw new Exception("Invalid metadata structure, see errors in the metadata.log file")
180 }
Sergey Otpuschennikov2538e132022-12-27 20:39:02 +0400181 }
Mykyta Karpin2da46c62020-05-18 18:22:50 +0300182 } finally {
azvyagintseva4d484a2023-01-04 22:41:56 +0200183 if (valuesFromFile) {
184 sh "rm -v ${tmpFile}"
Mykyta Karpin2da46c62020-05-18 18:22:50 +0300185 }
186 }
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200187 }
azvyagintsev7cd8eac2023-03-24 23:38:58 +0200188 if (validateOnce) {
189 def checkResult = sh(script: "metadata-app --path /workspace/metadata validate --structure", returnStatus: true)
190 def MetadataLogFile = readFile("metadata.log")
191 if (MetadataLogFile.size() > 0) {
192 if (catMetadataLog) {
193 common.errorMsg("Invalid metadata structure, metadata.log file content:\n${MetadataLogFile}")
194 }
195 }
196 if (checkResult != 0) {
197 throw new Exception("Invalid metadata structure, see errors in the metadata.log file")
198 }
199 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300200 }
201 }
202
Mykyta Karpinca423a72020-04-08 16:32:46 +0300203 String status = sh(script: "git -C ${repoDir} status -s", returnStdout: true).trim()
204 if (!status){
205 common.warningMsg('All values seem up to date, nothing to update')
206 return
207 }
208 common.infoMsg("""Next files will be updated:
209 ${status}
210 """)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200211 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200212 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200213
Michal Kobuse55c7182020-11-05 16:51:23 +0100214 |${ChangeId}\n""".stripMargin()
Alexandr Lovtsov78c0e212020-04-02 15:35:29 +0300215
216 // Add some useful info (if it present) to commit message
217 if (env.BUILD_URL) {
218 commitMessage += "Build-Url: ${env.BUILD_URL}\n"
219 }
220 if (env.GERRIT_CHANGE_COMMIT_MESSAGE) {
221 def jira = new com.mirantis.mk.Atlassian()
222 jira.extractJIRA(env.GERRIT_CHANGE_COMMIT_MESSAGE).each {
223 commitMessage += "Related-To: ${it}\n"
224 }
225 }
226
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200227 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300228 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200229 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300230 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200231 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300232}