Skip to content

Commit

Permalink
Don't lose vm volume attachments when refreshing cloud inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
mansam committed Feb 28, 2018
1 parent cfc0678 commit 588fda4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -361,18 +361,47 @@ def vms
public_network.ipaddress = s.public_ip_address
end

attachment_names = {'vda' => 'Root disk'}
disk_location = "vda"
if (root_size = flavor.try(:disk).to_i.gigabytes).zero?
root_size = 1.gigabytes
end
make_instance_disk(hardware, root_size, disk_location.dup, "Root disk")
make_instance_disk(hardware, root_size, disk_location.dup, attachment_names[disk_location])
ephemeral_size = flavor.try(:ephemeral).to_i.gigabytes
unless ephemeral_size.zero?
make_instance_disk(hardware, ephemeral_size, disk_location.succ!.dup, "Ephemeral disk")
disk_location = "vdb"
attachment_names[disk_location] = "Ephemeral disk"
make_instance_disk(hardware, ephemeral_size, disk_location, attachment_names[disk_location])
end
swap_size = flavor.try(:swap).to_i.megabytes
unless swap_size.zero?
make_instance_disk(hardware, swap_size, disk_location.succ!.dup, "Swap disk")
disk_location = disk_location.succ
attachment_names[disk_location] = "Swap disk"
make_instance_disk(hardware, swap_size, disk_location, attachment_names[disk_location])
end

# Make disks in the inventory for each of this server's volume attachments.
# Start by checking the raw attributes from fog to see whether whether this
# server has any attachments. If it does, then use the fog object's
# volume_attachments method to inflate them and get the device names.
# Checking the attribute first avoids making an expensive api call for
# every server when they may not all have attachments.
# Don't worry about filling in the volume, since the volume service refresh
# will take care of that.
if s.attributes.fetch("os-extended-volumes:volumes_attached", []).length > 0
s.volume_attachments.each do |attachment|
dev = File.basename(attachment['device'])
persister.disks.find_or_build_by(
:hardware => hardware,
# reuse the device names from above in the event that this is an
# instance that was booted from a volume
:device_name => attachment_names.fetch(dev, dev)
).assign_attributes(
:location => dev,
:device_type => "disk",
:controller_type => "openstack"
)
end
end
end
end
Expand Down Expand Up @@ -408,12 +437,12 @@ def make_instance_disk(hardware, size, location, name)
disk = persister.disks.find_or_build_by(
:hardware => hardware,
:device_name => name
).assign_attributes(
:location => location,
:size => size,
:device_type => "disk",
:controller_type => "openstack"
)
disk.device_name = name
disk.device_type = "disk"
disk.controller_type = "openstack"
disk.size = size
disk.location = location
disk
end

Expand Down
3 changes: 2 additions & 1 deletion spec/models/manageiq/providers/openstack/openstack_stubs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ def mocked_vms
:flavor => {"id" => i},
:availability_zone => "nova",
:private_ip_address => '10.10.10.1',
:public_ip_address => '172.1.1.2'
:public_ip_address => '172.1.1.2',
:attributes => {}
)
end
mocked_vms
Expand Down

0 comments on commit 588fda4

Please sign in to comment.