Class: Eql::GroupManager
- Inherits:
-
Object
- Object
- Eql::GroupManager
- Defined in:
- lib/eql.rb
Defined Under Namespace
Classes: GroupManagerError
Constant Summary collapse
- DEFAULT_METHOD =
Safer!
'ssh'
- DEFAULT_HOST =
'127.0.0.1'
- DEFAULT_SSH_PORT =
22
- DEFAULT_TELNET_PORT =
23
- DEFAULT_TIMEOUT =
10
- DEFAULT_USERNAME =
'grpadmin'
- DEFAULT_PASSWORD =
'grpadmin'
- PROMPT =
Typical command prompt
%r{^\S+>\s.*$}
- TERMINATOR =
Eql tty doesn’t translate n to rn
"\r"
Instance Attribute Summary collapse
-
#ops ⇒ Object
readonly
Returns the value of attribute ops.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(args = {}) ⇒ GroupManager
constructor
An options_file can be specified as well to keep user/passwords secret or abstract the config from code.
-
#members ⇒ Object
Get all available group members, returning the resulting Array.
-
#raw(args) ⇒ Object
Run an arbitrary command on the cli.
-
#snapshots(volume) ⇒ Object
Get all available snapshots on a given volume, returning the resulting Array.
-
#volumes ⇒ Object
Get all available group volumes, returning the resulting Array.
Constructor Details
#initialize(args = {}) ⇒ GroupManager
An options_file can be specified as well to keep user/passwords secret or abstract the config from code. It should be a yaml hash consisting of any of the ‘args[]’ below in the following format
:username: <user> :password: <pass> :method: [ssh|telnet] :host: <etc..>
See examples/
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/eql.rb', line 45 def initialize(args = {}) # Pecking order: # args > options_file > Defaults if args[:options_file] tmp = YAML.load_file(args[:options_file]) args = tmp.merge(args) end # Setup some standard, and non standard net::ssh/net::telnet options for # our connection @ops = { # nb: method isn't used by ssh/telnet, just here for consistency "Method" => args[:method] || DEFAULT_METHOD, "Host" => args[:host] || DEFAULT_HOST, "Port" => args[:port] || nil, "Timeout" => args[:timeout] || DEFAULT_TIMEOUT, # nb: Telnet doesn't use user/pass in this hash, but net::ssh does "Username" => args[:username] || DEFAULT_USERNAME, "Password" => args[:password] || DEFAULT_PASSWORD, "Dump_log" => args[:session_log] || nil, "Output_log" => args[:output_log] || nil, "Prompt" => PROMPT, # nb: Just net::ssh, net::telnet is autodetected "Terminator" => TERMINATOR, } # Don't pass it at all if undefined, they will try to open it @ops.delete("Dump_log") if @ops["Dump_log"].nil? @ops.delete("Output_log") if @ops["Output_log"].nil? if @ops["Method"] == "ssh" @ops["Port"] = DEFAULT_SSH_PORT if @ops["Port"].nil? @connection = Net::SSH::Telnet::new(@ops) elsif @ops["Method"] == "telnet" @ops["Port"] = DEFAULT_TELNET_PORT if @ops["Port"].nil? @connection = Net::Telnet::new(@ops) @connection.login(@ops["Username"], @ops["Password"]) end # Wish these were per session.. they could be read and restored on close # but the that would likely break down quickly. # # Make output easier to parse raw("cli-settings paging off") raw("cli-settings confirmation off") raw("cli-settings formatoutput off") # Keep spurious info off of terminal raw("cli-settings events off") # Finally set the display as wide as possible, this IS per session # thankfully raw("stty rows 256") raw("stty columns 255") raw("stty hardwrap off") end |
Instance Attribute Details
#ops ⇒ Object (readonly)
Returns the value of attribute ops.
16 17 18 |
# File 'lib/eql.rb', line 16 def ops @ops end |
Class Method Details
.open(args = {}) ⇒ Object
104 105 106 |
# File 'lib/eql.rb', line 104 def self.open(args = {}) new(args) end |
Instance Method Details
#close ⇒ Object
108 109 110 |
# File 'lib/eql.rb', line 108 def close @connection.close end |
#members ⇒ Object
Get all available group members, returning the resulting Array
155 156 157 |
# File 'lib/eql.rb', line 155 def members CliTable.parse(raw("show member")) end |
#raw(args) ⇒ Object
Run an arbitrary command on the cli. Returning a String or yielding on newlines in a block.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/eql.rb', line 114 def raw(args) timeout = @ops["Timeout"] if args.kind_of?(Hash) cmd = args[:cmd] timeout = args[:timeout] || timeout else cmd = args end data = @connection.cmd("String" => cmd, "Timeout" => timeout) # Errors seem to be in these formats: # # Error: Too many parameters # or # % Error - Member xxx does not exist. if data =~ %r{Error(:| -) (.*)\s?$} raise GroupManagerError.new($2) end if block_given? data.split("\n").each do |l| yield l end else data end end |