Class: RunitMan

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

Constant Summary collapse

VERSION =
RunitManVersion::VERSION
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



296
297
298
# File 'lib/runit-man/app.rb', line 296

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

.allowed_usersObject



304
305
306
# File 'lib/runit-man/app.rb', line 304

def allowed_users
  @allowed_users ||= {}
end

.enable_view_of(file_location) ⇒ Object



292
293
294
# File 'lib/runit-man/app.rb', line 292

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

.exec_rackup(command) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/runit-man/app.rb', line 263

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

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

.files_to_viewObject



300
301
302
# File 'lib/runit-man/app.rb', line 300

def files_to_view
  @files_to_view ||= []
end

.i18n_locationObject



75
76
77
# File 'lib/runit-man/app.rb', line 75

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

.loggerObject



308
309
310
# File 'lib/runit-man/app.rb', line 308

def logger
  settings.logger_option
end

.prepare_to_runObject



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

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



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/runit-man/app.rb', line 274

def register_as_runit_service
  all_r_dir    = File.join(RunitMan.all_services_directory, 'runit-man')
  active_r_dir = File.join(RunitMan.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



79
80
81
82
83
84
85
86
87
88
# File 'lib/runit-man/app.rb', line 79

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



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/runit-man/app.rb', line 176

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



238
239
240
241
242
243
# File 'lib/runit-man/app.rb', line 238

def log_action(name, text)
  env  = request.env
  addr = env.include?('X_REAL_IP') ? env['X_REAL_IP'] : env['REMOTE_ADDR']
  $stdout.puts "#{addr} - - [#{Time.now}] \"Do #{text} on #{name}\""
  $stdout.flush
end

#log_of_service(name, count) ⇒ Object



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

def log_of_service(name, count)
  count = count.to_i
  count = MIN_TAIL if count < MIN_TAIL
  count = MAX_TAIL if count > MAX_TAIL
  srv   = ServiceInfo[name]
  return nil if srv.nil? || !srv.logged?
  text = ''
  File::Tail::Logfile.open(srv.log_file_location, :backward => count, :return_if_eof => true) do |log|
    log.tail do |line|
      text += line
    end
  end

  {
    :name         => name,
    :count        => count,
    :log_location => srv.log_file_location,
    :text         => text
  }
end

#parse_language(header) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/runit-man/app.rb', line 120

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



111
112
113
114
115
116
117
118
# File 'lib/runit-man/app.rb', line 111

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