blob: 51f6218feba26cebc0ac23c57ed41460a3a61702 [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/**
Mykyta Karpina1029302019-07-25 18:16:31 +030072 * Update release metadata value and upload CR to release metadata repository
Ruslan Gustomiasovcef1bb42019-06-25 17:00:13 +020073 *
74 * @param key metadata key
75 * @param value metadata value
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
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020085 */
86
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050087def updateReleaseMetadata(String key, String value, Map params) {
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030088 String gitCredentialsId = params.get('metadataCredentialsId', 'mcp-ci-gerrit')
Anton Khlystfd6eb7c2019-09-25 13:32:17 +030089 String metadataRepoUrl = params.get('metadataGitRepoUrl', "ssh://${gitCredentialsId}@gerrit.mcp.mirantis.net:29418/mcp/artifact-metadata")
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030090 String metadataGerritBranch = params.get('metadataGitRepoBranch', 'master')
Anton Khlyst8ebb9fe2019-09-25 16:39:54 +030091 String repoDir = params.get('repoDir', 'artifact-metadata')
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +030092 String comment = params.get('comment', '')
93 String crTopic = params.get('crTopic', '')
94 String changeAuthorName = params.get('crAuthorName', 'MCP-CI')
95 String changeAuthorEmail = params.get('crAuthorEmail', 'mcp-ci-jenkins@ci.mcp.mirantis.net')
96
Alexandr Lovtsov35cdebc2019-07-01 21:32:03 +050097 def common = new com.mirantis.mk.Common()
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +020098 def python = new com.mirantis.mk.Python()
99 def gerrit = new com.mirantis.mk.Gerrit()
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300100 def git = new com.mirantis.mk.Git()
101
102 def cred = common.getCredentials(gitCredentialsId, 'key')
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200103 String gerritUser = cred.username
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300104 String gerritHost = metadataRepoUrl.tokenize('@')[-1].tokenize(':')[0]
105 String metadataProject = metadataRepoUrl.tokenize('/')[-2..-1].join('/')
106 String gerritPort = metadataRepoUrl.tokenize(':')[-1].tokenize('/')[0]
107 String workspace = common.getWorkspace()
108 String venvDir = "${workspace}/gitreview-venv"
109 String metadataDir = "${repoDir}/metadata"
110 String ChangeId
111 String commitMessage
112 String gitRemote
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200113 stage("Installing virtualenv") {
114 python.setupVirtualenv(venvDir, 'python3', ['git-review', 'PyYaml'])
115 }
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300116 checkoutReleaseMetadataRepo(params)
Mykyta Karpina1029302019-07-25 18:16:31 +0300117 dir(repoDir) {
118 gitRemote = sh(
119 script:
120 'git remote -v | head -n1 | cut -f1',
121 returnStdout: true,
122 ).trim()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200123 }
Mykyta Karpina1029302019-07-25 18:16:31 +0300124
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200125 stage('Creating CR') {
126 def gerritAuth = ['PORT': gerritPort, 'USER': gerritUser, 'HOST': gerritHost]
127 def changeParams = ['owner': gerritUser, 'status': 'open', 'project': metadataProject, 'branch': metadataGerritBranch, 'topic': crTopic]
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300128 def gerritChange = gerrit.findGerritChange(gitCredentialsId, gerritAuth, changeParams)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200129 git.changeGitBranch(repoDir, metadataGerritBranch)
130 if (gerritChange) {
131 def jsonChange = readJSON text: gerritChange
132 changeNum = jsonChange['number']
133 ChangeId = 'Change-Id: '
134 ChangeId += jsonChange['id']
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200135 } else {
136 ChangeId = ''
137 git.createGitBranch(repoDir, crTopic)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200138 }
Alexandr Lovtsovc1cc7502019-07-02 12:41:53 +0500139 cmdText = "python '${repoDir}/utils/app.py' --path '${metadataDir}' update --key '${key}' --value '${value}'"
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200140 python.runVirtualenvCommand(venvDir, cmdText)
141 commitMessage =
Ruslan Gustomiasov2e9e3452019-06-27 14:29:31 +0200142 """${comment}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200143
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200144 |${ChangeId}
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200145 """.stripMargin()
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200146 //commit change
Mykyta Karpin000d3642019-08-08 15:37:45 +0300147 git.commitGitChanges(repoDir, commitMessage, changeAuthorEmail, changeAuthorName, false)
Ruslan Gustomiasov37a17282019-06-27 13:17:07 +0200148 //post change
Alexandr Lovtsov071fadb2019-07-29 17:01:22 +0300149 gerrit.postGerritReview(gitCredentialsId, venvDir, repoDir, changeAuthorName, changeAuthorEmail, gitRemote, crTopic, metadataGerritBranch)
Ruslan Gustomiasovda4edd62019-06-25 14:26:28 +0200150 }
Alexandr Lovtsovcefc3d12019-07-29 14:48:33 +0300151}