Class: VM
- Inherits:
-
Object
- Object
- VM
- Defined in:
- lib/esxi.rb
Instance Method Summary collapse
- #create_snapshot(vmid, name, description) ⇒ Object
- #delete_all_snapshots(vmid) ⇒ Object
- #delete_snapshot(vmid, snapshot) ⇒ Object
- #get_snapshots(vmid) ⇒ Object
-
#initialize(config) ⇒ VM
constructor
A new instance of VM.
- #pause(vmid) ⇒ Object
- #reset(vmid) ⇒ Object
- #resume(vmid) ⇒ Object
- #revert_snapshot(vmid, snapshot_name) ⇒ Object
- #run_command(command) ⇒ Object
- #running?(vmid) ⇒ Boolean
- #shutdown ⇒ Object
- #start(vmid) ⇒ Object
- #stop(vmid) ⇒ Object
- #suspend(vmid) ⇒ Object
Constructor Details
#initialize(config) ⇒ VM
Returns a new instance of VM.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/esxi.rb', line 5 def initialize config unless config['host'] then raise ArgumentError, "Must provide a hostname" end unless config['user'] then raise ArgumentError, "Must provide a username" end @host = config["host"] @user = config["user"] @password = config["password"] if config["password"] @port = config["port"] ? config["port"] : 22 @session = Util.start_ssh_session({:host => @host, :port => @port, :user => @user, :password => @password, :retries => 5, :timeout => 75}) end |
Instance Method Details
#create_snapshot(vmid, name, description) ⇒ Object
41 42 43 44 |
# File 'lib/esxi.rb', line 41 def create_snapshot vmid, name, description description ||= "Snapshot created by https://github.com/prashanthrajagopal/esxi" Util.run(@session, "nohup vim-cmd vmsvc/snapshot.create #{vmid} #{name} #{description} 1 0 > nohup.log < /dev/null &") end |
#delete_all_snapshots(vmid) ⇒ Object
90 91 92 |
# File 'lib/esxi.rb', line 90 def delete_all_snapshots vmid Util.run(@session, "nohup vim-cmd vmsvc/snapshot.removeall #{vmid} > nohup.log < /dev/null &") end |
#delete_snapshot(vmid, snapshot) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/esxi.rb', line 60 def delete_snapshot vmid, snapshot snapshots = get_snapshots(vmid) snapshots.each do |snap| #puts "DEBUG: checking #{snapshot}" if snap["name"].downcase == snapshot_name.strip.downcase snap_id = snap["id"] #puts "DEBUG: I would remove #{snapshot}" Util.run(@session, "nohup vim-cmd vmsvc/snapshot.remove #{vmid} #{snap_id} > nohup.log < /dev/null &") return true end end raise "Invalid Snapshot Name" end |
#get_snapshots(vmid) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/esxi.rb', line 74 def get_snapshots vmid snapshots = Util.run(@session, "vim-cmd vmsvc/snapshot.get #{vmid}").split('|') snapshots.shift snapshot_list = [] snapshots.each do |snapshot| info = {} snap = snapshot.gsub('--','').split("\n") snap.shift snap.each do |x| info[x.split(':')[0].strip.downcase.gsub("snapshot ", '')] = x.split(':')[1].strip end snapshot_list << info unless info == {} end snapshot_list end |
#pause(vmid) ⇒ Object
29 30 31 |
# File 'lib/esxi.rb', line 29 def pause vmid Util.run(@session, "vim-cmd vmsvc/power.suspend #{vmid}") end |
#reset(vmid) ⇒ Object
37 38 39 |
# File 'lib/esxi.rb', line 37 def reset vmid Util.run(@session, "vim-cmd vmsvc/power.reset #{vmid}") end |
#resume(vmid) ⇒ Object
33 34 35 |
# File 'lib/esxi.rb', line 33 def resume vmid Util.run(@session, "vim-cmd vmsvc/power.suspendResume #{vmid}") end |
#revert_snapshot(vmid, snapshot_name) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/esxi.rb', line 46 def revert_snapshot vmid, snapshot_name snapshots = get_snapshots(vmid) snapshots.each do |snap| #puts "DEBUG: checking #{snapshot}" if snap["name"].downcase == snapshot_name.strip.downcase snap_id = snap["id"] #puts "DEBUG: I would revert to #{snapshot}" Util.run(@session, "nohup vim-cmd vmsvc/snapshot.revert #{vmid} #{snap_id} 0 > nohup.log < /dev/null &") return true end end raise "Snapshot not found" end |
#run_command(command) ⇒ Object
111 112 113 |
# File 'lib/esxi.rb', line 111 def run_command command Util.run(@session, command) end |
#running?(vmid) ⇒ Boolean
94 95 96 97 98 99 100 |
# File 'lib/esxi.rb', line 94 def running? vmid if Util.run(@session, "vim-cmd vmsvc/power.getstate #{vmid}").match /Powered on/ return true else return false end end |
#shutdown ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/esxi.rb', line 102 def shutdown puts "Use With Caution. This will shutdown your ESXI." puts "You have 5 seconds to abort" sleep 5 Util.run(@session, "for id in `vim-cmd vmsvc/getallvms | grep -v Vmid |awk '{print (}'`; do vim-cmd /vmsvc/power.off $id ; done") Util.run(@session, "vim-cmd hostsvc/maintenance_mode_enter") Util.run(@session, 'esxcli system shutdown poweroff -d 10 -r "Shell initiated system shutdown"') end |
#start(vmid) ⇒ Object
17 18 19 |
# File 'lib/esxi.rb', line 17 def start vmid Util.run(@session, "vim-cmd vmsvc/power.on #{vmid}") end |