-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_env.py
executable file
·278 lines (242 loc) · 10.9 KB
/
create_env.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
#!/usr/bin/python3.4
from boto import vpc
import time
amazon='ami-52978200'
ubuntu='ami-96f1c1c4'
redhat='ami-dc1c2b8e'
nat ='ami-1a9dac48'
PUPPET_INSTANCE = 't2.micro'
HADOOP_INSTANCE = 't2.large' # Mapr needs 4GB, and the OS needs some, so t2.medium isn't enough
REGION = 'ap-southeast-1'
IMAGE = ubuntu # Basic 64-bit Ubuntu AMI
KEY_NAME = 'my-ec2-key'
INSTANCE_TYPE = 't2.micro'
#ZONE = 'ap-southeast-1a' # Availability zone
SECURITY_GROUPS = ['Hadoop']
PROJECT = 'Hadoop1'
PUPPET_NAME = "puppetmaster"
PUPPET_USER_DATA = """#!/bin/bash
cat > /usr/local/sbin/configure-pat.sh << EOF
#!/bin/bash
# Configure the instance to run as a Port Address Translator (PAT) to provide
# Internet connectivity to private instances.
function log { logger -t "vpc" -- \$1; }
function die {
[ -n "\$1" ] && log "\$1"
log "Configuration of PAT failed!"
exit 1
}
# Sanitize PATH
PATH="/usr/sbin:/sbin:/usr/bin:/bin"
log "Determining the MAC address on eth0..."
ETH0_MAC=\$(cat /sys/class/net/eth0/address) ||
die "Unable to determine MAC address on eth0."
log "Found MAC \${ETH0_MAC} for eth0."
VPC_CIDR_URI="http://169.254.169.254/latest/meta-data/network/interfaces/macs/\${ETH0_MAC}/vpc-ipv4-cidr-block"
log "Metadata location for vpc ipv4 range: \${VPC_CIDR_URI}"
VPC_CIDR_RANGE=\$(curl --retry 3 --silent --fail \${VPC_CIDR_URI})
if [ \$? -ne 0 ]; then
log "Unable to retrive VPC CIDR range from meta-data, using 0.0.0.0/0 instead. PAT may masquerade traffic for Internet hosts!"
VPC_CIDR_RANGE="0.0.0.0/0"
else
log "Retrieved VPC CIDR range \${VPC_CIDR_RANGE} from meta-data."
fi
log "Enabling PAT..."
sysctl -q -w net.ipv4.ip_forward=1 net.ipv4.conf.eth0.send_redirects=0 && (
iptables -t nat -C POSTROUTING -o eth0 -s \${VPC_CIDR_RANGE} -j MASQUERADE 2> /dev/null ||
iptables -t nat -A POSTROUTING -o eth0 -s \${VPC_CIDR_RANGE} -j MASQUERADE ) ||
die
sysctl net.ipv4.ip_forward net.ipv4.conf.eth0.send_redirects | log
iptables -n -t nat -L POSTROUTING | log
log "Configuration of PAT complete."
exit 0
EOF
wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb -O /tmp/puppetlabs-release-trusty.deb
dpkg -i /tmp/puppetlabs-release-trusty.deb
chmod u+x /usr/local/sbin/configure-pat.sh
sed -ie 's/^exit 0/apt-get update\\n# Configure PAT\\n\/usr\/local\/sbin\/configure-pat.sh\\nexit 0/g' /etc/rc.local
/etc/rc.local
cat > /tmp/hiera.yaml << EOF
---
:backends:
- json
:json:
:datadir: /etc/puppet/data
:hierarchy:
- "hosts/%{fqdn}"
- "role/%{role}"
- common
EOF
cat > /tmp/hadoop-node.json << EOF
{
"classes": [ "mapr4::zookeeper", "mapr4::cldb", "mapr4::webserver", "mapr4::jobtracker", "mapr4::resourcemanager", "mapr4::nodemanager", "mapr4::historyserver", "mapr4::gateway", "java" ],
"java::distribution": "jdk",
"mapr4::mapr_subnets" : "PUT_HERE_THE_BE_SUBNET",
"mapr4::mapr_cldb": "127.0.0.1",
"mapr4::mapr_zookeeper": "127.0.0.1",
"mapr4::mapr_gid": "5000",
"mapr4::mapr_uid": "5000",
"mapr4::mapr_pass": "mapr",
"mapr4::core::disks": "disks-pythian.txt"
}
EOF
cat > /tmp/common.json << EOF
{
"classes": [ "facts" ]
}
EOF
echo PUT_HERE_THE_SERVER_NAME > /etc/hostname
echo PUT_HERE_THE_PUPPET_MASTER_IP PUT_HERE_THE_PUPPET_MASTER_NAME >> /etc/hosts
export DEBIAN_FRONTEND=noninteractive
#apt-get dist-upgrade -y && \\
apt-get install -y joe puppet git puppetmaster whois && \\
mv /tmp/hiera.yaml /etc/puppet/hiera.yaml && \\
rm /etc/hiera.yaml && \\
ln -s /etc/puppet/hiera.yaml /etc/hiera.yaml && \\
sed -ie 's/\[master\]/\[master\]\\nautosign = true/g' /etc/puppet/puppet.conf && \\
sed -ie 's/START=no/START=yes/g' /etc/default/puppet && \\
sed -ie 's/^templatedir=/#templatedir/g' /etc/puppet/puppet.conf && \\
#puppet module install ersiko-mapr4 && \\
git clone https://github.com/ersiko/mapr4-puppet-module.git && \\
mv mapr4-puppet-module /etc/puppet/modules/mapr4 && \\
puppet module install ersiko-facts && \\
puppet module install puppetlabs-java && \\
mkdir -p /etc/puppet/data/role && \\
mv /tmp/hadoop-node.json /etc/puppet/data/role && \\
mv /tmp/common.json /etc/puppet/data/ && \\
chown puppet /etc/puppet/data && \\
echo "hiera_include('classes')" >> /etc/puppet/manifests/site.pp && \\
reboot
"""
HADOOP_NAME = "hadoop-node"
HADOOP_USER_DATA = """#!/bin/bash
wget https://apt.puppetlabs.com/puppetlabs-release-trusty.deb -O /tmp/puppetlabs-release-trusty.deb
dpkg -i /tmp/puppetlabs-release-trusty.deb
sed -ie 's/^exit 0/apt-get update\\nexit 0/g' /etc/rc.local
/etc/rc.local
echo PUT_HERE_THE_SERVER_NAME > /etc/hostname
echo PUT_HERE_THE_PUPPET_MASTER_IP PUT_HERE_THE_PUPPET_MASTER_NAME >> /etc/hosts
export DEBIAN_FRONTEND=noninteractive
while [ ! -e /etc/puppet/puppet.conf ];do apt-get update && \\
#apt-get dist-upgrade -y && \\
apt-get install -y joe puppet git puppet;done
sed -ie 's/\[main\]/\[main\]\\nserver=PUT_HERE_THE_PUPPET_MASTER_NAME\\nruninterval=30/g' /etc/puppet/puppet.conf && \\
sed -ie 's/START=no/START=yes/g' /etc/default/puppet && \\
sed -ie 's/^templatedir=/#templatedir/g' /etc/puppet/puppet.conf && \\
sed -ie 's/^127.0.0.1 localhost/127.0.0.1 localhost PUT_HERE_THE_SERVER_NAME/g' /etc/hosts && \\
cat > /tmp/integers << EOF
0
1
2
3
4
5
6
7
8
9
EOF
cat > /tmp/hive-load.sh << EOF
hadoop fs -put /tmp/integers /
hive -e "create table test_table (test STRING);load data inpath '/integers' into table test_table;"
EOF
chmod a+x /tmp/hive-load.sh
cat > /tmp/hive-test.sh << EOF
hive -e "select * from test_table;select count(*) from test_table;"
EOF
chmod a+x /tmp/hive-test.sh
apt-get -y install mapr-hive
reboot
"""
def launch_instance(VPC_CON,INS_NAME,INS_USER_DATA,AMOUNT,INS_IMAGE=IMAGE,INS_TYPE=INSTANCE_TYPE,INS_KEY_NAME=KEY_NAME,INS_SECGROUPS=[],INS_SUBNET="",INS_PROJECT=PROJECT,PUPPET_MASTER_IP='127.0.0.1'):
created_instances=[]
for number in range(AMOUNT):
SERVER_NAME = INS_NAME + str(number+1).zfill(2)
USER_DATA_SERVERNAME = INS_USER_DATA.replace("PUT_HERE_THE_SERVER_NAME", SERVER_NAME) #I'm not proud of this dirty trick I frequently use on my bash scripting, but it's handy. Sorry!
USER_DATA_SERVERNAME = USER_DATA_SERVERNAME.replace("PUT_HERE_THE_PUPPET_MASTER_IP", PUPPET_MASTER_IP) # Ditto
USER_DATA_SERVERNAME = USER_DATA_SERVERNAME.replace("PUT_HERE_THE_PUPPET_MASTER_NAME", PUPPET_NAME + '01.' + REGION + '.compute.internal') # Ditto
USER_DATA_SERVERNAME = USER_DATA_SERVERNAME.replace("PUT_HERE_THE_BE_SUBNET", subnetbe.cidr_block)
print("Creating " + SERVER_NAME + " with user_data " + USER_DATA_SERVERNAME)
reservation = VPC_CON.run_instances(image_id =INS_IMAGE,
instance_type =INS_TYPE,
key_name =INS_KEY_NAME,
security_group_ids=INS_SECGROUPS,
user_data =USER_DATA_SERVERNAME,
subnet_id =INS_SUBNET.id)
time.sleep(3)
instance=reservation.instances[0]
instance.add_tag("Project", INS_PROJECT)
instance.add_tag("Name", SERVER_NAME)
created_instances.append(instance)
return created_instances
#CREATING VPC
print("Connecting to AWS")
vpc_con = vpc.connect_to_region("ap-southeast-1")
print("Creating VPC")
my_vpc = vpc_con.create_vpc('10.0.0.0/16')
vpc_con.modify_vpc_attribute(my_vpc.id, enable_dns_support=True)
vpc_con.modify_vpc_attribute(my_vpc.id, enable_dns_hostnames=True)
print("Tagging VPC")
my_vpc.add_tag("Name",PROJECT+"-VPC")
my_vpc.add_tag("Project",PROJECT)
print("Creating subnets")
subnetdmz = vpc_con.create_subnet(my_vpc.id,'10.0.1.0/24')
subnetbe = vpc_con.create_subnet(my_vpc.id,'10.0.2.0/24')
print("Tagging subnet")
subnetdmz.add_tag("Name",PROJECT+"-Subnet")
subnetdmz.add_tag("Project",PROJECT)
subnetbe.add_tag("Name",PROJECT+"-Subnet")
subnetbe.add_tag("Project",PROJECT)
print("Creating internet gateway")
gateway = vpc_con.create_internet_gateway()
gateway.add_tag("Project",PROJECT)
print("Attaching gateway to VPC")
vpc_con.attach_internet_gateway(gateway.id, my_vpc.id)
print("Creating route table")
route_table = vpc_con.create_route_table(my_vpc.id)
route_table.add_tag("Project",PROJECT)
print("Associating route table to subnet")
vpc_con.associate_route_table(route_table.id, subnetdmz.id)
print("Create route to the internet")
route = vpc_con.create_route(route_table.id, '0.0.0.0/0', gateway.id)
print("Creating Security group")
secgroup = vpc_con.create_security_group(PROJECT+'_group','A security_group for Hadoop', my_vpc.id)
print("Opening port 22 and other stuff in the secgroup ")
secgroup.authorize(ip_protocol='tcp', from_port=22, to_port=22, cidr_ip='0.0.0.0/0')
secgroup.authorize(ip_protocol='tcp', from_port=0, to_port=65535, cidr_ip='10.0.0.0/16')
secgroup.authorize(ip_protocol='icmp', from_port=-1, to_port=-1, cidr_ip='10.0.0.0/16')
secgroup.add_tag("Project",PROJECT)
print("Creating puppetmaster instance in VPC")
puppetmaster=launch_instance(AMOUNT=1,VPC_CON=vpc_con,INS_NAME=PUPPET_NAME,INS_USER_DATA=PUPPET_USER_DATA,INS_SECGROUPS=[secgroup.id],INS_SUBNET=subnetdmz)
while puppetmaster[0].update() != "running":
time.sleep(5)
print ("Waiting for puppetmaster to start")
print("Setting puppetmaster as NAT for private subnet")
vpc_con.modify_instance_attribute(puppetmaster[0].id,'sourceDestCheck',False)
default_route_table = vpc_con.get_all_route_tables(filters=({'vpc-id': my_vpc.id, 'association.main': 'true'}))
default_route_table[0].add_tag("Project",PROJECT)
natroute = vpc_con.create_route(default_route_table[0].id, '0.0.0.0/0', instance_id=puppetmaster[0].id)
print("Creating hadoop node instance(s) in VPC")
hadoop=launch_instance(AMOUNT=1,
VPC_CON=vpc_con,
INS_NAME=HADOOP_NAME,
INS_USER_DATA=HADOOP_USER_DATA,
INS_SECGROUPS=[secgroup.id],
INS_SUBNET=subnetbe,
INS_TYPE=HADOOP_INSTANCE,
PUPPET_MASTER_IP=puppetmaster[0].private_ip_address)
print("Creating volume for hadoop node")
my_vol=vpc_con.create_volume(10,hadoop[0].placement,volume_type='standard')
my_vol.add_tag("Project",PROJECT
)
while hadoop[0].update() != "running":
time.sleep(5)
print ("Waiting for hadoop node to start")
print("Attaching volume to hadoop node")
my_vol.attach(hadoop[0].id,'xvdf')
print("Creating elasticip")
elasticip = vpc_con.allocate_address(domain='vpc')
print("Associating elasticip to puppetmaster instance")
vpc_con.associate_address(instance_id=puppetmaster[0].id, allocation_id=elasticip.allocation_id)
print("ssh ubuntu@" + elasticip.public_ip + " -o \"StrictHostKeyChecking no\" -i my-ec2-key.pem -L 2222:" + hadoop[0].private_ip_address + ":22 -L 8443:" + hadoop[0].private_ip_address + ":8443;ssh-keygen -f ~/.ssh/known_hosts -R "+ elasticip.public_ip)
print("ssh-keygen -f ~/.ssh/known_hosts -R [localhost]:2222;ssh -o \"StrictHostKeyChecking no\" ubuntu@localhost -p 2222 -i my-ec2-key.pem")