forked from riptano/ComboAMI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds1_launcher.py
executable file
·225 lines (179 loc) · 8.04 KB
/
ds1_launcher.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
#!/usr/bin/env python
### Script provided by DataStax.
import grp
import os
import pwd
import subprocess
import shlex
import time
import urllib2
import logger
import conf
def initial_configurations():
# Begin configuration this is only run once in Public Packages
if conf.get_config("AMI", "CurrentStatus") != "Complete!":
# Configure DataStax variables
try:
import ds2_configure
ds2_configure.run()
except:
conf.set_config("AMI", "Error", "Exception seen in %s. Please check ~/datastax_ami/ami.log for more info." % 'ds1_launcher.py')
logger.exception('ds1_launcher.py')
# Set ulimit hard limits
logger.pipe('echo "* soft nofile 32768"', 'sudo tee -a /etc/security/limits.conf')
logger.pipe('echo "* hard nofile 32768"', 'sudo tee -a /etc/security/limits.conf')
logger.pipe('echo "root soft nofile 32768"', 'sudo tee -a /etc/security/limits.conf')
logger.pipe('echo "root hard nofile 32768"', 'sudo tee -a /etc/security/limits.conf')
# Change permission back to being ubuntu's and cassandra's
logger.exe('sudo chown -hR ubuntu:ubuntu /home/ubuntu')
logger.exe('sudo chown -hR cassandra:cassandra /raid0/cassandra', False)
logger.exe('sudo chown -hR cassandra:cassandra /mnt/cassandra', False)
# Ensure permissions
directory_list = [
('/home/ubuntu', 'ubuntu', 'ubuntu'),
('/raid0/cassandra', 'cassandra', 'cassandra'),
('/mnt/cassandra', 'cassandra', 'cassandra')
]
for directory in directory_list:
if os.path.isdir(directory[0]):
logger.info('Checking permissions for: %s' % directory[0])
attempt = 0
max_attempts = 10
permissions_set = False
while attempt < max_attempts:
logger.info('Attempt #%s' % attempt)
stat_info = os.stat(directory[0])
uid = stat_info.st_uid
gid = stat_info.st_gid
user = pwd.getpwuid(uid)[0]
group = grp.getgrgid(gid)[0]
if user == directory[1] and group == directory[2]:
permissions_set = True
break
attempt += 1
time.sleep(1)
if not permissions_set:
logger.warn('Permissions not set correctly. Please run manually:')
logger.warn('sudo chown -hR %s:%s %s' % (directory[1], directory[2], directory[0]))
logger.warn('sudo service dse restart')
else:
logger.info('Permissions set for %s as %s:%s' % (directory[0], user, group))
else:
logger.info('Skipping initial configurations.')
def write_bin_tools():
with open('/usr/bin/datastax_support', 'w') as f:
f.write("""#!/usr/bin/env python\nprint '''DataStax Support Links:
Cassandra Cluster Launcher:
https://github.com/joaquincasares/cassandralauncher
Documentation:
http://www.datastax.com/docs
AMI:
http://www.datastax.com/ami
Cassandra client libraries:
http://www.datastax.com/download/clientdrivers
Support Forums:
http://www.datastax.com/support-forums
For quick support, visit:
IRC: #cassandra channel on irc.freenode.net
'''
""")
with open('/usr/bin/datastax_demos', 'w') as f:
f.write("""#!/usr/bin/env python\nprint '''DataStax Demo Links:
Portfolio (Hive) Demo:
http://www.datastax.com/demos/portfolio
Pig Demo:
http://www.datastax.com/demos/pig
Wikipedia (Solr) Demo:
http://www.datastax.com/demos/wikipedia
Logging (Solr) Demo:
http://www.datastax.com/demos/logging
Sqoop Demo:
http://www.datastax.com/demos/sqoop
'''
""")
with open('/usr/bin/datastax_tools', 'w') as f:
f.write("""#!/usr/bin/env python\nprint '''Installed DSE/C Tools:
Nodetool:
nodetool --help
DSEtool:
dsetool --help
Cli:
cassandra-cli -h `hostname`
CQL Shell:
cqlsh
Hive:
dse hive (on Analytic nodes)
Pig:
dse pig (on Analytic nodes)
'''
""")
os.chmod('/usr/bin/datastax_support', 0755)
os.chmod('/usr/bin/datastax_demos', 0755)
os.chmod('/usr/bin/datastax_tools', 0755)
def restart_tasks():
logger.info("AMI Type: " + str(conf.get_config("AMI", "Type")))
# Create /raid0
logger.exe('sudo mount -a')
# Disable swap
logger.exe('sudo swapoff --all')
# Ensure the correct blockdev readahead since this sometimes resets after restarts
if conf.get_config('AMI', 'raid_readahead'):
logger.exe('sudo blockdev --setra %s /dev/md0' % (conf.get_config('AMI', 'raid_readahead')), expectError=True)
def wait_for_seed():
# Wait for the seed node to come online
req = urllib2.Request('http://169.254.169.254/latest/meta-data/local-ipv4')
internalip = urllib2.urlopen(req).read()
if internalip != conf.get_config("AMI", "LeadingSeed"):
logger.info("Waiting for seed node to come online...")
nodetoolStatement = "nodetool -h " + conf.get_config("AMI", "LeadingSeed") + " ring"
logger.info(nodetoolStatement)
while True:
nodetool_out = subprocess.Popen(shlex.split(nodetoolStatement), stderr=subprocess.PIPE, stdout=subprocess.PIPE).stdout.read()
# get rid of substring included in new jvm options printout that was causing an infinite loop
nodetool_out = nodetool_out.replace('+HeapDumpOnOutOfMemoryError', '')
if (nodetool_out.lower().find("error") == -1 and nodetool_out.lower().find("up") and len(nodetool_out) > 0):
logger.info("Seed node now online!")
time.sleep(5)
break
time.sleep(5)
logger.info("Retrying seed node...")
def launch_opscenter():
logger.info('Starting a background process to start OpsCenter after a given delay...')
subprocess.Popen(shlex.split('sudo -u ubuntu python /home/ubuntu/datastax_ami/ds3_after_init.py &'))
def start_services():
# Wait for system setup changes to settle
time.sleep(5)
# Actually start the application
if conf.get_config("AMI", "Type") == "Community" or conf.get_config("AMI", "Type") == "False":
logger.info('Starting DataStax Community...')
logger.exe('sudo service cassandra restart')
elif conf.get_config("AMI", "Type") == "Enterprise":
logger.info('Starting DataStax Enterprise...')
logger.exe('sudo service dse restart')
# Ensure that cassandra doesn't die shortly after first boot
# I've seen issues with a device not being available... but this is after the full raid
if not conf.get_config("AMI", "CompletedFirstBoot"):
start_time = time.time()
logger.info('Checking for 15 seconds to ensure Cassandra stays up...')
while time.time() - start_time < 15:
cassandra_running = False
if not logger.exe('nodetool info', expectError=True)[1]:
cassandra_running = True
if not cassandra_running:
# Restart the application
if conf.get_config("AMI", "Type") == "Community" or conf.get_config("AMI", "Type") == "False":
logger.info('Restarting DataStax Community...')
logger.exe('sudo service cassandra restart')
elif conf.get_config("AMI", "Type") == "Enterprise":
logger.info('Restarting DataStax Enterprise...')
logger.exe('sudo service dse restart')
time.sleep(3)
time.sleep(1)
def run():
initial_configurations()
write_bin_tools()
restart_tasks()
if conf.get_config("AMI", "LeadingSeed"):
wait_for_seed()
launch_opscenter()
start_services()