blob: 7d9fd894c3617cb40b3e708a2996aeb9f6af1a9c [file] [log] [blame]
asledzinskiy69307da2017-01-19 16:37:16 +02001package com.mirantis.mcp_qa
asledzinskiye0948fd2017-01-06 16:23:54 +02002
3/**
4 * Get latest artifacts
5 * @param imageRepoName is the repo name where image is located
6 * @param imageTagName is the name of the image tag to be used
7 */
8
9def getLatestArtifacts(imageRepoName, imageTagName) {
10 def imageRepo = env.getAt(imageRepoName)
11 def imageTag = env.getAt(imageTagName)
12 if ( imageTag != null && (! imageTag || imageTag.equals('latest')) ) {
13 if ( imageRepo ) {
14 def registry = imageRepo.replaceAll(/\/.*/, '')
15 def image = imageRepo.minus(registry + '/')
16 def hyperkubeImageTag = latestImageTagLookup(registry, image)
17 return "${imageTagName}=${hyperkubeImageTag}"
18 } else {
19 echo "${imageRepoName} variable isn't set, can't inspect 'latest' image!"
20 return null
21 }
22 }
23}
24
25def jsonParse(def json) {
26 new groovy.json.JsonSlurperClassic().parseText(json)
27}
28
29/**
30 * Get digest metadata
31 * @param tag is the image tag to be used
32 * @param registry is the url of registry
33 * @param image is the image which info is looked for
34 */
35
36def get_digest(def tag, def registry, def image) {
37 def digest_link = sprintf('https://%1$s/v2/%2$s/manifests/%3$s', [registry, image, tag])
38 def digest_url = new URL(digest_link)
39 def connection = digest_url.openConnection()
40 connection.setRequestProperty('Accept', 'application/vnd.docker.distribution.manifest.v2+json')
41 def digest = connection.getHeaderField("Docker-Content-Digest")
42 return digest
43}
44
45/**
46 * Get latest tag metadata
47 * @param registry is the url of registry
48 * @param image is the image which tags are looked for
49 */
50
51def latestImageTagLookup(registry, image) {
52 def tags_link = sprintf('https://%1$s/v2/%2$s/tags/list', [registry, image])
53 def tags_url = new URL(tags_link)
54 def tags = jsonParse(tags_url.getText())['tags']
55 def latest_digest = get_digest('latest', registry, image)
56 def same_digest_tags = []
57
58 for (tag in tags) {
59 if (tag == 'latest') {
60 continue
61 }
62 if (get_digest(tag, registry, image) == latest_digest) {
63 same_digest_tags<< tag
64 }
65 }
66
67 return same_digest_tags[0] ?: 'latest'
68}
69
70
71/**
72 * Fetch custom refs
73 * @param gerritUrl is url of gerrit
74 * @param project is the name of project in gerrit
75 * @param targetDir is dir where to fetch changes
76 * @param refs is refs that need to be fetched
77 */
78
79def getCustomRefs(gerritUrl, project, targetDir, refs) {
80 def remote = "${gerritUrl}/${project}"
81 dir(targetDir) {
82 for(int i=0; i<refs.size(); i++) {
83 sh "git fetch ${remote} ${refs[i]} && git checkout FETCH_HEAD"
84 }
85 }
86}
87
88/**
89 * Set downstream k8s artifacts
90 * @param jobSetParameters are current job parameters that can be extended with kubernetes tag
91 */
92
93def set_downstream_k8s_artifacts(jobSetParameters) {
94 def k8sTag = getLatestArtifacts('HYPERKUBE_IMAGE_REPO', 'HYPERKUBE_IMAGE_TAG')
95 if (k8sTag) {
96 jobSetParameters.add(k8sTag)
97 }
98 return jobSetParameters
99}