require 'singleton' require 'json' module Adminix class Config include Singleton DEFAULT_HOST = 'https://api.adminix.io'.freeze DEFAULT_WEBSOCKET_HOST = 'wss://websocket.adminix.io/cable'.freeze DEFAULT_SETUP_SERVER_PORT = (ENV['ADMINIX_SETUP_SERVER_PORT'] || '8080').freeze DEFAULT_WATCHER_SYNC_PERIOD = 10.freeze DEFAULT_LOGS_SYNC_PERIOD = 3.freeze DEFAULT_LOGS_ENABLED = true attr_accessor :service_id, :secret_key, :host, :commands, :daemon, :setup_server_port, :scripts, :watch_log_files, :watcher_sync_period, :logs_sync_period, :logs_enabled, :websocket_path, :mode, :working_dir, :iv, :password attr_reader :image_sname, :error_notifier_enabled def initialize @host = ENV['ADMINIX_HOST'] || DEFAULT_HOST @setup_server_port = ENV['ADMINIX_SETUP_SERVER_PORT'] || DEFAULT_SETUP_SERVER_PORT @commands = [] @watcher_sync_period = DEFAULT_WATCHER_SYNC_PERIOD @logs_sync_period = DEFAULT_LOGS_SYNC_PERIOD @logs_enabled = DEFAULT_LOGS_ENABLED @websocket_path = ENV['ADMINIX_WEBSOCKET_HOST'] || DEFAULT_WEBSOCKET_HOST @mode = 'classic' @working_dir = `pwd`.split("\n").first @iv = ENV['DECIPHER_IV'] || 'U7yfLthY77Yjtp9z' @scripts = { watcher_start: 'sudo systemctl start adminix.service', watcher_stop: 'sudo systemctl stop adminix.service', process_start: 'sudo systemctl start adminix_process.service', process_stop: 'sudo systemctl stop adminix_process.service', run_script: '~/.config/adminix/scripts/run_script' } @watch_log_files = [] @error_notifier_enabled = ENV['ADMINIX_ERROR_NOTIFIER'].nil? ? true : ENV['ADMINIX_ERROR_NOTIFIER'] == 'true' @config_store_path = "#{ENV['HOME']}/.config" @root_path = "#{@config_store_path}/adminix" @creds_path = "#{@root_path}/credentials" @config_path = "#{@root_path}/config" end def read_local_config if File.exists?(@creds_path) content = IO.read(@creds_path) data = JSON.parse(content) rescue {} @service_id ||= data['service_id'] @secret_key ||= data['secret_key'] end if File.exists?(@config_path) content = IO.read(@config_path) data = JSON.parse(content) rescue {} @mode = data['mode'] || 'classic' @working_dir = data['working_dir'] if data['working_dir'] scripts = data['scripts'] || {} @scripts = { watcher_start: scripts['watcher_start'], watcher_stop: scripts['watcher_stop'], process_start: scripts['process_start'], process_stop: scripts['process_stop'] } unless scripts['run_script'].nil? @scripts[:run_script] = scripts['run_script'] end @watch_log_files = data['watch_logs'] || [] @image_sname = data['image'] end end def sync_remote_config return if service_id.nil? || secret_key.nil? uri = URI.parse("#{@host}/v1/services/#{@service_id}/watcher_config") request = Net::HTTP::Get.new(uri) request["Authorization"] = "Bearer #{@secret_key}" opts = { use_ssl: uri.scheme == 'https' } response = Net::HTTP.start(uri.hostname, uri.port, opts) do |http| http.request(request) end data = JSON.parse(response.body) result = data['result'] || {} case response.code when '401' puts 'Incorrect credentials' exit end @watcher_sync_period = result['sync_period'] || DEFAULT_WATCHER_SYNC_PERIOD @logs_sync_period = result['logs_sync_period'] || DEFAULT_LOGS_SYNC_PERIOD @logs_enables = result.key?('logs_enabled') ? result['logs_enabled'] == true : DEFAULT_LOGS_ENABLED end def export_credentials create_config_root_if_not_exists open(@creds_path, 'w') do |f| f.puts(credentials.to_json) end end def creds_file_exists? File.exists?(@creds_path) end def credentials_defined? !service_id.nil? && !secret_key.nil? end private def credentials { service_id: @service_id, secret_key: @secret_key } end def create_config_root_if_not_exists unless Dir.exists?(@config_store_path) Dir.mkdir(@config_store_path) end unless Dir.exists?(@root_path) Dir.mkdir(@root_path) end end end end