-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrakefile
More file actions
69 lines (69 loc) · 3.18 KB
/
rakefile
File metadata and controls
69 lines (69 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
# variables
SERVER = ENV['SERVER']
DOMAIN = ENV['DOMAIN']
ENVIRONMENT = ENV['ENVIRONMENT'] # This is the puppet environment
REGION = ENV['REGION']
SIZE = ENV['SIZE'] # default should be 66 => 512M
IDTEMPLATE = ENV['IDTEMPLATE'] # should be 303619 for a i386 or 308287 for a amd64 debian image
SERVER_ROLE = ENV['SERVER_ROLE']
SCP = 'scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=quiet '
SSH = 'ssh -A -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=quiet -l root'
desc "Create certificate for droplets"
task :certificate_create do
puts "Creating puppet certificates for new server #{SERVER} ......"
sh "sudo puppet cert generate #{SERVER}"
sh "sudo tar -czvf #{SERVER}.tgz /var/lib/puppet/ssl/certs/#{SERVER}.pem /var/lib/puppet/ssl/certs/ca.pem /var/lib/puppet/ssl/private_keys/#{SERVER}.pem"
end
desc "Clean certificate from puppetmaster"
task :certificate_clean do
sh "rm -f #{SERVER}.tgz"
sh "sudo puppet node clean #{SERVER}"
sh "sudo rake -f /usr/share/puppet-dashboard/Rakefile RAILS_ENV=production node:del name=#{SERVER}"
end
desc "Creates a new droplet"
task :droplet_create do
sh "tugboat create #{SERVER} -i #{IDTEMPLATE} -r #{REGION} -k 32527 -s #{SIZE}"
# wait till droplet is in Active state
sh "tugboat wait -n #{SERVER} --state active"
TEMPIP=`tugboat info -n "#{SERVER}" |grep -m1 IP|awk {'print $2'}`
IP=TEMPIP.gsub(/\n/, "") #removing carriage return
puts "New deployed server's IP address: #{IP}"
puts "Sleeping 60 seconds to allow the VPS to boot"
sleep 60
end
task :droplet_configure do
puts "Deploying certificates ....... on #{IP}\:/#{SERVER}.tgz"
sh "#{SCP} #{SERVER}.tgz root@#{IP}\:/#{SERVER}.tgz"
sh "rm -f #{SERVER}.tgz"
commands = <<BOOTSTRAP
cd / && tar -xzvf #{SERVER}.tgz && rm #{SERVER}.tgz && \
echo search #{DOMAIN} >>/etc/resolv.conf && \
wget http://apt.puppetlabs.com/puppetlabs-release-jessie.deb && \
dpkg -i puppetlabs-release-jessie.deb && rm -f puppetlabs-release-jessie.deb \
apt-get update && apt-get dist-upgrade -y && \
apt-get -y install puppet augeas-tools && \
echo -e "set /files/etc/puppet/puppet.conf/main/pluginsync true\nset /files/etc/puppet/puppet.conf/main/environment #{ENVIRONMENT}\nsave" | augtool && \
echo " ==================== apt-get autoremove -y =============== " && \
apt-get autoremove -y && \
echo " ==================== puppet agent --enable =============== " && \
puppet agent --enable && \
echo " ==================== Local facts ========================= " && \
mkdir -p /etc/facter/facts.d && \
echo " ==================== Server Role ========================= " && \
echo '---' > /etc/facter/facts.d/server_role.yaml && \
echo "server_role: '#{SERVER_ROLE}'" >> /etc/facter/facts.d/server_role.yaml && \
puppet agent -t ; true
BOOTSTRAP
sh "#{SSH} #{IP} '#{commands}'"
end
desc "Destroy droplet"
task :droplet_destroy do
sh "tugboat destroy -n #{SERVER} -c"
end
desc "Create droplet and deploy puppet"
task :droplet_deploy => [:certificate_create, :droplet_create, :droplet_configure] do
puts "New server #{SERVER} with IP #{IP} created"
end
desc "Destroy droplet and clean certificates from puppet master"
task :droplet_decommission => [:certificate_clean, :droplet_destroy] do
end