class Adminix::Config

Constants

DEFAULT_API_HOST
DEFAULT_SERVER_PORT
MODE_CLASSIC
WATCHER_SYSTEMCTL_PATH

Attributes

api_host[R]
config_root_path[R]
data_storage_limit[R]
image[R]
log_files[R]
max_logs_storage[R]
scripts[R]
secret_key[R]
server_port[R]
service_id[R]
systemctl[R]
watch_system_load[R]
watcher_period[R]

Public Class Methods

new(props = {}) click to toggle source
# File lib/adminix/config.rb, line 15
def initialize(props = {})
  @api_host = ENV['ADMINIX_HOST'] || DEFAULT_API_HOST
  @service_id = props[:service_id] || ENV['ADMINIX_SERVICE_ID']
  @secret_key = props[:secret_key] || ENV['ADMINIX_SECRET_KEY']
  @server_port = props[:server_port] || ENV['ADMINIX_SERVER_PORT'] ||
                 DEFAULT_SERVER_PORT
  @config_root_path = props[:config_root_path] ||
                      "#{ENV['HOME']}/.config/adminix"
  @watcher_period = {
    sync_service: 5,
    send_logs: 10,
    send_system_load: 10,
    execute_jobs: 5,
    check_system_load: 5
  }
  @scripts = {
    restart_watcher: "#{@config_root_path}/scripts/restart_watcher",
    upgrade_watcher: "#{@config_root_path}/scripts/upgrade_watcher",
    update_process: "#{@config_root_path}/scripts/update_process",
    start_process: "#{@config_root_path}/scripts/start_process",
    stop_process: "#{@config_root_path}/scripts/stop_process",
    run_script: "#{@config_root_path}/scripts/run_script"
  }
  @log_files = []
  @data_storage_limit = {
    logs: 1000 || ENV['ADMINIX_MAX_LOGS_IN_MEMORY'],
    load_stamps: 100 || ENV['ADMINIX_MAX_LOAD_STAMPS_IN_MEMORY']
  }
  @watch_logs = true
  @watch_system_load = true
  @systemctl = {
    watcher_service_path: ENV['ADMINIX_WATCHER_SYSTEMCTL_PATH'] || WATCHER_SYSTEMCTL_PATH
  }
  @logger_mode = props[:logger_mode] || ENV['ADMINIX_LOGGER'] || 'silent'
  @logger_path = ENV['ADMINIX_LOGGER_PATH']

  setup_logger
  create_config_root_if_not_exists
  read_credentials_file unless credentials_defined?
  read_config_file
  create_default_scripts
end

Public Instance Methods

config_file(file) click to toggle source
# File lib/adminix/config.rb, line 208
def config_file(file)
  "#{config_root_path}/#{file}"
end
create_config_root_if_not_exists() click to toggle source
# File lib/adminix/config.rb, line 203
def create_config_root_if_not_exists
  return if Dir.exists?(config_root_path)
  Helpers::Files.mkdir_p(config_root_path)
end
create_default_scripts() click to toggle source
# File lib/adminix/config.rb, line 171
def create_default_scripts
  unless Dir.exist?("#{config_root_path}/scripts")
    Helpers::Files.mkdir_p("#{config_root_path}/scripts")
  end

  Dir["#{Adminix.root}/app/views/scripts/*"].each do |tpl_path|
    script_name = tpl_path.split('/').last.split('.sh.erb').first
    script_path = "#{config_root_path}/scripts/#{script_name}"
    next if File.exist?(script_path)
    tpl = File.open(tpl_path, 'rb', &:read)
    content = ERB.new(tpl).result(binding)
    Helpers::Files.write_plain_file(script_path, content)
    File.chmod(0o755, script_path)
  end
end
credentials_defined?() click to toggle source
# File lib/adminix/config.rb, line 187
def credentials_defined?
  !@service_id.nil? && !@secret_key.nil?
end
export_credentials() click to toggle source
# File lib/adminix/config.rb, line 149
def export_credentials
  file = config_file('credentials')
  content = { service_id: @service_id, secret_key: @secret_key }

  Helpers::Files.mkdir_p(config_root_path)
  Helpers::Files.write_json_file(file, content)
  Helpers::Output.display_message("File #{file} created")
end
input(key) click to toggle source
# File lib/adminix/config.rb, line 158
def input(key)
  case key
  when :service_id
    Helpers::Output.display_message('Please enter your service ID')
    @service_id = STDIN.gets.chomp
  when :secret_key
    Helpers::Output.display_message('Please enter your secret key')
    @secret_key = STDIN.gets.chomp
  else
    Helpers::Output.display_error_and_exit("#{key} is wrong config key")
  end
end
read_config_file() click to toggle source
# File lib/adminix/config.rb, line 102
def read_config_file
  config_path = config_file('config')
  unless File.exist?(config_path)
    Helpers::Files.write_json_file(config_path, {})
    return
  end

  data = Helpers::Files.read_json_file(config_path)
  validate_config_file(data)

  @mode = data['mode'] || MODE_CLASSIC
  @image = data['image']
  @web_debugger_enabled = data['enable_web_debugger'] || false
  @working_dir = data['working_dir']
  @log_files = data['watch_logs'] || []
  @watch_logs = @log_files.any?
  @watch_system_load = data['system_load_enabled'] || false
end
read_credentials_file() click to toggle source
# File lib/adminix/config.rb, line 86
def read_credentials_file
  credentials_path = config_file('credentials')
  return unless File.exist?(credentials_path)

  data = Helpers::Files.read_json_file(credentials_path)

  if data['service_id'].nil? || data['secret_key'].nil?
    Helpers::Output.display_error_and_exit(
      'Credentials file is incorrect!'
    )
  end

  @service_id ||= data['service_id']
  @secret_key ||= data['secret_key']
end
read_remote_config() click to toggle source
# File lib/adminix/config.rb, line 121
def read_remote_config
  return unless credentials_defined?
  Adminix.logger.info('Reading remote config...')

  success, result = Helpers::NetHTTP.get("services/#{@service_id}/watcher_config")
  return unless success

  @watcher_period[:sync_service] = result['sync_period']
  @watcher_period[:send_logs] = result['logs_sync_period']
  @watcher_period[:send_system_load] = result['system_load_sync_period'] || 10
  @watch_logs = result['logs_enabled']
  @watch_system_load = result['system_load_enabled'] || false
end
setup_logger() click to toggle source
# File lib/adminix/config.rb, line 58
def setup_logger
  case @logger_mode
  when 'stdout'
    Adminix.define_logger(STDOUT, Logger::INFO)
  when 'debug'
    Adminix.define_logger(STDOUT)
  when 'silent'
    Adminix.define_logger(STDOUT, Logger::FATAL)
  when 'file'
    Helpers::Files.touch(@logger_path) unless @logger_path.nil?
    if File.exist?(@logger_path)
      Adminix.define_logger(@logger_path)
    else
      Adminix.define_logger(STDOUT)
      Adminix.logger.error(
        "Logger can't create file #{@logger_path}, using STDOUT by default"
      )
    end
  else
    incorrect_mode = @logger_mode
    @logger_mode = 'stdout'
    Adminix.define_logger(STDOUT)
    Adminix.logger.error(
      "#{incorrect_mode} is incorrect mode for Logger, using STDOUT by default"
    )
  end
end
validate_config_file(data) click to toggle source
# File lib/adminix/config.rb, line 135
def validate_config_file(data)
  errors = []
  if data['mode'] && !%w[classic docker].include?(data['mode'])
    errors << 'incorrect mode type, can be "classic" or "docker"'
  end

  if errors.count > 0
    errors.each { |e| puts e }
    exit
  else
    true
  end
end
watch_logs?() click to toggle source
# File lib/adminix/config.rb, line 191
def watch_logs?
  @watch_logs == true
end
watch_system_load?() click to toggle source
# File lib/adminix/config.rb, line 195
def watch_system_load?
  @watch_system_load == true
end
web_debugger_enabled?() click to toggle source
# File lib/adminix/config.rb, line 199
def web_debugger_enabled?
  @web_debugger_enabled == true
end