-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdeploy.rb
More file actions
507 lines (420 loc) · 15.1 KB
/
deploy.rb
File metadata and controls
507 lines (420 loc) · 15.1 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# deploy.rb
def createDomainDNS(domain,ip)
puts "Creating DNS entry for #{domain}"
new_domain = DropletKit::Domain.new(name: domain, ip_address: ip)
client = DropletKit::Client.new(access_token: @token)
client.domains.create(new_domain)
end
def createArecord(domain,name,ip)
puts "Creating A record for #{name} pointing to #{ip}"
client = DropletKit::Client.new(access_token: @token)
domain_record = DropletKit::DomainRecord.new(
type: 'A',
name: name,
data: ip
)
client.domain_records.create(domain_record, for_domain: @domain)
end
def deployMySQL(droplet_size)
sitename = "mysql.#{@domain}"
image_slug = 'ubuntu-14-04-x64'
mysql_pass = SecureRandom.hex
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get -y install mysql-server;
mysqladmin -u root create wordpress;
mysqladmin -u root password "#{mysql_pass}";
sed -i.bak "s/127.0.0.1/$PRIVATE_IP/g" /etc/mysql/my.cnf;
service mysql restart;
mysql -uroot -p#{mysql_pass} -e "CREATE USER 'wordpress'@'%' IDENTIFIED BY '#{mysql_pass}'";
mysql -uroot -p#{mysql_pass} -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpress'@'%'";
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating MySQL Server..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
# Create a DNS record for this node
puts " "
puts "MySQL Server Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
mysql_info = {"public_ip" => public_ip, "private_ip" => private_ip, "mysql_pass" => mysql_pass}
return mysql_info
end
def deployGluster(num_of_nodes,droplet_size,replica)
image_slug = 'ubuntu-14-04-x64'
sitename = "gluster1.#{@domain}"
gluster_nodes = Array.new
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get install -y python-software-properties;
add-apt-repository -y ppa:gluster/glusterfs-3.5;
apt-get update;
apt-get install -y glusterfs-server;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating GlusterFS Server #1..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
a_rec = {"name" => 'gluster1', "ip" => public_ip}
@a_records.push(a_rec)
puts " "
puts "GlusterFS Node 1 Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
gluster_node = {"public_ip" => public_ip, "private_ip" => private_ip}
gluster_nodes.push(gluster_node)
# First node created, now create any others that are required.
nct = 2
if num_of_nodes.to_i > 2
nn = num_of_nodes.to_i - 2
nn.times do
sitename = "gluster#{nct}.#{@domain}"
firstnode_ip = gluster_nodes[0]['private_ip']
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get install -y python-software-properties;
add-apt-repository -y ppa:gluster/glusterfs-3.5;
apt-get update;
apt-get install -y glusterfs-server;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating GlusterFS Server ##{nct}..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
puts " "
puts "GlusterFS Node #{nct} Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
gluster_node = {"public_ip" => public_ip, "private_ip" => private_ip}
a_rec = {"name" => "gluster#{nct}", "ip" => public_ip}
@a_records.push(a_rec)
gluster_nodes.push(gluster_node)
nct += 1
end
end
# Now create the final node and add our volume
sitename = "gluster#{nct}.#{@domain}"
gluster_peer_probes = ''
gluster_peers = ''
gluster_nodes.each {|node|
gluster_peer_probes += "gluster peer probe #{node["private_ip"]};"
gluster_peers += "#{node["private_ip"]}:/gluster "
}
if replica == 1
replica_conf = ''
else
replica_conf = "replica #{replica}"
end
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get install -y python-software-properties;
add-apt-repository -y ppa:gluster/glusterfs-3.5;
apt-get update;
apt-get install -y glusterfs-server;
sleep 30
#{gluster_peer_probes}
gluster volume create file_store #{replica_conf}transport tcp #{gluster_peers}$PRIVATE_IP:/gluster force;
gluster volume start file_store;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating GlusterFS Server ##{nct}..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
a_rec = {"name" => "gluster#{nct}", "ip" => public_ip}
@a_records.push(a_rec)
puts " "
puts "GlusterFS Node #{nct} Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
gluster_node = {"public_ip" => public_ip, "private_ip" => private_ip}
gluster_nodes.push(gluster_node)
nct += 1
gmount_ip = gluster_nodes[0]["private_ip"]
gmount = "#{gmount_ip}:/file_store"
gluster_info = {nodes: gluster_nodes,mount: gmount}
return gluster_info
end
def deployNginxWeb(num_of_nodes,gluster_mount,mysql_ip,droplet_size,mysql_pass)
web_servers = []
image_slug = 'ubuntu-14-04-x64'
sitename = "web1.#{@domain}"
# Create the first node and populate the gluster mount point with WP files.
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get -y install nginx glusterfs-client php5-fpm php5-mysql;
sed -i s/\;cgi\.fix_pathinfo\=1/cgi\.fix_pathinfo\=0/g /etc/php5/fpm/php.ini;
mkdir /gluster;
mount -t glusterfs #{gluster_mount} /gluster;
echo "#{gluster_mount} /gluster glusterfs defaults,_netdev 0 0" >> /etc/fstab;
mkdir /gluster/www;
wget https://raw.githubusercontent.com/ryanpq/do-wpc/master/default -O /etc/nginx/sites-enabled/default;
service nginx restart;
# Get Wordpress Files
wget https://wordpress.org/latest.tar.gz -O /root/wp.tar.gz;
tar -zxf /root/wp.tar.gz -C /root/;
cp -Rf /root/wordpress/* /gluster/www/.;
cp /gluster/www/wp-config-sample.php /gluster/www/wp-config.php;
sed -i "s/'DB_NAME', 'database_name_here'/'DB_NAME', 'wordpress'/g" /gluster/www/wp-config.php;
sed -i "s/'DB_USER', 'username_here'/'DB_USER', 'wordpress'/g" /gluster/www/wp-config.php;
sed -i "s/'DB_PASSWORD', 'password_here'/'DB_PASSWORD', '#{mysql_pass}'/g" /gluster/www/wp-config.php;
sed -i "s/'DB_HOST', 'localhost'/'DB_HOST', '#{mysql_ip}'/g" /gluster/www/wp-config.php;
chown -Rf www-data:www-data /gluster/www;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating Nginx Web Server #1..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
a_rec = {"name" => "web1", "ip" => public_ip}
@a_records.push(a_rec)
# Create a DNS record for this node
puts " "
puts "Web Server #1 Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
ws = {'public_ip' => public_ip,"private_ip" => private_ip}
web_servers.push(ws)
# Now create the rest of the web nodes
if num_of_nodes.to_i > 1
nct = 2
nd = num_of_nodes.to_i - 1
nd.times do
sitename = "web#{nct}.#{@domain}"
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get -y install nginx glusterfs-client php5-fpm php5-mysql;
sed -i s/\;cgi\.fix_pathinfo\=1/cgi\.fix_pathinfo\=0/g /etc/php5/fpm/php.ini;
mkdir /gluster;
mount -t glusterfs #{gluster_mount} /gluster;
echo "#{gluster_mount} /gluster glusterfs defaults,_netdev 0 0" >> /etc/fstab;
mkdir /gluster/www;
wget https://raw.githubusercontent.com/ryanpq/do-wpc/master/default -O /etc/nginx/sites-enabled/default;
service nginx restart;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating Nginx Web Server ##{nct}..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
a_rec = {"name" => "web#{nct}", "ip" => public_ip}
@a_records.push(a_rec)
# Create a DNS record for this node
puts " "
puts "Web Server ##{nct} Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
ws = {'public_ip' => public_ip,"private_ip" => private_ip}
web_servers.push(ws)
nct += 1
end
return web_servers
end
end
def deployNginxLB(web_servers,droplet_size)
image_slug = 'ubuntu-14-04-x64'
sitename = "www.#{@domain}"
backends = ''
web_servers.each {|server|
p_ip = server["private_ip"]
backends += "server #{p_ip};\n"
}
userdata = <<-EOM
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive;
export PUBLIC_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
apt-get update;
apt-get -y install nginx;
lbconf="
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
proxy_pass http://backend;
include proxy_params;
}
}
upstream backend {
ip_hash;
#{backends}
}
"
echo $lbconf > /etc/nginx/sites-enabled/default;
service nginx restart;
EOM
client = DropletKit::Client.new(access_token: @token)
droplet = DropletKit::Droplet.new(name: sitename, region: @region, size: droplet_size, image: image_slug, user_data: userdata, ssh_keys: @ssh_keys, private_networking: true)
create = client.droplets.create(droplet)
createid = create.id.to_s
puts " "
print "Creating Load Balancer..."
create_complete = 0
while create_complete != 1 do
print "."
dobj = client.droplets.find(id: createid)
if dobj.status == 'active'
create_complete = 1
else
print "."
end
sleep(5)
end
if dobj.networks.v4[0].type == 'private'
private_ip = dobj.networks.v4[0].ip_address
public_ip = dobj.networks.v4[1].ip_address
else
private_ip = dobj.networks.v4[1].ip_address
public_ip = dobj.networks.v4[0].ip_address
end
# Create a DNS record for this node
puts " "
puts "Load Balancer Creation Complete."
puts "Private IP: #{private_ip}"
puts "Public IP: #{public_ip}"
return public_ip
end