#!/usr/bin/env ruby # # convert the old data model for the gaptool server # to the new struct libpath = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib")) $:.unshift(libpath) require "#{libpath}/helpers/redis" require "#{libpath}/helpers/data" DH = Gaptool::Data puts "Removing cruft" # First, remove cruft. Like all servers created but never registered, etc ['instance:*', '_*', 'apikey:*', 'service:*', 'running', 'ec2*', 'failover', 'capacity', 'sg:*'].each do |pattern| $redis.keys(pattern).each do |key| $redis.del key end end puts "Migrating roles..." $redis.keys('role:*').each do |key| puts " - #{key}" data = $redis.hgetall(key) role = key.sub('role:', '') unless data['ami'].nil? # We didn't have per-regin ami in roles, so # default to us-west-2 data['amis'] = {'us-west-2' => data['ami']} role.delete('ami') end DH.save_role_data(role, data) end puts "Migrating ami info to role..." $redis.keys('amis:*').each do |key| role = key.sub("amis:", '') puts " - #{key}" amis = $redis.hgetall(key) roledata = DH.get_role_data(role) roledata['amis'] = amis.merge(roledata['amis'] || {}) DH.save_role_data(role, roledata) $redis.del(key) end puts "Migrating apps..." $redis.keys('app:*').each do |key| puts " - #{key}" appname = key.sub('app:', '') role = $redis.hget(key, "role") if role.nil? || appname.nil? puts "Missing info for app: #{appname} role: #{role}" end $redis.del(key) DH.add_app(appname, role) unless role.nil? end puts "Migrating instances..." $redis.keys('host:*').each do |key| puts " - #{key}" pfx, role, env, iid = key.split(':') data = $redis.hgetall(key) data['role'] = role data['environment'] = env DH.addserver(iid, data, nil) $redis.del(key) # remove stale keys from hsh $redis.hdel "instance:#{iid}", ["capacity", "mirror", "secret", 'instance'] end puts "Removing unneeded info from instance data..." $redis.smembers('instances').each do |inst| $redis.hdel "instance:#{inst}", ["capacity", "mirror", "secret", "instance"] if $redis.hget("instance:#{inst}", 'chef_runlist') == "recipe[init]" $redis.hdel "instance:#{inst}", "chef_runlist" end term = $redis.hget("instance:#{inst}", "terminate") unless term.nil? unless term == 'true' $redis.hset("instance:#{inst}", "terminable", term) end $redis.hdel("instance:#{inst}", "terminate") end end puts "Rename old config keys" $redis.hset('config', 'chef_repo', $redis.hget('config', 'chefrepo')) $redis.hset('config', 'chef_branch', $redis.hget('config', 'chefbranch')) puts "Removing old config keys" ['remoteredis:host', 'remoteredis:port', 'aws_id', 'aws_secret', 'chefrepo', 'chefbranch'].each do |key| $redis.hdel 'config', key end $redis.save