set :application, "novotech-company"
set :repository,  "git@git.g77k.com:huxl/novotech.git"
set :branch, "version1.0"

set :scm, :git
set :deploy_via, :remote_cache
set :user, "root"
set :use_sudo, false
set :rails_env, 'production'

#ssh_options[:forward_agent] = true
#ssh_options[:port] = 22 

server "a.ariaboard.com", :app, :web, :db, :primary => true

#server "ariaboard.com", :app, :web, :db, :primary => true
default_run_options[:pty] = true
set :deploy_to, "/www/#{application}"

set :default_environment, {
    'PATH' => "/usr/local/nodejs12.16.1/bin:/usr/local/ruby2.6.5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"
}

after "deploy:update", "deploy:prepare"
after "deploy:update", "deploy:bundle"
after "deploy:update", "deploy:assets_precompile"
after "deploy:update", "deploy:db_migrate"
after "deploy:update", "deploy:restart"

namespace :deploy do
  task :prepare, :roles => :app do
    run "cp /www/#{application}/shared/secrets.yml         /www/#{application}/current/config/"
    run "ln -snf /www/#{application}/shared/production.sqlite3 #{release_path}/db/production.sqlite3"
    run "cp /www/#{application}/current/script/novotech-site /etc/nginx/sites-enabled/"
    run "cp /www/#{application}/current/script/novotech-unicorn   /etc/init.d/"
    run "chmod a+x /etc/init.d/novotech-unicorn"
    run "update-rc.d -f novotech-unicorn remove && update-rc.d novotech-unicorn defaults 44"
  end

  task :bundle, :roles => :app do
    run "cd #{release_path} && bundle"
  end

  task :assets_precompile, :roles => :app do
    run "cd /www/#{application}/current && RAILS_ENV=production bundle exec rake assets:precompile"
  end

  task :db_migrate, :roles => :app do
    run "cd /www/#{application}/current && RAILS_ENV=production bundle exec rake db:migrate"
  end

  task :restart, :roles => :app do
    run "service nginx restart"
    run "service novotech-unicorn restart"
  end

end
