Class: Penctl
- Inherits:
-
Object
- Object
- Penctl
- Defined in:
- lib/penctl.rb
Class Method Summary collapse
-
.execute(server, cmd, tries_left = 5) ⇒ Object
Connects to pen’s control port and issues a command.
-
.get_set_attribute(pen, attribute, value = nil) ⇒ Object
Takes an attribute name along with a value and returns the value.
-
.parse_server_line(line) ⇒ Object
Takes a line of the output of penctl’s ‘servers’ command and turns it into a hash with the keys :slot, :addr:, :port, :conn, :max, :hard, :sx, :rx.
-
.send_command(host, port, cmd) ⇒ Object
Opens a socket, sends a command to pen and returns the result.
-
.set_boolean_attribute(pen, attribute, value) ⇒ Object
Takes an attribute name along with a boolean value and turns it into a penctl command.
-
.update_server(server, slot, settings) ⇒ Object
Sends a penctl server command to update a server’s settings.
Class Method Details
.execute(server, cmd, tries_left = 5) ⇒ Object
Connects to pen’s control port and issues a command. It will wait a moment and
try to contact the pen server again, when it could not be reached (raising an
exception when it gives up).
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/penctl.rb', line 10 def self.execute( server, cmd, tries_left = 5 ) raise StandardError.new("Error talking to pen, giving up.") unless tries_left > 0 shell_cmd = "penctl #{server} #{cmd}" host, port = server.split ':' begin send_command( host, port, cmd ) rescue Errno::ECONNRESET, Errno::EPIPE sleep 0.5 return Penctl.execute( server, cmd, tries_left.pred) end end |
.get_set_attribute(pen, attribute, value = nil) ⇒ Object
Takes an attribute name along with a value and returns the value.
70 71 72 73 74 |
# File 'lib/penctl.rb', line 70 def self.get_set_attribute(pen, attribute, value = nil) cmd = attribute.to_s.chomp '=' value = value.to_s.empty? ? '' : " #{value}" tidy_output(Penctl.execute(pen, "#{cmd}#{value}".chomp)) end |
.parse_server_line(line) ⇒ Object
Takes a line of the output of penctl’s ‘servers’ command and turns it into
a hash with the keys :slot, :addr:, :port, :conn, :max, :hard, :sx, :rx
49 50 51 52 53 54 55 |
# File 'lib/penctl.rb', line 49 def self.parse_server_line( line ) keys = %w{ slot addr port conn max hard sx rx} hash = Hash[*("slot #{line}".split)] server = {} keys.each { |k| server[k.to_sym] = (k == "addr") ? hash[k] : hash[k].to_i } return server end |
.send_command(host, port, cmd) ⇒ Object
Opens a socket, sends a command to pen and returns the result
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/penctl.rb', line 25 def self.send_command( host, port, cmd ) socket = TCPSocket.open( host, port.to_i ) socket.puts cmd result = [] while line = socket.gets result << line.chomp end socket.close result end |
.set_boolean_attribute(pen, attribute, value) ⇒ Object
Takes an attribute name along with a boolean value and turns it into
a penctl command. Returns true on success, false on failure.
61 62 63 64 65 |
# File 'lib/penctl.rb', line 61 def self.set_boolean_attribute(pen, attribute, value) cmd = attribute.to_s.chomp '=' cmd = value ? cmd : "no " + cmd Penctl.execute(pen, cmd) == ["0"] end |
.update_server(server, slot, settings) ⇒ Object
Sends a penctl server command to update a server’s settings.
39 40 41 42 |
# File 'lib/penctl.rb', line 39 def self.update_server( server, slot, settings ) cmd = settings.to_a.sort_by{ |k,v| k.to_s }.flatten.join(' ') Penctl.execute( server, "server #{slot} #{cmd}" ) end |