Last active
September 23, 2020 13:01
-
-
Save robbmanes/6cdb222b2f77227c8d75f9f9e9cb6a83 to your computer and use it in GitHub Desktop.
Join two Vmware providers in ManageIQ
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
# GPLv2 License | |
# ===================== | |
# | |
# Copyright © 2018 Robb Manes <robb.manes@gmail.com> | |
# | |
# join_vmware_providers is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 2 of the License, or | |
# (at your option) any later version. | |
# | |
# join_vmware_providers is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with join_vmware_providers. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# cd /var/www/miq/vmdb/ | |
# bundle exec rails runner join_vmware_providers.rb --source <SRC_PROVIDER> --dest <DEST_PROVIDER> | |
require 'trollop' | |
require 'logger' | |
module JoinVmwareProvider | |
class Cli | |
attr_accessor :options | |
def parse(args, env = {}) | |
args.shift if args.first == "--" | |
self.options = Trollop.options(args) do | |
banner "" \ | |
"Usage: cd /var/www/miq/vmdb/" \ | |
" bundle exec rails runner tools/join_provider_data.rb [options]" | |
opt :source, "Source", :type => :string | |
opt :destination, "Destination", :type => :string | |
opt :verbose, "Verbose", :type => :boolean | |
end | |
self.options = options.delete_if { |_n, v| v.blank? } | |
Trollop::die :source, "must specify source provider" unless self.options.key? :source | |
Trollop::die :destination, "must specify destination provider" unless self.options.key? :destination | |
self | |
end | |
def run | |
::JoinVmwareProvider::ProviderMerger.new(options).run | |
end | |
def self.run(args, env = {}) | |
new.parse(args, env).run | |
end | |
end | |
class ProviderMerger | |
def initialize(args = {}) | |
@logger = Logger.new(STDOUT) | |
if args.verbose | |
@logger.level = Logger::DEBUG | |
else | |
@logger.level = Logger::INFO | |
end | |
@src_ems = get_provider args.source | |
@dst_ems = get_provider args.destination | |
@logger.info("Using source provider #{@src_ems.name}:#{@src_ems.id} and destination provider #{@dst_ems.name}:#{@dst_ems.id}.") | |
end | |
def get_provider(provider_string) | |
unless provider_string.scan(/\D/).empty? | |
ems = ManageIQ::Providers::Vmware::InfraManager.find_by(:name => provider_string) | |
else | |
ems = ManageIQ::Providers::Vmware::InfraManager.find_by(:id => provider_string) | |
end | |
if ems.nil? | |
@logger.error("Found no EMS for id or string #{provider_string}.") | |
exit | |
end | |
@logger.info("Found provider #{ems.name} with id #{ems.id}.") | |
ems | |
end | |
def run() | |
merge_duplicate_vms | |
merge_duplicate_hosts | |
merge_duplicate_clusters | |
end | |
def merge_duplicate_vms | |
src_vms = @src_ems.vms | |
dst_vms = @dst_ems.vms | |
@logger.debug("Found #{src_vms.count} VM's on source provider and #{dst_vms.count} VM's on destination provider.") | |
@logger.info("Merging duplicate VM data...") | |
dups = 0 | |
dst_vms.each do |dst_vm| | |
src_vm = src_vms.find_by(:name => dst_vm.name) | |
unless src_vm.nil? | |
@logger.debug("Beginning VM merge of #{dst_vm.name}...") | |
@logger.debug("Merging metrics for VM #{dst_vm.name}...") | |
src_metrics = src_vm.metrics.all | |
src_metrics.each do |metric| | |
dst_vm.metrics << metric | |
end | |
@logger.debug("Merging tags for VM #{dst_vm.name}...") | |
src_tags = src_vm.tags.all | |
src_tags.each do |tag| | |
dst_vm.tags << tag | |
end | |
@logger.debug("Updating EVM owner for VM #{dst_vm.name}...") | |
dst_vm.update(:evm_owner => src_vm.evm_owner) | |
@logger.debug("Merging groups for VM #{dst_vm.name}...") | |
dst_vm.update(:miq_group => src_vm.miq_group) | |
@logger.debug("Updating retirement settings for VM #{dst_vm.name}...") | |
dst_vm.retirement_state = src_vm.retirement_state | |
dst_vm.retires_on = src_vm.retires_on | |
dst_vm.save | |
dups = dups + 1 | |
@logger.debug("Finished merge for VM #{dst_vm.name}...") | |
end | |
end | |
@logger.info("Merged #{dups} duplicate VM's.") | |
end | |
def merge_duplicate_hosts | |
src_hosts = @src_ems.hosts | |
dst_hosts = @dst_ems.hosts | |
@logger.debug("Found #{src_hosts.count} hosts on source provider and #{dst_hosts.count} hosts on destination provider.") | |
@logger.info("Merging duplicate hosts data...") | |
dups = 0 | |
dst_hosts.each do |dst_vm| | |
src_host = src_hosts.find_by(:name => dst_host.name) | |
unless src_host.nil? | |
@logger.debug("Beginning host merge of #{dst_host.name}") | |
@logger.debug("Merging metrics for host #{dst_host.name}") | |
src_metrics = src_host.metrics.all | |
src_metrics.each do |metric| | |
dst_host.metrics << metric | |
end | |
@logger.debug("Merging tags for host #{dst_host.name}") | |
src_tags = src_host.tags.all | |
src_tags.each do |tag| | |
dst_host.tags << tag | |
end | |
dups = dups + 1 | |
@logger.debug("Finished merge for host #{dst_host.name}...") | |
end | |
end | |
@logger.info("Merged #{dups} duplicate hosts.") | |
end | |
def merge_duplicate_clusters | |
src_clusters = @src_ems.clusters | |
dst_clusters = @dst_ems.clusters | |
@logger.debug("Found #{src_clusters.count} clusters on source provider and #{dst_clusters.count} clusters on destination provider.") | |
@logger.info("Merging duplicate cluster data...") | |
dups = 0 | |
dst_clusters.each do |dst_cluster| | |
src_cluster = src_clusters.find_by(:name => dst_cluster.name) | |
unless src_cluster.nil? | |
@logger.debug("Beginning cluster merge of #{dst_cluster.name}") | |
@logger.debug("Merging metrics for cluster #{dst_cluster.name}") | |
src_metrics = src_cluster.metrics.all | |
src_metrics.each do |metric| | |
dst_cluster.metrics << metric | |
end | |
@logger.debug("Merging tags for cluster #{dst_cluster.name}") | |
src_tags = src_cluster.tags.all | |
src_tags.each do |tag| | |
dst_cluster.tags << tag | |
end | |
dups = dups + 1 | |
@logger.debug("Finished merge for cluster #{dst_cluster.name}...") | |
end | |
end | |
@logger.info("Merged #{dups} duplicate clusters.") | |
end | |
end | |
end | |
JoinVmwareProvider::Cli.run(ARGV, ENV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
trollop
gem was switched out for theoptimist
gem, because, rightfully so, it's pretty offensive. Anything on CFME >= 5.0 won't work with the above script.Changelog in Red Hat documentation is available here.
I wrote this for CFME 4.5 and haven't kept it updated, so it might be possible it doesn't work with the intended consequences anymore as well. You're welcome to try and switch out
trollop
foroptimist
where applicable, and if someone verifies that that's all that needs to be done (I no longer work on CloudForms) then I can update it here.