-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcloud_wrappers.py
90 lines (70 loc) · 2.35 KB
/
gcloud_wrappers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import time
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
def start_speech_vm(compute=None):
if compute is None:
credentials = GoogleCredentials.get_application_default()
compute = discovery.build(
'compute',
'v1',
credentials=credentials
)
start_request = compute.instances().start(
project='dt2119-speaker-verification',
zone='europe-west1-b',
instance='dt2119-speaker-verification-vm'
)
start_response = start_request.execute()
wait_for_operation(
compute=compute,
project='dt2119-speaker-verification',
zone='europe-west1-b',
operation=start_response['name']
)
def stop_speech_vm(compute=None):
if compute is None:
credentials = GoogleCredentials.get_application_default()
compute = discovery.build(
'compute',
'v1',
credentials=credentials
)
stop_request = compute.instances().stop(
project='dt2119-speaker-verification',
zone='europe-west1-b',
instance='dt2119-speaker-verification-vm'
)
stop_response = stop_request.execute()
wait_for_operation(
compute=compute,
project='dt2119-speaker-verification',
zone='europe-west1-b',
operation=stop_response['name']
)
def wait_for_operation(compute, project, zone, operation):
print('Waiting for operation to finish...')
while True:
result = compute.zoneOperations().get(
project=project,
zone=zone,
operation=operation).execute()
if result['status'] == 'DONE':
print("done.")
if 'error' in result:
raise Exception(result['error'])
return result
time.sleep(1)
# from google.cloud import storage
# def upload_blob(bucket_name, source_file_name, destination_blob_name):
# """Uploads a file to the bucket."""
# storage_client = storage.Client()
# bucket = storage_client.get_bucket(bucket_name)
# blob = bucket.blob(destination_blob_name)
# blob.upload_from_filename(source_file_name)
# print('File {} uploaded to {}.'.format(
# source_file_name,
# destination_blob_name))
if __name__ == "__main__":
# start_speech_vm()
# stop_speech_vm(compute)
pass