Class: Vagrant::Smartos::Zones::Models::Zone

Inherits:
Object
  • Object
show all
Includes:
Util::GlobalZone::Helper
Defined in:
lib/vagrant/smartos/zones/models/zone.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::GlobalZone::Helper

included, #sudo, #with_gz

Instance Attribute Details

#brandObject

Returns the value of attribute brand.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def brand
  @brand
end

#imageObject

Returns the value of attribute image.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def image
  @image
end

#machineObject

Returns the value of attribute machine.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def machine
  @machine
end

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def name
  @name
end

#stateObject

Returns the value of attribute state.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def state
  @state
end

#uuidObject

Returns the value of attribute uuid.



12
13
14
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 12

def uuid
  @uuid
end

Class Method Details

.all(machine) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 14

def self.all(machine)
  zones = []
  with_gz(machine, 'pfexec vmadm lookup -j') do |output|
    hashes = JSON.parse(output)
    zones += hashes.map { |h| from_hash(h, machine) }
  end
  zones
end

.create(name, machine) ⇒ Object



23
24
25
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 23

def self.create(name, machine)
  Util::Zones.new(machine).create(name)
end

.create_or_update(name, machine) ⇒ Object



27
28
29
30
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 27

def self.create_or_update(name, machine)
  return Util::Zones.new(machine).update(name) if find(machine, name, raise_on_error: false)
  Util::Zones.new(machine).create(name)
end

.find(machine, name, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 32

def self.find(machine, name, options = {})
  options = { raise_on_error: true }.merge(options)
  zone_hash = {}
  finder = "pfexec vmadm lookup -j -o uuid,alias,state,image_uuid,brand alias=#{name}"
  with_gz(machine, finder) do |output|
    hash = JSON.parse(output).first
    break zone_hash.merge!(hash) if hash
    raise ZoneNotFound if options[:raise_on_error]
    return
  end
  from_hash(zone_hash, machine)
end

.from_hash(zone, machine = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 45

def self.from_hash(zone, machine = nil)
  new.tap do |z|
    z.machine = machine
    z.name = zone['alias']
    z.uuid = zone['uuid']
    z.brand = zone['brand']
    z.state = zone['state']
    z.image = zone['image_uuid']
  end
end

Instance Method Details

#destroyObject



76
77
78
79
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 76

def destroy
  with_gz("pfexec vmadm delete #{uuid}")
  self.state = 'deleted'
end

#lx_brand?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 60

def lx_brand?
  brand == 'lx'
end

#running?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 56

def running?
  state == 'running'
end

#startObject



81
82
83
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 81

def start
  with_gz("pfexec vmadm start #{uuid}")
end

#stopObject



85
86
87
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 85

def stop
  with_gz("pfexec vmadm stop #{uuid}")
end

#test(cmd) ⇒ Object



64
65
66
67
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 64

def test(cmd)
  command = "pfexec zlogin #{uuid} #{cmd}"
  machine.communicate.gz_test(command)
end

#zlogin(cmd, options = {}) ⇒ Object



69
70
71
72
73
74
# File 'lib/vagrant/smartos/zones/models/zone.rb', line 69

def zlogin(cmd, options = {})
  command = "pfexec zlogin #{uuid} #{cmd}"
  with_gz(command, options) do |output|
    yield output if block_given?
  end
end