From 186d69d86e5c5686466db27cc1c708ee65210eff Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Tue, 2 Apr 2019 11:33:27 -0400 Subject: [PATCH] Add a tool to assist in reconnecting vms --- tools/reconnect_vms.rb | 55 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 tools/reconnect_vms.rb diff --git a/tools/reconnect_vms.rb b/tools/reconnect_vms.rb new file mode 100755 index 00000000000..35e610db415 --- /dev/null +++ b/tools/reconnect_vms.rb @@ -0,0 +1,55 @@ +#!/usr/bin/env ruby +require File.expand_path('../config/environment', __dir__) +require "optimist" + +opts = Optimist.options do + opt :ems_id, "The ID of the ExtManagementSystem to reconnect VMs for", :type => :integer + opt :ems_name, "The name of the ExtManagementSystem to reconnect VMs for", :type => :string + opt :by, "Property to treat as unique, defaults to :uid_ems", :type => :string, :default => "uid_ems" +end + +if opts[:ems_id].nil? && opts[:ems_name].nil? + Optimist.die :ems_id, "Must pass either --ems-id or --ems-name" +end + +ems = if opts[:ems_id].present? + ExtManagementSystem.find_by(:id => opts[:ems_id]) + else + ExtManagementSystem.find_by(:name => opts[:ems_id]) + end + +if ems.nil? + print "Failed to find EMS [#{opts.key?(:ems_id) ? opts[:ems_id] : opts[:ems_name]}]" + exit +end + +ems_id = ems.id + +# Find all VMs which match the unique key +vms_index = VmOrTemplate.where(opts[:by] => ems.vms_and_templates.pluck(opts[:by])).group_by { |vm| vm.send(opts[:by]) } + +# Select where there are exactly two with the same unique property, one archived and one active +duplicate_vms = vms_index.select { |_by, vms| vms.count == 2 && vms.select(&:active).count == 1 } +puts "Found #{duplicate_vms.count} duplicate VMs..." + +activated_vms = duplicate_vms.map do |_by, vms| + active_vms, inactive_vms = vms.partition(&:active) + + # There should only be one of each + active_vm = active_vms.first + inactive_vm = inactive_vms.first + + next if active_vm.nil? || inactive_vm.nil? + next if active_vm.created_on < inactive_vm.created_on # Always pick the older VM + + # Disconnect the new VM and activate the old VM + puts "Disconnecting Vm [#{active_vm.name}] id [#{active_vm.name}] uid_ems [#{active_vm.uid_ems}] ems_ref [#{active_vm.ems_ref}]" + active_vm.disconnect_inv + + puts "Activating Vm [#{inactive_vm.name}] id [#{inactive_vm.id}] uid_ems [#{inactive_vm.uid_ems}] ems_ref [#{inactive_vm.ems_ref}]" + inactive_vm.update_attributes!(:ems_id => ems_id, :uid_ems => active_vm.uid_ems, :ems_ref => active_vm.ems_ref) + + inactive_vm +end.compact + +puts "Activated #{activated_vms.count} VMs:\n#{activated_vms.map(&:name).join(", ")}"