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 Method Summary collapse

Methods included from Hyperv::ModelExtends

lazy_attributes

Methods included from Hyperv::ModelIncludes

#dirty?, #lazy_attributes, #parent

Instance Method Details

#add_interface(options = {}) ⇒ Object



123
124
125
# File 'lib/fog/hyperv/models/compute/server.rb', line 123

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

#biosObject Also known as: firmware



75
76
77
78
79
80
81
82
83
# File 'lib/fog/hyperv/models/compute/server.rb', line 75

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



114
115
116
117
118
119
120
121
# File 'lib/fog/hyperv/models/compute/server.rb', line 114

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



180
181
182
# File 'lib/fog/hyperv/models/compute/server.rb', line 180

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

#mac_addressesObject



176
177
178
# File 'lib/fog/hyperv/models/compute/server.rb', line 176

def mac_addresses
  network_adapters.map(&:mac_address)
end

#public_ip_addressObject



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

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

#ready?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/fog/hyperv/models/compute/server.rb', line 172

def ready?
  state_num == 2
end

#reloadObject



163
164
165
166
167
168
169
170
# File 'lib/fog/hyperv/models/compute/server.rb', line 163

def reload
  data = collection.get id

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

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



105
106
107
108
109
110
111
# File 'lib/fog/hyperv/models/compute/server.rb', line 105

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

#save(options = {}) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/fog/hyperv/models/compute/server.rb', line 127

def save(options = {})
  requires :name
  logger.debug "Saving server with; #{attributes}, #{options}"

  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



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

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

#stop(options = {}) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/fog/hyperv/models/compute/server.rb', line 97

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