This repository was archived by the owner on Sep 20, 2020. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathbuild_node.rb
More file actions
executable file
·104 lines (84 loc) · 3.18 KB
/
Copy pathbuild_node.rb
File metadata and controls
executable file
·104 lines (84 loc) · 3.18 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
#!/usr/bin/ruby
require 'rubygems'
require 'fog'
require 'erubis'
# Credentials... this needs to be abstracted away into a config file for security
# We're using EC2. This should easily be extractable into another IaaS provider
# As long as fog supports it.
require 'lib/dns.rb'
require '~/.hackday/env_config.rb'
# fog api object instantiation. We'll pass these guys into things later.
ec2 = Fog::Compute.new({
:provider => 'AWS',
:aws_access_key_id => ACCESS_KEY_ID,
:aws_secret_access_key => SECRET_ACCESS_KEY
})
dynect = Fog::DNS.new({
:provider => 'Dynect',
:dynect_customer => DYNECT_CUSTOMER,
:dynect_username => DYNECT_USERNAME,
:dynect_password => DYNECT_PASSWORD
})
#puts ARGV[1]
#puts ARGV[2]
#puts ARGV.length
#puts ARGV[1] <= ARGV[2]
# Get args. Need hostnames of boxes to launch. First is chef server, second is
# starting node number, third is ending node number.
# Important to get these right, or we'll clobber dns as we go.
#unless ARGV.length == 3 && ARGV[1] <= ARGV[2]
# puts "Usage: setup_env.rb chef_server_instance_id X Y"
# puts "X = start node number"
# puts "Y = end node number"
# puts "This will create a chef server node, chef N"
# puts "and hackX.dtolabs.com through hackY.dtolabs.com"
# exit
#end
chefnode = ARGV[0]
nodeX = ARGV[1].to_i
nodeY = ARGV[2].to_i
number_of_nodes = nodeY - nodeX + 1
# Use default security group, it's ready to go. Otherwise, we'd have to open it up.
# Who is our chef server
chef = ec2.servers.get(chefnode)
chef.username = AWS_USERNAME
chef.private_key_path = AWS_PATH_TO_KEY
validator = chef.ssh('sudo cat /etc/chef/validation.pem').first
chef_server_hostname = chef.ssh('hostname').first.stdout.chomp
chef_server_ec2_hostname = chef.dns_name
# Prep the userdata file
hostname = "hack#{nodeX}"
fqdn = "hack#{nodeX}.dtolabs.com"
userdata_file = File.read('bootstrap_chef_client.sh.erb')
eruby = Erubis::Eruby.new(userdata_file)
userdata = eruby.result(:hostname => hostname, :fqdn => fqdn, :target_server => chef_server_ec2_hostname)
node = ec2.servers.create(:image_id => @ami,
:key_name => @key_name,
:flavor_id => @instance_size,
:user_data => userdata
)
print "Launching #{fqdn}: "
node.wait_for{ ready? }
puts "---------------------------------------------"
puts "Chef client instance id: " + node.id
puts "Chef client IP: #{node.public_ip_address}"
puts "Chef client ec2 DNS: #{node.dns_name}"
# Set up knife and upload cookbooks
# Run chef-client on chef server
# This step done w/ userdata. That bit works well.
# Do DNS data for server:
# Set up ssh creds:
node.username = AWS_USERNAME
node.private_key_path = AWS_PATH_TO_KEY
set_cname(dynect, DYNECT_ZONE, fqdn, node.dns_name)
# Wait 60 seconds for the box to boot... important
sleep(60)
node.ssh("touch /tmp/fog_was_here")
output = ""
node.wait_for{ node.ssh("ls /home/ubuntu/.chef").first.status == 0 }
sleep(60)
puts "Node up, chef client is installed"
puts node.ssh("mkdir /home/ubuntu/.chef").first.stdout
node.scp("tmp/#{chef_server_hostname}.validation.pem", '/home/ubuntu/.chef/validation.pem')
node.ssh("sudo cp /home/ubuntu/.chef/validation.pem /etc/chef/validation.pem")
node.ssh("sudo chef-client")