Skip to content

Commit

Permalink
Merge pull request #84 from ripienaar/83
Browse files Browse the repository at this point in the history
(#83) support external applications
  • Loading branch information
ripienaar authored Dec 26, 2020
2 parents d618c3c + fde81c0 commit 43a01ae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
AllCops:
TargetRubyVersion: "2.4"
DisplayCopNames: true
DisplayStyleGuide: true
SuggestExtensions: false
TargetRubyVersion: 2.4
Exclude:
- "module/**/*"
- "vendor/**/*"
Expand Down
39 changes: 38 additions & 1 deletion lib/mcollective/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def description(descr)
self[:description] = descr
end

# Executes an external program instead of implement the logic in ruby
#
# @param [Hash] command the command to run
# @option command [String] :command the command to run
# @option command [Array] :args arguments to pass to the command
def external(command)
self[:external] = command
end

# Executes an external program to show help instead of supplying options
#
# @param [Hash] command the command to run
# @option command [String] :command the command to run
# @option command [Array] :args arguments to pass to the command
def external_help(command)
self[:external_help] = command
end

# Supplies usage information, calling multiple times will
# create multiple usage lines in --help output
def usage(usage)
Expand Down Expand Up @@ -82,7 +100,9 @@ def intialize_application_options
@application_options = {:description => nil,
:usage => [],
:cli_arguments => [],
:exclude_arg_sections => []}
:exclude_arg_sections => [],
:external => nil,
:external_help => nil}
end
end

Expand Down Expand Up @@ -267,14 +287,23 @@ def application_failure(err, err_dest=$stderr)
exit 1
end

def external_help
ext = application_options[:external_help]
exec(ext[:command], ext[:args])
end

def help
return external_help if application_options[:external_help]

application_parse_options(true)
end

# The main logic loop, builds up the options, validate configuration and calls
# the main as supplied by the user. Disconnects when done and pass any exception
# onto the application_failure helper
def run
return external_main if application_options[:external]

application_parse_options

validate_configuration(configuration) if respond_to?(:validate_configuration)
Expand All @@ -293,6 +322,14 @@ def disconnect
rescue # rubocop:disable Lint/SuppressedException
end

def external_main
ext = application_options[:external]
args = ext[:args] || []
args.concat(ARGV)

exec(ext[:command], *args)
end

# Fake abstract class that logs if the user tries to use an application without
# supplying a main override method.
def main
Expand Down
76 changes: 2 additions & 74 deletions lib/mcollective/application/ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,7 @@ module MCollective
class Application::Ping < Application # rubocop:disable Style/ClassAndModuleChildren
description "Ping all nodes"

option :graph,
:description => "Shows a graph of ping distribution",
:arguments => ["--graph", "-g"],
:default => false,
:type => :bool

# Convert the times structure into a array representing
# buckets of responses in 50 ms intervals. Return a small
# sparkline graph using UTF8 characters
def spark(resp_times)
return "" unless configuration[:graph] || Config.instance.pluginconf["rpc.graph"]

ticks = %w[ ]

histo = {}

# round each time to its nearest 50ms
# and keep a count for each 50ms
resp_times.each do |time|
time = Integer(time + 50 - (time % 50))
histo[time] ||= 0
histo[time] += 1
end

# set the 50ms intervals that saw no traffic to 0
((histo.keys.max - histo.keys.min) / 50).times do |i|
time = (i * 50) + histo.keys.min
histo[time] = 0 unless histo[time]
end

# get a numerically sorted list of times
histo = histo.keys.sort.map {|k| histo[k]}

scale = ticks.size - 1
distance = histo.max.to_f / scale

histo.map do |val|
tick = (val / distance).round
tick = 0 if tick < 0

ticks[tick]
end.join
end

def main
# If the user did not override the default timeout include the discovery timeout
if options[:timeout] == 5
discovery_timeout = options[:disctimeout] || Config.instance.discovery_timeout || 0
options[:timeout] = options[:timeout] + discovery_timeout
end
client = MCollective::Client.new(options)

start = Time.now.to_f
times = []

client.req("ping", "discovery") do |resp|
times << (Time.now.to_f - start) * 1000

puts "%-40s time=%.2f ms" % [resp[:senderid], times.last]
end

puts("\n\n---- ping statistics ----")

if !times.empty?
sum = times.inject(0) {|acc, i| acc + i}
avg = sum / times.length.to_f

puts "%d replies max: %.2f min: %.2f avg: %.2f %s" % [times.size, times.max, times.min, avg, spark(times)]
else
puts("No responses received")
end

halt client.stats
end
external(:command => "choria", :args => ["ping"])
external_help(:command => "choria", :args => ["ping", "--help"])
end
end
8 changes: 6 additions & 2 deletions spec/unit/mcollective/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ module MCollective
expect(Application.application_options).to eq({:description => nil,
:usage => [],
:cli_arguments => [],
:exclude_arg_sections => []})
:exclude_arg_sections => [],
:external => nil,
:external_help => nil})
end
end

Expand All @@ -36,7 +38,9 @@ module MCollective
expect(Application.intialize_application_options).to eq({:description => nil,
:usage => [],
:cli_arguments => [],
:exclude_arg_sections => []})
:exclude_arg_sections => [],
:external => nil,
:external_help => nil})
end
end

Expand Down

0 comments on commit 43a01ae

Please sign in to comment.