<commit_before>module VirtualMonkey
  module Command
    # This command does all the steps create/run/conditionaly destroy
    def self.troop
      raise "Aborting" unless VirtualMonkey::Toolbox::api0_1?
      @@options = Trollop::options do
        text "This command performs all the operations of the monkey in one execution.  Create/Run/Destroy"
        opt :file, "troop config, see config/troop/*sample.json for example format", :type => :string, :required => true
        opt :no_spot, "do not use spot instances"
        opt :steps, "use the troop config file to do either: create, run, or destroy", :type => :strings
        opt :tag, "add an additional tag to the deployments", :type => :string
        opt :create, "interactive mode: create troop config"
        opt :mci_override, "list of mcis to use instead of the ones from the server template. expects full hrefs.", :type => :string, :multi => true, :required => false
        opt :no_delete, "only terminate, no deletion.", :short => "-d"
        opt :verbose, "Print all output to STDOUT as well as the log files", :short => "-v"
        opt :list_trainer, "run through the interactive white- and black-list trainer after the tests complete, before the deployments are destroyed"
        opt :qa, "Before destroying deployments, does a strict blacklist check (ignores whitelist)"
        opt :cloud_override, "Space-separated list of cloud_ids to use", :type => :integers, :short => '-i'
        opt :only, "Regex string to use for subselection matching on MCIs to enumerate Eg. --only Ubuntu", :type => :string
      end

      # PATHs SETUP
      features_glob = Dir.glob(File.join(@@features_dir, "**")).collect { |c| File.basename(c) }
      cloud_variables_glob = Dir.glob(File.join(@@cv_dir, "**")).collect { |c| File.basename(c) }
      common_inputs_glob = Dir.glob(File.join(@@ci_dir, "**")).collect { |c| File.basename(c) }
      
      # CREATE NEW CONFIG
      if @@options[:create]
        troop_config = {}
        troop_config[:tag] = ask("What tag to use for creating the deployments?")
        troop_config[:server_template_ids] = ask("What Server Template ids (or names) would you like to use to create the deployments (comma delimited)?").split(",")
        troop_config[:server_template_ids].each {|st| st.strip!}

        file_or_nums =
          choose do |menu|
            menu.prompt = "Use a single cloud_variables config file, or a list of cloud_ids?"
            menu.index = :number
            menu.choices("cloud_variables Config File", "List of Cloud IDs")
          end

        if file_or_nums =~ /cloud_variables/
          troop_config[:cloud_variables] =
            choose do |menu|
              menu.prompt = "Which cloud_variables config file?"
              menu.index = :number
              menu.choices(*cloud_variables_glob)
            end
        else
          puts "Available Clouds:"
          get_available_clouds().each { |cloud| puts "#{c['cloud_id']}: #{c['name']}" }
          list_of_clouds = ask("Enter a space-separated list of cloud_ids to use").split(" ")
          troop_config[:clouds] = list_of_clouds.map { |c| c.to_i }
        end

        troop_config[:common_inputs] =
          choose do |menu|
            menu.prompt = "Which common_inputs config file?"
            menu.index = :number
            menu.choices(*common_inputs_glob)
          end

        troop_config[:feature] = 
          choose do |menu|
            menu.prompt = "Which feature file?"
            menu.index = :number
            menu.choices(*features_glob)
          end
        
        write_out = troop_config.to_json( :indent => "  ", 
                                          :object_nl => "\n",
                                          :array_nl => "\n" )
        File.open(@@options[:file], "w") { |f| f.write(write_out) }
        say("created config file #{@@options[:file]}")
        say("Done.")
      else
        # Execute Main
        config = JSON::parse(IO.read(@@options[:file]))
        @@options[:tag] += "-" if @@options[:tag]
        @@options[:tag] = "" unless @@options[:tag]
        @@options[:tag] += config['tag']
        if @@options[:steps].is_a?(Array)
          @@options[:steps] = @@options[:steps].join(" ")
        end
        @@options[:steps] = "all" unless @@options[:steps] =~ /(create)|(run)|(destroy)/
        @@options[:cloud_variables] = File.join(@@cv_dir, config['cloud_variables']) unless @@options[:cloud_override]
        @@options[:clouds] = @@options[:cloud_override] if @@options[:cloud_override]
        @@options[:common_inputs] = config['common_inputs'].map { |cipath| File.join(@@ci_dir, cipath) }
        @@options[:feature] = File.join(@@features_dir, config['feature'])
        @@options[:runner] = get_runner_class
        @@options[:terminate] = true if @@options[:steps] =~ /(all)|(destroy)/
        unless @@options[:steps] =~ /(all)|(create)|(run)|(destroy)/
          raise "Invalid --steps argument. Valid steps are: 'all', 'create', 'run', or 'destroy'"
        end

        # CREATE PHASE
        if @@options[:steps] =~ /((all)|(create))/
          @@dm = DeploymentMonk.new(@@options[:tag], config['server_template_ids'])
          unless @@dm.deployments.size > 0
            create_logic # NEW INTERNAL COMMAND
          else
            puts "Existing deployments matching --tag #{@@options[:tag]} found. Skipping deployment creation."
          end
        end<commit_msg>Backward compatibility with old troop files<commit_after>module VirtualMonkey
  module Command
    # This command does all the steps create/run/conditionaly destroy
    def self.troop
      raise "Aborting" unless VirtualMonkey::Toolbox::api0_1?
      @@options = Trollop::options do
        text "This command performs all the operations of the monkey in one execution.  Create/Run/Destroy"
        opt :file, "troop config, see config/troop/*sample.json for example format", :type => :string, :required => true
        opt :no_spot, "do not use spot instances"
        opt :steps, "use the troop config file to do either: create, run, or destroy", :type => :strings
        opt :tag, "add an additional tag to the deployments", :type => :string
        opt :create, "interactive mode: create troop config"
        opt :mci_override, "list of mcis to use instead of the ones from the server template. expects full hrefs.", :type => :string, :multi => true, :required => false
        opt :no_delete, "only terminate, no deletion.", :short => "-d"
        opt :verbose, "Print all output to STDOUT as well as the log files", :short => "-v"
        opt :list_trainer, "run through the interactive white- and black-list trainer after the tests complete, before the deployments are destroyed"
        opt :qa, "Before destroying deployments, does a strict blacklist check (ignores whitelist)"
        opt :cloud_override, "Space-separated list of cloud_ids to use", :type => :integers, :short => '-i'
        opt :only, "Regex string to use for subselection matching on MCIs to enumerate Eg. --only Ubuntu", :type => :string
      end

      # PATHs SETUP
      features_glob = Dir.glob(File.join(@@features_dir, "**")).collect { |c| File.basename(c) }
      cloud_variables_glob = Dir.glob(File.join(@@cv_dir, "**")).collect { |c| File.basename(c) }
      common_inputs_glob = Dir.glob(File.join(@@ci_dir, "**")).collect { |c| File.basename(c) }
      
      # CREATE NEW CONFIG
      if @@options[:create]
        troop_config = {}
        troop_config[:tag] = ask("What tag to use for creating the deployments?")
        troop_config[:server_template_ids] = ask("What Server Template ids (or names) would you like to use to create the deployments (comma delimited)?").split(",")
        troop_config[:server_template_ids].each {|st| st.strip!}

        file_or_nums =
          choose do |menu|
            menu.prompt = "Use a single cloud_variables config file, or a list of cloud_ids?"
            menu.index = :number
            menu.choices("cloud_variables Config File", "List of Cloud IDs")
          end

        if file_or_nums =~ /cloud_variables/
          troop_config[:cloud_variables] =
            choose do |menu|
              menu.prompt = "Which cloud_variables config file?"
              menu.index = :number
              menu.choices(*cloud_variables_glob)
            end
        else
          puts "Available Clouds:"
          get_available_clouds().each { |cloud| puts "#{c['cloud_id']}: #{c['name']}" }
          list_of_clouds = ask("Enter a space-separated list of cloud_ids to use").split(" ")
          troop_config[:clouds] = list_of_clouds.map { |c| c.to_i }
        end

        troop_config[:common_inputs] =
          choose do |menu|
            menu.prompt = "Which common_inputs config file?"
            menu.index = :number
            menu.choices(*common_inputs_glob)
          end

        troop_config[:feature] = 
          choose do |menu|
            menu.prompt = "Which feature file?"
            menu.index = :number
            menu.choices(*features_glob)
          end
        
        write_out = troop_config.to_json( :indent => "  ", 
                                          :object_nl => "\n",
                                          :array_nl => "\n" )
        File.open(@@options[:file], "w") { |f| f.write(write_out) }
        say("created config file #{@@options[:file]}")
        say("Done.")
      else
        # Execute Main
        config = JSON::parse(IO.read(@@options[:file]))
        @@options[:tag] += "-" if @@options[:tag]
        @@options[:tag] = "" unless @@options[:tag]
        @@options[:tag] += config['tag']
        if @@options[:steps].is_a?(Array)
          @@options[:steps] = @@options[:steps].join(" ")
        end
        @@options[:steps] = "all" unless @@options[:steps] =~ /(create)|(run)|(destroy)/
        @@options[:cloud_variables] = File.join(@@cv_dir, config['cloud_variables']) unless @@options[:cloud_override]
        @@options[:clouds] = @@options[:cloud_override] if @@options[:cloud_override]
        @@options[:common_inputs] = config['common_inputs'].map { |cipath| File.join(@@ci_dir, cipath) }
        @@options[:feature] = File.join(@@features_dir, config['feature'])
        @@options[:runner] = (config['runner'] ? config['runner'] : get_runner_class)
        @@options[:terminate] = true if @@options[:steps] =~ /(all)|(destroy)/
        unless @@options[:steps] =~ /(all)|(create)|(run)|(destroy)/
          raise "Invalid --steps argument. Valid steps are: 'all', 'create', 'run', or 'destroy'"
        end

        # CREATE PHASE
        if @@options[:steps] =~ /((all)|(create))/
          @@dm = DeploymentMonk.new(@@options[:tag], config['server_template_ids'])
          unless @@dm.deployments.size > 0
            create_logic # NEW INTERNAL COMMAND
          else
            puts "Existing deployments matching --tag #{@@options[:tag]} found. Skipping deployment creation."
          end
        end
{'fraction_non_alphanumeric': 0.13747268754552075, 'fraction_numerical': 0.0005462490895848507, 'mean_word_length': 2.568875893437297, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2251120', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3098, 'n_words': 902}