Class: CloudstackSpec::Resource::VirtualMachine

Inherits:
Base
  • Object
show all
Defined in:
lib/cloudstack_spec/resource/virtual_machine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#get_zone, #inspect, #job_status?, #to_ary, #to_s

Constructor Details

#initialize(name = 'rspec-test1', zonename = nil) ⇒ VirtualMachine

Returns a new instance of VirtualMachine.



6
7
8
9
10
11
12
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 6

def initialize(name='rspec-test1', zonename=nil)
  @name   = name
  @template_name = ''
  @connection = CloudstackSpec::Helper::Api.new.connection
  @zone = get_zone(zonename)
  @runner = Specinfra::Runner
end

Instance Attribute Details

#nameObject (readonly)

do nothing



4
5
6
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 4

def name
  @name
end

#template_nameObject (readonly)

do nothing



4
5
6
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 4

def template_name
  @template_name
end

#zonenameObject (readonly)

do nothing



4
5
6
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 4

def zonename
  @zonename
end

Instance Method Details

#created?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 50

def created?
  # start the VM creation job
  # Return {true/false}
  if self.exist?
    creation_job = "Already exist" 
    puts "  #{creation_job}"
    return false
  end

  if ! self.template_name.nil?
    if validate_template_status == false
      puts "   Template not valid"
    end
  end
  begin
    newvm = create_virtual_machine(@name)
    creation_job = newvm['jobid']
  rescue Exception => exception
    creation_job = "vm creation fail with #{exception.message}"
  end
  if UUID.validate(creation_job) == true
    return job_status?(creation_job)
  else
    puts "  #{creation_job}"
    return false
  end
end

#destroy?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 78

def destroy?
  if self.exist?
    job = @connection.destroy_virtual_machine(id: vm['id'], expunge: true)
    job_status?(job['jobid'])
  else
    puts "  Does not exist"
    return false
  end
end

#exist?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 14

def exist?
  begin  
    if vm.empty?
      return false
    else
      return true
    end
  rescue Exception => e
    return false
  end
end

#open_pf_sshObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 88

def open_pf_ssh
  # open port forwarding for ssh (tcp:22)
  port = 22
  proto = 'tcp'
  sleep(5)  # give move time to the vm to complete booting.
  if get_pf_rule.empty?
    new_rule = @connection.create_port_forwarding_rule(
                ipaddressid: publicip_id, 
                virtualmachineid: vm['id'],
                privateport: port,
                publicport: port,
                networkid: vm['nic'].first['networkid'], 
                protocol: proto )
  else
    #puts "      Port Forwarging rule already exist"
  end
  return true

end

#reachable?(port, proto, timeout) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 38

def reachable?(port, proto, timeout)
  pf_rule = get_pf_rule
  if pf_rule.empty?
    ip = vm['nic'].first['ipaddress']
  else
    ip = pf_rule['ipaddress']
    port = pf_rule['publicport']
    proto = pf_rule['protocol']
  end
  @runner.check_host_is_reachable(ip, port, proto, timeout)
end

#running?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cloudstack_spec/resource/virtual_machine.rb', line 26

def running?
  begin
    if vm['state'] == 'Running'
      return true
    else
      return false
    end
  rescue
      return false
  end
end