-
Notifications
You must be signed in to change notification settings - Fork 69
Proxmox extention for foreman_bootdisk #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,50 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module ForemanBootdisk | ||||||
module ComputeResources | ||||||
module Proxmox | ||||||
CDROM_VOLUME = 'ide2' | ||||||
|
||||||
def capabilities | ||||||
super + [:bootdisk] | ||||||
end | ||||||
|
||||||
def iso_upload(iso, vm_uuid) | ||||||
server = find_vm_by_uuid(vm_uuid) | ||||||
server.ssh_options = { password: fog_credentials[:proxmox_password] } | ||||||
server.ssh_ip_address = proxmox_host | ||||||
server.username = client.credentials[:current_user].split('@').first | ||||||
server.scp_upload(iso, '/var/lib/vz/template/iso/') | ||||||
server.reload | ||||||
storage = storages(server.node_id, 'iso')[0] | ||||||
storage.volumes.any? { |v| v.volid.include? File.basename(iso) } | ||||||
end | ||||||
|
||||||
def iso_attach(iso, vm_uuid) | ||||||
server = find_vm_by_uuid(vm_uuid) | ||||||
storage = storages(server.node_id, 'iso')[0] | ||||||
volume = storage.volumes.detect { |v| v.volid.include? File.basename(iso) } | ||||||
disks = server.disks.map { |disk| disk.split(":")[0] }.join(";") | ||||||
server.update({ ide2: "#{volume.volid},media=cdrom" }) | ||||||
server.update({ boot: "order=ide2;#{disks}" }) | ||||||
server.reboot | ||||||
end | ||||||
|
||||||
def iso_detach(vm_uuid) | ||||||
server = find_vm_by_uuid(vm_uuid) | ||||||
|
||||||
# get volid to delete iso after detaching from vm | ||||||
volid = server.volumes.get(CDROM_VOLUME).volid | ||||||
server.update({ ide2: "none,media=cdrom" }) | ||||||
|
||||||
# cdrom will be ejected on next power off | ||||||
server.detach(CDROM_VOLUME) | ||||||
|
||||||
# delete the iso file from proxmox server | ||||||
storage = storages(server.node_id, 'iso')[0] | ||||||
volume = storage.volumes.detect { |v| v.volid.include? volid } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When would this volume ID not be exactly equal? If the volume IDs are integers I'd imagine this also end up being
Suggested change
Also, previously you already used
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't want to destroy the disk 'ide2' as the operation is not possible when vm is powered on. We just want to destroy the volume so that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, server.volumes are disks of the server so it returns the disks, |
||||||
volume.destroy | ||||||
end | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_plugin_helper' | ||
|
||
module ForemanBootdisk | ||
class ProxmoxTest < ActiveSupport::TestCase | ||
|
||
describe '#capabilities' do | ||
setup do | ||
skip unless ForemanBootdisk.with_proxmox? | ||
@cr = FactoryBot.build(:proxmox_cr) | ||
end | ||
|
||
test 'should include bootdisk' do | ||
assert_includes @cr.capabilities, :bootdisk | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_plugin_helper' | ||
|
||
module ForemanBootdisk | ||
class OrchestrationProxmoxComputeTest < ActiveSupport::TestCase | ||
setup do | ||
disable_orchestration | ||
skip unless ForemanBootdisk.with_proxmox? | ||
@proxmox_cr = FactoryBot.build(:proxmox_cr) | ||
@proxmox_host = FactoryBot.build(:host, :managed, | ||
compute_resource: @proxmox_cr, | ||
provision_method: 'bootdisk') | ||
end | ||
|
||
test 'provisioning a host with provision method bootdisk in proxmox should upload iso' do | ||
@proxmox_cr.expects(:iso_upload) | ||
@proxmox_host.send(:setIsoImage) | ||
end | ||
|
||
test 'provisioning a host with provision method bootdisk in proxmox should attach iso' do | ||
@proxmox_cr.expects(:iso_attach) | ||
@proxmox_host.send(:setAttachIsoImage) | ||
end | ||
|
||
test 'provisioning a host with provision method bootdisk in proxmox should detach iso' do | ||
@proxmox_cr.expects(:iso_detach) | ||
@proxmox_host.send(:setDetachIsoImage) | ||
end | ||
|
||
test 'provisioning a new proxmox host with provision method bootdisk should queue bootdisk tasks' do | ||
@proxmox_host.stubs(:compute?).returns(true) | ||
@proxmox_host.stubs(:build?).returns(true) | ||
@proxmox_host.send(:queue_bootdisk_compute) | ||
tasks = @proxmox_host.queue.all.map(&:name) | ||
assert_includes tasks, "Generating ISO image for #{@proxmox_host.name}" | ||
assert_includes tasks, "Upload ISO image to datastore for #{@proxmox_host.name}" | ||
assert_includes tasks, "Attach ISO image to CDROM drive for #{@proxmox_host.name}" | ||
assert_not_includes tasks, "Detach ISO image from CDROM drive for #{@proxmox_host.name}" | ||
end | ||
|
||
test 'rebuilding a proxmox host with provision method bootdisk should queue bootdisk tasks' do | ||
@proxmox_host.stubs(:compute?).returns(true) | ||
old = stub() | ||
old.stubs(:build?).returns(false) | ||
@proxmox_host.stubs(:old).returns(old) | ||
@proxmox_host.stubs(:build?).returns(true) | ||
@proxmox_host.send(:queue_bootdisk_compute) | ||
tasks = @proxmox_host.queue.all.map(&:name) | ||
assert_includes tasks, "Generating ISO image for #{@proxmox_host.name}" | ||
assert_includes tasks, "Upload ISO image to datastore for #{@proxmox_host.name}" | ||
assert_includes tasks, "Attach ISO image to CDROM drive for #{@proxmox_host.name}" | ||
assert_not_includes tasks, "Detach ISO image from CDROM drive for #{@proxmox_host.name}" | ||
end | ||
|
||
test 'the iso should be detached when the proxmox host leaves build mode' do | ||
@proxmox_host.stubs(:compute?).returns(true) | ||
old = stub() | ||
old.stubs(:build?).returns(true) | ||
@proxmox_host.stubs(:old).returns(old) | ||
@proxmox_host.stubs(:build?).returns(false) | ||
@proxmox_host.send(:queue_bootdisk_compute) | ||
tasks = @proxmox_host.queue.all.map(&:name) | ||
assert_not_includes tasks, "Generating ISO image for #{@proxmox_host.name}" | ||
assert_not_includes tasks, "Upload ISO image to datastore for #{@proxmox_host.name}" | ||
assert_not_includes tasks, "Attach ISO image to CDROM drive for #{@proxmox_host.name}" | ||
assert_includes tasks, "Detach ISO image from CDROM drive for #{@proxmox_host.name}" | ||
end | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.