blob: bfb115574e4537516fab51bb6135069092954753 [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
Dmitry Teselkin187d3a12020-02-13 14:58:32 +030061 docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") {
62 result = sh(script: "cd /workspace && tox -qq -e metadata -- ${opts} get --key ${key}", returnStdout: true).trim()
Alexandr Lovtsov3121fc42019-07-19 19:20:39 +050063 }
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/**
Oleksii Grudeva1004b52020-02-13 14:41:57 +020072 * Pre-create release metadata directory structure
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) {
Oleksii Grudeva1004b52020-02-13 14:41:57 +020080 common.infoMsg("This method is deprecated and should not be used with new metadata app")
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030081 def keySize = key.split(':').size() - 1
82 if (dirdepth > 0 && dirdepth - 1 <= keySize) {
83 def dirPath = metadataDir + '/' + key.split(':')[0..dirdepth - 1].join('/')
84 sh "if ! test -d \"${dirPath}\" ; then mkdir -p \"${dirPath}\"; fi"
85 if (dirdepth - 1 != keySize) {
86 def pathToDummyFile = dirPath + '/' + key.split(':')[dirdepth] + '.yml'
87 sh "if ! test -f \"${pathToDummyFile}\" ; then touch \"${pathToDummyFile}\"; fi"
88 }
89 }
90}
91
92/**
93 * Update release metadata value and upload CR to release metadata repository
94 *
95 * @param key metadata key (Several keys could be passed joined by ';' character)
96 * @param value metadata value (Several values could be passed joined by ';' character)
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030097 * @param params map with expected parameters:
98 * - metadataCredentialsId
99 * - metadataGitRepoUrl
100 * - metadataGitRepoBranch
101 * - repoDir
102 * - comment
103 * - crTopic
104 * - crAuthorName
105 * - crAuthorEmail
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300106 * @param dirdepth level of creation dirs from key param
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200107 */
108
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300109def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) {
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300110 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +0300111 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300112 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200113 String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +0300114 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300115 String comment = params.get('comment', '')
116 String crTopic = params.get('crTopic', '')
117 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
118 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
119
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +0500120 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200121 def python = new com.mirantis.mk.Python()
122 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300123 def git = new com.mirantis.mk.Git()
124
125 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200126 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300127 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
128 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
129 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
130 String workspace = common.getWorkspace()
131 String venvDir = "${workspace}/gitreview-venv"
132 String metadataDir = "${repoDir}/metadata"
133 String ChangeId
134 String commitMessage
135 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200136 stage("Installing virtualenv") {
137 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
138 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300139 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300140 dir(repoDir) {
141 gitRemote = sh(
142 script:
143 'git remote -v | head -n1 | cut -f1',
144 returnStdout: true,
145 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200146 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300147
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200148 stage('Creating CR') {
149 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
150 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300151 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200152 git.changeGitBranch(repoDir, metadataGerritBranch)
153 if (gerritChange) {
154 def jsonChange = readJSON text: gerritChange
155 changeNum = jsonChange['number']
156 ChangeId = 'Change-Id: '
157 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200158 } else {
159 ChangeId = ''
160 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200161 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300162
Ivan Udovichenkof13b8062019-11-15 16:17:54 +0300163 def keyArr = key.split(';')
164 def valueArr = value.split(';')
165 if (keyArr.size() == valueArr.size()) {
Dmitry Teselkin187d3a12020-02-13 14:58:32 +0300166 docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") {
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200167 for (i in 0..keyArr.size()-1) {
Dmitry Teselkin187d3a12020-02-13 14:58:32 +0300168 sh "cd /workspace && tox -qq -e metadata -- update --key '${keyArr[i]}' --value '${valueArr[i]}'"
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200169 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300170 }
171 }
172
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200173 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200174 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200175
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200176 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200177 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200178 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300179 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200180 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300181 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200182 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300183}