-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance_automation_apache.py
More file actions
114 lines (94 loc) · 3.21 KB
/
instance_automation_apache.py
File metadata and controls
114 lines (94 loc) · 3.21 KB
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
#!/usr/bin/python
##https://cloud.google.com/compute/docs/tutorials/python-guide
##centos7
from oauth2client.client import GoogleCredentials
from googleapiclient import discovery
import googleapiclient
import pprint
import json
#import create_ldap
#from cretae_ldap import create_instance
credentials = GoogleCredentials.get_application_default()
compute = googleapiclient.discovery.build('compute','v1', credentials=credentials)
project = "artful-sled-217801"
zone = "us-east1-b"
#machine type requested and name
#machine type helps derive name
name = "final-project-trouble-c"
def list_instances(compute, project, zone):
result = compute.instances().list(project=project, zone=zone).execute()
return result['items']
def create_instance(compute, project, zone, name):
# Configure the machine
startup_script = open('startup-script-apache.sh', 'r').read()
image_response = compute.images().getFromFamily(project='centos-cloud', family='centos-7').execute()
source_disk_image = image_response['selfLink']
machine_type = "zones/%s/machineTypes/f1-micro" % zone
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,
}
}
],
# Specify a network interface with NAT to access the public
# internet.
'networkInterfaces': [{
'network': 'global/networks/default',
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
# Allow the instance to access cloud storage and logging.
'serviceAccounts': [{
'email': 'default',
'scopes': [
'https://www.googleapis.com/auth/devstorage.read_write',
'https://www.googleapis.com/auth/logging.write'
]
}],
#Enable https/http for select instances
"labels": {
"http-server": "",
"https-server": ""
},
"tags": {
"items": [
"http-server",
"https-server"
]
},
# 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': 'url',
#'value': 'IMAGE_URL'
# }, {
# 'key': 'text',
# 'value': 'TEXT'
# }, {
# 'key': 'bucket',
# 'value': 'CS_BUCKET'
}]
}
}
return compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
newinstance = create_instance(compute,project, zone, name)
instance = list_instances(compute, project, zone)
pprint.pprint(newinstance)
pprint.pprint(instance)