blob: 10de20289fbe1426f8271dd489b008893ca867da [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/**
Ivan Udovichenkof13b8062019-11-15 16:17:54 +030072 * Update release metadata value and upload CR to release metadata repository
73 *
74 * @param key metadata key (Several keys could be passed joined by ';' character)
75 * @param value metadata value (Several values could be passed joined by ';' character)
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030076 * @param params map with expected parameters:
77 * - metadataCredentialsId
78 * - metadataGitRepoUrl
79 * - metadataGitRepoBranch
80 * - repoDir
81 * - comment
82 * - crTopic
83 * - crAuthorName
84 * - crAuthorEmail
Ivan Udovichenko01c99412019-10-28 13:33:23 +030085 * @param dirdepth level of creation dirs from key param
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020086 */
87
Ivan Udovichenko01c99412019-10-28 13:33:23 +030088def updateReleaseMetadata(String key, String value, Map params, Integer dirdepth = 0) {
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030089 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +030090 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030091 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
Alexandr Lovtsovf8743052020-01-21 14:32:14 +020092 String toxDockerImage = params.get('toxDockerImage', 'docker-prod-virtual.docker.mirantis.net/mirantis/external/tox')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030093 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030094 String comment = params.get('comment', '')
95 String crTopic = params.get('crTopic', '')
96 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
97 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
98
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050099 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200100 def python = new com.mirantis.mk.Python()
101 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300102 def git = new com.mirantis.mk.Git()
103
104 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200105 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300106 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
107 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
108 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
109 String workspace = common.getWorkspace()
110 String venvDir = "${workspace}/gitreview-venv"
111 String metadataDir = "${repoDir}/metadata"
112 String ChangeId
113 String commitMessage
114 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200115 stage("Installing virtualenv") {
116 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
117 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300118 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300119 dir(repoDir) {
120 gitRemote = sh(
121 script:
122 'git remote -v | head -n1 | cut -f1',
123 returnStdout: true,
124 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200125 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300126
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200127 stage('Creating CR') {
128 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
129 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300130 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200131 git.changeGitBranch(repoDir, metadataGerritBranch)
132 if (gerritChange) {
133 def jsonChange = readJSON text: gerritChange
134 changeNum = jsonChange['number']
135 ChangeId = 'Change-Id: '
136 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200137 } else {
138 ChangeId = ''
139 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200140 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300141
Ivan Udovichenkof13b8062019-11-15 16:17:54 +0300142 def keyArr = key.split(';')
143 def valueArr = value.split(';')
144 if (keyArr.size() == valueArr.size()) {
Dmitry Teselkin187d3a12020-02-13 14:58:32 +0300145 docker.image(toxDockerImage).inside("--volume ${repoDir}:/workspace") {
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200146 for (i in 0..keyArr.size()-1) {
Oleksii Grudev45150542020-02-13 16:27:58 +0200147 sh "cd /workspace && tox -qq -e metadata -- update --create --key '${keyArr[i]}' --value '${valueArr[i]}'"
Alexandr Lovtsovf8743052020-01-21 14:32:14 +0200148 }
Ivan Udovichenko01c99412019-10-28 13:33:23 +0300149 }
150 }
151
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200152 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200153 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200154
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200155 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200156 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200157 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300158 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200159 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300160 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200161 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300162}