set :source_machine, "mothercompiler"
set :install_dir,  "/opt/hypertable"
set :hypertable_version, "0.9.2.8"
set :default_dfs, "hadoop"
set :default_config, "test-aol-basic.cfg"

role :master, "mothercompiler"
role :hyperspace, "mothercompiler", "motherlode000", "motherlode001"
role :slave,  "motherlode000", "motherlode001", "motherlode002", "motherlode003", \
              "motherlode004", "motherlode005", "motherlode006", "motherlode007", \
              "motherlode008"
role :localhost, "mothercompiler"

######################### END OF USER CONFIGURATION ############################

set(:dfs) do
  "#{default_dfs}"
end unless exists?(:dfs)

set(:config) do
  "#{default_config}"
end unless exists?(:config)

set :config_file, "#{config}".split('/')[-1]
set :config_option, \
    "--config=#{install_dir}/#{hypertable_version}/conf/#{config_file}"

 desc <<-DESC
    Copies config file to installation on localhost.
    This task runs on localhost and copies the config file specified \
    by the variable 'config' (default=#{config}) \
    to the installation directory specified by the variable 'install_dir' \
    (default=#{install_dir})
 DESC
task :copy_config, :roles => :localhost do
  run("rsync #{config} #{install_dir}/#{hypertable_version}/conf")
end

 desc <<-DESC
    rsyncs installation directory to cluster.  For each machine in the \
    cluster, his commannd  rsyncs the installation from the source \
    installation machine specified by the variable 'source_machine' \
    (default=#{source_machine})
 DESC
task :rsync_installation, :roles => [:master, :slave] do
  run "rsync -av --exclude=log --exclude=run --exclude=demo --exclude=fs " \
      "--exclude=hyperspace/ " \
      "#{source_machine}:#{install_dir}/#{hypertable_version} #{install_dir}"
end

 desc <<-DESC
    sets up the symbolic link 'current' in the installation area \
    to point to the directory of the current version
    (default=#{hypertable_version})
 DESC
task :set_current, :roles => [:master, :slave] do
  run <<-CMD
   cd #{install_dir} &&
   rm -f current &&
   ln -s #{hypertable_version} current
  CMD
end

 desc <<-DESC
    Distributes installation.  This task copiles the config file and \
    then rsyncs the installation to each machine in the cluster
 DESC

task :dist do
  transaction do
    copy_config
    rsync_installation
    set_current
  end
end

desc "Starts all processes."
task :start do
  transaction do
    start_hyperspace
    start_master
    start_slaves
  end
end

desc "Starts hyperspace processes."
task :start_hyperspace, :roles => :hyperspace do
  run <<-CMD
   #{install_dir}/current/bin/start-hyperspace.sh \
      #{config_option}
  CMD
end

desc "Starts master processes."
task :start_master, :roles => :master do
  run <<-CMD
   #{install_dir}/current/bin/start-dfsbroker.sh #{dfs} \
      #{config_option} &&
   #{install_dir}/current/bin/start-master.sh #{config_option}
  CMD
end

desc "Starts slave processes."
task :start_slaves, :roles => :slave do
  run <<-CMD
   #{install_dir}/current/bin/random-wait.sh 5 &&
   #{install_dir}/current/bin/start-dfsbroker.sh #{dfs} \
      #{config_option} &&
   #{install_dir}/current/bin/start-rangeserver.sh \
      #{config_option} &&
   #{install_dir}/current/bin/start-thriftbroker.sh \
      #{config_option}
  CMD
end

desc "Starts DFS brokers."
task :start_dfsbrokers, :roles => [:master, :slave] do
  run "#{install_dir}/current/bin/start-dfsbroker.sh #{dfs} \
      #{config_option}"
end

desc "Stops all servers."
task :stop do
  transaction do
    stop_slaves
    stop_master
    stop_hyperspace
  end
end

desc "Stops slave processes."
task :stop_slaves, :roles => :slave do
  run <<-CMD
  #{install_dir}/current/bin/stop-servers.sh --no-hyperspace
  CMD
end

desc "Stops master processes."
task :stop_master, :roles => :master do
  run <<-CMD
  #{install_dir}/current/bin/stop-servers.sh --no-hyperspace
  CMD
end

desc "Stops hyperspace processes."
task :stop_hyperspace, :roles => :hyperspace do
  run <<-CMD 
  #{install_dir}/current/bin/stop-hyperspace.sh
  CMD
end

desc "Cleans hyperspace & rangeservers, removing all tables."
task :cleandb do
  transaction do
    clean_ranges
    clean_hyperspace
  end  
end

desc "Cleans rangeservers and master state but not hyperspace."
task :clean_ranges, :roles => [:master, :slave] do
  run <<-CMD
   #{install_dir}/current/bin/start-dfsbroker.sh #{dfs} \
      #{config_option} && \
   #{install_dir}/current/bin/clean-database.sh;
  CMD
end

desc "Cleans hyperspace."
task :clean_hyperspace, :roles => :hyperspace do
  run <<-CMD
   #{install_dir}/current/bin/clean-hyperspace.sh
  CMD
end

desc "Reports status for all processes."
task :status do
  transaction do
    dfs_status
    master_status
    hyperspace_status
    rangeserver_status
  end
end

desc "Get status for dfs processes."
task :dfs_status, :roles => [:master, :slave] do
  run <<-CMD
   #{install_dir}/current/bin/ht serverup dfsbroker
  CMD
end

desc "Get status for Hypertable.Master process."
task :master_status, :roles => [:master] do
  run <<-CMD
   #{install_dir}/current/bin/ht serverup master
  CMD
end

desc "Get status for Hyperspace.Master process."
task :hyperspace_status, :roles => [:hyperspace] do
  run <<-CMD
   #{install_dir}/current/bin/ht serverup hyperspace
  CMD
end

desc "Get status for rangeserver processes."
task :rangeserver_status, :roles => [:slave] do
  run <<-CMD
   #{install_dir}/current/bin/ht serverup rangeserver
  CMD
end

set :default_dumpfile, "/tmp/rsdump.txt"

set(:dumpfile) do
  "#{default_dumpfile}"
end unless exists?(:dumpfile)

desc "Run dump command on each rangeserver"
task :rangeserver_dump, :roles => [:slave] do
  run <<-CMD
   echo "dump NOKEYS '#{dumpfile}';" | #{install_dir}/current/bin/ht ht_rsclient --batch #{config_option}
  CMD
end
