-
Notifications
You must be signed in to change notification settings - Fork 898
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a tool to assist in reconnecting vms
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env ruby | ||
require File.expand_path('../config/environment', __dir__) | ||
require "optimist" | ||
require "byebug" | ||
|
||
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 # There should only be one of each | ||
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.update_attributes!(:ems_id => ems_id) | ||
|
||
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) | ||
|
||
inactive_vm | ||
end.compact | ||
|
||
puts "Activated #{activated_vms.count} VMs:\n#{activated_vms.map(&:name).join(", ")}" |