Class: Perus::Pinger::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/perus/pinger/command.rb

Defined Under Namespace

Classes: ShellCommandError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option_values, id = nil) ⇒ Command

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.



96
97
98
99
100
101
102
103
104
105
# 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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



37
38
39
# File 'lib/perus/pinger/command.rb', line 37

def id
  @id
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/perus/pinger/command.rb', line 37

def options
  @options
end

Class Method Details

.abstract!Object



84
85
86
# File 'lib/perus/pinger/command.rb', line 84

def self.abstract!
    @abstract = true
end

.abstract?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/perus/pinger/command.rb', line 88

def self.abstract?
    @abstract
end

.description(text = nil) ⇒ Object

set or get command/metric description



44
45
46
47
48
49
50
# File 'lib/perus/pinger/command.rb', line 44

def self.description(text = nil)
    if text
        @description = text
    else
        @description
    end
end

.human_nameObject



39
40
41
# File 'lib/perus/pinger/command.rb', line 39

def self.human_name
    name.demodulize.underscore.humanize.titlecase
end

.inherited(subclass) ⇒ Object



66
67
68
69
# File 'lib/perus/pinger/command.rb', line 66

def self.inherited(subclass)
    subclass.options.concat(self.options)
    Command.subclasses << subclass
end

.metric!Object

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



76
77
78
# File 'lib/perus/pinger/command.rb', line 76

def self.metric!
    @metric = true
end

.metric?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/perus/pinger/command.rb', line 80

def self.metric?
    @metric
end

.option(name, option_settings = {}) ⇒ Object

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)



57
58
59
60
# File 'lib/perus/pinger/command.rb', line 57

def self.option(name, option_settings = {})
    @options ||= []
    @options << Option.new(name, option_settings, self)
end

.optionsObject



62
63
64
# File 'lib/perus/pinger/command.rb', line 62

def self.options
    @options ||= []
end

.subclassesObject



71
72
73
# File 'lib/perus/pinger/command.rb', line 71

def self.subclasses
    @subclasses ||= []
end

Instance Method Details

#cleanupObject



119
120
121
122
# File 'lib/perus/pinger/command.rb', line 119

def cleanup
    # called after sending data to server. remove temporary
    # files etc.
end

#darwin?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/perus/pinger/command.rb', line 134

def darwin?
    shell('uname -s').strip == 'Darwin'
end

#runObject



107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object

Raises:



127
128
129
130
131
132
# File 'lib/perus/pinger/command.rb', line 127

def shell(command)
    out, err, status = Open3.capture3(command)
    raise ShellCommandError.new(err.strip) unless err.empty?
    raise ShellCommandError.new(out.strip) if status.exitstatus > 0
    out
end