Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 1 | package 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 | */ |
| 11 | def 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 | */ |
| 21 | def 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 | */ |
| 40 | def 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 | */ |
| 55 | def 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 | */ |
| 66 | def 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 | */ |
| 84 | def 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 |
Denis Egorenko | 42eaec6 | 2019-07-31 16:52:18 +0400 | [diff] [blame] | 100 | * @param useDifferentGcloudSDKDir Allow to use different SDK config dirs/accounts in parallel |
| 101 | * @param gcloudSDKDir Actual path to SDK config dir, defaults to gcloud default: /home/<user>/.gcloud |
| 102 | * or if HOME is unset to /tmp/.gcloud |
| 103 | * @param revokeAccount Revoke account after actions |
Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 104 | * |
| 105 | * Returns URLs list of uploaded files |
| 106 | */ |
| 107 | def uploadArtifactToGoogleStorageBucket(Map config) { |
| 108 | def gcloudDir = config.get('gcloudDir', '/tmp/gcloud') |
Denis Egorenko | 42eaec6 | 2019-07-31 16:52:18 +0400 | [diff] [blame] | 109 | def gcloudSDKDir = config.get('gcloudSDKDir', "${env.HOME ?: '/tmp'}/.gcloud") |
Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 110 | def creds = config.get('creds') |
| 111 | def project = config.get('project') |
| 112 | def acls = config.get('acls', ['AllUsers:R']) |
| 113 | def sources = config.get('sources') |
| 114 | def dest = config.get('dest') |
| 115 | def entireTree = config.get('entireTree', false) |
Denis Egorenko | 42eaec6 | 2019-07-31 16:52:18 +0400 | [diff] [blame] | 116 | def useDifferentGcloudSDKDir = config.get('useDifferentGcloudSDKDir', true) |
| 117 | def revokeAccount = config.get('revokeAccount', true) |
Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 118 | def fileURLs = [] |
| 119 | if (!checkGcloudBinary(gcloudDir)) { |
| 120 | downloadGcloudUtil(gcloudDir) |
| 121 | } |
Denis Egorenko | 42eaec6 | 2019-07-31 16:52:18 +0400 | [diff] [blame] | 122 | if (useDifferentGcloudSDKDir) { |
| 123 | gcloudSDKDir = "${gcloudSDKDir}_" + UUID.randomUUID().toString() |
| 124 | sh "mkdir -p ${gcloudSDKDir}" |
| 125 | } |
| 126 | withEnv(["CLOUDSDK_CONFIG=${gcloudSDKDir}"]) { |
| 127 | try { |
| 128 | authGcloud(gcloudDir, creds, project) |
| 129 | for(String src in sources) { |
| 130 | def fileURL = cpFile(gcloudDir, src, dest, entireTree) |
| 131 | setAcl(gcloudDir, fileURL, acls) |
| 132 | fileURLs << fileURL |
| 133 | } |
| 134 | } finally { |
| 135 | if (revokeAccount) { |
| 136 | revokeGcloud(gcloudDir) |
| 137 | sh "rm -rf ${gcloudSDKDir}" |
| 138 | } |
Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 139 | } |
Denis Egorenko | 815758d | 2019-07-08 15:54:08 +0400 | [diff] [blame] | 140 | } |
| 141 | return fileURLs |
| 142 | } |