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.



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

Raises:

  • (ArgumentError)


121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cow/application.rb', line 121

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)


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

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

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cow/application.rb', line 156

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



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cow/application.rb', line 169

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



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/cow/application.rb', line 182

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



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/cow/application.rb', line 194

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
37
38
39
40
41
42
43
44
45
46
# File 'lib/cow/application.rb', line 34

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



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

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



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

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cow/application.rb', line 97

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_helpObject



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/cow/application.rb', line 216

def show_help
  puts "cow - Console Server Wrapper\n\nUsage:\n  cow [command] [options]\n\nCommands:\n  add [hostname] [type] [snmp_community]\nAdd new console server.\navailable types: \#{Cow::SERVER_TYPES.keys.join(' ')}\n\n  connect [portname]\nConnect to console port.\n\n  find [keyword]\nFind console port which includes given name.\n\n  list [hostname(optional)]\nShow console server port list. If hostname is not specified,\nall console ports will be shown.\n\n  server-list\nShow all console servers.\n\n  update [hostname(optional)]\nUpdate console server port list. If hostname is not specified,\nall port lists will be updated.\n"
end