<commit_before># === Synopsis:
#   RightAgent Deployer (rad) - (c) 2009-2011 RightScale
#
#   rad is a command line tool for building the configuration file for a RightAgent
#
#   The configuration file is generated in:
#     <agent name>/config.yml
#   in platform-specific RightAgent configuration directory
#
# === Examples:
#   Build configuration for agent named AGENT with default options:
#     rad AGENT
#
#   Build configuration for agent named AGENT so it uses given AMQP settings:
#     rad AGENT --user USER --pass PASSWORD --vhost VHOST --port PORT --host HOST
#     rad AGENT -u USER -p PASSWORD -v VHOST -P PORT -h HOST
#
# === Usage:
#    rad AGENT [options]
#
#    options:
#      --root-dir, -r DIR       Set agent root directory (containing actors, certs, and init subdirectories)
#      --cfg-dir, -c DIR        Set directory where generated configuration files for all agents are stored
#      --pid-dir, -z DIR        Set directory containing process id file
#      --actors-dirs, -a DIRS   Set comma-separated list of directories to search for actors in addition to
#                               default location in agent root directory
#      --identity, -i ID        Use base id ID to build agent's identity
#      --token, -t TOKEN        Use token TOKEN to build agent's identity
#      --prefix, -x PREFIX      Use prefix PREFIX to build agent's identity
#      --type TYPE              Use agent type TYPE to build agent's' identity,
#                               defaults to AGENT with any trailing '_[0-9]+' removed
#      --secure-identity, -S    Derive actual token from given TOKEN and ID
#      --url                    Set agent AMQP connection URL (host, port, user, pass, vhost)
#      --user, -u USER          Set agent AMQP username
#      --password, -p <PASSWORD>      Set agent AMQP password
#      --vhost, -v VHOST        Set agent AMQP virtual host
#      --host, -h HOST          Set AMQP broker host
#      --port, -P PORT          Set AMQP broker port
#      --prefetch COUNT         Set maximum requests AMQP broker is to prefetch before current is ack'd
#      --http-proxy PROXY       Use a proxy for all agent-originated HTTP traffic
#      --http-no-proxy NOPROXY  Comma-separated list of proxy exceptions (e.g. metadata server)
#      --time-to-live SEC       Set maximum age in seconds before a request times out and is rejected
#      --retry-timeout SEC      Set maximum number of seconds to retry request before give up
#      --retry-interval SEC     Set number of seconds before initial request retry, increases exponentially
#      --check-interval SEC     Set number of seconds between failed connection checks, increases exponentially
#      --ping-interval SEC      Set minimum number of seconds since last message receipt for the agent
#                               to ping the mapper to check connectivity, 0 means disable ping
#      --reconnect-interval SEC Set number of seconds between broker reconnect attempts
#      --grace-timeout SEC      Set number of seconds before graceful termination times out
#      --[no-]dup-check         Set whether to check for and reject duplicate requests, .e.g., due to retries
#      --options, -o KEY=VAL    Set options that act as final override for any persisted configuration settings
#      --monit, -m              Generate monit configuration file
#      --test                   Build test deployment using default test settings
#      --quiet, -Q              Do not produce output
#      --help                   Display help

require 'optparse'
require 'yaml'
require 'ftools'
require 'fileutils'
require 'rdoc/ri/ri_paths' # For backwards compat with ruby 1.8.5
require 'rdoc/usage'
require File.expand_path(File.join(File.dirname(__FILE__), 'rdoc_patch'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'right_agent'))
require File.expand_path(File.join(File.dirname(__FILE__), 'common_parser'))

module RightScale

  class AgentDeployer

    include CommonParser

    # Create and run deployer
    #
    # === Return
    # true:: Always return true
    def self.run
      d = AgentDeployer.new
      d.deploy(d.parse_args)
    end

    # Generate configuration from specified options and the agent's base options
    # and write them to a file
    #
    # === Parameters
    # options(Hash):: Command line options
    #
    # === Return
    # true:: Always return true
    def deploy(options)
      # Initialize directory settings
      AgentConfig.root_dir = options[:root_dir]
      AgentConfig.cfg_dir = options[:cfg_dir]
      AgentConfig.pid_dir = options[:pid_dir]

      # Configure agent
      cfg = load_init_config
      cfg[:actors_dirs] = options[:actor_dirs] if options[:actors_dirs]
      check_agent(options, cfg)
      cfg = configure(options, cfg)

      # Persist configuration
      persist(options, cfg)

      # Setup agent monitoring
      monitor(options) if options[:monit]
      true
    end

    # Create options hash from command line arguments
    #
    # === Return
    # options(Hash):: Parsed options
    def parse_args
      options = {}
      options[:agent_name] = ARGV[0]
      options[:options] = { :secure => true }
      options[:quiet] = false
      fail('No agent specified on the command line.', print_usage = true) if options[:agent_name].nil?

      opts = OptionParser.new do |opts|
        parse_common(opts, options)
        parse_other_args(opts, options)

        opts.on('-r', '--root-dir DIR') do |d|
          options[:root_dir] = d
        end

        opts.on('-c', '--cfg-dir DIR') do |d|
          options[:cfg_dir] = d
        end

        opts.on('-z', '--pid-dir DIR') do |d|
          options[:pid_dir] = d
        end

        opts.on('-a', '--actors-dirs DIRS') do |d|
          options[:actors_dirs] = d.split(',')
        end

        opts.on('-S', '--secure-identity') do
          options[:secure_identity] = true
        end

        opts.on('--http-proxy PROXY') do |proxy|
          options[:http_proxy] = proxy
        end

        opts.on('--http-no-proxy NOPROXY') do |no_proxy|
          options[:http_no_proxy] = no_proxy
        end

        opts.on('--time-to-live SEC') do |sec|
          options[:time_to_live] = sec.to_i<commit_msg>Add missing --monit option to agent_deployer<commit_after># === Synopsis:
#   RightAgent Deployer (rad) - (c) 2009-2011 RightScale
#
#   rad is a command line tool for building the configuration file for a RightAgent
#
#   The configuration file is generated in:
#     <agent name>/config.yml
#   in platform-specific RightAgent configuration directory
#
# === Examples:
#   Build configuration for agent named AGENT with default options:
#     rad AGENT
#
#   Build configuration for agent named AGENT so it uses given AMQP settings:
#     rad AGENT --user USER --pass PASSWORD --vhost VHOST --port PORT --host HOST
#     rad AGENT -u USER -p PASSWORD -v VHOST -P PORT -h HOST
#
# === Usage:
#    rad AGENT [options]
#
#    options:
#      --root-dir, -r DIR       Set agent root directory (containing actors, certs, and init subdirectories)
#      --cfg-dir, -c DIR        Set directory where generated configuration files for all agents are stored
#      --pid-dir, -z DIR        Set directory containing process id file
#      --actors-dirs, -a DIRS   Set comma-separated list of directories to search for actors in addition to
#                               default location in agent root directory
#      --identity, -i ID        Use base id ID to build agent's identity
#      --token, -t TOKEN        Use token TOKEN to build agent's identity
#      --prefix, -x PREFIX      Use prefix PREFIX to build agent's identity
#      --type TYPE              Use agent type TYPE to build agent's' identity,
#                               defaults to AGENT with any trailing '_[0-9]+' removed
#      --secure-identity, -S    Derive actual token from given TOKEN and ID
#      --url                    Set agent AMQP connection URL (host, port, user, pass, vhost)
#      --user, -u USER          Set agent AMQP username
#      --password, -p PASS      Set agent AMQP password
#      --vhost, -v VHOST        Set agent AMQP virtual host
#      --host, -h HOST          Set AMQP broker host
#      --port, -P PORT          Set AMQP broker port
#      --prefetch COUNT         Set maximum requests AMQP broker is to prefetch before current is ack'd
#      --http-proxy PROXY       Use a proxy for all agent-originated HTTP traffic
#      --http-no-proxy NOPROXY  Comma-separated list of proxy exceptions (e.g. metadata server)
#      --time-to-live SEC       Set maximum age in seconds before a request times out and is rejected
#      --retry-timeout SEC      Set maximum number of seconds to retry request before give up
#      --retry-interval SEC     Set number of seconds before initial request retry, increases exponentially
#      --check-interval SEC     Set number of seconds between failed connection checks, increases exponentially
#      --ping-interval SEC      Set minimum number of seconds since last message receipt for the agent
#                               to ping the mapper to check connectivity, 0 means disable ping
#      --reconnect-interval SEC Set number of seconds between broker reconnect attempts
#      --grace-timeout SEC      Set number of seconds before graceful termination times out
#      --[no-]dup-check         Set whether to check for and reject duplicate requests, .e.g., due to retries
#      --options, -o KEY=VAL    Set options that act as final override for any persisted configuration settings
#      --monit, -m              Generate monit configuration file
#      --test                   Build test deployment using default test settings
#      --quiet, -Q              Do not produce output
#      --help                   Display help

require 'optparse'
require 'yaml'
require 'ftools'
require 'fileutils'
require 'rdoc/ri/ri_paths' # For backwards compat with ruby 1.8.5
require 'rdoc/usage'
require File.expand_path(File.join(File.dirname(__FILE__), 'rdoc_patch'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'right_agent'))
require File.expand_path(File.join(File.dirname(__FILE__), 'common_parser'))

module RightScale

  class AgentDeployer

    include CommonParser

    # Create and run deployer
    #
    # === Return
    # true:: Always return true
    def self.run
      d = AgentDeployer.new
      d.deploy(d.parse_args)
    end

    # Generate configuration from specified options and the agent's base options
    # and write them to a file
    #
    # === Parameters
    # options(Hash):: Command line options
    #
    # === Return
    # true:: Always return true
    def deploy(options)
      # Initialize directory settings
      AgentConfig.root_dir = options[:root_dir]
      AgentConfig.cfg_dir = options[:cfg_dir]
      AgentConfig.pid_dir = options[:pid_dir]

      # Configure agent
      cfg = load_init_config
      cfg[:actors_dirs] = options[:actor_dirs] if options[:actors_dirs]
      check_agent(options, cfg)
      cfg = configure(options, cfg)

      # Persist configuration
      persist(options, cfg)

      # Setup agent monitoring
      monitor(options) if options[:monit]
      true
    end

    # Create options hash from command line arguments
    #
    # === Return
    # options(Hash):: Parsed options
    def parse_args
      options = {}
      options[:agent_name] = ARGV[0]
      options[:options] = { :secure => true }
      options[:quiet] = false
      fail('No agent specified on the command line.', print_usage = true) if options[:agent_name].nil?

      opts = OptionParser.new do |opts|
        parse_common(opts, options)
        parse_other_args(opts, options)

        opts.on('-r', '--root-dir DIR') do |d|
          options[:root_dir] = d
        end

        opts.on('-c', '--cfg-dir DIR') do |d|
          options[:cfg_dir] = d
        end

        opts.on('-z', '--pid-dir DIR') do |d|
          options[:pid_dir] = d
        end

        opts.on('-a', '--actors-dirs DIRS') do |d|
          options[:actors_dirs] = d.split(',')
        end

        opts.on('-w', '--monit') do
          options[:monit] = true
        end

        opts.on('-S', '--secure-identity') do
          options[:secure_identity] = true
        end

        opts.on('--http-proxy PROXY') do |proxy|
          options[:http_proxy] = proxy
        end

        opts.on('--http-no-proxy NOPROXY') do |no_proxy|
          options[:http_no_proxy] = no_proxy
        end

        opts.on('--time-to-live SEC') do |sec|
          options[:time_to_live] = sec.to_i
{'fraction_non_alphanumeric': 0.0960335682052094, 'fraction_numerical': 0.002375108859156045, 'mean_word_length': 2.2607124419204956, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 8, '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': '2491085', 'n_tokens_mistral': 3622, 'n_tokens_neox': 3471, 'n_words': 1296}