1# 2# Copyright (C) 2018 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import logging 17import os 18import re 19import zipfile 20 21from vts.proto import VtsReportMessage_pb2 as ReportMsg 22from vts.runners.host import keys 23from vts.utils.python.common import cmd_utils 24from vts.utils.python.web import feature_utils 25 26 27class GcsUtils(feature_utils.Feature): 28 """GCS (Google Cloud Storage) utility provider. 29 30 Attributes: 31 _TOGGLE_PARAM: String, the name of the parameter used to toggle the feature 32 _REQUIRED_PARAMS: list, the list of parameter names that are required 33 _OPTIONAL_PARAMS: list, the list of parameter names that are optional 34 """ 35 36 _TOGGLE_PARAM = None 37 _REQUIRED_PARAMS = [keys.ConfigKeys.IKEY_SERVICE_JSON_PATH] 38 _OPTIONAL_PARAMS = [] 39 40 def __init__(self, user_params): 41 """Initializes the gcs util provider. 42 43 Args: 44 user_params: A dictionary from parameter name (String) to parameter value. 45 """ 46 self.ParseParameters( 47 toggle_param_name=self._TOGGLE_PARAM, 48 required_param_names=self._REQUIRED_PARAMS, 49 optional_param_names=self._OPTIONAL_PARAMS, 50 user_params=user_params) 51 52 def GetGcloudAuth(self): 53 """Connects to a service account with access to the gcloud bucket.""" 54 gcloud_path = GcsUtils.GetGcloudPath() 55 gcloud_key = getattr(self, keys.ConfigKeys.IKEY_SERVICE_JSON_PATH) 56 if gcloud_path is not None: 57 auth_cmd = "%s auth activate-service-account --key-file %s" % ( 58 gcloud_path, gcloud_key) 59 _, stderr, ret_code = cmd_utils.ExecuteOneShellCommand(auth_cmd) 60 if ret_code == 0: 61 logging.info(stderr) 62 else: 63 logging.error(stderr) 64 65 @staticmethod 66 def GetGcloudPath(): 67 """Returns the gcloud file path if found; None otherwise.""" 68 sh_stdout, _, ret_code = cmd_utils.ExecuteOneShellCommand( 69 "which gcloud") 70 if ret_code == 0: 71 return sh_stdout.strip() 72 else: 73 logging.error("`gcloud` doesn't exist on the host; " 74 "please install Google Cloud SDK before retrying.") 75 return None 76 77 @staticmethod 78 def GetGsutilPath(): 79 """Returns the gsutil file path if found; None otherwise.""" 80 sh_stdout, sh_stderr, ret_code = cmd_utils.ExecuteOneShellCommand( 81 "which gsutil") 82 if ret_code == 0: 83 return sh_stdout.strip() 84 else: 85 logging.error("`gsutil` doesn't exist on the host; " 86 "please install Google Cloud SDK before retrying.") 87 return None 88