This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
151 lines (118 loc) · 4.17 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import sys
import time
from oauth2client.client import GoogleCredentials
from googleapiclient.discovery import build
# list instances
def list_instances(compute, project, zone):
result = compute.instances().list(project=project, zone=zone).execute()
return result['items']
# create instance
def create_instance(compute, project, zone, name, package):
source_disk_image = \
"projects/debian-cloud/global/images/debian-7-wheezy-v20150320"
machine_type = "zones/%s/machineTypes/n1-standard-1" % zone
startup_script = open('startup-script.sh', 'r').read()
config = {
'name': name,
'machineType': machine_type,
# Specify the boot disk and the image to use as a source.
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
'tags': {
'items': [
'http-server'
]
},
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
'serviceAccounts': [{
'email': 'default',
'scopes': [
'https://www.googleapis.com/auth/devstorage.read_write',
'https://www.googleapis.com/auth/logging.write'
]
}],
# Metadata is readable from the instance and allows you to
# pass configuration from deployment scripts to instances.
'metadata': {
'items': [{
# Startup script is automatically executed by the
# instance upon startup.
'key': 'startup-script',
'value': startup_script
}, {
'key': 'package',
'value': package
}, {
# Every project has a default Cloud Storage bucket that's
# the same name as the project.
'key': 'bucket',
'value': project
}]
}
}
return compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
# delete_instance
def delete_instance(compute, project, zone, name):
return compute.instances().delete(
project=project,
zone=zone,
instance=name).execute()
# wait_for_operation
def wait_for_operation(compute, project, zone, operation):
sys.stdout.write('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
else:
sys.stdout.write('.')
sys.stdout.flush()
time.sleep(1)
# START run
def run(project, zone, instance_name, package):
credentials = GoogleCredentials.get_application_default()
compute = build('compute', 'v1', credentials=credentials)
print 'Creating instance.'
#creating instance
operation = create_instance(compute, project, zone, instance_name, package)
#execute operations
wait_for_operation(compute, project, zone, operation['name'])
#listing instances
instances = list_instances(compute, project, zone)
print 'Instances in project %s and zone %s:' % (project, zone)
for instance in instances:
print ' - ' + instance['name']
print """ Instance created """
raw_input()
print 'Deleting instance'
operation = delete_instance(compute, project, zone, instance_name)
wait_for_operation(compute, project, zone, operation['name'])
def main():
project = raw_input('project ID? ')
zone = raw_input('zone? [us-central1-a] ') or 'us-central1-a'
instance_name = raw_input('name? [mobilevm1] ') or 'mobilevm1'
package = raw_input('package to use [website] ') or 'Site3'
run(project, zone, instance_name, package)
if __name__ == '__main__':
main()