Class: MCollective::Application::Rpc

Inherits:
MCollective::Application show all
Defined in:
lib/mcollective/application/rpc.rb

Instance Method Summary collapse

Methods inherited from MCollective::Application

[], []=, #application_cli_arguments, #application_description, #application_failure, #application_options, application_options, #application_parse_options, #application_usage, #clioptions, #configuration, description, #disconnect, exclude_argument_sections, #halt, #halt_code, #help, intialize_application_options, option, #options, #rpcclient, #run, usage, #validate_cli_options, #validate_option

Methods included from RPC

const_missing, discovered, #empty_filter?, #printrpc, #printrpcstats, #rpcclient, #rpcoptions, stats

Instance Method Details

#mainObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mcollective/application/rpc.rb', line 80

def main
  mc = rpcclient(configuration[:agent])

  mc.agent_filter(configuration[:agent])

  string_to_ddl_type(configuration[:arguments], mc.ddl.action_interface(configuration[:action])) if mc.ddl

  mc.validate_request(configuration[:action], configuration[:arguments])

  if mc.reply_to
    configuration[:arguments][:process_results] = true

    puts "Request sent with id: " + mc.send(configuration[:action], configuration[:arguments]) + " replies to #{mc.reply_to}"
  elsif !configuration[:show_results]
    configuration[:arguments][:process_results] = false

    puts "Request sent with id: " + mc.send(configuration[:action], configuration[:arguments])
  else
    discover_args = {:verbose => true}
    # IF the discovery method hasn't been explicitly overridden
    #  and we're not being run interactively,
    #  and someone has piped us some data
    # Then we assume it's a discovery list - this can be either:
    #  - list of hosts in plaintext
    #  - JSON that came from another rpc or printrpc
    if mc.default_discovery_method && !STDIN.tty? && !STDIN.eof?
        # Then we override discovery to try to grok the data on STDIN
        mc.discovery_method = 'stdin'
        mc.discovery_options = 'auto'
        discover_args = {:verbose => false}
    end

    mc.discover discover_args

    printrpc mc.send(configuration[:action], configuration[:arguments])

    printrpcstats :summarize => true, :caption => "#{configuration[:agent]}##{configuration[:action]} call stats" if mc.discover.size > 0

    halt mc.stats
  end
end

#post_option_parser(configuration) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mcollective/application/rpc.rb', line 28

def post_option_parser(configuration)
  # handle the alternative format that optparse cant parse
  unless (configuration.include?(:agent) && configuration.include?(:action))
    if ARGV.length >= 2
      configuration[:agent] = ARGV[0]
      ARGV.delete_at(0)

      configuration[:action] = ARGV[0]
      ARGV.delete_at(0)

      ARGV.each do |v|
        if v =~ /^(.+?)=(.+)$/
          configuration[:arguments] = [] unless configuration.include?(:arguments)
          configuration[:arguments] << v
        else
          STDERR.puts("Could not parse --arg #{v}")
          exit(1)
        end
      end
    else
      STDERR.puts("No agent, action and arguments specified")
      exit(1)
    end
  end

  # convert arguments to symbols for keys to comply with simplerpc conventions
  args = configuration[:arguments].clone
  configuration[:arguments] = {}

  args.each do |v|
    if v =~ /^(.+?)=(.+)$/
      configuration[:arguments][$1.to_sym] = $2
    end
  end
end

#string_to_ddl_type(arguments, ddl) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mcollective/application/rpc.rb', line 64

def string_to_ddl_type(arguments, ddl)
  return if ddl.empty?

  arguments.keys.each do |key|
    if ddl[:input].keys.include?(key)
      case ddl[:input][key][:type]
        when :boolean
          arguments[key] = MCollective::DDL.string_to_boolean(arguments[key])

        when :number, :integer, :float
          arguments[key] = MCollective::DDL.string_to_number(arguments[key])
      end
    end
  end
end