Class: Cow::Application

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeApplication

Returns a new instance of Application.



47
48
49
# File 'lib/cow/application.rb', line 47

def initialize
  @name = 'cow'
end

Instance Method Details

#cmd_add(_hostname, _type, _snmp_community) ⇒ Object

Raises:

  • (ArgumentError)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/cow/application.rb', line 140

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

Raises:

  • (ArgumentError)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cow/application.rb', line 176

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_delete(_hostname) ⇒ Object

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cow/application.rb', line 159

def cmd_delete(_hostname)
  raise ArgumentError unless _hostname.class == String


  load_cache

  unless @cache.find_server(_hostname)
    puts "#{_hostname} is not found!!"
  end

  puts "#{_hostname} will be deleted"

  edit_cache do |cache|
    cache.delete_server _hostname
  end
end

#cmd_find(_keyword) ⇒ Object

Raises:

  • (ArgumentError)


192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/cow/application.rb', line 192

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



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cow/application.rb', line 205

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_listObject



218
219
220
221
222
223
224
225
226
227
228
# File 'lib/cow/application.rb', line 218

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



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/cow/application.rb', line 230

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



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cow/application.rb', line 51

def create_cache(_cache = CACHE_FILE)
  dir_name = File.dirname(_cache)
  if !File.exist?(dir_name)
    puts 'Cache directory is not found. Creating...'
    Dir.mkdir(dir_name)
  else
    if !File.directory?(dir_name)
      raise 'Cache directory path is already found but it is not a directory!!!'
    end
  end

  File.write(_cache, Cow::Application::Cache.new.to_yaml)
end

#edit_cache(_cache = CACHE_FILE) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cow/application.rb', line 85

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cow/application.rb', line 65

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

#runObject



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
# File 'lib/cow/application.rb', line 114

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 'delete', 'd'
    cmd_delete(option1)
  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_helpObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/cow/application.rb', line 252

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(' ')}

  delete [hostname]
Delete existing console server.

  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