<commit_before>require 'capistrano/unicorn_nginx/helpers'

include Capistrano::UnicornNginx::Helpers

namespace :load do
  task :defaults do
    set :templates_path, 'config/deploy/templates'
    set :nginx_config_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
    set :nginx_pid, '/run/nginx.pid'
    # set :nginx_server_name # default set in the `nginx:defaults` task
    # ssl options
    set :nginx_use_ssl, false
    set :nginx_ssl_cert, -> { "#{fetch(:nginx_server_name)}.crt" }
    set :nginx_ssl_cert_key, -> { "#{fetch(:nginx_server_name)}.key" }
    set :nginx_upload_local_cert, true
    set :nginx_ssl_cert_local_path, -> { ask(:nginx_ssl_cert_local_path, 'Local path to ssl certificate: ') }
    set :nginx_ssl_cert_key_local_path, -> { ask(:nginx_ssl_cert_key_local_path, 'Local path to ssl certificate key: ') }
  end
end

namespace :nginx do

  task :defaults do
    on roles :web do
      set :nginx_server_name, fetch(:nginx_server_name, host.to_s)
    end
  end

  desc 'Setup nginx configuration'
  task :setup do
    on roles :web do
      config_name = fetch(:nginx_config_name)
      next if file_exists? "/etc/nginx/sites-available/#{config_name}"

      execute :mkdir, '-p', shared_path.join('log')
      template 'nginx_conf.erb', "#{fetch(:tmp_dir)}/#{config_name}"
      sudo :mv, "#{fetch(:tmp_dir)}/#{config_name}", "/etc/nginx/sites-available/#{config_name}"
      sudo :ln, '-fs', "/etc/nginx/sites-available/#{config_name}", "/etc/nginx/sites-enabled/#{config_name}"
    end
  end

  desc 'Setup nginx ssl certs'
  task :setup_ssl do
    on roles :web do
      next unless fetch(:nginx_use_ssl)
      next if file_exists?("/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}") && file_exists?("/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}")

      if fetch(:nginx_upload_local_cert)
        upload! fetch(:nginx_ssl_cert_local_path), "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert)}"
        upload! fetch(:nginx_ssl_cert_key_local_path), "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert_key)}"
        sudo :mv, "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert)}", "/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}"
        sudo :mv, "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert_key)}", "/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}"
      end
      sudo :chown, 'root:root', "/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}"
      sudo :chown, 'root:root', "/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}"
    end
  end

  desc 'Reload nginx configuration'
  task :reload do
    on roles :web do
      sudo '/etc/init.d/nginx reload'
    end
  end

  before :setup, :defaults
  before :setup_ssl, :defaults
end

namespace :deploy do
  after :started, 'nginx:setup'
  after :started, 'nginx:setup_ssl'
  after :publishing, 'nginx:reload'
end<commit_msg>Faster skipping of nginx:setup_ssl if ssl is not used<commit_after>require 'capistrano/unicorn_nginx/helpers'

include Capistrano::UnicornNginx::Helpers

namespace :load do
  task :defaults do
    set :templates_path, 'config/deploy/templates'
    set :nginx_config_name, -> { "#{fetch(:application)}_#{fetch(:stage)}" }
    set :nginx_pid, '/run/nginx.pid'
    # set :nginx_server_name # default set in the `nginx:defaults` task
    # ssl options
    set :nginx_use_ssl, false
    set :nginx_ssl_cert, -> { "#{fetch(:nginx_server_name)}.crt" }
    set :nginx_ssl_cert_key, -> { "#{fetch(:nginx_server_name)}.key" }
    set :nginx_upload_local_cert, true
    set :nginx_ssl_cert_local_path, -> { ask(:nginx_ssl_cert_local_path, 'Local path to ssl certificate: ') }
    set :nginx_ssl_cert_key_local_path, -> { ask(:nginx_ssl_cert_key_local_path, 'Local path to ssl certificate key: ') }
  end
end

namespace :nginx do

  task :defaults do
    on roles :web do
      set :nginx_server_name, fetch(:nginx_server_name, host.to_s)
    end
  end

  desc 'Setup nginx configuration'
  task :setup do
    on roles :web do
      config_name = fetch(:nginx_config_name)
      next if file_exists? "/etc/nginx/sites-available/#{config_name}"

      execute :mkdir, '-p', shared_path.join('log')
      template 'nginx_conf.erb', "#{fetch(:tmp_dir)}/#{config_name}"
      sudo :mv, "#{fetch(:tmp_dir)}/#{config_name}", "/etc/nginx/sites-available/#{config_name}"
      sudo :ln, '-fs', "/etc/nginx/sites-available/#{config_name}", "/etc/nginx/sites-enabled/#{config_name}"
    end
  end

  desc 'Setup nginx ssl certs'
  task :setup_ssl do
    next unless fetch(:nginx_use_ssl)
    on roles :web do
      next if file_exists?("/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}") && file_exists?("/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}")

      if fetch(:nginx_upload_local_cert)
        upload! fetch(:nginx_ssl_cert_local_path), "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert)}"
        upload! fetch(:nginx_ssl_cert_key_local_path), "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert_key)}"
        sudo :mv, "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert)}", "/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}"
        sudo :mv, "#{fetch(:tmp_dir)}/#{fetch(:nginx_ssl_cert_key)}", "/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}"
      end
      sudo :chown, 'root:root', "/etc/ssl/certs/#{fetch(:nginx_ssl_cert)}"
      sudo :chown, 'root:root', "/etc/ssl/private/#{fetch(:nginx_ssl_cert_key)}"
    end
  end

  desc 'Reload nginx configuration'
  task :reload do
    on roles :web do
      sudo '/etc/init.d/nginx reload'
    end
  end

  before :setup, :defaults
  before :setup_ssl, :defaults
end

namespace :deploy do
  after :started, 'nginx:setup'
  after :started, 'nginx:setup_ssl'
  after :publishing, 'nginx:reload'
end
{'fraction_non_alphanumeric': 0.15527168046059733, 'fraction_numerical': 0.0, 'mean_word_length': 4.2492917847025495, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '7367842', 'n_tokens_mistral': 2084, 'n_tokens_neox': 2027, 'n_words': 393}