Class: Vsimple::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/vsimple/vm.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ VM

Returns a new instance of VM.



6
7
8
9
# File 'lib/vsimple/vm.rb', line 6

def initialize(path)
    @vm = Vsimple::Config[:dc].vmFolder.traverse(path, RbVmomi::VIM::VirtualMachine)
    raise Vsimple::Error.new "VM #{path} not found" unless @vm
end

Class Method Details

.exist?(path) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/vsimple/vm.rb', line 15

def self.exist?(path)
    if Vsimple::Config[:dc].vmFolder.traverse(path, RbVmomi::VIM::VirtualMachine)
        true
    else
        false
    end
end

Instance Method Details

#clone(name, vm_config = {}) ⇒ Object

Clone the VirtualMachine.

Parameters:

name

Name of the machine. Can be the FQDN.

vm_config

machine_options:

  • :path => Path of the VM

  • :hostname => Hostname of the VM

  • :domain => Domain name of the VM

  • :powerOn => Flags to start the VM after the clone (true or false)

  • :ip_dns => IP separed by commate of the DNS server

  • :network => Network configuration. exemple:

    vm_config[:network] = {
        "Network adapter 1" => {
            :port_group => "port_group_name",
            :ip   => "172.16.1.1/24",
            :gw   => "172.16.1.254"
        }
    }
    

Windows only:

  • :commandList

  • :password

  • :timeZone

  • :identification_domainAdmin

  • :identification_domainAdminPassword

  • :identification_joinDomain

  • :identification_joinWorkgroup

  • :userData_orgName

  • :userData_productId



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vsimple/vm.rb', line 95

def clone(name, vm_config={})
    unless vm_config[:path]
        if name.include?('/')
            vm_config[:path] = File.dirname(name)
        else
            vm_config[:path] = ""
        end
    end
    name = File.basename(name)

    vm_config[:hostname] ||= name[/^([^.]*)/, 1]
    vm_config[:domain]   ||= name[/^[^.]*.(.*)$/, 1]

    clone_spec  = generate_clone_spec(vm_config)
    dest_folder = Vsimple::Config[:dc].vmFolder.traverse!(vm_config[:path], RbVmomi::VIM::Folder)

    begin
        @vm.CloneVM_Task(
            :folder => dest_folder,
            :name   => name,
            :spec   => clone_spec
        ).wait_for_completion
    rescue => e
        raise Vsimple::Error.new "Clone vm #{@vm.name}: #{e.message}"
    end

    Vsimple::VM.new("#{vm_config[:path]}/#{name}")
end

#guestToolsReady?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/vsimple/vm.rb', line 59

def guestToolsReady?
    @vm.guest.toolsRunningStatus == "guestToolsRunning"
end

#is_windows?Boolean

Check if the machine is a windows

Parameters:

vm

VM instance given by rbvmomi

Returns:

  • (Boolean)


129
130
131
# File 'lib/vsimple/vm.rb', line 129

def is_windows?
    @vm.summary.config.guestFullName =~ /^Microsoft Windows/
end

#nameObject



11
12
13
# File 'lib/vsimple/vm.rb', line 11

def name
    @vm.name
end

#poweredOffObject



31
32
33
34
35
36
37
# File 'lib/vsimple/vm.rb', line 31

def poweredOff
    begin
        @vm.PowerOffVM_Task.wait_for_completion
    rescue => e
        raise Vsimple::Error.new "Power off vm #{@vm.name}: #{e.message}"
    end
end

#powerOnObject



23
24
25
26
27
28
29
# File 'lib/vsimple/vm.rb', line 23

def powerOn
    begin
        @vm.PowerOnVM_Task.wait_for_completion
    rescue => e
        raise Vsimple::Error.new "Power on vm #{@vm.name}: #{e.message}"
    end
end

#powerStateObject



47
48
49
# File 'lib/vsimple/vm.rb', line 47

def powerState
    @vm.summary.runtime.powerState
end

#rebootGuestObject



51
52
53
54
55
56
57
# File 'lib/vsimple/vm.rb', line 51

def rebootGuest
    begin
        @vm.RebootGuest
    rescue => e
        raise Vsimple::Error.new "RebootGuest vm #{@vm.name}: #{e.message}"
    end
end

#shutdownGuestObject



39
40
41
42
43
44
45
# File 'lib/vsimple/vm.rb', line 39

def shutdownGuest
    begin
        @vm.ShutdownGuest
    rescue => e
        raise Vsimple::Error.new "Shutdown vm #{@vm.name}: #{e.message}"
    end
end

#wait_guest_tools_ready(timeout = nil) ⇒ Object

Wait until the guest tools of the machine is start or until timeout if given.

Parameters:

server

VM instance given by rbvmomi

timeout

Timeout



141
142
143
144
145
146
147
148
# File 'lib/vsimple/vm.rb', line 141

def wait_guest_tools_ready(timeout=nil)
    wait = 1
    while @vm.guest.toolsRunningStatus != "guestToolsRunning" && (!timeout || wait < timeout)
        sleep 1
        wait += 1
    end
    @vm.guest.toolsRunningStatus == "guestToolsRunning"
end

#wait_to_stop(timeout = nil) ⇒ Object

Wait until the machine stop or until timeout if given

Parameters:

server

VM instance given by rbvmomi

timeout

Timeout



158
159
160
161
162
163
164
165
# File 'lib/vsimple/vm.rb', line 158

def wait_to_stop(timeout=nil)
    wait = 1
    while @vm.summary.runtime.powerState != "poweredOff" && (!timeout || wait < timeout)
        sleep 1
        wait += 1
    end
    @vm.summary.runtime.powerState == "poweredOff"
end