Skip to content
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

Added support for VM delete #184

Merged
merged 1 commit into from
Feb 23, 2018

Conversation

VojkoR
Copy link

@VojkoR VojkoR commented Feb 19, 2018

@VojkoR VojkoR force-pushed the destroy_vm_for_vmware_provider branch from 087e465 to 7252c43 Compare February 19, 2018 12:16
@VojkoR
Copy link
Author

VojkoR commented Feb 19, 2018

Use "Power -> Delete" (shown in image) to delete VM.

how-to-delete-vm

Copy link
Contributor

@miha-plesko miha-plesko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @VojkoR , I only have one tiny comment, otherwise LGTM 👍

def raw_destroy
raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system
with_provider_object(&:undeploy)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking, but could you please remove this empty line?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line removed

@miha-plesko
Copy link
Contributor

@miq-bot assign @agrare
@miq-bot add_label enhancement,gaprindashvili/yes

def raw_destroy
raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system
with_provider_object(&:undeploy)
ext_management_system.with_provider_connection do |service|
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since with_provider_object calls with_provider_connection can you do the :undeploy in this block so you only have to connect once?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VojkoR I believe what @agrare is suggesting is not to use the with_provider_object to undeploy but rather use it like this

ext_management_system.with_provider_connection do |service|
  service.post_undeploy_vapp(ems_ref)
  service.delete_vapp(ems_ref)
end

https://github.com/xlab-si/fog-vcloud-director/blob/master/lib/fog/vcloud_director/requests/compute/post_undeploy_vapp.rb#L36

ext_management_system.with_provider_connection do |service|
service.delete_vapp(ems_ref)
end
update_attributes!(:raw_power_state => "DELETED")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is DELETED a valid power state? How about "off" since it is a valid vcloud_director state and also recognized by the vcloud provider

@VojkoR VojkoR force-pushed the destroy_vm_for_vmware_provider branch from 8b9b085 to 6daddc7 Compare February 22, 2018 08:15
raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system
ext_management_system.with_provider_connection do |service|
begin
response = service.post_undeploy_vapp(ems_ref, :UndeployPowerAction => 'shutdown')
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before delete action is called we need to stop VM. In case VM is already stopped action will rise an error. To successfully delete VM we are catching this error and just log the message what happened.

ensure
response = service.delete_vapp(ems_ref)
service.process_task(response.body)
update_attributes!(:raw_power_state => "terminated")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think correct power state for deleted VM should be "terminated". Maybe I should set power state to "unknown" instead of "terminated"?

@VojkoR
Copy link
Author

VojkoR commented Feb 22, 2018

Hi @agrare I changed functionality as requested. I added some extra code, to correctly implement DELETE action. I wrote some comments for the code that was added and why it was added.

begin
response = service.post_undeploy_vapp(ems_ref, :UndeployPowerAction => 'shutdown')
service.process_task(response.body)
rescue => err
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rescue the exact exception that is raised if the VM is already stopped?

rescue => err
$vcloud_log.debug("vm=[#{name}], error: VM not running! Ignoring error: #{err}")
ensure
response = service.delete_vapp(ems_ref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like doing this in the ensure block assuming that you're rescuing a "vm already powered off" exception because it could be something completely different and we'll still try to do this.
I'd rather rescue the exact exception then do this outside the begin/rescue block like:

begin
  service.post_undeploy_vapp(ems_ref, :UndeployPowerAction => 'shutdown')
rescue Vcloud::VmAlreadyPoweredOff /* not sure what this exception actually is, just for demonstration */
  # this is expected if the vapp is already off
end

service.process_task(response.body)
update_attributes!(:raw_power_state => "terminated")

This commit now adds support for removal of a stopped VM from vApp.
Besides removal operation it overrides the `supports :terminate` feature
because the VM must be powered off to allow to be deleted from VCD.

Signed-off-by: Vojko Rozic <[email protected]>
Signed-off-by: Gregor Berginc <[email protected]>
@gberginc gberginc force-pushed the destroy_vm_for_vmware_provider branch from 6daddc7 to 0427809 Compare February 23, 2018 10:12
@miq-bot
Copy link
Member

miq-bot commented Feb 23, 2018

Checked commit https://github.com/VojkoR/manageiq-providers-vmware/commit/042780967d87c0bc9524a57d71cf174099398791 with ruby 2.3.3, rubocop 0.52.0, haml-lint 0.20.0, and yamllint 1.10.0
3 files checked, 1 offense detected

app/models/manageiq/providers/vmware/cloud_manager.rb

Copy link
Contributor

@gberginc gberginc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agrare We have simplified the code to handle removal of VM in a way VCD handles it. I've added two comments in the code to explain the rationale.

I hope this will be ok.


included do
supports :terminate do
unsupported_reason_add(:terminate, "The VM is powered on") unless current_state == "off"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@agrare we have modified the code to resemble the behaviour of VCD. Now, it is not possible to delete a VM that is running (powered on). To this end we had to redefine the supports_terminate? by looking at the current_state of the VM.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I like that, we actually have a helper method for this that you can use, https://github.com/ManageIQ/manageiq/blob/master/app/models/vm/operations/power.rb#L22-L24

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @agrare. I’ll make another PR to use this.

def raw_destroy
raise "VM has no #{ui_lookup(:table => "ext_management_systems")}, unable to destroy VM" unless ext_management_system
ext_management_system.with_provider_connection do |service|
response = service.delete_vapp(ems_ref)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking for exceptions we are simply passing to the underlying provider connection. Looking at OpenStack and Amazon, this is exactly how these two are handled. In case an exception occurs it will be propagated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 perfect

@agrare
Copy link
Member

agrare commented Feb 23, 2018

This looks good, can you send a followup to use that vm_powered_on? helper? I don't want to hold this up for that though.

@agrare agrare merged commit 1dfa303 into ManageIQ:master Feb 23, 2018
@agrare agrare added this to the Sprint 80 Ending Feb 26, 2018 milestone Feb 23, 2018
@miha-plesko
Copy link
Contributor

@miha-plesko
Copy link
Contributor

@agrare apologies for delay, but here comes the followup PR: #204

simaishi pushed a commit that referenced this pull request Mar 7, 2018
@simaishi
Copy link
Contributor

simaishi commented Mar 7, 2018

Gaprindashvili backport details:

$ git log -1
commit fa49cf6bbc6e66d5373f03ed62bc1e37f6127ec9
Author: Adam Grare <[email protected]>
Date:   Fri Feb 23 09:34:48 2018 -0500

    Merge pull request #184 from VojkoR/destroy_vm_for_vmware_provider
    
    Added support for VM delete
    (cherry picked from commit 1dfa303668712cc1ee028d640cfbbaed734ca5f3)
    
    https://bugzilla.redhat.com/show_bug.cgi?id=1552683

agrare pushed a commit to agrare/manageiq-providers-vmware that referenced this pull request Apr 15, 2019
…h-based-on-events

Do targeted refresh based on Openstack events.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants