#!/usr/bin/ruby

require 'rubygems'
require "rest_connection"

# Set this to key in the st_collection that matches the ID
# of the ServerTemplate you want to replicated MCIs from.
SOURCE_KEY = :base

# Collection of all ServerTemplate IDs to update
# Staging
st_collection = {
 :base_rsb => 243993001,
 :lamp_rsb => 243995001,
 :base => 243994001,
 :lamp_mysql_5_1 => 243996001,
 :lamp_mysql_5_5 => 243997001,
 :lamp_trial => 243998001,
 :load_balancer => 243999001,
 :php_appserver => 244000001,
 :rails_passenger => 244001001,
 :tomcat_app => 244002001,
 :db_mysql_5_1 => 244003001,
 :db_mysql_5_5 => 244004001,
 :storage_toolbox => 244005001,
 :sys_dns => 237966001
}

# Production (v13)
#st_collection = {
# :base_rsb => 91124,
# :lamp_rsb => 129529,
# :base => 104181,
# :lamp_mysql_5_1 => 106233,
# :lamp_mysql_5_5 => 226676001,
# :lamp_trial => 226834001,
# :load_balancer => 120077,
# :php_appserver => 27958,  
# :rails_passenger => 139492,
# :tomcat_app => 122208,
# :db_mysql_5_1 => 107666,
# :db_mysql_5_5 => 214130001,
# :storage_toolbox => 104346 
#}

# Display Mapping (useful for verifying collection is okay)
#st_collection.each { |k,v| puts "\n\n#{k} == #{ServerTemplate.find(v).nickname}\n\n\n" }

puts "Rest_connection log set to /tmp/rc.log"
ENV['REST_CONNECTION_LOG'] = "/tmp/rc.log"

# Run mcicp from SOURCE_KEY to collection of servertemplates
src_id = st_collection[SOURCE_KEY]
st_collection.each do |dst_key, dst_id| 
  next if dst_id == src_id
  puts '-'*25
  puts "Copying MCIs from #{SOURCE_KEY} to #{dst_key}"
  skip_param = dst_key.to_s =~ /mysql_5_5/ ? "--skip Ubuntu" : ""
  bin = File.expand_path("../mcicp",__FILE__)
  cmd = "#{bin} --from #{src_id} --to #{dst_id} #{skip_param}"
  puts cmd
  puts `#{cmd}`
  puts '-'*25
end

