Class: Perus::Pinger::Command
- Inherits:
-
Object
- Object
- Perus::Pinger::Command
show all
- Defined in:
- lib/perus/pinger/command.rb
Direct Known Subclasses
CPU, ChromeCommand, HD, KillProcess, Mem, Process, RemovePath, Replace, Restart, Screenshot, Script, Sleep, Temp, Upgrade, Upload, Uptime, Value
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
@id = id
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
37
38
39
|
# File 'lib/perus/pinger/command.rb', line 37
def id
@id
end
|
#options ⇒ Object
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
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_name ⇒ Object
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
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
|
.options ⇒ Object
62
63
64
|
# File 'lib/perus/pinger/command.rb', line 62
def self.options
@options ||= []
end
|
.subclasses ⇒ Object
71
72
73
|
# File 'lib/perus/pinger/command.rb', line 71
def self.subclasses
@subclasses ||= []
end
|
Instance Method Details
#cleanup ⇒ Object
119
120
121
122
|
# File 'lib/perus/pinger/command.rb', line 119
def cleanup
end
|
#darwin? ⇒ Boolean
134
135
136
|
# File 'lib/perus/pinger/command.rb', line 134
def darwin?
shell('uname -s').strip == 'Darwin'
end
|
#run ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/perus/pinger/command.rb', line 107
def run
end
|
#shell(command) ⇒ Object
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
|