Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Created January 9, 2018 14:22
Show Gist options
  • Save robbmanes/9803d3e0f82b7ec5bd25bfd053163d3e to your computer and use it in GitHub Desktop.
Save robbmanes/9803d3e0f82b7ec5bd25bfd053163d3e to your computer and use it in GitHub Desktop.
ManageIQ Unretire Service Tool
# cd /var/www/miq/vmdb/
# bundle exec rails runner tools/unretire_service.rb <service name>
require 'logger'
module UnretireService
class Cli
attr_accessor :options
def parse(args, env = {})
args.shift if args.first == "--"
if args.first.to_s.empty?
puts "Please specify a service name or service id to unretire"
exit
end
service = args.first
end
def self.run(args, env = {})
service = new.parse(args, env)
sp = ServiceManager.new(service)
sp.repair_ancestry_tree
end
end
class ServiceManager
def initialize(service)
@logger = Logger.new(STDOUT)
@logger.level = Logger::DEBUG
@service = get_service service
@logger.info "Found service: name:#{@service.name}, id:#{@service.id}"
@logger.info "Gathering child services for #{@service.name}"
@children = get_children @service
@logger.info "#{@children.length} descendants of service #{@service.name} found"
end
def get_service(name)
unless name.scan(/\D/).empty?
services = Service.where(name: name)
else
services = Service.where(id: name)
end
if services.empty?
@logger.error "No services found with name #{name}."
exit
end
if services.first != services.last
@logger.error "Multiple services contain the name #{name}."
@logger.error "Please re-run the command using one of the following IDs:"
services.each do |s|
puts "#{s.id}"
end
exit
end
service = services.first
end
def get_children(service)
Service.where("ancestry LIKE ?", "%#{service.id}%")
end
def set_unretire(service)
service.update_attributes(:retired => "false", :retires_on => "nil", :retirement_state => "nil")
end
def repair_ancestry_tree
children = @children
children.each do |child|
set_unretire(child)
ancestor = child.ancestry.split("/").last
parent = Service.find(ancestor.to_i)
parent.add_resource!(child)
@logger.debug "Added child service #{child.name}:#{child.id} to parent #{parent.name}:#{parent.id}"
end
end
end
end
UnretireService::Cli.run(ARGV, ENV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment