
#!/usr/bin/ruby
require "rubygems"
require "json"
require "net/ssh"
require "net/scp"
# deploystuff.rb
# DevOps scripting exercise for Kixeye interview
#
# Matt Feenstra 916-914-4009
# matt.a.feenstra@gmail.com
# configuration parameters
#
# normally we wouldn't use the plaintext password, but would distribute a ssh key instead
artifact_ext = ".tar.gz"
username = "matt"
password = "abc"
source_dir = "/tmp"
tar = "tar -zxvf"
dest_dir = "/mnt/kixeye"
# get command line arg for build revision (should be a number)
# mild sanity check
buildvers = nil
if ARGV[0] =~ /\d+/ && ARGV[1] =~ /.+/ && ARGV[2] =~ /.+/ then
buildvers = ARGV[0]
configfile = ARGV[1]
artifact_dir = ARGV[2]
puts "build version is #{buildvers}\n"
else
puts "incorrect syntax: #{$0} #{ARGV[0]} #{ARGV[1]} #{ARGV[2]}\n"
abort "correct syntax: #{$0} <build #> <config file> <source folder>"
end
# read in the configuration json
file = open(configfile)
json = file.read
file.close
config = JSON.parse(json)
# loop through the config.json hash (config)
config.each do |key,value|
## artifact keyword loop (web, base, map)
artifact_name = key
config[artifact_name].each do |key2, value2|
if key2 =~ /hosts/
config[artifact_name][key2].each do |hostname|
filename = "#{artifact_name}-#{buildvers}#{artifact_ext}"
# commands: 1) move the tarball to the game directory
# 2) cd to game directory and untar (has build # on extraction dir)
# 3) remove old symbolic link and relink to the new files
# ^ this is to allow for easy rollback to previous version by changing symlink
#
command = "mv #{source_dir}/#{filename} #{dest_dir}",
"cd #{dest_dir}; #{tar} #{filename}",
"cd #{dest_dir}; rm -rf #{artifact_name}; ln -s #{artifact_name}-#{buildvers} #{artifact_name}"
puts "uploading #{filename} to #{hostname}.."
# scp files and install packages
ssh = Net::SSH.start(hostname, username, :password => password)
scp = ssh.scp
scp.upload!("#{artifact_dir}/#{filename}", "#{source_dir}")
puts "extracting #{filename} to #{dest_dir}..."
command.each do |cmd|
result = ssh.exec!(cmd)
puts result
end
ssh.close
puts "done.\n\n"
end # config[artifact_name][key2].each
end # if key2 =~ /hosts/
end # config[artifact_name].each
end # config.each
No comments:
Post a Comment