dependencies: an ssh key and environment variables, requires >= ruby1.9.3 for JSON
rake getvmip injectssl hcserverversion chefdone webready injectphantomjs
rake getamiip hcserverversion chefdone webready injectphantomjs
require 'json' require 'rake'
-i /home/ubuntu/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no
if !ENV['bamboo_ssh_opts'] puts "ERROR: bamboo_ssh_opts is not set" exit 1 else bamboo_ssh_opts = ENV['bamboo_ssh_opts'] end
bamboo_vmbuilderhost = ENV['bamboo_vmbuilderhost'] bamboo_vmname = ENV['bamboo_vmname'] bamboo_ipfile = ENV['bamboo_ipfile'] bamboo_fqdnfile = ENV['bamboo_fqdnfile'] bamboo_shenvfile = ENV['bamboo_shenvfile'] bamboo_instance = ENV['bamboo_instancejson'] bamboo_instance_initialized = ENV['bamboo_instanceinitializedjson']
vmip = ENV['bamboo_vmip'] || '' vmfqdn = '' vmhost = ENV['bamboo_vmhost'] || '' LOGFILE = "./myrakedebug.log"
def write_file(filename, content, log=true, echo=true) if echo puts content end File.open("#{filename}", 'w') {|f| f.write(content)} if log File.open("#{LOGFILE}", 'a') {|f| f.write( Time.now.strftime("%Y-%m-%d--%H-%M") + content + "\n")} end end
task :default do puts "bamboo_ssh_opts: #{bamboo_ssh_opts}" puts "bamboo_vmbuilderhost: #{bamboo_vmbuilderhost}" puts "bamboo_vmname: #{bamboo_vmname}" puts "bamboo_ipfile: #{bamboo_ipfile}" puts "bamboo_fdqnfile: #{bamboo_fdqnfile}" puts "bamboo_shenvfile: #{bamboo_shenvfile}" puts "bamboo_instancejson: #{bamboo_instance}" puts "bamboo_instanceinitializedjson: #{bamboo_instance_initialized}" end
task :my_task, [:arg1, :arg2] do |t, args| puts "Args were: #{args}" end
command line: rake my_task[1,2]
http://stackoverflow.com/questions/825748/how-do-i-pass-command-line-arguments-to-a-rake-task
task :invoke_my_task do Rake.application.invoke_task("my_task[1, 2]") end
task :getamiip do unless (bamboo_ssh_opts and bamboo_instance and bamboo_instance_initialized and bamboo_vmname and bamboo_ipfile and bamboo_fqdnfile) puts "ERROR: a required variable is not set" exit 1 end data_hash = JSON.parse(File.read("#{bamboo_instance}")) puts data_hash instance_id = data_hash["Instances"][0]["InstanceId"] puts "InstanceID=#{instance_id}" puts "waiting for instance to initialize before getting ip address" i = 0 retrymax = 60 loop do puts "attempt #{i} at instance running or wait for 10 seconds" i += 1 sh "/usr/local/bin/aws ec2 describe-instances --instance-ids #{instance_id} > #{bamboo_instance_initialized}" data_hash = JSON.parse(File.read("#{bamboo_instance_initialized}")) finished = data_hash["Reservations"][0]["Instances"][0]["State"]["Name"] puts "received: #{finished}" break if (i >= retrymax || finished == "running") sleep(10) end puts data_hash vmip = data_hash["Reservations"][0]["Instances"][0]["PublicIpAddress"] if !vmip puts "ERROR: need the VM IP address" exit 1 end write_file("#{bamboo_ipfile}", vmip) vmfqdn = data_hash["Reservations"][0]["Instances"][0]["PublicDnsName"] if !vmfqdn puts "ERROR: need the EC2 Public FQDN" exit 1 end puts vmfqdn write_file("#{bamboo_fqdnfile}", vmfqdn, true) vmhost = "admin@#{vmip.strip}" puts vmhost end
task :getvmip do unless (bamboo_ssh_opts and bamboo_vmbuilderhost and bamboo_vmname and bamboo_ipfile and bamboo_fqdnfile) puts "ERROR: a required variable is not set" exit 1 end sh "ssh #{bamboo_ssh_opts} #{bamboo_vmbuilderhost} 'cd btf; git pull'" result = sh "ssh #{bamboo_ssh_opts} #{bamboo_vmbuilderhost} python /home/bamboo/btf/test/vm_ip.py #{bamboo_vmname} datastore1 > ./#{bamboo_ipfile} || exit 1" file = File.open("#{bamboo_ipfile}", "rb") vmip = file.read ip_a = vmip.split(".") puts vmip if !vmip puts "ERROR: need the VM IP address" exit 1 end vmfqdn = "dhcp#{ip_a.last.strip}.example.com" puts vmfqdn write_file("#{bamboo_fqdnfile}", vmfqdn, true) vmhost = "admin@#{vmip.strip}" puts vmhost end
task :injectssl do unless (bamboo_ssh_opts and vmhost and vmfqdn) puts "ERROR: a required variable is not set" exit 1 end puts "Start SCP to #{vmhost}: send ssl.pem" # assumes btf/test/bamboo/Rakefile sh "scp #{bamboo_ssh_opts} ../CA/#{vmfqdn}.pem #{vmhost}:/tmp || exit 1" end
task :chefdone do
unless (bamboo_ssh_opts and vmhost)
puts "ERROR: a required variable is not set"
exit 1
end
i = 0
retrymax = 60
loop do
puts "attempt #{i} at checking if chef.log finished or waiting 10 seconds"
i += 1
finished = ssh #{bamboo_ssh_opts} #{vmhost} "cat /var/log/chef.log | grep \'Starting barb\'"
puts finished
break if (i >= retrymax || !finished.empty?)
sleep(10)
end
end
task :webready do
unless (bamboo_ssh_opts and vmip)
puts "ERROR: a required variable is not set"
exit 1
end
i = 0
retrymax = 60
loop do
puts "attempt #{i} at waiting 10 seconds for curl to receive an OK 200 response"
i += 1
finished = curl -L --silent --max-time 1 -o /dev/null --insecure -I -w "%{http_code}" --insecure https://#{vmip}
puts "received: #{finished}"
break if (i >= retrymax || finished == "200")
sleep(10)
end
end
task :sshready do
unless (bamboo_ssh_opts and vmhost)
puts "ERROR: a required variable is not set"
exit 1
end
i = 0
retrymax = 60
loop do
puts "attempt #{i} at waiting 5 seconds for ssh to receive /etc/debian_version response"
i += 1
# finished = ssh -q #{bamboo_ssh_opts} #{vmhost} exit
finished = scp #{bamboo_ssh_opts} #{vmhost}:/etc/debian_version /dev/stdout
puts "received: #{finished.strip}"
break if (i >= retrymax || finished.strip == "wheezy/sid")
sleep(5)
end
end
task :injectphantomjs do unless (bamboo_ssh_opts and vmhost) puts "ERROR: a required variable is not set" exit 1 end # assumes btf and web repos are on the same level AND btf/test/bamboo/Rakefile sh "tar -czf ./integration-tests.tar.gz ../../../web/tests"
puts "Start SCP to #{vmhost}: send web-tests.tar.gz" sh "scp #{bamboo_ssh_opts} ./integration-tests.tar.gz #{vmhost}:/tmp || exit 1" sh "ssh #{bamboo_ssh_opts} #{vmhost} '/bin/tar -xf /tmp/integration-tests.tar.gz'" sh "ssh #{bamboo_ssh_opts} #{vmhost} 'nohup /home/admin/web/tests/setup-selenium-phantomjs.sh > phantomjs.out 2> phantomjs.err < /dev/null &'"
i = 0
retrymax = 60
loop do
puts "attempt #{i} at phantomjs pid or wait for 10 seconds"
i += 1
finished = ssh #{bamboo_ssh_opts} #{vmhost} '/bin/pidof phantomjs'
puts "received: #{finished}"
break if (i >= retrymax || !finished.empty?)
sleep(10)
end
end
task :resultstoxml do unless (bamboo_ssh_opts and vmhost) puts "ERROR: a required variable is not set" exit 1 end sh "ssh #{bamboo_ssh_opts} #{vmhost} 'ls -ahl /home/admin/web/tests/PageObjects/*.subunit'" sh "ssh #{bamboo_ssh_opts} #{vmhost} 'subunit2junitxml --no-passthrough < /home/admin/web/tests/PageObjects/test-results.subunit > /tmp/test-results.xml || true'" puts "cleaning the bug where skip> needs to be skipped in junit" sh "ssh #{bamboo_ssh_opts} #{vmhost} 'ls -ahl /tmp/test-results.xml'" end
task :getresults do unless (bamboo_ssh_opts and vmhost) puts "ERROR: a required variable is not set" exit 1 end sh "scp #{bamboo_ssh_opts} #{vmhost}:/tmp/test-results.xml ./test-results.xml" puts File.read("./test-results.xml") end
puts "done"