require 'logger' require 'erb' module Adminix class Config attr_reader :service_id, :secret_key, :image, :watcher_period, :api_host, :server_port, :config_root_path, :log_files, :max_logs_storage, :data_storage_limit, :systemctl, :scripts, :watch_system_load MODE_CLASSIC = 'classic'.freeze DEFAULT_API_HOST = 'https://api.adminix.io'.freeze DEFAULT_SERVER_PORT = 8080 WATCHER_SYSTEMCTL_PATH = '/etc/systemd/system/adminix.service'.freeze 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 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 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 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 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 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 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 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 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 def credentials_defined? !@service_id.nil? && !@secret_key.nil? end def watch_logs? @watch_logs == true end def watch_system_load? @watch_system_load == true end def web_debugger_enabled? @web_debugger_enabled == true end def create_config_root_if_not_exists return if Dir.exists?(config_root_path) Helpers::Files.mkdir_p(config_root_path) end def config_file(file) "#{config_root_path}/#{file}" end end end