<commit_before>module MCollective
  class Application
    class Tasks < Application
      description "Puppet Task Orchestrator"

      usage <<-USAGE

    mco tasks [--detail]
    mco tasks <TASK NAME>
    mco tasks run <TASK NAME> [OPTIONS]
    mco tasks status <REQUEST> [FLAGS]

 The Bolt Task Orchestrator is designed to provide a consistent
 management environment for Bolt Tasks.

 It will download tasks from your Puppet Server onto all nodes
 and after verifying they were able to correctly download the
 same task across the entire fleet will run the task.

 Tasks are run in the background, the CLI can wait for up to 60
 seconds for your task to complete and show the status or you
 can use the status comment to review a completed task later.
      USAGE

      option :__environment,
             :arguments => ["--environment"],
             :description => "Environment to retrieve tasks from",
             :default => "production",
             :type => String

      exclude_argument_sections "common", "rpc"

      def post_option_parser(configuration)
        configuration[:__command] = ARGV.shift || "list"
      end

      def list_options
        self.class.option :__detail,
                          :arguments => ["--detail"],
                          :description => "Show command descriptions",
                          :default => false,
                          :type => :boolean
      end

      def status_options
        application_options[:usage].clear

        self.class.usage <<-USAGE

    mco tasks status <REQUEST> [FLAGS]

 Retrieves the status for a task you previously requested.  It can be running or completed.

 By default only failed exuecutions are shown, use --verbose to see them all.

        USAGE

        self.class.option :__summary,
                          :arguments => ["--summary"],
                          :description => "Only show a overall summary of the task",
                          :default => false,
                          :type => :boolean

        self.class.option :__metadata,
                          :arguments => ["--metadata"],
                          :description => "Only show task metadata for each node",
                          :default => false,
                          :type => :boolean
      end

      def run_options
        application_options[:usage].clear

        self.class.usage <<-USAGE

    mco tasks run <TASK NAME> [OPTIONS]

 Runs a task in the background and wait up to 50 seconds for it to complete.

 Task inputs are handled using --argument=value for basic String, Numeric and Boolean
 types, others can be passed using --input

 Input can also be read from a file using "--input @file.json" or "--input @file.yaml".

 For complex data types like Hashes, Arrays or Variants you have to supply input
 as YAML or JSON.

 Once a task is run the task ID will be displayed which can later be used with
 the "mco tasks status" command to extract results.

Examples:

    Run myapp::upgrade task in the background and wait for it to complete:

       mco tasks run myapp::upgrade --version 1.0.0

    Run myapp::upgrade task in the background and return immediately:

       mco tasks run myapp::upgrade --version 1.0.0 --background

    Supply complex data input to the task:

       mco tasks run myapp::upgrade --version 1.0.0 --input \\
          '{"source": {
            "url": "http://repo/archive-1.0.0.tgz",
            "hash": "68b329da9893e34099c7d8ad5cb9c940"}}'

        USAGE

        self.class.option :__summary,
                          :arguments => ["--summary"],
                          :description => "Only show a overall summary of the task",
                          :default => false,
                          :type => :boolean

        self.class.option :__background,
                          :arguments => ["--background"],
                          :description => "Do not wait for the task to complete",
                          :default => false,
                          :type => :boolean

        self.class.option :__json_input,
                          :arguments => ["--input INPUT"],
                          :description => "JSON input to pass to the task",
                          :required => false,
                          :type => String
      end

      def run_command
        task = ARGV.shift
        abort("Please specify a task to run") unless task

        puts("Attempting to download and run task %s on %d nodes" % [Util.colorize(:bold, task), bolt_task.discover.size])
        puts
        puts("Retrieving task metadata for task %s from the Puppet Server" % task)

        begin
          meta = tasks_support.task_metadata(task, configuration[:__environment])
        rescue
          abort($!.to_s)
        end

        download_files(task, meta["files"])

        request = {
          :task => task,
          :files => meta["files"].to_json
        }

        request[:input] = configuration[:__json_input] if configuration[:__json_input]

        puts<commit_msg>(#347) parse yaml and json files for input<commit_after>module MCollective
  class Application
    class Tasks < Application
      description "Puppet Task Orchestrator"

      usage <<-USAGE

    mco tasks [--detail]
    mco tasks <TASK NAME>
    mco tasks run <TASK NAME> [OPTIONS]
    mco tasks status <REQUEST> [FLAGS]

 The Bolt Task Orchestrator is designed to provide a consistent
 management environment for Bolt Tasks.

 It will download tasks from your Puppet Server onto all nodes
 and after verifying they were able to correctly download the
 same task across the entire fleet will run the task.

 Tasks are run in the background, the CLI can wait for up to 60
 seconds for your task to complete and show the status or you
 can use the status comment to review a completed task later.
      USAGE

      option :__environment,
             :arguments => ["--environment"],
             :description => "Environment to retrieve tasks from",
             :default => "production",
             :type => String

      exclude_argument_sections "common", "rpc"

      def post_option_parser(configuration)
        configuration[:__command] = ARGV.shift || "list"
      end

      def list_options
        self.class.option :__detail,
                          :arguments => ["--detail"],
                          :description => "Show command descriptions",
                          :default => false,
                          :type => :boolean
      end

      def status_options
        application_options[:usage].clear

        self.class.usage <<-USAGE

    mco tasks status <REQUEST> [FLAGS]

 Retrieves the status for a task you previously requested.  It can be running or completed.

 By default only failed exuecutions are shown, use --verbose to see them all.

        USAGE

        self.class.option :__summary,
                          :arguments => ["--summary"],
                          :description => "Only show a overall summary of the task",
                          :default => false,
                          :type => :boolean

        self.class.option :__metadata,
                          :arguments => ["--metadata"],
                          :description => "Only show task metadata for each node",
                          :default => false,
                          :type => :boolean
      end

      def run_options
        application_options[:usage].clear

        self.class.usage <<-USAGE

    mco tasks run <TASK NAME> [OPTIONS]

 Runs a task in the background and wait up to 50 seconds for it to complete.

 Task inputs are handled using --argument=value for basic String, Numeric and Boolean
 types, others can be passed using --input

 Input can also be read from a file using "--input @file.json" or "--input @file.yaml".

 For complex data types like Hashes, Arrays or Variants you have to supply input
 as YAML or JSON.

 Once a task is run the task ID will be displayed which can later be used with
 the "mco tasks status" command to extract results.

Examples:

    Run myapp::upgrade task in the background and wait for it to complete:

       mco tasks run myapp::upgrade --version 1.0.0

    Run myapp::upgrade task in the background and return immediately:

       mco tasks run myapp::upgrade --version 1.0.0 --background

    Supply complex data input to the task:

       mco tasks run myapp::upgrade --version 1.0.0 --input \\
          '{"source": {
            "url": "http://repo/archive-1.0.0.tgz",
            "hash": "68b329da9893e34099c7d8ad5cb9c940"}}'

        USAGE

        self.class.option :__summary,
                          :arguments => ["--summary"],
                          :description => "Only show a overall summary of the task",
                          :default => false,
                          :type => :boolean

        self.class.option :__background,
                          :arguments => ["--background"],
                          :description => "Do not wait for the task to complete",
                          :default => false,
                          :type => :boolean

        self.class.option :__json_input,
                          :arguments => ["--input INPUT"],
                          :description => "JSON input to pass to the task",
                          :required => false,
                          :type => String
      end

      def task_input
        return nil unless configuration[:__json_input]

        input = configuration[:__json_input]

        return input unless input.start_with?("@")

        input.sub!("@", "")

        return File.read(input) if input.end_with?("json")
        return YAML.safe_load(File.read(input)).to_json if input.end_with?("yaml")

        abort("Could not parse input from --input as YAML or JSON")
      end

      def run_command
        task = ARGV.shift

        abort("Please specify a task to run") unless task

        # here to test it early and fail fast
        input = task_input

        puts("Attempting to download and run task %s on %d nodes" % [Util.colorize(:bold, task), bolt_task.discover.size])
        puts
        puts("Retrieving task metadata for task %s from the Puppet Server" % task)

        begin
          meta = tasks_support.task_metadata(task, configuration[:__environment])
        rescue
          abort($!.to_s)
        end

        download_files(task, meta["files"])

        request = {
          :task => task,
          :files => meta["files"].to_json,
          :input => input
        }

        puts
{'fraction_non_alphanumeric': 0.0859899725664554, 'fraction_numerical': 0.007284079084287201, 'mean_word_length': 1.82598235765838, 'pattern_counts': {'":': 6, '<': 27, '<?xml version=': 0, '>': 74, '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': '5433063', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2580, 'n_words': 1030}