Skip to content

Instantly share code, notes, and snippets.

@pstengel
Created July 6, 2011 15:39
Show Gist options
  • Save pstengel/1067549 to your computer and use it in GitHub Desktop.
Save pstengel/1067549 to your computer and use it in GitHub Desktop.
class BFG9000Logger
def logger(options = nil)
@logger ||= Logger.new options
end
end
#!/usr/bin/env ruby
$:.unshift File.join(File.dirname(__FILE__), 'lib')
# System/Gems
require 'rubygems'
require 'pp'
require 'yaml'
require 'trollop'
require 'mysql2'
require 'logger'
require 'open-uri'
require 'net/ssh'
require 'openssl'
# My libs
require 'bfg9000-logger'
require 'migrate-server'
require 'migrate-user'
# Main
self.logger STDOUT
sql = nil
sources = Array.new
destinations = Array.new
opts = Trollop::options do
opt :'m3-config', "Location of m3.conf for the DB information", :required => true, :type => :string
opt :source, "Source server - there can be more than one, and it can reference a newline-separated file", :multi => true, :required => true, :type => :string
opt :destination, "Destination server - there can be more than one, and it can a newline-separated reference a file", :multi => true, :required => true, :type => :string
opt :commit, "The default mode is to output actions that would be taken - this flag actually commits the changes to the DB", :default => false
end
@logger.info "Loading the BFG 9000"
# Load the m3.conf and test before bothering the do the rest of this stuff
if opts[:commit]
@logger.info "Loading m3.conf"
raise "#{opts[:'m3-config']} doesn't exist!" unless File.exists?(opts[:'m3-config'])
m3 = YAML.load_file(opts[:'m3-config'])
begin
@logger.info "Testing MySQL connection"
sql = Mysql2::Client.new(:host => m3["database_hostname"],
:username => m3["database_username"],
:password => m3["database_password"],
:database => m3["database"])
rescue
log.fatal "MySQL connection failed!"
raise "Invalid SQL credentials!"
end
@logger.info "MySQL connection successful."
end
# Load the source servers
@logger.info "Initializing the source server list"
opts[:source].each do |s|
if File.exists?(s)
File.open(s).each_line { |l| sources << MigrateServer.new(l.strip) }
else
sources << MigrateServer.new(s)
end
end
# Load the destination servers
@logger.info "Initializing the destination server list"
opts[:destination].each do |s|
if File.exists?(s)
File.open(s).each_line { |l| destinations << MigrateServer.new(l.strip) }
else
destinations << MigrateServer.new(s)
end
end
class MigrateServer < BFG9000Logger
attr_reader :hostname, :ip, :slots_available, :usernames, :domains, :users
def load_synco
@logger.info "Loading Synco record for #{@hostname}"
synco = YAML.load(open('SOME URL I DONT WANT YOU TO SEE',
:http_basic_authentication => ['EFF', 'OFF'],
:ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE))
server = synco.detect { |s| s['name'] == @hostname }
raise "#{@hostname} not found in Synco!" if server.nil?
@slots_available = server['slots']
@ip = server['ip']
end
def load_users
@logger.info "Getting user list for #{@hostname}"
Net::SSH.start(@ip, 'root') do |ssh|
YAML.load(ssh.exec!("YOU ARENT ALLOWED TO SEE THIS EITHER").sub(/^stdin: is not a tty.*/, '')).each do |user, info|
@usernames << user
@domains << info['domain']
info['subs'].each { |k, i| @usernames << k; @domains << i['domain'] } if info['subs']
@users << MigrateUser.new(user, info)
end
end
end
def initialize(name)
@hostname = name
@usernames = Array.new
@domains = Array.new
@users = Array.new
self.load_synco
self.load_users
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment