# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"

set :application, "easyblue-marketing"
set :repo_url, "git@gitlab.com:easyblue/easyblue.io/marketing.easyblue.io.git"

set :ssh_options, {
    verify_host_key: :never,
    forward_agent: true
}

append :linked_files, ".env"
append :linked_dirs, "node_modules"

set :keep_releases, 3

set :permission_method, :acl
set :file_permissions_users, ["www-data"]
set :file_permissions_paths, ["."]

set :yarn_target_path, -> { release_path.join('.') }
set :yarn_flags, '--silent --no-progress'

set :slackistrano, {
  channel: '#deployer',
  webhook: 'https://hooks.slack.com/services/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'
}

namespace :pm2 do
  task :start do
    on roles(:app) do
      within fetch(:yarn_target_path, release_path) do
        execute :pm2, 'start ecosystem.json'
      end
    end
  end

  task :delete do
      on roles(:app) do
        within fetch(:yarn_target_path, release_path) do
          execute :pm2, 'delete ecosystem.json'
        end
      end
    end

  task :restart do
    on roles(:app) do
      within fetch(:yarn_target_path, release_path) do
        execute :pm2, 'reload ecosystem.json'
      end
    end
  end

  task :stop do
    on roles(:app) do
      within fetch(:yarn_target_path, release_path) do
        execute :pm2, 'stop ecosystem.json'
      end
    end
  end
end

namespace :deploy do
  task :yarn_deploy do
    on roles fetch(:yarn_roles) do
      within fetch(:yarn_target_path, release_path) do
        execute fetch(:yarn_bin), 'build'
      end
    end
  end

  task :restart do
    invoke 'pm2:delete'
    invoke 'pm2:start'
  end

  before "symlink:release", "deploy:yarn_deploy"
  after "deploy:yarn_deploy", "deploy:restart"
end
