-
Notifications
You must be signed in to change notification settings - Fork 12
/
datastax.py
332 lines (284 loc) · 11.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import yaml
import random
import string
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']
cluster_name = 'clusters-' + context.env['name']
# Generate a random bucket name
bucket_suffix = ''.join([random.choice(string.ascii_lowercase + string.digits) for n in xrange(10)])
sshkey_bucket = context.env['deployment'] + '-ssh-pub-key-bucket-' + bucket_suffix
# DSE version
dse_version = context.properties['dseVersion']
# Set cassandra's user password
db_pwd = context.properties['cassandraPwd']
# Set DataStax Academy credentials
dsa_username = context.properties['dsa_username']
dsa_password = context.properties['dsa_password']
# Set default OpsCenter Admin password
opsc_admin_pwd = context.properties['opsCenterAdminPwd']
# Set DC size, number of DCs and cluster's size
dc_size = context.properties['nodesPerZone']
num_dcs = len(context.properties['zones'])
cluster_size = dc_size * num_dcs
seed_nodes_dns_names = context.env['deployment'] + '-' + context.properties['zones'][0] + '-1-vm.c.' + context.env[
'project'] + '.internal.'
opscenter_node_name = context.env['deployment'] + '-opscenter-vm'
opscenter_dns_name = opscenter_node_name + '.c.' + context.env['project'] + '.internal.'
# Prepare a storage bucket to store our randomly generated SSH key pair for LCM's DSE install
ssh_pub_key_bucket = {
'name': sshkey_bucket,
'type': 'storage.v1.bucket',
'properties': {
'name': sshkey_bucket,
}
}
config['resources'].append(ssh_pub_key_bucket)
# Script to run inside a DSE node during instance instantiation
dse_node_script = '''
#!/usr/bin/env bash
# If dse already installed, do nothing
dpkg -s dse &> /dev/null
retVal=$?
if [ $retVal -eq 0 ]; then
exit 0
fi
# Prepare for fresh DSE installation
mkdir /mnt
mkfs.ext4 -m 0 -F -E lazy_itable_init=0,lazy_journal_init=0,discard /dev/disk/by-id/google-${HOSTNAME}-data-disk
mount -o discard,defaults /dev/disk/by-id/google-${HOSTNAME}-data-disk /mnt
echo "/dev/disk/by-id/google-${HOSTNAME}-data-disk /mnt ext4 discard,defaults 0 2" | tee -a /etc/fstab
mkdir -p /mnt/data1
mkdir -p /mnt/data1/data
mkdir -p /mnt/data1/saved_caches
mkdir -p /mnt/data1/commitlog
mkdir -p /mnt/data1/dsefs
chmod -R 777 /mnt/data1
##### Install DSE the LCM way
cd ~ubuntu
release="master"
gsutil cp gs://dse-gcp-marketplace/dse-gcp-install-$release.tar.gz .
tar -xvf dse-gcp-install-$release.tar.gz
# install extra OS packages
pushd dse-gcp-install-$release
./extra_packages.sh
./install_java.sh -o
popd
public_ip=`curl --retry 10 icanhazip.com`
private_ip=`echo $(hostname -I)`
node_id=$private_ip
cluster_name=''' + cluster_name + '''
rack="rack1"
db_pwd=''' + db_pwd + '''
zone=$(curl -s -H "Metadata-Flavor: Google" "http://metadata.google.internal/computeMetadata/v1/instance/zone" | grep -o [[:alnum:]-]*$)
data_center_name=$zone
# Retrieve internal OPSC IP address
opscenter_dns_name=''' + opscenter_dns_name + '''
opsc_ip=`dig +short $opscenter_dns_name`
# Grab lcm_pem.pub pubilc key from Google Cloud Storage
cd ~ubuntu/.ssh/
sshkey_bucket=''' + sshkey_bucket + '''
gsutil cp gs://$sshkey_bucket/lcm_pem.pub .
while [ $? -ne 0 ]
do
sleep 1s
gsutil cp gs://$sshkey_bucket/lcm_pem.pub .
done
chown ubuntu:ubuntu lcm_pem.pub
cat lcm_pem.pub >> authorized_keys
pushd ~ubuntu/dse-gcp-install-$release
opsc_admin_pwd=''' + opsc_admin_pwd + '''
./addNode.py \
--opscpw $opsc_admin_pwd \
--opsc-ip $opsc_ip \
--clustername $cluster_name \
--dcname $data_center_name \
--rack $rack \
--pubip $private_ip \
--privip $private_ip \
--nodeid $node_id \
popd
'''
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-enterprise-ubuntu-1604-xenial-v20180824',
'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',
'bootDiskSizeGb': 20,
'metadata': {
'items': [
{
'key': 'startup-script',
'value': dse_node_script
}
]
}
}
}
opscenter_script = '''
#!/usr/bin/env bash
# If opscenter already installed, do nothing
dpkg -s opscenter &> /dev/null
retVal=$?
if [ $retVal -eq 0 ]; then
exit 0
fi
# Prepare for fresh OpsCenter installation
cd ~ubuntu
release="master"
gsutil cp gs://dse-gcp-marketplace/dse-gcp-install-$release.tar.gz .
tar -xvf dse-gcp-install-$release.tar.gz
# install extra OS packages, Java, and OpsCenter
pushd dse-gcp-install-$release
./extra_packages.sh
./install_java.sh -o
./installOpsc.sh
# Update password for default DSE OpsCenter administrator (admin)
opsc_admin_pwd=''' + opsc_admin_pwd + '''
./set_opsc_pw_https.sh $opsc_admin_pwd
popd
# Generate lcm_pem private and pubilc keys
pushd ~ubuntu/.ssh/
ssh-keygen -t rsa -N '' -f lcm_pem
chown ubuntu:ubuntu lcm_pem*
privkey=$(readlink -f ~ubuntu/.ssh/lcm_pem)
sshkey_bucket=''' + sshkey_bucket + '''
gsutil cp ./lcm_pem.pub gs://$sshkey_bucket/
popd
# Set up cluster in OpsCenter the LCM way
cd ~ubuntu/dse-gcp-install-$release
# Generate cluster name
cluster_name=''' + cluster_name + '''
# Generate number of DCs
num_dcs=''' + str(num_dcs) + '''
# Generate cluster size
cluster_size=''' + str(cluster_size) + '''
# DSE version
dse_version=''' + dse_version + '''
# Generate cassandra user's password
db_pwd=''' + db_pwd + '''
# Generate DataStax Academy credentials
dsa_username=''' + dsa_username + '''
dsa_password=''' + dsa_password + '''
# Retrieve OpsCenter's public IP address
private_ip=`echo $(hostname -I)`
sleep 1m
./setupCluster.py --user ubuntu --pause 60 --opscpw $opsc_admin_pwd --trys 40 --opsc-ip $private_ip --clustername $cluster_name --privkey $privkey --datapath /mnt/data1 --repouser $dsa_username --repopw $dsa_password --dbpasswd $db_pwd --dsever $dse_version
./triggerInstall.py --opsc-ip $private_ip --opscpw $opsc_admin_pwd --clustername $cluster_name --clustersize $cluster_size
./waitForJobs.py --num 1 --opsc-ip $private_ip --opscpw $opsc_admin_pwd
# Alter required keyspaces for multi-DC
./alterKeyspaces.py --opscpw $opsc_admin_pwd --delay 60 >> ../../repair.log &
# Remove public key from Google cloud storage bucket
gsutil rm gs://$sshkey_bucket/lcm_pem.pub
'''
opscenter_node_name = context.env['deployment'] + '-opscenter-vm'
opscenter_node = {
'name': opscenter_node_name,
'type': 'vm_instance.py',
'properties': {
'instanceName': opscenter_node_name,
'sourceImage': 'https://www.googleapis.com/compute/v1/projects/datastax-public/global/images/datastax-enterprise-ubuntu-1604-xenial-v20180824',
'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', 'https://www.googleapis.com/auth/devstorage.full_control']
}],
'metadata': {
'items': [
{
'key': 'startup-script',
'value': opscenter_script
}
]
}
}
}
config['resources'].append(zonal_clusters)
config['resources'].append(opscenter_node)
first_enterprise_node_name = context.env['deployment'] + '-' + context.properties['zones'][0] + '-1-vm'
outputs = [
{
'name': 'project',
'value': context.env['project']
},
{
'name': 'opsCenterNodeName',
'value': opscenter_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': opscenter_node_name
}
]
config['outputs'] = outputs
return yaml.dump(config)