<commit_before>#
# Cookbook Name:: rails_part
# Recipe:: deploy
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#

app_name, app = node['cloudconductor']['applications'].find { |_app_name, app| app['type'] != 'static' }
db = node['rails_part']['db']
cloudconductor = node['cloudconductor']

bash 'pre_deploy_script' do
  code app['pre_deploy']
  only_if { app['pre_deploy'] && !app['pre_deploy'].empty? }
end

case app['protocol']
when 'git'
  application app_name do
    action :deploy
    path "#{node['rails_part']['app']['base_path']}/#{app_name}"
    owner node['rails_part']['user']['name']
    group node['rails_part']['user']['group']
    repository app['url']
    revision app['revision'] || 'HEAD'
    migrate node['rails_part']['app']['migrate']
    # migration_command node['rails_part']['app']['migration_command']
    migration_command '/opt/rbenv/shims/bundle exec rake db:migrate 2>&1 > /tmp/test.log'
    environment_name node['rails_part']['app']['rails_env']

    rails do
      database do
        adapter db['adapter']
        host cloudconductor['db_host']
        database db['database']
        username db['user']
        password db['password']
      end
      bundler node['rails_part']['app']['bundler']
      bundle_command node['rails_part']['app']['bundle_command']
    end
  end
when 'http'
  tmp_dir = "/tmp/#{app_name}"
  directory tmp_dir do
    recursive true
    action :create
  end

  app_root = "#{node['rails_part']['app']['base_path']}/#{app_name}"
  app_dir = "#{app_root}/releases/#{app['version']}"

  directory "#{app_root}/releases" do
    recursive true
    action :create
  end

  unless Dir.exist?(app_dir)
    remote_file 'application_tarball' do
      source app['url']
      path "#{tmp_dir}/#{app_name}.tar.gz"
    end

    bash 'extract_static_files' do
      code <<-EOS
        tar -zxvf #{tmp_dir}/#{app_name}.tar.gz -C #{tmp_dir}
        rm #{tmp_dir}/#{app_name}.tar.gz
        mv #{tmp_dir}/* #{app_dir}
      EOS
    end
  end

  template "#{app_dir}/config/database.yml" do
    source 'database.yml.erb'
    variables db: node['rails_part']['db'], environment: node['rails_part']['app']['rails_env']
  end

  bash 'bundle_install' do
    cwd app_dir
    code '/opt/rbenv/shims/bundle install --without test development'
  end

  bash 'db_migrate' do
    cwd app_dir
    environment 'RAILS_ENV' => node['rails_part']['app']['rails_env']
    code '/opt/rbenv/shims/bundle exec rake db:migrate'
  end

  link "#{app_root}/current" do
    to app_dir
  end
end

puma_config app_name do
  directory     "#{node['rails_part']['app']['base_path']}/#{app_name}"
  environment   node['rails_part']['app']['rails_env']
  bind          node['rails_part']['puma']['bind']
  output_append node['rails_part']['puma']['output_append']
  logrotate     node['rails_part']['puma']['logrotate']
  thread_min    node['rails_part']['puma']['thread_min']
  thread_max    node['rails_part']['puma']['thread_max']
  workers       node['rails_part']['puma']['workers']
  daemonize     true
  monit         false
end

template "/etc/init.d/#{app_name}" do
  source 'puma_init_script.erb'
  owner 'root'
  group 'root'
  mode '0755'
  variables(
    app_name: app_name,
    app_path: "#{node['rails_part']['app']['base_path']}/#{app_name}",
    environment: node['rails_part']['app']['rails_env']
  )
end

service app_name do
  action [:enable, :start]
end

bash 'post_deploy_script' do
  code app['post_deploy']
  only_if { app['post_deploy'] && !app['post_deploy'].empty? }
end<commit_msg>Support multiple dynamic applications<commit_after>#
# Cookbook Name:: rails_part
# Recipe:: deploy
#
# Copyright 2014, YOUR_COMPANY_NAME
#
# All rights reserved - Do Not Redistribute
#
def dynamic?
  -> (_, application) { application[:type] == 'dynamic' }
end

db = node['rails_part']['db']
cloudconductor = node['cloudconductor']

node['cloudconductor']['applications'].select(&dynamic?).each do |app_name, app|
  bash "pre_deploy_script_#{app_name}" do
    code app['pre_deploy']
    only_if { app['pre_deploy'] && !app['pre_deploy'].empty? }
  end

  case app['protocol']
  when 'git'
    application app_name do
      action :deploy
      path "#{node['rails_part']['app']['base_path']}/#{app_name}"
      owner node['rails_part']['user']['name']
      group node['rails_part']['user']['group']
      repository app['url']
      revision app['revision'] || 'HEAD'
      migrate node['rails_part']['app']['migrate']
      # migration_command node['rails_part']['app']['migration_command']
      migration_command '/opt/rbenv/shims/bundle exec rake db:migrate 2>&1 > /tmp/test.log'
      environment_name node['rails_part']['app']['rails_env']

      rails do
        database do
          adapter db['adapter']
          host cloudconductor['db_host']
          database db['database']
          username db['user']
          password db['password']
        end
        bundler node['rails_part']['app']['bundler']
        bundle_command node['rails_part']['app']['bundle_command']
      end
    end
  when 'http'
    tmp_dir = "/tmp/#{app_name}"
    directory tmp_dir do
      recursive true
      action :create
    end

    app_root = "#{node['rails_part']['app']['base_path']}/#{app_name}"
    app_dir = "#{app_root}/releases/#{app['version']}"

    directory "#{app_root}/releases" do
      recursive true
      action :create
    end

    unless Dir.exist?(app_dir)
      remote_file "application_tarball_#{app_name}" do
        source app['url']
        path "#{tmp_dir}/#{app_name}.tar.gz"
      end

      bash "extract_static_files_#{app_name}" do
        code <<-EOS
          tar -zxvf #{tmp_dir}/#{app_name}.tar.gz -C #{tmp_dir}
          rm #{tmp_dir}/#{app_name}.tar.gz
          mv #{tmp_dir}/* #{app_dir}
        EOS
      end
    end

    template "#{app_dir}/config/database.yml" do
      source 'database.yml.erb'
      variables db: node['rails_part']['db'], environment: node['rails_part']['app']['rails_env']
    end

    bash "bundle_install_#{app_name}" do
      cwd app_dir
      code '/opt/rbenv/shims/bundle install --without test development'
    end

    bash "db_migrate_#{app_name}" do
      cwd app_dir
      environment 'RAILS_ENV' => node['rails_part']['app']['rails_env']
      code '/opt/rbenv/shims/bundle exec rake db:migrate'
    end

    link "#{app_root}/current" do
      to app_dir
    end
  end

  puma_config app_name do
    directory     "#{node['rails_part']['app']['base_path']}/#{app_name}"
    environment   node['rails_part']['app']['rails_env']
    bind          node['rails_part']['puma']['bind']
    output_append node['rails_part']['puma']['output_append']
    logrotate     node['rails_part']['puma']['logrotate']
    thread_min    node['rails_part']['puma']['thread_min']
    thread_max    node['rails_part']['puma']['thread_max']
    workers       node['rails_part']['puma']['workers']
    daemonize     true
    monit         false
  end

  template "/etc/init.d/#{app_name}" do
    source 'puma_init_script.erb'
    owner 'root'
    group 'root'
    mode '0755'
    variables(
      app_name: app_name,
      app_path: "#{node['rails_part']['app']['base_path']}/#{app_name}",
      environment: node['rails_part']['app']['rails_env']
    )
  end

  service app_name do
    action [:enable, :start]
  end

  bash "post_deploy_script_#{app_name}" do
    code app['post_deploy']
    only_if { app['post_deploy'] && !app['post_deploy'].empty? }
  end
end
{'fraction_non_alphanumeric': 0.16691186304667646, 'fraction_numerical': 0.0026748696001069948, 'mean_word_length': 3.624613481756339, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 10, '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': '4823027', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2467, 'n_words': 494}