Class: MiqOpenStackInstance

Inherits:
Object
  • Object
show all
Includes:
MiqOpenStackCommon
Defined in:
lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb

Overview

TODO: Create common base class for MiqOpenStackInstance and MiqOpenStackImage and factor out common code. Also, refactor MiqVm so it can be a proper super class.

Constant Summary collapse

SUPPORTED_METHODS =
[:rootTrees, :extract, :diskInitErrors, :vmConfig, :volumeManager]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MiqOpenStackCommon

#create_image_from_snapshot, #disk_format, #disk_format_glance_v1, #disk_format_glance_v2, #download_image_data_glance_v2, #get_image_file_common, #get_image_file_glance_v1, #get_image_file_glance_v2, #get_image_metadata_snapshot_id

Constructor Details

#initialize(instance_id, openstack_handle) ⇒ MiqOpenStackInstance

Returns a new instance of MiqOpenStackInstance.



16
17
18
19
20
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 16

def initialize(instance_id, openstack_handle)
  @instance_id      = instance_id
  @openstack_handle = openstack_handle
  @vmConfigFile     = instance_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object (private)



201
202
203
204
205
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 201

def method_missing(sym, *args)
  return super unless SUPPORTED_METHODS.include? sym
  return miq_vm.send(sym) if args.empty?
  miq_vm.send(sym, args)
end

Instance Attribute Details

#vmConfigFileObject (readonly)

Returns the value of attribute vmConfigFile.



12
13
14
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 12

def vmConfigFile
  @vmConfigFile
end

Instance Method Details

#compute_serviceObject



22
23
24
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 22

def compute_service
  @compute_service ||= @openstack_handle.compute_service
end

#create_evm_snapshot(options = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 73

def create_evm_snapshot(options = {})
  log_prefix = "MIQ(#{self.class.name}##{__method__}) instance_id=[#{@instance_id}]"

  miq_snapshot = nil
  if snapshot_image_id
    $log.debug "#{log_prefix}: Found pointer to existing snapshot: #{snapshot_image_id}"
    miq_snapshot = begin
      image_service.images.get(snapshot_image_id)
    rescue => err
      $log.debug "#{log_prefix}: #{err}"
      $log.debug err.backtrace.join("\n")
      nil
    end

    if miq_snapshot
      raise "Already has an EVM snapshot: #{miq_snapshot.name}, with id: #{miq_snapshot.id}"
    else
      $log.debug "#{log_prefix}: Snapshot does not exist, deleting metadata"
      .destroy
    end
  else
    $log.debug "#{log_prefix}: No existing snapshot detected for: #{instance.name}"
  end

  $log.debug "#{log_prefix}: Snapshotting instance: #{instance.name}..."
  # TODO: pass in snapshot name.
  rv = compute_service.create_image(instance.id, "EvmSnapshot", :description => options[:desc])
  rv.body['image'][:service] = image_service

  miq_snapshot = image_service.images.get(rv.body['image']['id'])

  until miq_snapshot.status.upcase == "ACTIVE"
    $log.debug "#{log_prefix}: #{miq_snapshot.status}"
    sleep 1
    # TODO(lsmola) identity is missing in Glance V2 object, fix it in Fog, then miq_snapshot.reload will work
    # miq_snapshot.reload
    miq_snapshot = image_service.images.get(miq_snapshot.id)
  end
  $log.debug "#{log_prefix}: #{miq_snapshot.status}"
  $log.debug "#{log_prefix}: EVM snapshot creation complete"

  instance..update(:miq_snapshot => miq_snapshot.id)
  return miq_snapshot
rescue => err
  $log.error "#{log_prefix}, error: #{err}"
  $log.debug err.backtrace.join("\n") if $log.debug?
  raise
end

#create_snapshot(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 52

def create_snapshot(options = {})
  log_prefix = "MIQ(#{self.class.name}##{__method__}) instance_id=[#{@instance_id}]"

  $log.debug "#{log_prefix}: Snapshotting instance: #{instance.name}..."

  snapshot = compute_service.create_image(instance.id, options[:name], :description => options[:desc])

  $log.debug "#{log_prefix}: #{snapshot.status}"
  $log.debug "#{log_prefix}: snapshot creation complete"

  return snapshot.body["image"]
rescue => err
  $log.error "#{log_prefix}, error: #{err}"
  $log.debug err.backtrace.join("\n") if $log.debug?
  raise
end

#delete_evm_snapshot(image_id) ⇒ Object



122
123
124
125
126
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
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 122

def delete_evm_snapshot(image_id)
  log_prefix = "MIQ(#{self.class.name}##{__method__}) snapshot=[#{image_id}]"

  snapshot = begin
    image_service.images.get(image_id)
  rescue => err
    $log.debug "#{log_prefix}: #{err}"
    $log.debug err.backtrace.join("\n")
    nil
  end

  if snapshot
    begin
      if snapshot_image_id != image_id
        $log.warn "#{log_prefix}: pointer from instance doesn't match #{snapshot_image_id}"
      end
      $log.info "#{log_prefix}: deleting snapshot image"

      image = compute_service.images.get(image_id)
      image..each do |m|
        next unless m.key == "block_device_mapping"
        m.value.each do |volume_snapshot|
          volume_snapshot_id = volume_snapshot["snapshot_id"]
          delete_volume_snapshot(volume_snapshot_id) if volume_snapshot_id
        end
      end
      snapshot.destroy
      .destroy
    rescue => err
      $log.debug "#{log_prefix}: #{err}"
      $log.debug err.backtrace.join("\n") if $log.debug?
    end
  else
    $log.info "#{log_prefix}: no longer exists, deleting references"
  end
end

#delete_snapshot(image_id) ⇒ Object



69
70
71
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 69

def delete_snapshot(image_id)
  delete_evm_snapshot(image_id)
end

#image_serviceObject



26
27
28
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 26

def image_service
  @image_service ||= @openstack_handle.detect_image_service
end

#instanceObject



34
35
36
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 34

def instance
  @instance ||= compute_service.servers.get(@instance_id)
end

#snapshot_image_idObject



42
43
44
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 42

def snapshot_image_id
  @snapshot_image_id ||=  && .value
end

#snapshot_metadataObject



38
39
40
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 38

def 
  @snapshot_metadata ||= instance..length > 0 && instance..get(:miq_snapshot)
end

#unmountObject



46
47
48
49
50
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 46

def unmount
  return unless @miq_vm
  @miq_vm.unmount
  @temp_image_file.unlink
end

#volume_serviceObject



30
31
32
# File 'lib/OpenStackExtract/MiqOpenStackVm/MiqOpenStackInstance.rb', line 30

def volume_service
  @volume_service ||= @openstack_handle.detect_volume_service
end