Skip to content

Commit a15ff2f

Browse files
authored
Enable google drive support (#899)
1 parent eb8a4ba commit a15ff2f

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

benchmark_runner/common/google_drive/google_drive_operations.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import os
3-
import logging
3+
import json
44

55
from googleapiclient.discovery import build
66
from google.oauth2.credentials import Credentials
@@ -20,14 +20,14 @@ class GoogleDriveOperations:
2020
1. credentials.json - Obtain from the "Google Cloud Console" under "APIs & Services" > "Credentials" > "Create Credentials" and select "OAuth 2.0 Client ID".
2121
2. token.json - This is generated automatically. If a browser is not available, copy the token from a machine with a browser.
2222
"""
23-
def __init__(self, google_drive_path: str, credentials_path: str, token_path: str, shared_drive_id: str):
23+
def __init__(self, google_drive_path: str, google_drive_credentials: str, google_drive_token: str, google_drive_shared_drive_id: str):
2424
"""
2525
Initializes GoogleDriveOperations with authentication.
2626
"""
2727
self._google_drive_path = google_drive_path
28-
self._credentials_path = credentials_path
29-
self._token_path = token_path
30-
self._shared_drive_id = shared_drive_id
28+
self._google_drive_credentials = google_drive_credentials
29+
self._google_drive_token = google_drive_token
30+
self._google_drive_shared_drive_id = google_drive_shared_drive_id
3131
self.creds = self.authenticate()
3232
self.service = build('drive', 'v3', credentials=self.creds)
3333

@@ -37,16 +37,23 @@ def authenticate(self):
3737
:return: Credentials object
3838
"""
3939
creds = None
40-
if os.path.exists(self._token_path):
41-
creds = Credentials.from_authorized_user_file(self._token_path, SCOPES)
40+
41+
# Load credentials from the token string if it exists
42+
if self._google_drive_token:
43+
creds = Credentials.from_authorized_user_info(json.loads(self._google_drive_token), SCOPES)
44+
45+
# Check if the credentials are invalid or expired
4246
if not creds or not creds.valid:
4347
if creds and creds.expired and creds.refresh_token:
4448
creds.refresh(Request())
4549
else:
46-
flow = InstalledAppFlow.from_client_secrets_file(self._credentials_path, SCOPES)
50+
# Load client secrets from the credentials string and run the local server for auth
51+
flow = InstalledAppFlow.from_client_config(json.loads(self._google_drive_credentials), SCOPES)
4752
creds = flow.run_local_server(port=0)
48-
with open(self._token_path, 'w') as token_file:
49-
token_file.write(creds.to_json())
53+
54+
# Save the new token to the variable as a string
55+
self._google_drive_token = creds.to_json()
56+
5057
return creds
5158

5259
def list_folders_and_files_in_shared_drive(self, shared_drive_id):

benchmark_runner/main/environment_variables.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ def __init__(self):
201201

202202
# Google drive
203203
self._environment_variables_dict['google_drive_path'] = EnvironmentVariables.get_env('GOOGLE_DRIVE_PATH', '')
204-
self._environment_variables_dict['credentials_path'] = EnvironmentVariables.get_env('CREDENTIALS_PATH', '')
205-
self._environment_variables_dict['token_path'] = EnvironmentVariables.get_env('TOKEN_PATH', '')
206-
self._environment_variables_dict['shared_drive_id'] = EnvironmentVariables.get_env('SHARED_DRIVE_ID', '')
204+
self._environment_variables_dict['google_drive_credentials'] = EnvironmentVariables.get_env('GOOGLE_DRIVE_CREDENTIALS', '')
205+
self._environment_variables_dict['google_drive_token'] = EnvironmentVariables.get_env('GOOGLE_DRIVE_TOKEN', '')
206+
self._environment_variables_dict['google_drive_shared_drive_id'] = EnvironmentVariables.get_env('GOOGLE_DRIVE_SHARED_DRIVE_ID', '')
207207

208208
# Grafana
209209
self._environment_variables_dict['grafana_url'] = EnvironmentVariables.get_env('GRAFANA_URL', '')

benchmark_runner/workloads/bootstorm_vm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,10 @@ def _verify_vm_ssh(self):
210210
self._oc.generate_cnv_must_gather(destination_path=self._run_artifacts_path, cnv_version=self._cnv_version)
211211
self._oc.generate_odf_must_gather(destination_path=self._run_artifacts_path, odf_version=self._odf_version)
212212
# google drive
213-
if self._shared_drive_id:
213+
if self._google_drive_shared_drive_id:
214214
self.upload_run_artifacts_to_google_drive()
215215
# s3
216-
elif self._endpoint_url and not self._shared_drive_id:
216+
elif self._endpoint_url and not self._google_drive_shared_drive_id:
217217
self.upload_run_artifacts_to_s3()
218218
# local
219219
else:

benchmark_runner/workloads/workloads_operations.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,14 @@ def __init__(self):
110110
self._windows_os = os.path.splitext(file_name)[0]
111111
# google drive
112112
self._google_drive_path = self._environment_variables_dict.get('google_drive_path', '')
113-
self._credentials_path = self._environment_variables_dict.get('credentials_path', '')
114-
self._token_path = self._environment_variables_dict.get('token_path', '')
115-
self._shared_drive_id = self._environment_variables_dict.get('shared_drive_id', '')
116-
self._google_drive_operation = GoogleDriveOperations(google_drive_path=self._google_drive_path,
117-
credentials_path=self._credentials_path,
118-
token_path=self._token_path,
119-
shared_drive_id=self._shared_drive_id)
113+
self._google_drive_credentials = self._environment_variables_dict.get('google_drive_credentials', '')
114+
self._google_drive_token = self._environment_variables_dict.get('google_drive_token', '')
115+
self._google_drive_shared_drive_id = self._environment_variables_dict.get('google_drive_shared_drive_id', '')
116+
if self._google_drive_path:
117+
self._google_drive_operation = GoogleDriveOperations(google_drive_path=self._google_drive_path,
118+
google_drive_credentials=self._google_drive_credentials,
119+
google_drive_token=self._google_drive_token,
120+
google_drive_shared_drive_id=self._google_drive_shared_drive_id)
120121

121122
def _get_workload_file_name(self, workload):
122123
"""
@@ -360,7 +361,7 @@ def get_run_artifacts_google_drive(self):
360361
"""
361362
workload = self._workload.replace('_', '-')
362363
run_artifacts_hierarchy = self._get_run_artifacts_hierarchy(workload_name=workload)
363-
return self._google_drive_operation.get_drive_folder_url(folder_path=run_artifacts_hierarchy, parent_folder_id=self._shared_drive_id)
364+
return self._google_drive_operation.get_drive_folder_url(folder_path=run_artifacts_hierarchy, parent_folder_id=self._google_drive_shared_drive_id)
364365

365366
@logger_time_stamp
366367
def upload_run_artifacts_to_google_drive(self):
@@ -377,7 +378,7 @@ def upload_run_artifacts_to_google_drive(self):
377378
upload_file = f"{workload_file_name}.tar.gz"
378379
self._google_drive_operation.upload_file_to_folder(file_path=tar_run_artifacts_path,
379380
folder_path=str(run_artifacts_hierarchy),
380-
parent_folder_id=self._shared_drive_id)
381+
parent_folder_id=self._google_drive_shared_drive_id)
381382

382383

383384
def __get_metadata(self, kind: str = None, status: str = None, result: dict = None) -> dict:

jenkins/PerfCI_Chaos/04_PerfCI_Chaos_Upgrade_OpenShift_Deployment/Jenkinsfile

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pipeline {
77
LSO_VERSION = '4.15'
88
ODF_VERSION = '4.15'
99
CNV_VERSION = '4.15'
10+
WAIT_FOR_UPGRADE_VERSION = '4.15.23'
1011
CONTACT_EMAIL1 = credentials('perfci_contact_email1')
1112
PROVISION_PRIVATE_KEY_FILE = credentials('perfci_provision_private_key_file')
1213
PROVISION_IP = credentials('perfci_provision_ip')
@@ -39,6 +40,10 @@ pipeline {
3940
IBM_SECRET_ACCESS_KEY = credentials('perfci_ibm_secret_access_key')
4041
IBM_BUCKET = credentials('perfci_ibm_bucket')
4142
IBM_KEY = credentials('perfci_ibm_key')
43+
GOOGLE_DRIVE_PATH = credentials('perfci_google_drive_path')
44+
GOOGLE_DRIVE_CREDENTIALS = credentials('perfci_google_drive_credentials')
45+
GOOGLE_DRIVE_TOKEN = credentials('perfci_google_drive_token')
46+
GOOGLE_DRIVE_SHARED_DRIVE_ID = credentials('perfci_google_drive_shared_drive_id')
4247
RUN_ARTIFACTS_URL = credentials('perfci_run_artifacts_url')
4348
REDIS = credentials('perfci_redis')
4449
WORKER_DISK_IDS = credentials('perfci_worker_disk_ids')
@@ -212,6 +217,7 @@ END
212217
sh """
213218
sudo podman run --rm -t \
214219
-e WORKLOAD='${workload}' \
220+
-e WAIT_FOR_UPGRADE_VERSION='${WAIT_FOR_UPGRADE_VERSION}' \
215221
-e KUBEADMIN_PASSWORD='${KUBEADMIN_PASSWORD}' \
216222
-e PIN_NODE_BENCHMARK_OPERATOR='${PIN_NODE_BENCHMARK_OPERATOR}' \
217223
-e PIN_NODE1='${PIN_NODE1}' \
@@ -224,6 +230,10 @@ END
224230
-e IBM_SECRET_ACCESS_KEY='${IBM_SECRET_ACCESS_KEY}' \
225231
-e IBM_BUCKET='${IBM_BUCKET}' \
226232
-e IBM_KEY='${IBM_KEY}' \
233+
-e GOOGLE_DRIVE_PATH='${GOOGLE_DRIVE_PATH}' \
234+
-e GOOGLE_DRIVE_CREDENTIALS='${GOOGLE_DRIVE_CREDENTIALS}' \
235+
-e GOOGLE_DRIVE_TOKEN='${GOOGLE_DRIVE_TOKEN}' \
236+
-e GOOGLE_DRIVE_SHARED_DRIVE_ID='${GOOGLE_DRIVE_SHARED_DRIVE_ID}' \
227237
-e RUN_ARTIFACTS_URL='${RUN_ARTIFACTS_URL}' \
228238
-e BUILD_VERSION='${build_version}' \
229239
-e RUN_TYPE='${RUN_TYPE}' \
@@ -265,7 +275,7 @@ END
265275
}
266276
}
267277
}
268-
} // WORKLOADS Deployment
278+
} // WORKLOADS Verifications
269279
}
270280
}
271281
} // stages

tests/unittest/benchmark_runner/common/google_drive/mock_google_drive.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def decorated(*args, **kwargs):
2121
# Patch the GoogleDriveOperations to use the mock service
2222
with patch('benchmark_runner.common.google_drive.google_drive_operations.GoogleDriveOperations.authenticate', return_value=None):
2323
with patch('benchmark_runner.common.google_drive.google_drive_operations.build', return_value=mock_service):
24-
google_drive_operations = GoogleDriveOperations(google_drive_path='google_drive_path', credentials_path='credentials.json', token_path='token.json', shared_drive_id='shared_drive_id')
24+
google_drive_operations = GoogleDriveOperations(google_drive_path='google_drive_path', google_drive_credentials='google_drive_credentials', google_drive_token='google_drive_token', google_drive_shared_drive_id='google_drive_shared_drive_id')
2525
google_drive_operations.service = mock_service
2626

2727
return func(google_drive_operations, mock_service, *args, **kwargs)

0 commit comments

Comments
 (0)