set :source_machine, "motherlode000"
set :install_dir,  "/opt/hypertable/sanjit"
set :hypertable_version, "0.9.2.4"
set :default_dfs, "hadoop"
set :default_config, "test-sequential-load-count.cfg"

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

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

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

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("cp #{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_master
    start_slaves
  end
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-hyperspace.sh \
      #{config_option} &&
   #{install_dir}/current/bin/start-master.sh #{config_option}
  CMD
end

desc "Stops master processes."
task :stop_master, :roles => :master do
  run "#{install_dir}/current/bin/stop-servers.sh"
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 "Stops slave processes."
task :stop_slaves, :roles => :slave do
  run "#{install_dir}/current/bin/stop-servers.sh"
end

desc "Stops all servers."
task :stop, :roles => [:master, :slave] do
  transaction do
    stop_slaves
    stop_master
  end
end

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

desc "Cleans database, removing all tables."
task :cleandb, :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 "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 => [:master] 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

