Class: VMServer
- Inherits:
-
Object
- Object
- VMServer
- Defined in:
- lib/vmserver.rb
Instance Attribute Summary collapse
-
#datastore ⇒ Object
Returns the value of attribute datastore.
-
#guest_password ⇒ Object
Returns the value of attribute guest_password.
-
#guest_user ⇒ Object
Returns the value of attribute guest_user.
-
#host ⇒ Object
Returns the value of attribute host.
-
#logging_enabled ⇒ Object
Returns the value of attribute logging_enabled.
-
#vm_password ⇒ Object
Returns the value of attribute vm_password.
-
#vm_user ⇒ Object
Returns the value of attribute vm_user.
Instance Method Summary collapse
-
#capture_screen(output_file) ⇒ Object
Remove Directory form the Guest OS.
-
#copy_file_from_guest_to_host(src, dest) ⇒ Object
Copy a file from Guest OS to Host OS.
-
#copy_file_from_host_to_guest(src, dest) ⇒ Object
Copy a file from host OS to Guest OS.
-
#delete_snapshot(name) ⇒ Object
Delete snapshot of the Virtual Machine.
-
#file_exists_in_guest?(file) ⇒ Boolean
Checks if a file exists in the guest OS.
-
#get_processes_in_guest ⇒ Object
Get a list of processes running in the Guest OS.
-
#initialize {|_self| ... } ⇒ VMServer
constructor
A new instance of VMServer.
-
#kill_process_in_guest(pid) ⇒ Object
Kill a process with the given PID in the Guest OS.
-
#log(msg) ⇒ Object
Logs if logging is enabled.
-
#ls(dir) ⇒ Object
List a directory in Guest OS.
-
#mkdir(dir) ⇒ Object
————————————– Working with Files and Directory in Guest OS ————————-.
-
#pause ⇒ Object
Pause the Virtual Machine.
-
#reset(mode = 'soft') ⇒ Object
Reset the Virtual Machine Mode is soft by default.
-
#revert_to_snapshot(name) ⇒ Object
Revert to previous snapshot.
-
#rmdir(dir) ⇒ Object
Remove Directory form the Guest OS.
-
#rmfile(file) ⇒ Object
Remove Directory form the Guest OS.
-
#run_program_in_guest(program, prog_args = {}) ⇒ Object
Execute a program in the Guest OS.
-
#snapshot(name = "snapshot_#{Time.now.strftime("%m%d")}") ⇒ Object
Take a snapshot of the Virtual Machine.
-
#start ⇒ Object
——————————– Controlling Virtual Machine Power States with vmrun ————————- .
-
#stop(mode = 'soft') ⇒ Object
Stops the Virtual Machine Mode is soft by default.
-
#suspend(mode = 'soft') ⇒ Object
Suspend the Virtual Machine Mode is soft by default.
-
#unpause ⇒ Object
Pause the Virtual Machine.
Constructor Details
#initialize {|_self| ... } ⇒ VMServer
Returns a new instance of VMServer.
5 6 7 8 9 10 |
# File 'lib/vmserver.rb', line 5 def initialize yield self raise ArgumentError, "Please make sure you have set host ,vm_user and vm_password in the configuration!" unless @host || @vm_user || @vm_password # This is the base command used for all the commands. @base_command = "vmrun -T server -h #{@host} -u #{@vm_user} -p #{@vm_password}" end |
Instance Attribute Details
#datastore ⇒ Object
Returns the value of attribute datastore.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def datastore @datastore end |
#guest_password ⇒ Object
Returns the value of attribute guest_password.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def guest_password @guest_password end |
#guest_user ⇒ Object
Returns the value of attribute guest_user.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def guest_user @guest_user end |
#host ⇒ Object
Returns the value of attribute host.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def host @host end |
#logging_enabled ⇒ Object
Returns the value of attribute logging_enabled.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def logging_enabled @logging_enabled end |
#vm_password ⇒ Object
Returns the value of attribute vm_password.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def vm_password @vm_password end |
#vm_user ⇒ Object
Returns the value of attribute vm_user.
3 4 5 |
# File 'lib/vmserver.rb', line 3 def vm_user @vm_user end |
Instance Method Details
#capture_screen(output_file) ⇒ Object
Remove Directory form the Guest OS
281 282 283 284 285 286 287 288 |
# File 'lib/vmserver.rb', line 281 def capture_screen(output_file) command = 'captureScreen' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{output_file}"} log vm_command result = system(vm_command) result ? log("File deleted successfully.") : log("Error! Failed to delete file.") result end |
#copy_file_from_guest_to_host(src, dest) ⇒ Object
Copy a file from Guest OS to Host OS
231 232 233 234 235 236 237 238 |
# File 'lib/vmserver.rb', line 231 def copy_file_from_guest_to_host(src,dest) command = 'copyFileFromGuestToHost' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{src}" "#{dest}"} log vm_command result = system(vm_command) result ? log("Copy successful.") : log("Error! Copy failed.") result end |
#copy_file_from_host_to_guest(src, dest) ⇒ Object
Copy a file from host OS to Guest OS
218 219 220 221 222 223 224 225 |
# File 'lib/vmserver.rb', line 218 def copy_file_from_host_to_guest(src, dest) command = 'copyFileFromHostToGuest' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{src}" "#{dest}"} log vm_command result = system(vm_command) result ? log("Copy successful.") : log("Error! Copy failed.") result end |
#delete_snapshot(name) ⇒ Object
Delete snapshot of the Virtual Machine
138 139 140 141 142 143 144 145 |
# File 'lib/vmserver.rb', line 138 def delete_snapshot(name) command = 'deleteSnapshot' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{name}} log vm_command result = system(vm_command) result ? log("SnapShot deleted successful") : log("Error! VM SnapShot delete failed.") result end |
#file_exists_in_guest?(file) ⇒ Boolean
Checks if a file exists in the guest OS
204 205 206 207 208 209 210 211 212 |
# File 'lib/vmserver.rb', line 204 def file_exists_in_guest?(file) output = `#{@base_command} -gu #{@guest_user} -gp #{@guest_password} fileExistsInGuest "#{datastore}" "#{file}"` # output = system(vm_command) if output =~ /The file exists/ return true else return false end end |
#get_processes_in_guest ⇒ Object
Get a list of processes running in the Guest OS
244 245 246 247 248 |
# File 'lib/vmserver.rb', line 244 def get_processes_in_guest command = 'listProcessesInGuest' processes = `#{@base_command} -gu #{@guest_user} -gp #{guest_password} #{command} \"#{@datastore}\"` processes end |
#kill_process_in_guest(pid) ⇒ Object
Kill a process with the given PID in the Guest OS
268 269 270 271 272 273 274 275 |
# File 'lib/vmserver.rb', line 268 def kill_process_in_guest(pid) command = 'killProcessInGuest' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{guest_password} #{command} "#{@datastore}" #{pid}} log vm_command result = system(vm_command) result ? log("Program executed successfully in guest.") : log("Error! Failed to execute program in guest.") result end |
#log(msg) ⇒ Object
Logs if logging is enabled
16 17 18 |
# File 'lib/vmserver.rb', line 16 def log(msg) puts "#{Time.now} #{msg}" if @logging_enabled end |
#ls(dir) ⇒ Object
List a directory in Guest OS
192 193 194 195 196 197 198 |
# File 'lib/vmserver.rb', line 192 def ls(dir) command = 'listDirectoryInGuest' entries = `#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} \"#{@datastore}\" \"#{dir}\"` # The entries would be a list of entries separated by new line. Convert this to an array. entries = entries.split("\n") entries end |
#mkdir(dir) ⇒ Object
————————————– Working with Files and Directory in Guest OS ————————-
Create a directory in the Virtual Machine
153 154 155 156 157 158 159 160 |
# File 'lib/vmserver.rb', line 153 def mkdir(dir) command = 'createDirectoryInGuest' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{dir}"} log vm_command result = system(vm_command) result ? log("Directory created successfully in guest.") : log("Error! Directory could not be created.") result end |
#pause ⇒ Object
Pause the Virtual Machine
84 85 86 87 88 89 90 91 |
# File 'lib/vmserver.rb', line 84 def pause command = 'pause' vm_command = %Q{#{@base_command} #{command} "#{@datastore}"} log vm_command result = system(vm_command) result ? log("VM has been paused") : log("Error! VM could not be paused.") result end |
#reset(mode = 'soft') ⇒ Object
Reset the Virtual Machine Mode is soft by default. If it is overridden to be ‘hard’ it acts in a similar fashion to that pf physically switching off a machine.
56 57 58 59 60 61 62 63 |
# File 'lib/vmserver.rb', line 56 def reset(mode='soft') command = 'reset' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{mode}} log vm_command result = system(vm_command) result ? log("VM has been resetted.") : log("Error! VM could not be reset.") result end |
#revert_to_snapshot(name) ⇒ Object
Revert to previous snapshot
125 126 127 128 129 130 131 132 |
# File 'lib/vmserver.rb', line 125 def revert_to_snapshot(name) command = 'revertToSnapshot' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{name}} log vm_command result = system(vm_command) result ? log("Revert SnapShot successful") : log("Error! VM Revert SnapShot failed.") result end |
#rmdir(dir) ⇒ Object
Remove Directory form the Guest OS
166 167 168 169 170 171 172 173 |
# File 'lib/vmserver.rb', line 166 def rmdir(dir) command = 'deleteDirectoryInGuest' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{dir}"} log vm_command result = system(vm_command) result ? log("Directory deleted successfully.") : log("Error! Failed to delete directory.") result end |
#rmfile(file) ⇒ Object
Remove Directory form the Guest OS
179 180 181 182 183 184 185 186 |
# File 'lib/vmserver.rb', line 179 def rmfile(file) command = 'deleteFileInGuest' vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" "#{file}"} log vm_command result = system(vm_command) result ? log("File deleted successfully.") : log("Error! Failed to delete file.") result end |
#run_program_in_guest(program, prog_args = {}) ⇒ Object
Execute a program in the Guest OS
254 255 256 257 258 259 260 261 262 |
# File 'lib/vmserver.rb', line 254 def run_program_in_guest(program,prog_args={}) command = 'runProgramInGuest' prog_args = prog_args[:prog_args] vm_command = %Q{#{@base_command} -gu #{@guest_user} -gp #{@guest_password} #{command} "#{@datastore}" -activeWindow "#{program}" #{prog_args}} log vm_command result = system(vm_command) result ? log("Program executed successfully in guest.") : log("Error! Failed to execute program in guest.") result end |
#snapshot(name = "snapshot_#{Time.now.strftime("%m%d")}") ⇒ Object
Take a snapshot of the Virtual Machine
112 113 114 115 116 117 118 119 |
# File 'lib/vmserver.rb', line 112 def snapshot(name="snapshot_#{Time.now.strftime("%m%d")}") command = 'snapshot' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{name}} log vm_command result = system(vm_command) result ? log("SnapShot successful") : log("Error! VM SnapShot failed.") result end |
#start ⇒ Object
——————————– Controlling Virtual Machine Power States with vmrun ————————-
Start up the Virtual Machine
26 27 28 29 30 31 32 33 |
# File 'lib/vmserver.rb', line 26 def start command = 'start' vm_command = %Q{#{@base_command} #{command} "#{@datastore}"} log vm_command result = system(vm_command) result ? log("VM started successfully has been executed.") : log("Error! VM could not be started.") result end |
#stop(mode = 'soft') ⇒ Object
Stops the Virtual Machine Mode is soft by default. If it is overridden to be ‘hard’ it acts in a similar fashion to that pf physically switching off a machine.
41 42 43 44 45 46 47 48 |
# File 'lib/vmserver.rb', line 41 def stop(mode='soft') command = 'stop' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{mode}} log vm_command result = system(vm_command) result ? log("VM stopped successfully.") : log("Error! VM could not be stopped.") result end |
#suspend(mode = 'soft') ⇒ Object
Suspend the Virtual Machine Mode is soft by default. If it is overridden to be ‘hard’ it acts in a similar fashion to that pf physically switching off a machine.
71 72 73 74 75 76 77 78 |
# File 'lib/vmserver.rb', line 71 def suspend(mode='soft') command = 'reset' vm_command = %Q{#{@base_command} #{command} "#{@datastore}" #{mode}} log vm_command result = system(vm_command) result ? log("VM has been suspended.") : log("Error! VM could not be suspended.") result end |
#unpause ⇒ Object
Pause the Virtual Machine
97 98 99 100 101 102 103 104 |
# File 'lib/vmserver.rb', line 97 def unpause command = 'unpause' vm_command = %Q{#{@base_command} #{command} "#{@datastore}"} log vm_command result = system(vm_command) result ? log("VM has been unpaused") : log("Error! VM could not be unpaused.") result end |