forked from DSPN/google-compute-engine-dse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datastax.py
222 lines (197 loc) · 7.52 KB
/
datastax.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import yaml
import common
import default
def GenerateFirewall(context):
name = context.env['deployment'] + '-opscenter-firewall'
firewalls = [
{
'name': name,
'type': 'compute.v1.firewall',
'properties': {
'network': common.MakeGlobalComputeLink(context, default.NETWORK),
'sourceRanges': [
'0.0.0.0/0'
],
'allowed': [{
'IPProtocol': 'tcp',
'ports': ['8888', '8443']
}]
}
}
]
return firewalls
def GenerateReferencesList(context):
reference_list = []
n_of_copies = context.properties['nodesPerZone']
dep_name = context.env['deployment']
for zone in context.properties['zones']:
for idx in range(1, n_of_copies + 1):
node_name = '$(ref.' + dep_name + '-' + zone + '-' + str(idx) + '-vm' + '.selfLink)'
reference_list.append(node_name)
return ' '.join(reference_list)
def GetZonesList(context):
zones = []
if context.properties['usEast1b']:
zones.append('us-east1-b')
if context.properties['usEast1c']:
zones.append('us-east1-c')
if context.properties['usEast1d']:
zones.append('us-east1-d')
if context.properties['usCentral1a']:
zones.append('us-central1-a')
if context.properties['usCentral1b']:
zones.append('us-central1-b')
if context.properties['usCentral1c']:
zones.append('us-central1-c')
if context.properties['usCentral1f']:
zones.append('us-central1-f')
if context.properties['europeWest1b']:
zones.append('europe-west1-b')
if context.properties['europeWest1c']:
zones.append('europe-west1-c')
if context.properties['europeWest1d']:
zones.append('europe-west1-d')
if context.properties['asiaEast1a']:
zones.append('asia-east1-a')
if context.properties['asiaEast1b']:
zones.append('asia-east1-b')
if context.properties['asiaEast1c']:
zones.append('asia-east1-c')
assert len(zones) > 0, 'No zones selected for DataStax Enterprise nodes'
return zones
def GenerateConfig(context):
config = {'resources': []}
# Set zones list based on zone booleans.
if ('zones' not in context.properties or len(context.properties['zones']) == 0):
context.properties['zones'] = GetZonesList(context)
# Set zone property to match ops center zone. Needed for calls to common.MakeGlobalComputeLink.
context.properties['zone'] = context.properties['opsCenterZone']
seed_nodes_dns_names = context.env['deployment'] + '-' + context.properties['zones'][0] + '-1-vm.c.' + context.env['project'] + '.internal'
dse_node_script = '''
#!/usr/bin/env bash
mkdir /mnt
/usr/share/google/safe_format_and_mount -m "mkfs.ext4 -F" /dev/disk/by-id/google-${HOSTNAME}-data-disk /mnt
wget https://github.com/DSPN/install-datastax/archive/master.zip
apt-get -y install unzip
unzip master.zip
cd install-datastax-master/bin
cloud_type="google"
zone=$(curl -s -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone" | grep -o [[:alnum:]-]*$)
data_center_name=$zone
seed_nodes_dns_names=''' + seed_nodes_dns_names + '''
echo "Configuring nodes with the settings:"
echo cloud_type $cloud_type
echo seed_nodes_dns_names $seed_nodes_dns_names
echo data_center_name $data_center_name
./dse.sh $cloud_type $seed_nodes_dns_names $data_center_name
'''
zonal_clusters = {
'name': 'clusters-' + context.env['name'],
'type': 'regional_multi_vm.py',
'properties': {
'sourceImage': 'https://www.googleapis.com/compute/v1/projects/datastax-public/global/images/datastax-ubuntu1404-img-03172016',
'zones': context.properties['zones'],
'machineType': context.properties['machineType'],
'network': context.properties['network'],
'numberOfVMReplicas': context.properties['nodesPerZone'],
'disks': [
{
'deviceName': 'vm-data-disk',
'type': 'PERSISTENT',
'boot': 'false',
'autoDelete': 'true',
'initializeParams': {
'diskType': context.properties['dataDiskType'],
'diskSizeGb': context.properties['diskSize']
}
}
],
'bootDiskType': 'pd-standard',
'metadata': {
'items': [
{
'key': 'startup-script',
'value': dse_node_script
}
]
}
}
}
ops_center_script = '''
#!/usr/bin/env bash
wget https://github.com/DSPN/install-datastax/archive/master.zip
apt-get -y install unzip
unzip master.zip
cd install-datastax-master/bin
cloud_type="google"
seed_nodes_dns_names=''' + seed_nodes_dns_names + '''
echo "Configuring nodes with the settings:"
echo cloud_type $cloud_type
echo seed_nodes_dns_names $seed_nodes_dns_names
./opscenter.sh $cloud_type $seed_nodes_dns_names
'''
ops_center_node_name = context.env['deployment'] + '-opscenter-vm'
ops_center_node = {
'name': ops_center_node_name,
'type': 'vm_instance.py',
'properties': {
'instanceName': ops_center_node_name,
'sourceImage': 'https://www.googleapis.com/compute/v1/projects/datastax-public/global/images/datastax-ubuntu1404-img-03172016',
'zone': context.properties['opsCenterZone'],
'machineType': context.properties['machineType'],
'network': context.properties['network'],
'bootDiskType': 'pd-standard',
'serviceAccounts': [{
'email': 'default',
'scopes': ['https://www.googleapis.com/auth/compute']
}],
'metadata': {
'items': [
{
'key': 'startup-script',
'value': ops_center_script
},
{
'key': 'bogus-references',
'value': GenerateReferencesList(context)
}
]
}
}
}
config['resources'].append(zonal_clusters)
config['resources'].append(ops_center_node)
config['resources'].extend(GenerateFirewall(context))
first_enterprise_node_name = context.env['deployment'] + '-' + context.properties['zones'][0] + '-1-vm'
outputs = [
{
'name': 'project',
'value': context.env['project']
},
{
'name': 'opsCenterNodeName',
'value': ops_center_node_name
},
{
'name': 'firstEnterpriseNodeName',
'value': first_enterprise_node_name
},
{
'name': 'firstEnterpriseNodeSelfLink',
'value': '$(ref.' + first_enterprise_node_name + '.selfLink)'
},
{
'name': 'zoneList',
'value': ', '.join(context.properties['zones'])
},
{
'name': 'x-status-type',
'value': 'console'
},
{
'name': 'x-status-instance',
'value': ops_center_node_name
}
]
config['outputs'] = outputs
return yaml.dump(config)