<commit_before>module CMDClean
  def clean(_argv)
    machine_def = check_and_get_stack
    do_clean(machine_def)
  end

  def do_clean(machine_def)
    # Note that the ordering here is important - must have killed VMs before
    # removing their puppet cert, otherwise we have a race condition
    nagios_schedule_downtime(machine_def)
    clean_nodes(machine_def)
    puppet_clean(machine_def)
  end

  private

  def clean_nodes(machine_def)
    computecontroller = Compute::Controller.new
    computecontroller.clean(machine_def.to_specs) do
      on :success do |vm, msg|
        logger(Logger::INFO) { "successfully cleaned #{vm}: #{msg}" }
      end
      on :failure do |vm, msg|
        logger(Logger::ERROR) { "failed to clean #{vm}: #{msg}" }
      end
      on :unaccounted do |vm|
        logger(Logger::WARN) { "VM was unaccounted for: #{vm}" }
      end
    end
  end

  def puppet_clean(machine_def)
    puppet_certs_to_clean = []
    machine_def.accept do |child_machine_def|
      if child_machine_def.respond_to?(:mgmt_fqdn)
        if child_machine_def.needs_signing?
          puppet_certs_to_clean << child_machine_def.mgmt_fqdn
        else
          logger(Logger::INFO) { "removal of cert not needed for #{child_machine_def.mgmt_fqdn}" }
        end
      end
    end

    include Support::MCollectivePuppet
    ca_clean(puppet_certs_to_clean) do
      on :success do |machine|
        logger(Logger::INFO) { "successfully removed cert for #{machine}" }
      end
      on :failed do |machine|
        logger(Logger::WARN) { "failed to remove cert for #{machine}" }
      end
    end
  end
end<commit_msg>tomd: hrmm... move include into the module, not sure you can import into a method...<commit_after>require 'stackbuilder/support/mcollective_puppet'

module CMDClean
  def clean(_argv)
    machine_def = check_and_get_stack
    do_clean(machine_def)
  end

  def do_clean(machine_def)
    # Note that the ordering here is important - must have killed VMs before
    # removing their puppet cert, otherwise we have a race condition
    nagios_schedule_downtime(machine_def)
    clean_nodes(machine_def)
    puppet_clean(machine_def)
  end

  private

  def clean_nodes(machine_def)
    computecontroller = Compute::Controller.new
    computecontroller.clean(machine_def.to_specs) do
      on :success do |vm, msg|
        logger(Logger::INFO) { "successfully cleaned #{vm}: #{msg}" }
      end
      on :failure do |vm, msg|
        logger(Logger::ERROR) { "failed to clean #{vm}: #{msg}" }
      end
      on :unaccounted do |vm|
        logger(Logger::WARN) { "VM was unaccounted for: #{vm}" }
      end
    end
  end

  include Support::MCollectivePuppet

  def puppet_clean(machine_def)
    puppet_certs_to_clean = []
    machine_def.accept do |child_machine_def|
      if child_machine_def.respond_to?(:mgmt_fqdn)
        if child_machine_def.needs_signing?
          puppet_certs_to_clean << child_machine_def.mgmt_fqdn
        else
          logger(Logger::INFO) { "removal of cert not needed for #{child_machine_def.mgmt_fqdn}" }
        end
      end
    end

    ca_clean(puppet_certs_to_clean) do
      on :success do |machine|
        logger(Logger::INFO) { "successfully removed cert for #{machine}" }
      end
      on :failed do |machine|
        logger(Logger::WARN) { "failed to remove cert for #{machine}" }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.08932268559597752, 'fraction_numerical': 0.0, 'mean_word_length': 3.0600240096038416, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1869185', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1076, 'n_words': 281}