blob: 5bf5ad71e742452ba78637edc00985bcaecbf666 [file] [log] [blame]
Denis Egorenko815758d2019-07-08 15:54:08 +04001package com.mirantis.mk
2
3/**
4 Work with Google Cloud Storage
5**/
6
7/** Exists or not gcloud binary file
8 *
9 * @param gcloudBinDir Path to check
10*/
11def checkGcloudBinary(String gcloudBinDir) {
12 def status = sh(script: "${gcloudBinDir}/google-cloud-sdk/bin/gcloud info > /dev/null", returnStatus: true)
13 return "${status}" == "0"
14}
15
16/** Download gcloud archive with binarties
17 *
18 * @param gcloudBinDir Path to save binaries
19 * @param url Specific URL to use to download
20*/
21def downloadGcloudUtil(String gcloudBinDir, String url="") {
22 if (!url) {
23 url="https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-253.0.0-linux-x86_64.tar.gz"
24 }
25 dir(gcloudBinDir) {
26 def archiveName='google-cloud-sdk'
27 sh """
28 wget -O ${archiveName}.tar.gz ${url}
29 tar -xf ${archiveName}.tar.gz
30 """
31 }
32}
33
34/** Auth gcloud util with provided creds
35 *
36 * @param gcloudBinDir Path to save binaries
37 * @param creds Creds name to use, saved as secret file
38 * @param projectName Project name to use
39*/
40def authGcloud(String gcloudBinDir, String creds, String projectName) {
41 ws {
42 withCredentials([
43 file(credentialsId: creds,
44 variable: 'key_file')
45 ]) {
46 sh "${gcloudBinDir}/google-cloud-sdk/bin/gcloud auth activate-service-account --key-file $key_file --project ${projectName}"
47 }
48 }
49}
50
51/** Revoke gcloud auth account
52 *
53 * @param gcloudBinDir Path to save binaries
54*/
55def revokeGcloud(String gcloudBinDir) {
56 sh "${gcloudBinDir}/google-cloud-sdk/bin/gcloud auth revoke"
57}
58
59/** Copy file to Google Storage Bucket
60 *
61 * @param gcloudBinDir Path to save binaries
62 * @param src Source file
63 * @param dst Destination path in storage
64 * @param entireTree Copy entire directory tree
65*/
66def cpFile(String gcloudBinDir, String src, String dst, Boolean entireTree=false) {
67 def fileURL = ''
68 if (entireTree) {
69 sh "${gcloudBinDir}/google-cloud-sdk/bin/gsutil cp -r ${src} ${dst}"
70 return dst
71 } else {
72 def fileBaseName = sh(script:"basename ${src}", returnStdout: true).trim()
73 sh "${gcloudBinDir}/google-cloud-sdk/bin/gsutil cp ${src} ${dst}/${fileBaseName}"
74 return "${dst}/${fileBaseName}"
75 }
76}
77
78/** Set ACL on files in bucket
79 *
80 * @param gcloudBinDir Path to save binaries
81 * @param path Path to file in bucket
82 * @param acls ACLs to be set for file
83*/
84def setAcl(String gcloudBinDir, String path, ArrayList acls) {
85 for(String acl in acls) {
86 sh "${gcloudBinDir}/google-cloud-sdk/bin/gsutil acl ch -u ${acl} ${path}"
87 }
88}
89
90/** Upload files to Google Cloud Storage Bucket
91 *
92 * @param config LinkedHashMap with next parameters:
93 * @param gcloudBinDir Path to save binaries
94 * @param creds Creds name to use, saved as secret file
95 * @param projectName Project name to use
96 * @param sources List of file to upload
97 * @param dest Destination path in Google Storage, in format: gs://<path>
98 * @param acls ACLs for uploaded files
99 * @param entireTree Copy entire directory to bucket
100 *
101 * Returns URLs list of uploaded files
102*/
103def uploadArtifactToGoogleStorageBucket(Map config) {
104 def gcloudDir = config.get('gcloudDir', '/tmp/gcloud')
105 def creds = config.get('creds')
106 def project = config.get('project')
107 def acls = config.get('acls', ['AllUsers:R'])
108 def sources = config.get('sources')
109 def dest = config.get('dest')
110 def entireTree = config.get('entireTree', false)
111 def fileURLs = []
112 if (!checkGcloudBinary(gcloudDir)) {
113 downloadGcloudUtil(gcloudDir)
114 }
115 try {
116 authGcloud(gcloudDir, creds, project)
117 for(String src in sources) {
118 def fileURL = cpFile(gcloudDir, src, dest, entireTree)
119 setAcl(gcloudDir, fileURL, acls)
120 fileURLs << fileURL
121 }
122 } finally {
123 revokeGcloud(gcloudDir)
124 }
125 return fileURLs
126}