Class: Fog::Compute::Hyperv::Server

Inherits:
Server
  • Object
show all
Extended by:
Hyperv::ModelExtends
Includes:
Hyperv::ModelIncludes
Defined in:
lib/fog/hyperv/models/compute/server.rb

Constant Summary collapse

VM_STATUS_ENUM_VALUES =
{
  1     => :Unknown,
  2     => :Running,
  3     => :Off,
  4     => :Stopping,
  6     => :Saved,
  9     => :Paused,
  10    => :Starting,
  11    => :Reset,
  32773 => :Saving,
  32776 => :Pausing,
  32777 => :Resuming,
  32779 => :FastSaved,
  32780 => :FastSaving,
  32781 => :ForceShutdown,
  32782 => :ForceReboot,
  32783 => :RunningCritical,
  32784 => :OffCritical,
  32785 => :StoppingCritical,
  32786 => :SavedCritical,
  32787 => :PausedCritical,
  32788 => :StartingCritical,
  32789 => :ResetCritical,
  32790 => :SavingCritical,
  32791 => :PausingCritical,
  32792 => :ResumingCritical,
  32793 => :FastSavedCritical,
  32794 => :FastSavingCritical
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Hyperv::ModelExtends

lazy_attributes

Methods included from Hyperv::ModelIncludes

#cluster, #computer, #dirty?, #lazy_attributes, #parent, #vm

Constructor Details

#initialize(attrs = {}) ⇒ Server

Returns a new instance of Server.



81
82
83
84
85
86
# File 'lib/fog/hyperv/models/compute/server.rb', line 81

def initialize(attrs = {})
  super

  @cluster = attrs.delete :cluster
  @computer = attrs.delete :computer
end

Instance Attribute Details

#cluster_nameObject

Returns the value of attribute cluster_name.



63
64
65
# File 'lib/fog/hyperv/models/compute/server.rb', line 63

def cluster_name
  @cluster_name
end

Instance Method Details

#add_interface(options = {}) ⇒ Object



136
137
138
# File 'lib/fog/hyperv/models/compute/server.rb', line 136

def add_interface(options = {})
  network_adapters.create options
end

#biosObject Also known as: firmware



88
89
90
91
92
93
94
95
96
# File 'lib/fog/hyperv/models/compute/server.rb', line 88

def bios
  @bios ||= begin
    if generation == 1
      Fog::Compute::Hyperv::Bios.new(service.get_vm_bios(computer_name: computer_name, vm_name: name).merge service: service)
    elsif generation == 2
      Fog::Compute::Hyperv::Firmware.new(service.get_vm_firmware(computer_name: computer_name, vm_name: name).merge service: service)
    end
  end
end

#destroy(options = {}) ⇒ Object



127
128
129
130
131
132
133
134
# File 'lib/fog/hyperv/models/compute/server.rb', line 127

def destroy(options = {})
  requires :name, :computer_name
  stop turn_off: true if ready?
  service.remove_vm options.merge(
    name: name,
    computer_name: computer_name
  )
end

#ip_addressesObject



192
193
194
# File 'lib/fog/hyperv/models/compute/server.rb', line 192

def ip_addresses
  network_adapters.map(&:ip_addresses).flatten
end

#mac_addressesObject



188
189
190
# File 'lib/fog/hyperv/models/compute/server.rb', line 188

def mac_addresses
  network_adapters.map(&:mac_address)
end

#public_ip_addressObject



196
197
198
199
200
# File 'lib/fog/hyperv/models/compute/server.rb', line 196

def public_ip_address
  ip_addresses
    .reject { |a| a =~ /^(169\.254|fe80)/ }
    .first
end

#ready?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/fog/hyperv/models/compute/server.rb', line 184

def ready?
  state_num == 2
end

#reloadObject



175
176
177
178
179
180
181
182
# File 'lib/fog/hyperv/models/compute/server.rb', line 175

def reload
  data = collection.get id

  clear_lazy
  merge_attributes(data.attributes)
  @old = data
  self
end

#restart(options = {}) ⇒ Object Also known as: reboot



118
119
120
121
122
123
124
# File 'lib/fog/hyperv/models/compute/server.rb', line 118

def restart(options = {})
  requires :name, :computer_name
  service.restart_vm options.merge(
    name: name,
    computer_name: computer_name
  )
end

#save(options = {}) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/fog/hyperv/models/compute/server.rb', line 140

def save(options = {})
  requires :name

  data = \
    if !persisted?
      usable = %i(name memory_startup generation boot_device switch_name no_vhd new_vhd_path new_vhd_size_bytes).freeze
      service.new_vm \
        attributes.select { |k, _v| usable.include? k }
        .merge(options)
        .merge(_return_fields: self.class.attributes, _json_depth: 1)
    else
      service.set_vm options.merge(
        computer_name: old.computer_name,
        name: old.name,
        passthru: true,

        processor_count: changed!(:processor_count),
        dynamic_memory: changed?(:dynamic_memory_enabled) && dynamic_memory_enabled,
        static_memory: changed?(:dynamic_memory_enabled) && !dynamic_memory_enabled,
        memory_minimum_bytes: changed?(:memory_minimum) && dynamic_memory_enabled && memory_minimum,
        memory_maximum_bytes: changed?(:memory_maximum) && dynamic_memory_enabled && memory_maximum,
        memory_startup_bytes: changed!(:memory_startup),
        notes: changed!(:notes),
        new_name: changed!(:name),

        _return_fields: self.class.attributes,
        _json_depth: 1
      )
    end

  merge_attributes(data)
  @old = dup
  self
end

#start(options = {}) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/fog/hyperv/models/compute/server.rb', line 102

def start(options = {})
  requires :name, :computer_name
  service.start_vm options.merge(
    name: name,
    computer_name: computer_name
  )
end

#stop(options = {}) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/fog/hyperv/models/compute/server.rb', line 110

def stop(options = {})
  requires :name, :computer_name
  service.stop_vm options.merge(
    name: name,
    computer_name: computer_name
  )
end