| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2018 - The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | """google SDK tools installer. |
| 17 | |
| 18 | This class will return the path to the google sdk tools and download them into |
| 19 | a temporary dir if they don't exist. The caller is expected to call cleanup |
| 20 | when they're done. |
| 21 | |
| 22 | Example usage: |
| 23 | google_sdk = GoogleSDK() |
| 24 | google_sdk_bin_path = google_sdk.GetSDKBinPath() |
| 25 | |
| 26 | # Caller is done. |
| 27 | google_sdk.CleanUp() |
| 28 | """ |
| 29 | |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 30 | import logging |
| 31 | import os |
| 32 | import platform |
| 33 | import shutil |
| 34 | import sys |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 35 | import tempfile |
| chojoyce | f7f5c1e | 2019-10-25 15:14:44 +0800 | [diff] [blame] | 36 | from six.moves import urllib |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 37 | |
| Sam Chiu | 7de3b23 | 2018-12-06 19:45:52 +0800 | [diff] [blame] | 38 | from acloud import errors |
| chojoyce | cd004bc | 2018-09-13 10:39:00 +0800 | [diff] [blame] | 39 | from acloud.internal.lib import utils |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 40 | |
| herbertxue | 1512f8a | 2019-06-27 13:56:23 +0800 | [diff] [blame] | 41 | |
| 42 | logger = logging.getLogger(__name__) |
| 43 | |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 44 | SDK_BIN_PATH = os.path.join("google-cloud-sdk", "bin") |
| 45 | GCLOUD_BIN = "gcloud" |
| herbertxue | 2179050 | 2020-02-03 18:24:07 +0800 | [diff] [blame] | 46 | GCLOUD_COMPONENT_NOT_INSTALLED = "Not Installed" |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 47 | GCP_SDK_VERSION = "209.0.0" |
| 48 | GCP_SDK_TOOLS_URL = "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads" |
| 49 | LINUX_GCP_SDK_64_URL = "%s/google-cloud-sdk-%s-linux-x86_64.tar.gz" % ( |
| 50 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 51 | LINUX_GCP_SDK_32_URL = "%s/google-cloud-sdk-%s-linux-x86.tar.gz" % ( |
| 52 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 53 | WIN_GCP_SDK_64_URL = "%s/google-cloud-sdk-%s-windows-x86_64.zip" % ( |
| 54 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 55 | WIN_GCP_SDK_32_URL = "%s/google-cloud-sdk-%s-windows-x86.zip" % ( |
| 56 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 57 | MAC_GCP_SDK_64_URL = "%s/google-cloud-sdk-%s-darwin-x86_64.tar.gz" % ( |
| 58 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 59 | MAC_GCP_SDK_32_URL = "%s/google-cloud-sdk-%s-darwin-x86.tar.gz" % ( |
| 60 | GCP_SDK_TOOLS_URL, GCP_SDK_VERSION) |
| 61 | LINUX = "linux" |
| 62 | WIN = "windows" |
| 63 | MAC = "darwin" |
| 64 | |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 65 | |
| 66 | def GetSdkUrl(): |
| 67 | """Get google SDK tool url. |
| 68 | |
| 69 | Return: |
| 70 | String, a URL of google SDK tools. |
| 71 | |
| 72 | Raises: |
| 73 | OSTypeError when OS type is neither linux, mac, or windows. |
| 74 | """ |
| 75 | is_64bits = sys.maxsize > 2**32 |
| 76 | os_type = platform.system().lower() |
| 77 | if is_64bits: |
| 78 | if os_type == LINUX: |
| 79 | return LINUX_GCP_SDK_64_URL |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 80 | if os_type == MAC: |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 81 | return MAC_GCP_SDK_64_URL |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 82 | if os_type == WIN: |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 83 | return WIN_GCP_SDK_64_URL |
| 84 | else: |
| 85 | if os_type == LINUX: |
| 86 | return LINUX_GCP_SDK_32_URL |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 87 | if os_type == MAC: |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 88 | return MAC_GCP_SDK_32_URL |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 89 | if os_type == WIN: |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 90 | return WIN_GCP_SDK_32_URL |
| 91 | raise errors.OSTypeError("no gcloud for os type: %s" % (os_type)) |
| 92 | |
| 93 | |
| 94 | def SDKInstalled(): |
| 95 | """Check google SDK tools installed. |
| 96 | |
| 97 | We'll try to find where gcloud is and assume the other google sdk tools |
| 98 | live in the same location. |
| 99 | |
| 100 | Return: |
| 101 | Boolean, return True if gcloud is installed, False otherwise. |
| 102 | """ |
| chojoyce | 6e30cc5 | 2019-11-04 16:18:09 +0800 | [diff] [blame] | 103 | if utils.FindExecutable(GCLOUD_BIN): |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 104 | return True |
| 105 | return False |
| 106 | |
| 107 | |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 108 | class GoogleSDK(): |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 109 | """Google SDK tools installer.""" |
| 110 | |
| 111 | def __init__(self): |
| 112 | """GoogleSDKInstaller initialize. |
| 113 | |
| 114 | Make sure the GCloud SDK is installed. If not, this function will assist |
| 115 | users to install. |
| 116 | """ |
| 117 | self._tmp_path = None |
| 118 | self._tmp_sdk_path = None |
| 119 | if not SDKInstalled(): |
| 120 | self.DownloadGcloudSDKAndExtract() |
| 121 | |
| herbertxue | d69dc51 | 2019-05-30 15:37:15 +0800 | [diff] [blame] | 122 | @staticmethod |
| 123 | def InstallGcloudComponent(gcloud_runner, component): |
| 124 | """Install gcloud component. |
| 125 | |
| 126 | Args: |
| 127 | gcloud_runner: A GcloudRunner class to run "gcloud" command. |
| 128 | component: String, name of gcloud component. |
| 129 | """ |
| herbertxue | 2179050 | 2020-02-03 18:24:07 +0800 | [diff] [blame] | 130 | result = gcloud_runner.RunGcloud([ |
| 131 | "components", "list", "--format", "get(state.name)", "--filter", |
| 132 | "ID=%s" % component |
| 133 | ]) |
| 134 | if result.strip() == GCLOUD_COMPONENT_NOT_INSTALLED: |
| 135 | gcloud_runner.RunGcloud( |
| 136 | ["components", "install", "--quiet", component]) |
| herbertxue | d69dc51 | 2019-05-30 15:37:15 +0800 | [diff] [blame] | 137 | |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 138 | def GetSDKBinPath(self): |
| 139 | """Get google SDK tools bin path. |
| 140 | |
| 141 | We assumed there are google sdk tools somewhere and will raise if we |
| 142 | can't find it. The order we're looking for is: |
| 143 | 1. Builtin gcloud (usually /usr/bin), we'll return /usr/bin with the |
| 144 | assumption other sdk tools live there. |
| 145 | 2. Downloaded google sdk (self._tmp_dir), we assumed the caller already |
| 146 | downloaded/extracted and set the self._tmp_sdk_path for us to return. |
| 147 | We'll make sure it exists prior to returning it. |
| 148 | |
| 149 | Return: |
| 150 | String, return google SDK tools bin path. |
| 151 | |
| 152 | Raise: |
| 153 | NoGoogleSDKDetected if we can't find the sdk path. |
| 154 | """ |
| chojoyce | 6e30cc5 | 2019-11-04 16:18:09 +0800 | [diff] [blame] | 155 | builtin_gcloud = utils.FindExecutable(GCLOUD_BIN) |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 156 | if builtin_gcloud: |
| 157 | return os.path.dirname(builtin_gcloud) |
| herbertxue | d9809d1 | 2021-08-04 14:53:33 +0800 | [diff] [blame^] | 158 | if os.path.exists(self._tmp_sdk_path): |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 159 | return self._tmp_sdk_path |
| 160 | raise errors.NoGoogleSDKDetected("no sdk path.") |
| 161 | |
| 162 | def DownloadGcloudSDKAndExtract(self): |
| 163 | """Download the google SDK tools and decompress it. |
| 164 | |
| 165 | Download the google SDK from the GCP web. |
| 166 | Reference https://cloud.google.com/sdk/docs/downloads-versioned-archives. |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 167 | """ |
| 168 | self._tmp_path = tempfile.mkdtemp(prefix="gcloud") |
| 169 | url = GetSdkUrl() |
| 170 | filename = url[url.rfind("/") + 1:] |
| 171 | file_path = os.path.join(self._tmp_path, filename) |
| 172 | logger.info("Download file from: %s", url) |
| 173 | logger.info("Save the file to: %s", file_path) |
| chojoyce | f7f5c1e | 2019-10-25 15:14:44 +0800 | [diff] [blame] | 174 | url_stream = urllib.request.urlopen(url) |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 175 | metadata = url_stream.info() |
| chojoyce | f7f5c1e | 2019-10-25 15:14:44 +0800 | [diff] [blame] | 176 | file_size = int(metadata.get("Content-Length")) |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 177 | logger.info("Downloading google SDK: %s bytes.", file_size) |
| 178 | with open(file_path, 'wb') as output: |
| 179 | output.write(url_stream.read()) |
| chojoyce | cd004bc | 2018-09-13 10:39:00 +0800 | [diff] [blame] | 180 | utils.Decompress(file_path, self._tmp_path) |
| herbertxue | 0cf6cef | 2018-07-03 21:57:48 +0800 | [diff] [blame] | 181 | self._tmp_sdk_path = os.path.join(self._tmp_path, SDK_BIN_PATH) |
| 182 | |
| 183 | def CleanUp(self): |
| 184 | """Clean google sdk tools install folder.""" |
| 185 | if self._tmp_path and os.path.exists(self._tmp_path): |
| 186 | shutil.rmtree(self._tmp_path) |