<commit_before>#
# Cookbook Name:: oc-id
# Recipe:: default::_install
# Author:: <NAME> <<EMAIL>>
# All rights reserved - Do Not Redistribute
#

include_recipe 'apt'
include_recipe 'build-essential'
include_recipe 'nodejs::install_from_binary'
include_recipe 'opscode-base'
include_recipe 'opscode-ruby'
include_recipe 'users::opscode'
include_recipe 'opscode-github'
include_recipe 'runit'
include_recipe 'unicorn'

package 'sqlite3'
package 'sqlite3-doc'
package 'libsqlite3-dev'

hostnames = data_bag_item('hostnames', node.chef_environment)
vips = data_bag_item('vips', node.chef_environment)
env = data_bag_item('environments', node.chef_environment)

node_attrs = node['oc-id']
install_dir = node_attrs['install_dir']
group_name = node_attrs['group']
user_name = node_attrs['user']
rails_env = node_attrs['rails_env'] || 'production'
revision = env['oc-id']['revision'] || 'master'
remote = env['default-remote'] || 'opscode'

dev = node.chef_environment == 'dev'
local = node['oc-id']['source'] == 'local'

if dev
  
  execute 'replace key' do
    user 'root'
    group 'root'
    command 'chmod 644 /tmp/oc-id/config/webui_priv.pem && rm /etc/opscode/webui_priv.pem && cp /tmp/oc-id/config/webui_priv.pem /etc/opscode/webui_priv.pem'
    notifies :run, 'execute[deploy]', :immediately if local
  end

  if local

    release_dir = "#{install_dir}/current"

    directory release_dir do
      owner user_name
      group group_name
      mode 0755
      recursive true
    end

    execute 'install' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command "bundle install --path ../bundle #{'--without test development' if rails_env == 'production'}"
      notifies :run, 'execute[precompile]', :immediately
      action :nothing
    end

    execute 'precompile' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command 'bundle exec rake assets:precompile'
      notifies :run, 'execute[migrate]', :immediately
      action :nothing
    end

    execute 'migrate' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command 'bundle exec rake db:migrate'
      notifies :restart, 'service[oc-id]'
      action :nothing
    end

    execute 'deploy' do
      user user_name
      group group_name
      command "rm -rf #{release_dir} && mkdir -p #{release_dir} && cp -R /tmp/oc-id/* #{release_dir}"
      notifies :run, 'execute[install]', :immediately
      action :nothing
    end
  end
end

if !local

  deploy_revision('oc-id') do
    user user_name
    group group_name
    repo "<EMAIL>:#{remote}/oc-id.git"
    revision revision
    remote remote
    deploy_to install_dir
    migration_command 'bundle exec rake db:migrate'
    migrate true

    environment({
      'LC_CTYPE' => 'en_US.UTF-8',
      'LANG' => 'en_US.UTF-8',
      'PATH' => "/usr/local/bin:#{ENV['PATH']}",
      'RAILS_ENV' => rails_env
    })

    symlink_before_migrate({})
    symlinks({'tmp' => 'tmp', 'log' => 'log'})

    before_migrate do

      template "#{release_path}/config/settings/#{rails_env}.yml" do
        user user_name
        group group_name
        source 'settings.yml.erb'
        mode 0644
        variables node_attrs.merge({
          :hostnames => hostnames
        }).to_hash
      end<commit_msg>Add shared dirs<commit_after>#
# Cookbook Name:: oc-id
# Recipe:: default::_install
# Author:: <NAME> <<EMAIL>>
# All rights reserved - Do Not Redistribute
#

include_recipe 'apt'
include_recipe 'build-essential'
include_recipe 'nodejs::install_from_binary'
include_recipe 'opscode-base'
include_recipe 'opscode-ruby'
include_recipe 'users::opscode'
include_recipe 'opscode-github'
include_recipe 'runit'
include_recipe 'unicorn'

package 'sqlite3'
package 'sqlite3-doc'
package 'libsqlite3-dev'

hostnames = data_bag_item('hostnames', node.chef_environment)
vips = data_bag_item('vips', node.chef_environment)
env = data_bag_item('environments', node.chef_environment)

node_attrs = node['oc-id']
install_dir = node_attrs['install_dir']
group_name = node_attrs['group']
user_name = node_attrs['user']
rails_env = node_attrs['rails_env'] || 'production'
revision = env['oc-id']['revision'] || 'master'
remote = env['default-remote'] || 'opscode'

dev = node.chef_environment == 'dev'
local = node['oc-id']['source'] == 'local'

if dev
  
  execute 'replace key' do
    user 'root'
    group 'root'
    command 'chmod 644 /tmp/oc-id/config/webui_priv.pem && rm /etc/opscode/webui_priv.pem && cp /tmp/oc-id/config/webui_priv.pem /etc/opscode/webui_priv.pem'
    notifies :run, 'execute[deploy]', :immediately if local
  end

  if local

    release_dir = "#{install_dir}/current"

    directory release_dir do
      owner user_name
      group group_name
      mode 0755
      recursive true
    end

    execute 'install' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command "bundle install --path ../bundle #{'--without test development' if rails_env == 'production'}"
      notifies :run, 'execute[precompile]', :immediately
      action :nothing
    end

    execute 'precompile' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command 'bundle exec rake assets:precompile'
      notifies :run, 'execute[migrate]', :immediately
      action :nothing
    end

    execute 'migrate' do
      user user_name
      group group_name
      cwd release_dir
      environment({ 'RAILS_ENV' => rails_env })
      command 'bundle exec rake db:migrate'
      notifies :restart, 'service[oc-id]'
      action :nothing
    end

    execute 'deploy' do
      user user_name
      group group_name
      command "rm -rf #{release_dir} && mkdir -p #{release_dir} && cp -R /tmp/oc-id/* #{release_dir}"
      notifies :run, 'execute[install]', :immediately
      action :nothing
    end
  end
end

if !local

  %w{shared shared/system shared/config shared/pids shared/log shared/vendor}.each do |dir|
    directory "#{install_dir}/#{dir}" do
      owner user_name
      group group_name
      mode 02775
    end
  end

  deploy_revision('oc-id') do
    user user_name
    group group_name
    repo "<EMAIL>:#{remote}/oc-id.git"
    revision revision
    remote remote
    deploy_to install_dir
    migration_command 'bundle exec rake db:migrate'
    migrate true

    environment({
      'LC_CTYPE' => 'en_US.UTF-8',
      'LANG' => 'en_US.UTF-8',
      'PATH' => "/usr/local/bin:#{ENV['PATH']}",
      'RAILS_ENV' => rails_env
    })

    symlink_before_migrate({})
    symlinks('system' => 'public/system', 'pids' => 'tmp/pids', 'log' => 'log', 'bundle' => 'vendor/bundle')

    before_migrate do

      template "#{release_path}/config/settings/#{rails_env}.yml" do
        user user_name
        group group_name
        source 'settings.yml.erb'
        mode 0644
        variables node_attrs.merge({
          :hostnames => hostnames
        }).to_hash
      end
{'fraction_non_alphanumeric': 0.124053826745164, 'fraction_numerical': 0.005186431174656574, 'mean_word_length': 3.2546213476446035, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 33, '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': '2257997', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2381, 'n_words': 570}