class Perus::Pinger::Command

Attributes

id[R]
options[R]

Public Class Methods

abstract!() click to toggle source
# File lib/perus/pinger/command.rb, line 84
def self.abstract!
    @abstract = true
end
abstract?() click to toggle source
# File lib/perus/pinger/command.rb, line 88
def self.abstract?
    @abstract
end
description(text = nil) click to toggle source

set or get command/metric description

# File lib/perus/pinger/command.rb, line 44
def self.description(text = nil)
    if text
        @description = text
    else
        @description
    end
end
human_name() click to toggle source
# File lib/perus/pinger/command.rb, line 39
def self.human_name
    name.demodulize.underscore.humanize.titlecase
end
inherited(subclass) click to toggle source
# File lib/perus/pinger/command.rb, line 66
def self.inherited(subclass)
    subclass.options.concat(self.options)
    Command.subclasses << subclass
end
metric!() click to toggle source

command classes which are intended to run as metrics call this method

# File lib/perus/pinger/command.rb, line 76
def self.metric!
    @metric = true
end
metric?() click to toggle source
# File lib/perus/pinger/command.rb, line 80
def self.metric?
    @metric
end
new(option_values, id = nil) click to toggle source

create a command instance and initialise it with the provided option values (hash where keys are option names). any restricted options are validated first, and an exception is thrown if the provided value is not one of the allowed values for the option.

# File lib/perus/pinger/command.rb, line 96
def initialize(option_values, id = nil)
    @options = OpenStruct.new
    self.class.options.each do |option|
        option.process(@options, option_values)
    end

    # commands (not metrics) have ids that uniquely identify a command
    # instance and its response
    @id = id
end
option(name, option_settings = {}) click to toggle source

add an option to the command/metric. both the class and instances of the class have an options method. the class version returns a list of Option objects representing possible options for the command. the object version returns an OpenStruct hash of options and their values (defaults merged with provided values)

# File lib/perus/pinger/command.rb, line 57
def self.option(name, option_settings = {})
    @options ||= []
    @options << Option.new(name, option_settings, self)
end
options() click to toggle source
# File lib/perus/pinger/command.rb, line 62
def self.options
    @options ||= []
end
subclasses() click to toggle source
# File lib/perus/pinger/command.rb, line 71
def self.subclasses
    @subclasses ||= []
end

Public Instance Methods

cleanup() click to toggle source
# File lib/perus/pinger/command.rb, line 119
def cleanup
    # called after sending data to server. remove temporary
    # files etc.
end
darwin?() click to toggle source
# File lib/perus/pinger/command.rb, line 141
def darwin?
    shell('uname -s').strip == 'Darwin'
end
run() click to toggle source
# File lib/perus/pinger/command.rb, line 107
def run
    # run command/metric, return types for a command are:
    # true: successful (will show as success on the server)
    # string: failed, string should provide the reason
    # file: file to upload
    # proc: code to run after returning success to server
    # hash: metrics are expected to only return a hash with
    # keys indicating metric names, and values restricted to
    # files, numerics and strings.
    # exceptions are caught and shown as errors on the server.
end
shell(command) click to toggle source
# File lib/perus/pinger/command.rb, line 127
def shell(command)
    out, err, status = Open3.capture3(command)

    if status.exitstatus > 0 && !err.empty?
        raise ShellCommandError.new("(cmd: #{command}) => #{err.strip}; #{out.strip}; exit: #{status.exitstatus}")
    end

    if status.exitstatus > 0
        raise ShellCommandError.new("(cmd: #{command}) => #{out.strip}; exit: #{status.exitstatus}")
    end

    out
end