Class: Cow::Application
- Inherits:
-
Object
- Object
- Cow::Application
- Defined in:
- lib/cow/application.rb
Defined Under Namespace
Classes: Cache
Constant Summary collapse
- CACHE_FILE =
'/var/cache/cow/cow.cache'.freeze
Instance Method Summary collapse
- #cmd_add(_hostname, _type, _snmp_community) ⇒ Object
- #cmd_connect(_portname) ⇒ Object
- #cmd_find(_keyword) ⇒ Object
- #cmd_list(_hostname) ⇒ Object
- #cmd_server_list ⇒ Object
- #cmd_update(_hostname) ⇒ Object
- #create_cache(_cache = CACHE_FILE) ⇒ Object
- #edit_cache(_cache = CACHE_FILE) ⇒ Object
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #load_cache(_cache = CACHE_FILE) ⇒ Object
- #run ⇒ Object
- #show_help ⇒ Object
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
30 31 32 |
# File 'lib/cow/application.rb', line 30 def initialize @name = 'cow' end |
Instance Method Details
#cmd_add(_hostname, _type, _snmp_community) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cow/application.rb', line 111 def cmd_add(_hostname, _type, _snmp_community) raise ArgumentError unless _hostname.class == String && _type.class == String && _snmp_community.class == String raise ArgumentError unless Cow::SERVER_TYPES.keys.include?(_type.to_sym) puts "#{_hostname}(#{_type}) will be added" server = Cow::SERVER_TYPES[_type.to_sym].new(_hostname, _snmp_community) server.ports.each do |port| puts '%s %s' % ["#{server.hostname}/#{port.port}".ljust(30), port.name.to_s.ljust(20)] end load_cache edit_cache do |cache| cache.add_server server end if server end |
#cmd_connect(_portname) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/cow/application.rb', line 130 def cmd_connect(_portname) raise ArgumentError unless _portname.class == String load_cache @cache.servers.each do |server| server.find(_portname).each do |port| if port.name == _portname puts "Found on #{server.hostname}/#{port.port}. Connecting..." server.connect(port) break end end end end |
#cmd_find(_keyword) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/cow/application.rb', line 146 def cmd_find(_keyword) raise ArgumentError unless _keyword.class == String load_cache puts '%s %s %s' % ['SERVER/PORT'.ljust(30), 'PORTNAME'.ljust(20), 'COMMAND'] @cache.servers.each do |server| server.find(_keyword).each do |port| puts '%s %s %s' % ["#{server.hostname}/#{port.port}".ljust(30), port.name.to_s.ljust(20), server.connect_command(port)] end end end |
#cmd_list(_hostname) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/cow/application.rb', line 159 def cmd_list(_hostname) load_cache puts '%s %s %s' % ['SERVER/PORT'.ljust(30), 'PORTNAME'.ljust(20), 'COMMAND'] @cache.servers.each do |server| if _hostname.nil? || server.hostname == _hostname server.ports.each do |port| puts '%s %s %s' % ["#{server.hostname}/#{port.port}".ljust(30), port.name.to_s.ljust(20), server.connect_command(port)] end end end end |
#cmd_server_list ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cow/application.rb', line 172 def cmd_server_list load_cache puts 'SERVER(TYPE)' @cache.servers.each do |server| type = nil Cow::SERVER_TYPES.each_key{|t| type = t if Cow::SERVER_TYPES[t] == server.class} puts "#{server.hostname}(#{type})" end end |
#cmd_update(_hostname) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/cow/application.rb', line 184 def cmd_update(_hostname) load_cache puts 'Updating local cache...' edit_cache do |cache| cache.servers.each do |server| if _hostname.nil? || server.hostname == _hostname puts "Updating server #{server.hostname}" server.update_ports puts '%s %s' % ['SERVER/PORT'.ljust(30), 'PORTNAME'.ljust(20)] server.ports.each do |port| puts '%s %s' % ["#{server.hostname}/#{port.port}".ljust(30), port.name.to_s.ljust(20)] end cache.add_server server end end end end |
#create_cache(_cache = CACHE_FILE) ⇒ Object
34 35 36 |
# File 'lib/cow/application.rb', line 34 def create_cache(_cache = CACHE_FILE) File.write(_cache, Cow::Application::Cache.new.to_yaml) end |
#edit_cache(_cache = CACHE_FILE) ⇒ Object
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 |
# File 'lib/cow/application.rb', line 58 def edit_cache(_cache = CACHE_FILE) unless @cache load_cache end cache = @cache File.open(_cache, 'w') do |file| puts 'Updating cache file.' if file.flock(File::LOCK_EX | File::LOCK_NB) begin yield cache rescue cache = @cache end if cache.class == Cow::Application::Cache file.write(cache.to_yaml) else file.write(@cache.to_yaml) end else raise 'File is already locked.' end end load_cache(_cache) end |
#load_cache(_cache = CACHE_FILE) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cow/application.rb', line 38 def load_cache(_cache = CACHE_FILE) unless File.exist?(_cache) puts 'Cache file is not found. Creating...' create_cache(_cache) end if File.size(_cache) < 5 puts 'Cache file looks empty. Creating...' create_cache(_cache) end cache = YAML.load(File.read(_cache)) if cache.class == Cow::Application::Cache @cache = cache else raise 'Cache file is corrupt?' end end |
#run ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/cow/application.rb', line 87 def run command = ARGV[0] option1 = ARGV[1] option2 = ARGV[2] option3 = ARGV[3] case command when 'add', 'a' cmd_add(option1, option2, option3) when 'connect', 'c' cmd_connect(option1) when 'find', 'f' cmd_find(option1) when 'list', 'l' cmd_list(option1) when 'server-list', 's' cmd_server_list when 'update', 'u' cmd_update(option1) else show_help end end |
#show_help ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/cow/application.rb', line 206 def show_help puts <<EOF cow - Console Server Wrapper Usage: cow [command] [options] Commands: add [hostname] [type] [snmp_community] Add new console server. available types: #{Cow::SERVER_TYPES.keys.join(' ')} connect [portname] Connect to console port. find [keyword] Find console port which includes given name. list [hostname(optional)] Show console server port list. If hostname is not specified, all console ports will be shown. server-list Show all console servers. update [hostname(optional)] Update console server port list. If hostname is not specified, all port lists will be updated. EOF end |