diff --git a/bin/rt-client.rb b/bin/rt-client.rb index 103a0e9..a2802bb 100644 --- a/bin/rt-client.rb +++ b/bin/rt-client.rb @@ -4,22 +4,43 @@ SERVER_URL = "http://pagerank.gorillanation.com" -puts "> Importing sites from Asset Tracker ..." +puts "> Syncing with sites from Asset Tracker ..." # get hosts from RT hosts = `rt list -t asset "Type='Site' AND Status='production'" | awk -F ":" '{print $2}' | awk '{print $1}'`.split -# get sites from PageRankDashboard +# get sites from PageRankDashboard and hash them by name +site_hash = {} response = RestClient.get "#{SERVER_URL}/sites.json", :content_type => 'application/json', :accept => :json -site_names = JSON.parse(response).collect{|site| site['name'] } +JSON.parse(response).map{|site_data| site_hash[site_data['name']] = site_data} +site_names = site_hash.keys new_sites = hosts - site_names +decomissioned_sites = site_names - hosts +# add new sites from RT to page rank new_sites.each do |site_name| next if site_name =~ /gorillanation\.com/ # skip sites with GN domains - puts "+ Importing #{site_name}" + print "+ Importing #{site_name}..." json_string = { :site => { :name => site_name } }.to_json - RestClient.post "#{SERVER_URL}/sites.json", json_string, :content_type => :json, :accept => :json + begin + RestClient.post "#{SERVER_URL}/sites.json", json_string, :content_type => :json, :accept => :json + puts "OK" + rescue => e + puts "ERROR" + end end -puts "> Import complete" \ No newline at end of file +# remove decomissioned sites +decomissioned_sites.each do |site_name| + print "- Removing #{site_name}..." + site_id = site_hash[site_name]['id'] + begin + RestClient.delete "#{SERVER_URL}/sites/#{site_id}.json", :content_type => :json, :accept => :json + puts "OK" + rescue => e + puts "ERROR" + end +end + +puts "> Sync complete" \ No newline at end of file