first commit
[past_intern.git] / hw-intern-09 / scripts / job-exe / functions.rb
blob3a223507ee4c54717055b415c2270f3c0ab44d60
1 #!/usr/bin/ruby -w
2 # This file contains functions to perform the 
3 # functionalities of trivial paralllization
4 # By Chris Choi
6 require 'rubygems'
7 require 'fileutils'
8 # The following library are REQUIRED! 
9 # Read the README file for more info.
10 require 'net/ssh'
11 require 'net/scp'
13 class TrivParaSession
15         # function downloads files from the cluster machines to a 
16         # designated location on the local machine
17         def download (addr, login, passwd, from)
18                 check_status = 0
19                 download = 0
21                 # Checking if the location exists
22                 while check_status == 0
23                         puts "Where do you want to save the files  to?"
24                         puts "You are currently in #{Dir.getwd}"
25                         puts ""
26                         printf("path: ")
27                         save_path = gets.chomp
28                         data_file_path = from + "/data/."
29                         
30                         if File.directory?(save_path) then
31                                 # You are set to donwload
32                                 download = 1
33                                 check_status = 1
34                         else 
35                                 puts "Error!: #{save_path} does not exist!"
36                                 puts "Try again?(y/n)"
37                                 ans = gets.chomp
38                                 if ans == "y" || ans == "Y" then
39                                         check_status = 0                
40                                 else
41                                         puts "Error!: Could not find the save path: #{save_path}"
42                                         check_status = 1
43                                 end
44                         end
45                         if download == 1 then
46                                 Net::SCP.start( addr, login, :password => passwd ) do |scp|
47                                         channel = scp.download( data_file_path , save_path, :recursive => true )
48                                         puts "Downloading ..."
49                                         channel.wait
50                                         puts "Download Complete!"
51                                 end
52                         end
53                 end
54                 
55         end
57         # uploads files from a user specified path, to a default remote location on
58         # cluster machines
59         def upload (addr, login, passwd, to)
61                 check_status = 0
62                 upload = 0
63                 while check_status == 0
64                         # ask for the file to upload
65                         puts "Where is the file you wish to upload?"
66                         puts "****************************************************"
67                         puts "*    REMEMBER TO PUT THE FULL OR RELATIVE PATH     *"
68                         puts "****************************************************"
69                         puts "You are currently in #{Dir.getwd}"
70                         puts "Here are the contents of the dir:"
71                         puts ""
72                         Dir.glob("*") do |f| puts f     end
73                         printf("\npath: ")
74                         local_path = gets.chomp
76                         # check if the file exists
77                         if File.exists?(local_path) then
78                                 # you are set to upload
79                                 upload = 1
80                                 check_status = 1
81                         else
82                                 # if the file doesn't exist ask again 
83                                 puts "Error!: #{local_path} does not exist!"
84                                 puts "Try again?(y/n)"
85                                 ans = gets.chomp
86                                 if ans == "y" || ans == "Y" then
87                                         check_status = 0                
88                                 else
89                                         puts "Error!: Could not find the file: #{local_path}"
90                                         check_status = 1
91                                 end
92                         end
93                         
94                         # if file is found, we are ready to upload
95                         if upload == 1 then
96                                 Net::SCP.start( addr, login, :password => passwd) do |scp|
97                                         channel = scp.upload( local_path, to)
98                                         puts "Uploading ..."
99                                         channel.wait
100                                         puts "Upload Complete"
101                                 end
102                         end
103                 end
105         end
108         def exejob( addr, login, passwd, path_of_exe)
109                 puts "Have you uploaded the job file?(y/n)"
110                 ans = gets.chomp
111                 
112                 check_status = 0
113                 done = 0
114                 execute = 0
116                 while done == 0 
117                         if ans == "y" || ans == "Y" then
118                                 puts "OK! starting the job"
119                                 # ssh into the gateway and start the "executer!"
120                                 Net::SSH.start( addr, login, :password => passwd) do |ssh|
121                                         # the following passes a bash command to start the executer,
122                                         # it takes argumenst of username, password, and job file
123                                         ssh.exec("ruby #{path_of_exe} #{login} #{passwd} job.xml")
124                                         puts "Done!"
125                                 end
126                                 done = 1
127                         else
128                                 puts "Where is the job file you wish to start processing?"
129                                 puts "You are currently in #{Dir.getwd}"
130                                 puts "Here are the contents of the dir:"
131                                 printf("\npath: ")
132                                 jobfile_path = gets.chomp
134                                 # Checking if the file exists
135                                 while check_status == 0
136                                         if File.exists?(jobfile_path) then
137                                                 # You are set to upload
138                                                 upload = 1
139                                         else 
140                                                 puts "Error!: #{jobfile_path} does not exist!"
141                                                 puts "Try again?(y/n)"
142                                                 ans = gets.chomp
143                                                 if ans == "y" || ans == "Y" then
144                                                         check_status = 0                
145                                                 else
146                                                         puts "Error!: Could not find the job file: #{jobfile_path}"
147                                                         check_status = 1
148                                                 end
149                                         end
150                                 end # end of 2nd while loop (check if jobfile exists on local)
151                         end # end of if else in 1st while loop 
152                 end # end of 1st while loop (check if exejob is done)
153         end