Class: RunitMan::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/runit-man/app.rb

Constant Summary collapse

MIN_TAIL =
100
MAX_TAIL =
10000
GEM_FOLDER =
File.expand_path(File.join('..', '..'), File.dirname(__FILE__)).freeze
CONTENT_TYPES =
{
  :html => 'text/html',
  :txt  => 'text/plain',
  :css  => 'text/css',
  :js   => 'application/x-javascript',
  :json => 'application/json'
}.freeze
DEFAULT_LOGGER =
'svlogd'.freeze
DEFAULT_ALL_SERVICES_DIR =
'/etc/sv'.freeze
DEFAULT_ACTIVE_SERVICES_DIR =
'/etc/service'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_user(name, password) ⇒ Object



349
350
351
# File 'lib/runit-man/app.rb', line 349

def add_user(name, password)
  allowed_users[name] = password
end

.allowed_usersObject



357
358
359
# File 'lib/runit-man/app.rb', line 357

def allowed_users
  @allowed_users ||= {}
end

.enable_view_of(file_location) ⇒ Object



345
346
347
# File 'lib/runit-man/app.rb', line 345

def enable_view_of(file_location)
  files_to_view << File.expand_path(file_location, '/')
end

.exec_rackup(command) ⇒ Object



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/runit-man/app.rb', line 311

def exec_rackup(command)
  ENV['RUNIT_ALL_SERVICES_DIR']    = RunitMan::App.all_services_directory
  ENV['RUNIT_ACTIVE_SERVICES_DIR'] = RunitMan::App.active_services_directory
  ENV['RUNIT_LOGGER']              = RunitMan::App.runit_logger
  ENV['RUNIT_MAN_VIEW_FILES']      = RunitMan::App.files_to_view.join(',')
  ENV['RUNIT_MAN_CREDENTIALS']     = RunitMan::App.allowed_users.keys.map { |user| "#{user}:#{RunitMan::App.allowed_users[user]}" }.join(',')
  ENV['RUNIT_MAN_READWRITE_MODE']  = RunitMan::App.read_write_mode.to_s

  Dir.chdir(File.dirname(__FILE__))
  exec(command)
end

.files_to_viewObject



353
354
355
# File 'lib/runit-man/app.rb', line 353

def files_to_view
  @files_to_view ||= []
end

.i18n_locationObject



50
51
52
# File 'lib/runit-man/app.rb', line 50

def self.i18n_location
  File.join(GEM_FOLDER, 'i18n')
end

.prepare_to_runObject



361
362
363
364
365
366
367
# File 'lib/runit-man/app.rb', line 361

def prepare_to_run
  unless allowed_users.empty?
    use Rack::Auth::Basic, 'runit-man' do |username, password|
      allowed_users.include?(username) && allowed_users[username] == password
    end
  end
end

.register_as_runit_serviceObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/runit-man/app.rb', line 323

def register_as_runit_service
  all_r_dir    = File.join(RunitMan::App.all_services_directory, 'runit-man')
  active_r_dir = File.join(RunitMan::App.active_services_directory, 'runit-man')
  my_dir       = File.join(GEM_FOLDER, 'sv')
  log_dir      = File.join(all_r_dir, 'log')

  if File.symlink?(all_r_dir)
    File.unlink(all_r_dir)
  end

  unless File.directory?(all_r_dir)
    FileUtils.mkdir_p(log_dir)
    create_log_run_script(all_r_dir)
  end

  create_run_script(all_r_dir)

  unless File.symlink?(active_r_dir)
    File.symlink(all_r_dir, active_r_dir)
  end
end

.setup_i18n_filesObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/runit-man/app.rb', line 54

def self.setup_i18n_files
  files = []

  Dir.glob("#{i18n_location}/*.yml") do |full_path|
    next  unless File.file?(full_path)

    files << full_path
  end

  I18n.load_path = files
  I18n.reload!
  nil
end

Instance Method Details

#data_of_file_view(request) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/runit-man/app.rb', line 198

def data_of_file_view(request)
  if !request.GET.has_key?('file')
    return nil
  end

  file_path = request.GET['file']
  return nil  unless all_files_to_view.include?(file_path)

  text = IO.read(file_path)
  {
     :name => file_path,
     :text => text
  }
end

#log_action(name, text) ⇒ Object



273
274
275
276
# File 'lib/runit-man/app.rb', line 273

def log_action(name, text)
  env  = request.env
  log "#{addr} - - [#{Time.now}] \"Do #{text} on #{name}\""
end

#log_denied_action(name, text) ⇒ Object



278
279
280
281
# File 'lib/runit-man/app.rb', line 278

def log_denied_action(name, text)
  env  = request.env
  log "#{addr} - - [#{Time.now}] \"Receive #{text} for #{name}. Denied.\""
end

#log_of_service(name, count, no) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/runit-man/app.rb', line 173

def log_of_service(name, count, no)
  count = count.to_i
  count = MIN_TAIL  if count < MIN_TAIL
  count = MAX_TAIL  if count > MAX_TAIL
  srv   = ServiceInfo.klass[name]
  return nil  if srv.nil? || !srv.logged?

  logs = []
  if no.nil?
    srv.log_file_locations.each_with_index do |filepath, no|
      logs << log_of_service_n(filepath, count, no)
    end
  else
    filepath = srv.log_file_locations[no]
    return nil  if filepath.nil?
    logs << log_of_service_n(filepath, count, no)
  end

  {
    :name  => name,
    :count => count,
    :logs  =>  logs
  }
end

#log_of_service_n(filepath, count, no) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/runit-man/app.rb', line 156

def log_of_service_n(filepath, count, no)
  text = ''
  if File.readable?(filepath)
    File::Tail::Logfile.open(filepath, :backward => count, :return_if_eof => true) do |log|
      log.tail do |line|
        text += line
      end
    end
  end

  {
    :location => filepath,
    :text     => text,
    :id       => no
  }
end

#parse_language(header) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/runit-man/app.rb', line 109

def parse_language(header)
  weighted_locales = []

  if header
    header.split(',').each do |s|
      if s =~ /^(.+)\;q\=(\d(?:\.\d)?)$/
        weighted_locales << { :locale => $1.to_sym, :weight => $2.to_f }
      else
        weighted_locales << { :locale => s.to_sym, :weight => 1.0 }
      end
    end
  end

  weighted_locales << { :locale => :en, :weight => 0.0 }

  if weighted_locales.length >= 2
    weighted_locales.sort! do |a, b|
      b[:weight] <=> a[:weight]
    end
  end

  locales = weighted_locales.map { |wl| wl[:locale] }
  setup_i18n(locales)
end

#setup_i18n(locales) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/runit-man/app.rb', line 100

def setup_i18n(locales)
  locales.each do |locale|
    if I18n.available_locales.include?(locale)
      I18n.locale = locale
      break
    end
  end
end