<commit_before>require 'fulmar/domain/service/helper/vhost_helper'
include Fulmar::Domain::Service::Helper::VhostHelper

VHOST_DEFAULT_CONFIG = {
  webserver: 'nginx',
  sites_enabled_dir: '../sites-enabled',

}

vhost_count = 0
configuration.each { |_env, _target, data| vhost_count += 1 unless data[:vhost_template].blank? }

namespace :vhost do
  configuration.each do |env, target, data|
    next if data[:vhost_template].blank?

    desc "Create a vhost for #{env}"
    task (vhost_count > 1 ? "create:#{env}" : 'create') do
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      # Store remote_path for recovery
      remote_path = configuration[:remote_path]

      # Set some default variables:
      configuration[:sites_available_dir] ||= "/etc/#{configuration[:webserver]}/sites-available"
      configuration[:remote_path] = configuration[:sites_available_dir]
      configuration[:vhost_name] = vhost_name

      render_templates
      rendered_vhost_config = File.dirname(configuration[:local_path] + '/' + configuration[:vhost_template]) + \
                              '/' + File.basename(configuration[:vhost_template], '.erb')
      config_file_name = "#{File.dirname(rendered_vhost_config)}/auto_vhost_#{configuration[:vhost_name]}.conf"
      FileUtils.mv rendered_vhost_config, config_file_name
      upload config_file_name
      config_remote_path = configuration[:sites_available_dir] + '/' + File.basename(config_file_name)
      remote_shell.run [
                         "rm -f #{configuration[:sites_enabled_dir]}/#{File.basename(config_file_name)}", # remove any existing link
                         "ln -s #{config_remote_path} #{configuration[:sites_enabled_dir]}/#{File.basename(config_file_name)}",
                         "service #{configuration[:webserver]} reload"
                       ]

      # recover remote path
      configuration[:remote_path] = remote_path
    end

    desc "List existing vhosts for #{env}"
    task (vhost_count > 1 ? "list:#{env}" : 'list') do
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      remote_shell.run 'ls -1'
      remote_shell.last_output.each do |line|
        match = line.match(/auto_vhost_(.*)\.conf/)
        if match
          name = match[1]
          puts "- #{name}, delete via 'fulmar vhost:delete[#{name}]'"
        end
      end
    end

    desc "Delete a vhost for #{env}"
    task (vhost_count > 1 ? "delete:#{env}" : 'delete'), [:name] do |_t, argv|
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      remote_shell.run [
                         "rm auto_vhost_#{argv[:name]}.conf",
                         "rm #{configuration[:sites_enabled_dir]}/auto_vhost_#{argv[:name]}.conf",
                         "service #{configuration[:webserver] || 'nginx'} reload"
                       ]
    end
  end
end<commit_msg>[BUGFIX] Remove rendered vhost config after uploading<commit_after>require 'fulmar/domain/service/helper/vhost_helper'
include Fulmar::Domain::Service::Helper::VhostHelper

VHOST_DEFAULT_CONFIG = {
  webserver: 'nginx',
  sites_enabled_dir: '../sites-enabled',

}

vhost_count = 0
configuration.each { |_env, _target, data| vhost_count += 1 unless data[:vhost_template].blank? }

namespace :vhost do
  configuration.each do |env, target, data|
    next if data[:vhost_template].blank?

    desc "Create a vhost for #{env}"
    task (vhost_count > 1 ? "create:#{env}" : 'create') do
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      # Store remote_path for recovery
      remote_path = configuration[:remote_path]

      # Set some default variables:
      configuration[:sites_available_dir] ||= "/etc/#{configuration[:webserver]}/sites-available"
      configuration[:remote_path] = configuration[:sites_available_dir]
      configuration[:vhost_name] = vhost_name

      render_templates
      rendered_vhost_config = File.dirname(configuration[:local_path] + '/' + configuration[:vhost_template]) + \
                              '/' + File.basename(configuration[:vhost_template], '.erb')
      config_file_name = "#{File.dirname(rendered_vhost_config)}/auto_vhost_#{configuration[:vhost_name]}.conf"
      FileUtils.mv rendered_vhost_config, config_file_name
      upload config_file_name
      config_remote_path = configuration[:sites_available_dir] + '/' + File.basename(config_file_name)
      remote_shell.run [
                         "rm -f #{configuration[:sites_enabled_dir]}/#{File.basename(config_file_name)}", # remove any existing link
                         "ln -s #{config_remote_path} #{configuration[:sites_enabled_dir]}/#{File.basename(config_file_name)}",
                         "service #{configuration[:webserver]} reload"
                       ]

      FileUtils.rm config_file_name

      # recover remote path
      configuration[:remote_path] = remote_path
    end

    desc "List existing vhosts for #{env}"
    task (vhost_count > 1 ? "list:#{env}" : 'list') do
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      remote_shell.run 'ls -1'
      remote_shell.last_output.each do |line|
        match = line.match(/auto_vhost_(.*)\.conf/)
        if match
          name = match[1]
          puts "- #{name}, delete via 'fulmar vhost:delete[#{name}]'"
        end
      end
    end

    desc "Delete a vhost for #{env}"
    task (vhost_count > 1 ? "delete:#{env}" : 'delete'), [:name] do |_t, argv|
      configuration.environment = env
      configuration.target      = target
      configuration.merge(VHOST_DEFAULT_CONFIG)

      remote_shell.run [
                         "rm auto_vhost_#{argv[:name]}.conf",
                         "rm #{configuration[:sites_enabled_dir]}/auto_vhost_#{argv[:name]}.conf",
                         "service #{configuration[:webserver] || 'nginx'} reload"
                       ]

    end
  end
end
{'fraction_non_alphanumeric': 0.12040618955512572, 'fraction_numerical': 0.002256608639587363, 'mean_word_length': 2.8636363636363638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5701603', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1759, 'n_words': 347}