#!/usr/bin/env oo-ruby
require 'socket'
require '/var/www/openshift/broker/config/environment'
# Disable analytics for root
Rails.configuration.analytics[:enabled] = false

lock = ARGV.shift
done = ARGV.shift
collection = OpenShift::DataStore.db.collection(:ose_upgrade)
if done
  # let others know we finished successfully
  collection.insert({:step_complete => lock})
  exit 0
elsif OpenShift::DistributedLock.obtain_lock(lock, Socket.gethostname, true)
  # note: we never release the lock. it should only ever finish once from one host.
  exit 0
elsif collection.find_one({:step_complete => lock})
  # another broker completed this step; skip it on this one.
  puts "SKIP"
  exit 0
else
  puts "Waiting for another broker to complete this step."
  exit 1
end
