<commit_before>require 'yaml'

# If you use this definition by yourself, make sure to pass a properly setup
# instance. You can create one using the chiliproject_instance helper from a
# databag item
#
# extend ChiliProject::Helpers
# inst = chiliproject_instance "my_instance"

define :chiliproject, :name => "default", :instance => nil do
  extend ChiliProject::Helpers

  inst = params[:instance]

  # Reset the list of additional files to symlink before migration
  # These can be amended by sub definitions
  node.run_state['chiliproject_deploy_symlinks'] = {}
  node.run_state['chiliproject_plugin_symlinks'] = {}
  node.run_state['chiliproject_plugin_callbacks'] = {}

  #############################################################################
  # Install package dependencies

  gem_package "bundler" do
    action :install
    version ">= 1.0.14" # minimal version for ChiliProject
  end

  #############################################################################
  # Users and Groups

  # Create the user and group, one for each instance
  group inst['group']
  user inst['user'] do
    comment 'ChiliProject'
    gid inst['group']
    home inst['deploy_to']
    system true
    shell '/bin/bash'
  end

  # Create a shared group containing all the users of each instance
  # Use for shared files to ensure minimal permissions
  group "chiliproject" do
    members inst['user']
    append true
  end

  #############################################################################
  # Basic Directory Structure

  [inst['deploy_to'], "#{inst['deploy_to']}/shared"].each do |dir|
    directory dir do
      owner inst['user']
      group inst['group']
      mode '0755'
      recursive true
    end
  end

  %w[pids tmp vendor vendor/bundle].each do |dir|
    directory "#{inst['deploy_to']}/shared/#{dir}" do
      owner inst['user']
      group inst['group']
      mode '0750'
      recursive true
    end
  end

  # Shared directory for uploaded files, repositories, ...
  directory inst['shared_dir'] do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
  end

  # Uploaded attachments
  directory "#{inst['shared_dir']}/files" do
    owner inst['user']
    group inst['group']
    mode "2750"
  end

  # Log files
  directory node['chiliproject']['log_dir'] do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
  end if inst['log_dir'].start_with?(node['chiliproject']['log_dir'])

  directory inst['log_dir'] do
    owner inst['user']
    group inst['group']
    mode '0750'
  end
  file "#{inst['log_dir']}/#{inst['rails_env']}.log" do
    action :create_if_missing
    owner inst['user']
    group inst['group']
    mode '0640'
    content "# Logfile created on #{Time.now.to_s}"
  end

  #############################################################################
  # Session configuration

  session_config = {
    'key' => "_chili_#{inst['id']}_session",
    'session_path' => inst['base_uri'].path
  }
  session_config['secure'] = true if inst['base_uri'].scheme == "https"
  session_config.merge!(inst['session']) if inst['session']
  template "#{inst['deploy_to']}/shared/session_store.rb" do
    source "session_store.rb.erb"
    owner inst['user']
    group inst['group']
    mode "0400"
    variables :session_config => session_config
  end

  #############################################################################
  # configuration.yml

  configuration_default = {}
  configuration_default.merge!(node['chiliproject']['configuration'] || {})
  configuration_default.merge!(inst['configuration'] || {})
  configuration_default.merge!({
    "attachments_storage_path" => "#{node['chiliproject']['shared_dir']}/#{inst['id']}/files"
  })
  # Configure the autologin cookie similar to the session cookie
  # Can be overridden in the node and instance configuration
  configuration_default['autologin_cookie_name'] ||= "_chili_#{inst['id']}_autologin"
  configuration_default['autologin_cookie_path'] ||= inst['base_uri']
  unless configuration_default.has_key?("autologin_cookie_secure")
    configuration_default['autologin_cookie_secure'] = true if inst['base_uri'].scheme == "https"
  end

  configuration_production = {'email_delivery' => {}}
  configuration_production['email_delivery'].tap do |cfg|
    node_cfg = node['chiliproject']['email_delivery'] || {}
    inst_cfg = inst['email_delivery'] || {}

    cfg['delivery_method'] = :async_smtp
    cfg['smtp_settings'] = {
      'address' => get_hosts(inst, 'email_delivery').first["ipaddress"],
      'port' => inst_cfg['port'] || node_cfg['port'],
      'domain' => node['fqdn'],
      'enable_starttls_auto' => true
    }
    if (authentication = inst_cfg.has_key?('authentication') ? inst_cfg['authentication'] : node_cfg['authentication'])
      cfg['smtp_settings'].merge!({
        'authentication' => authentication.to_sym,
        "user_name" => inst_cfg['username'] || node_cfg['username'],
        "password" => inst_cfg['password'] || node_cfg['password']
      })
    end<commit_msg>Use the external URI for the cookie configuration<commit_after>require 'yaml'

# If you use this definition by yourself, make sure to pass a properly setup
# instance. You can create one using the chiliproject_instance helper from a
# databag item
#
# extend ChiliProject::Helpers
# inst = chiliproject_instance "my_instance"

define :chiliproject, :name => "default", :instance => nil do
  extend ChiliProject::Helpers

  inst = params[:instance]

  # Reset the list of additional files to symlink before migration
  # These can be amended by sub definitions
  node.run_state['chiliproject_deploy_symlinks'] = {}
  node.run_state['chiliproject_plugin_symlinks'] = {}
  node.run_state['chiliproject_plugin_callbacks'] = {}

  #############################################################################
  # Install package dependencies

  gem_package "bundler" do
    action :install
    version ">= 1.0.14" # minimal version for ChiliProject
  end

  #############################################################################
  # Users and Groups

  # Create the user and group, one for each instance
  group inst['group']
  user inst['user'] do
    comment 'ChiliProject'
    gid inst['group']
    home inst['deploy_to']
    system true
    shell '/bin/bash'
  end

  # Create a shared group containing all the users of each instance
  # Use for shared files to ensure minimal permissions
  group "chiliproject" do
    members inst['user']
    append true
  end

  #############################################################################
  # Basic Directory Structure

  [inst['deploy_to'], "#{inst['deploy_to']}/shared"].each do |dir|
    directory dir do
      owner inst['user']
      group inst['group']
      mode '0755'
      recursive true
    end
  end

  %w[pids tmp vendor vendor/bundle].each do |dir|
    directory "#{inst['deploy_to']}/shared/#{dir}" do
      owner inst['user']
      group inst['group']
      mode '0750'
      recursive true
    end
  end

  # Shared directory for uploaded files, repositories, ...
  directory inst['shared_dir'] do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
  end

  # Uploaded attachments
  directory "#{inst['shared_dir']}/files" do
    owner inst['user']
    group inst['group']
    mode "2750"
  end

  # Log files
  directory node['chiliproject']['log_dir'] do
    owner 'root'
    group 'root'
    mode '0755'
    recursive true
  end if inst['log_dir'].start_with?(node['chiliproject']['log_dir'])

  directory inst['log_dir'] do
    owner inst['user']
    group inst['group']
    mode '0750'
  end
  file "#{inst['log_dir']}/#{inst['rails_env']}.log" do
    action :create_if_missing
    owner inst['user']
    group inst['group']
    mode '0640'
    content "# Logfile created on #{Time.now.to_s}"
  end

  #############################################################################
  # Session configuration

  session_config = {
    'key' => "_chili_#{inst['id']}_session",
    'session_path' => inst['external_uri'].path
  }
  session_config['secure'] = true if inst['external_uri'].scheme == "https"
  session_config.merge!(inst['session']) if inst['session']
  template "#{inst['deploy_to']}/shared/session_store.rb" do
    source "session_store.rb.erb"
    owner inst['user']
    group inst['group']
    mode "0400"
    variables :session_config => session_config
  end

  #############################################################################
  # configuration.yml

  configuration_default = {}
  configuration_default.merge!(node['chiliproject']['configuration'] || {})
  configuration_default.merge!(inst['configuration'] || {})
  configuration_default.merge!({
    "attachments_storage_path" => "#{node['chiliproject']['shared_dir']}/#{inst['id']}/files"
  })
  # Configure the autologin cookie similar to the session cookie
  # Can be overridden in the node and instance configuration
  configuration_default['autologin_cookie_name'] ||= "_chili_#{inst['id']}_autologin"
  configuration_default['autologin_cookie_path'] ||= inst['external_uri'].path.to_s
  unless configuration_default.has_key?("autologin_cookie_secure")
    configuration_default['autologin_cookie_secure'] = true if inst['external_uri'].scheme == "https"
  end

  configuration_production = {'email_delivery' => {}}
  configuration_production['email_delivery'].tap do |cfg|
    node_cfg = node['chiliproject']['email_delivery'] || {}
    inst_cfg = inst['email_delivery'] || {}

    cfg['delivery_method'] = :async_smtp
    cfg['smtp_settings'] = {
      'address' => get_hosts(inst, 'email_delivery').first["ipaddress"],
      'port' => inst_cfg['port'] || node_cfg['port'],
      'domain' => node['fqdn'],
      'enable_starttls_auto' => true
    }
    if (authentication = inst_cfg.has_key?('authentication') ? inst_cfg['authentication'] : node_cfg['authentication'])
      cfg['smtp_settings'].merge!({
        'authentication' => authentication.to_sym,
        "user_name" => inst_cfg['username'] || node_cfg['username'],
        "password" => inst_cfg['password'] || node_cfg['password']
      })
    end
{'fraction_non_alphanumeric': 0.2026466521825005, 'fraction_numerical': 0.00711040884850879, 'mean_word_length': 4.4213062098501075, 'pattern_counts': {'":': 0, '<': 3, '<?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': '1850780', 'n_tokens_mistral': 3085, 'n_tokens_neox': 2926, 'n_words': 708}