blob: c27fa8a306c6d9cf7eaa17d4596804e71a83adab [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()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030014
Mykyta Karpina1029302019-07-25 18:16:31 +030015 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +030016 String gitUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Mykyta Karpina1029302019-07-25 18:16:31 +030017 String gitBranch = params.get('metadataGitRepoBranch', 'master')
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030018 String gitRef = params.get('metadataGitRepoRef', '')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030019 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030020 Boolean cloneRepo = params.get('cloneRepo', true)
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030021 if (cloneRepo) {
Mykyta Karpina1029302019-07-25 18:16:31 +030022 stage('Cleanup repo dir') {
23 dir(repoDir) {
24 deleteDir()
25 }
26 }
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030027 stage('Cloning artifact-metadata repository') {
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030028 git.checkoutGitRepository(repoDir, gitUrl, gitBranch, gitCredentialsId, true, 10, 0, gitRef)
Mykyta Karpina1029302019-07-25 18:16:31 +030029 }
30 } else {
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +030031 git.changeGitBranch(repoDir, gitRef ?: gitBranch)
Mykyta Karpina1029302019-07-25 18:16:31 +030032 }
33}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020034
35/**
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050036 * Get release metadata value for given key
37 *
38 * @param key metadata key
39 * @param params map with expected parameters:
40 * - toxDockerImage
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030041 * - outputFormat
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050042 * - 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')
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050048 String outputFormat = params.get('outputFormat', 'json')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030049 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050050
51 // Libs
Mykyta Karpina1029302019-07-25 18:16:31 +030052 def common = new com.mirantis.mk.Common()
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050053
54 String opts = ''
55 if (outputFormat && !outputFormat.isEmpty()) {
56 opts += " --${outputFormat}"
57 }
Mykyta Karpina1029302019-07-25 18:16:31 +030058
59 checkoutReleaseMetadataRepo(params)
60
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050061 docker.image(toxDockerImage).inside {
62 result = sh(script: "cd ${repoDir} && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim()
63 }
Mykyta Karpina1029302019-07-25 18:16:31 +030064 common.infoMsg("""
65 Release metadata key ${key} has value:
66 ${result}
67 """)
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050068 return result
69}
70
71/**
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030072 * Get release metadata value for given key
Ruslan Gustomiasovcef1bb42019-06-25 17:00:13 +020073 *
74 * @param key metadata key
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030075 * @param metadataDir metadata directory
76 * @param dirdepth the level at which YAML file should be created
77 */
78
79def precreateKeyReleaseMetadataFile(String key, String metadataDir, Integer dirdepth = 0) {
80 def keySize = key.split(':').size() - 1
81 if (dirdepth > 0 && dirdepth - 1 <= keySize) {
82 def dirPath = metadataDir + '/' + key.split(':')[0..dirdepth - 1].join('/')
83 sh "if ! test -d \"${dirPath}\" ; then mkdir -p \"${dirPath}\"; fi"
84 if (dirdepth - 1 != keySize) {
85 def pathToDummyFile = dirPath + '/' + key.split(':')[dirdepth] + '.yml'
86 sh "if ! test -f \"${pathToDummyFile}\" ; then touch \"${pathToDummyFile}\"; fi"
87 }
88 }
89}
90
91/**
92 * Update release metadata value and upload CR to release metadata repository
93 *
94 * @param key metadata key (Several keys could be passed joined by ';' character)
95 * @param value metadata value (Several values could be passed joined by ';' character)
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030096 * @param params map with expected parameters:
97 * - metadataCredentialsId
98 * - metadataGitRepoUrl
99 * - metadataGitRepoBranch
100 * - repoDir
101 * - comment
102 * - crTopic
103 * - crAuthorName
104 * - crAuthorEmail
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300105 * @param dirdepth level of creation dirs from key param
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200106 */
107
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300108def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) {
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300109 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +0300110 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300111 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +0300112 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300113 String comment = params.get('comment', '')
114 String crTopic = params.get('crTopic', '')
115 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
116 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
117
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +0500118 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200119 def python = new com.mirantis.mk.Python()
120 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300121 def git = new com.mirantis.mk.Git()
122
123 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200124 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300125 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
126 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
127 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
128 String workspace = common.getWorkspace()
129 String venvDir = "${workspace}/gitreview-venv"
130 String metadataDir = "${repoDir}/metadata"
131 String ChangeId
132 String commitMessage
133 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200134 stage("Installing virtualenv") {
135 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
136 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300137 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300138 dir(repoDir) {
139 gitRemote = sh(
140 script:
141 'git remote -v | head -n1 | cut -f1',
142 returnStdout: true,
143 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200144 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300145
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200146 stage('Creating CR') {
147 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
148 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300149 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200150 git.changeGitBranch(repoDir, metadataGerritBranch)
151 if (gerritChange) {
152 def jsonChange = readJSON text: gerritChange
153 changeNum = jsonChange['number']
154 ChangeId = 'Change-Id: '
155 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200156 } else {
157 ChangeId = ''
158 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200159 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300160
Ivan Udovichenkof13b8062019-11-15 16:17:54 +0300161 def keyArr = key.split(';')
162 def valueArr = value.split(';')
163 if (keyArr.size() == valueArr.size()) {
164 for (i in 0..keyArr.size()-1) {
165 precreateKeyReleaseMetadataFile(keyArr[i], metadataDir, dirdepth)
166
167 cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${keyArr[i]}' --value '${valueArr[i]}'"
168 python.runVirtualenvCommand(venvDir, cmdText)
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300169 }
170 }
171
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200172 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200173 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200174
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200175 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200176 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200177 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300178 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200179 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300180 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200181 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300182}