first commit
[past_intern.git] / hw-intern-09 / scripts / job-exe / executer-test.rb
blob15cff53adf3a1d4604e2351cf8a83da9a5b3bced
1 #!/usr/bin/ruby -w
2 # This is an executer script that is launched remotely.
3 # It reads the job.xml file and acts as a launcher to 
4 # execute different jobs remotely one by one.
6 # The script accepts commandline arguments
7 # job-exe "username" "password"
9 require 'rubygems'
10 require 'xmlsimple'
11 require 'net/ssh'
12 require 'rubyscript2exe'
14 # sensitive information
15 username = "sg188"
16 password = "8946567"
18 # paths to job and cluster xml files
19 job_file_path = "./job.xml"
21 # Parse job and cluster xml file with xmlsimple library
22 puts "Parsing the job xml file"
23 job_file = XmlSimple.xml_in( job_file_path, { 'KeyAttr' => 'name' }) 
25 # Now that we have the all the machine addresses
26 # loop through the job.xml for jobs to process and start them
27 machine = Array.new
28 command = Array.new
29 loop = 1
30 counter = 0
32 while loop == 1
33         p job_file['server'][0]
34         if job_file['server'][counter] then
35                 machine << job_file['server'][counter]
36                 counter += 1
37         else
38                 loop = 0
39         end
40 end     
42 machine.each do |host|
43         # put all the commands in array ~ command
44         job_file['server'][host]['command'].each do |x|
45                 command << x
46         end
48         # start the jobs
49         Net::SSH.start( host, username, :password => password) do |ssh|
50                 # execute command to host
51                 command.each do |c|
52                         ssh.exec!(c)
53                         puts "Execution of command #{c} on #{host} has started!"
54                 end
55         end
56         puts "No more jobs for #{host}"
57 end