<commit_before>require 'erb'
require 'mina/multistage'
require 'mina/puma'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'

set :user_path, lambda { "/home/#{user}" }
set :deploy_to, lambda { "#{user_path}/#{application}" }
set :full_current_path, lambda { "#{deploy_to}/#{current_path}" }
set :full_shared_path, lambda { "#{deploy_to}/#{shared_path}" }
set :branch, 'master'
set :shared_paths, ['.env', 'log', 'config/nginx.conf', 'config/puma.rb', 'public/system']
set :forward_agent, true
set :rails_env, lambda { "#{stage}" }

# Puma settings
set :puma_socket, lambda { "#{deploy_to}/tmp/puma/sockets/puma.sock" }
set :puma_pid, lambda { "#{deploy_to}/tmp/puma/pid" }
set :puma_state, lambda { "#{deploy_to}/tmp/puma/state" }
set :pumactl_socket, lambda { "#{deploy_to}/tmp/puma/sockets/pumactl.sock" }
set :puma_config, lambda { "#{full_current_path}/config/puma.rb" }
set :puma_error_log, lambda { "#{full_shared_path}/log/puma.error.log" }
set :puma_access_log, lambda { "#{full_shared_path}/log/puma.access.log" }
set :puma_env, lambda { "#{rails_env}" }

# Assets settings
set :precompiled_assets_dir, 'public/assets'

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  invoke :'rbenv:load'
end

task :reminders do
  system %[echo ""]
  system %[echo "------------------------- REMINDERS -------------------------"]
  system %[echo ""]

  invoke 'reminders:before_deploy'
  invoke 'reminders:after_deploy'
end

namespace :reminders do
  task :before_deploy do
    system %[echo ""]
    system %[echo "-------- Before First Deploy --------"]
    system %[echo ""]

    invoke 'reminders:before_deploy:edit_env'
    invoke 'reminders:before_deploy:add_github_to_known_hosts'
  end

  task :after_deploy do
    system %[echo ""]
    system %[echo "-------- After First Deploy --------"]
    system %[echo ""]

    invoke 'reminders:after_deploy:symlink_nginx'
    invoke 'reminders:after_deploy:add_to_puma_jungle'
  end

  namespace :before_deploy do
    task :edit_env do
      system  %[echo ""]
      system  %[echo "-----> You need to add your app's secrets #{full_shared_path}/.env on the server;"]
      system  %[echo "otherwise, the app won't know your credentials and database migrations will fail on deploy."]
      system  %[echo ""]
    end

    task :add_github_to_known_hosts do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to add github to the list of known hosts. This will"]
      system  %[echo "-----> allow you to deploy (otherwise the git clone step will fail)."]
      system  %[echo ""]
      system  %[echo "ssh-keyscan -H github.com >> ~/.ssh/known_hosts"]
      system  %[echo ""]
    end
  end

  namespace :after_deploy do
    task :symlink_nginx do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to create the symlink from the "]
      system  %[echo "nginx sites-enabled directory to the app's nginx.conf file:"]
      system  %[echo ""]
      system  %[echo "sudo ln -nfs #{full_current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"]
      system  %[echo ""]
    end

    task :add_to_puma_jungle do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to add your app to the list of puma apps in "]
      system  %[echo "the file /etc/puma.conf. All apps in this file are automatically started"]
      system  %[echo "whenever the server is booted up. They can also be controlled with the script "]
      system  %[echo "/etc/init.d/puma (i.e. try running the command '/etc/init.d/puma status')."]
      system  %[echo ""]
      system  %[echo "sudo /etc/init.d/puma add #{deploy_to} #{user} #{full_current_path}/config/puma.rb #{full_shared_path}/log/puma.log"]
      system  %[echo ""]
    end
  end
end

# Put any custom mkdir's in here for when `mina setup` is run.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
  temp_env_example_path = "#{user_path}/.env.example-#{application}"
  shared_env_path = "#{full_shared_path}/.env"

  capture(%[ls #{full_shared_path}/.env]).split(" ")[0] == "#{shared_env_path}" ? env_exists = true : env_exists = false

  if !env_exists
    system %[scp .env.example #{user}@#{domain}:#{temp_env_example_path}]
  end
  queue! %[mkdir -p "#{full_shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/log"]

  queue! %[mkdir -p "#{full_shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/config"]

  queue! %[mkdir -p "#{full_shared_path}/public/system"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/public/system"]

  queue! %[mkdir -p "#{deploy_to}/tmp/puma/sockets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/tmp/puma/sockets"]

  queue! %[mkdir -p "#{deploy_to}/tmp/assets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/tmp/assets"]

  if !env_exists
    queue! %[echo "Moving copy of local .env.example to #{shared_env_path}"]
    queue! %[mv #{temp_env_example_path} #{shared_env_path}]
  end

  queue! %[echo ""]
  queue! %[echo "------------------------- IMPORTANT -------------------------"]
  queue! %[echo ""]
  queue! %[echo "Before deploying for the first time, run 'mina #{stage} reminders'"]
  queue! %[echo "to see important commands that should be run before and after first deploy"]
  queue! %[echo ""]
  queue! %[echo "------------------------- IMPORTANT -------------------------"]
  queue! %[echo ""]
end

namespace :deploy do
  task :check_revision do
    unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}`
      puts "WARNING: HEAD is not the same as origin/#{branch}"
      puts "Run `git push` to sync changes."
      exit
    end

    unless `git status`.include? 'nothing to commit, working directory clean'
      puts "WARNING: There are uncommitted changes to the local git repository, which"
      puts "may cause problems for locally precompiling assets."
      puts "Please clean local repository with `git stash` or `git reset`."
      exit
    end<commit_msg>A couple tweaks to mina setup task.<commit_after>require 'erb'
require 'mina/multistage'
require 'mina/puma'
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rbenv'

set :user_path, lambda { "/home/#{user}" }
set :deploy_to, lambda { "#{user_path}/#{application}" }
set :full_current_path, lambda { "#{deploy_to}/#{current_path}" }
set :full_shared_path, lambda { "#{deploy_to}/#{shared_path}" }
set :branch, 'master'
set :shared_paths, ['.env', 'log', 'config/nginx.conf', 'config/puma.rb', 'public/system']
set :forward_agent, true
set :rails_env, lambda { "#{stage}" }

# Puma settings
set :puma_socket, lambda { "#{deploy_to}/tmp/puma/sockets/puma.sock" }
set :puma_pid, lambda { "#{deploy_to}/tmp/puma/pid" }
set :puma_state, lambda { "#{deploy_to}/tmp/puma/state" }
set :pumactl_socket, lambda { "#{deploy_to}/tmp/puma/sockets/pumactl.sock" }
set :puma_config, lambda { "#{full_current_path}/config/puma.rb" }
set :puma_error_log, lambda { "#{full_shared_path}/log/puma.error.log" }
set :puma_access_log, lambda { "#{full_shared_path}/log/puma.access.log" }
set :puma_env, lambda { "#{rails_env}" }

# Assets settings
set :precompiled_assets_dir, 'public/assets'

# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
  invoke :'rbenv:load'
end

task :reminders do
  system %[echo ""]
  system %[echo "------------------------- REMINDERS -------------------------"]
  system %[echo ""]

  invoke 'reminders:before_deploy'
  invoke 'reminders:after_deploy'
end

namespace :reminders do
  task :before_deploy do
    system %[echo ""]
    system %[echo "-------- Before First Deploy --------"]
    system %[echo ""]

    invoke 'reminders:before_deploy:edit_env'
    invoke 'reminders:before_deploy:add_github_to_known_hosts'
  end

  task :after_deploy do
    system %[echo ""]
    system %[echo "-------- After First Deploy --------"]
    system %[echo ""]

    invoke 'reminders:after_deploy:symlink_nginx'
    invoke 'reminders:after_deploy:add_to_puma_jungle'
  end

  namespace :before_deploy do
    task :edit_env do
      system  %[echo ""]
      system  %[echo "-----> You need to add your app's secrets #{full_shared_path}/.env on the server;"]
      system  %[echo "otherwise, the app won't know your credentials and database migrations will fail on deploy."]
      system  %[echo ""]
    end

    task :add_github_to_known_hosts do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to add github to the list of known hosts. This will"]
      system  %[echo "-----> allow you to deploy (otherwise the git clone step will fail)."]
      system  %[echo ""]
      system  %[echo "ssh-keyscan -H github.com >> ~/.ssh/known_hosts"]
      system  %[echo ""]
    end
  end

  namespace :after_deploy do
    task :symlink_nginx do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to create the symlink from the "]
      system  %[echo "nginx sites-enabled directory to the app's nginx.conf file:"]
      system  %[echo ""]
      system  %[echo "sudo ln -nfs #{full_current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"]
      system  %[echo ""]
    end

    task :add_to_puma_jungle do
      system  %[echo ""]
      system  %[echo "-----> Run the following command on your server to add your app to the list of puma apps in "]
      system  %[echo "the file /etc/puma.conf. All apps in this file are automatically started"]
      system  %[echo "whenever the server is booted up. They can also be controlled with the script "]
      system  %[echo "/etc/init.d/puma (i.e. try running the command '/etc/init.d/puma status')."]
      system  %[echo ""]
      system  %[echo "sudo /etc/init.d/puma add #{deploy_to} #{user} #{full_current_path}/config/puma.rb #{full_shared_path}/log/puma.log"]
      system  %[echo ""]
    end
  end
end

task :setup => :environment do
  temp_env_example_path = "#{user_path}/.env.example-#{application}"
  shared_env_path = "#{full_shared_path}/.env"

  capture(%[ls #{full_shared_path}/.env]).split(" ")[0] == "#{shared_env_path}" ? env_exists = true : env_exists = false

  unless env_exists
    system %[scp .env.example #{user}@#{domain}:#{temp_env_example_path}]
  end

  queue! %[mkdir -p "#{full_shared_path}/log"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/log"]

  queue! %[mkdir -p "#{full_shared_path}/config"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/config"]

  queue! %[mkdir -p "#{full_shared_path}/public/system"]
  queue! %[chmod g+rx,u+rwx "#{full_shared_path}/public/system"]

  queue! %[mkdir -p "#{deploy_to}/tmp/puma/sockets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/tmp/puma/sockets"]

  queue! %[mkdir -p "#{deploy_to}/tmp/assets"]
  queue! %[chmod g+rx,u+rwx "#{deploy_to}/tmp/assets"]

  unless env_exists
    queue! %[echo "Moving copy of local .env.example to #{shared_env_path}"]
    queue! %[mv #{temp_env_example_path} #{shared_env_path}]
  end

  queue! %[echo ""]
  queue! %[echo "------------------------- IMPORTANT -------------------------"]
  queue! %[echo ""]
  queue! %[echo "Before deploying for the first time, run 'mina #{stage} reminders'"]
  queue! %[echo "to see important commands that should be run before and after first deploy"]
  queue! %[echo ""]
  queue! %[echo "------------------------- IMPORTANT -------------------------"]
  queue! %[echo ""]
end

namespace :deploy do
  task :check_revision do
    unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}`
      puts "WARNING: HEAD is not the same as origin/#{branch}"
      puts "Run `git push` to sync changes."
      exit
    end

    unless `git status`.include? 'nothing to commit, working directory clean'
      puts "WARNING: There are uncommitted changes to the local git repository, which"
      puts "may cause problems for locally precompiling assets."
      puts "Please clean local repository with `git stash` or `git reset`."
      exit
    end
{'fraction_non_alphanumeric': 0.1720773897360223, 'fraction_numerical': 0.00016396130513198886, 'mean_word_length': 3.2653846153846153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1018393', 'n_tokens_mistral': 4201, 'n_tokens_neox': 4080, 'n_words': 1175}