Class: Adminix::Config
- Inherits:
-
Object
- Object
- Adminix::Config
- Defined in:
- lib/adminix/config.rb
Constant Summary collapse
- 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
Instance Attribute Summary collapse
-
#api_host ⇒ Object
readonly
Returns the value of attribute api_host.
-
#config_root_path ⇒ Object
readonly
Returns the value of attribute config_root_path.
-
#data_storage_limit ⇒ Object
readonly
Returns the value of attribute data_storage_limit.
-
#image ⇒ Object
readonly
Returns the value of attribute image.
-
#log_files ⇒ Object
readonly
Returns the value of attribute log_files.
-
#max_logs_storage ⇒ Object
readonly
Returns the value of attribute max_logs_storage.
-
#scripts ⇒ Object
readonly
Returns the value of attribute scripts.
-
#secret_key ⇒ Object
readonly
Returns the value of attribute secret_key.
-
#server_port ⇒ Object
readonly
Returns the value of attribute server_port.
-
#service_id ⇒ Object
readonly
Returns the value of attribute service_id.
-
#systemctl ⇒ Object
readonly
Returns the value of attribute systemctl.
-
#watch_system_load ⇒ Object
readonly
Returns the value of attribute watch_system_load.
-
#watcher_period ⇒ Object
readonly
Returns the value of attribute watcher_period.
Instance Method Summary collapse
- #config_file(file) ⇒ Object
- #create_config_root_if_not_exists ⇒ Object
- #create_default_scripts ⇒ Object
- #credentials_defined? ⇒ Boolean
- #export_credentials ⇒ Object
-
#initialize(props = {}) ⇒ Config
constructor
A new instance of Config.
- #input(key) ⇒ Object
- #read_config_file ⇒ Object
- #read_credentials_file ⇒ Object
- #read_remote_config ⇒ Object
- #setup_logger ⇒ Object
- #validate_config_file(data) ⇒ Object
- #watch_logs? ⇒ Boolean
- #watch_system_load? ⇒ Boolean
- #web_debugger_enabled? ⇒ Boolean
Constructor Details
#initialize(props = {}) ⇒ Config
Returns a new instance of Config.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
Instance Attribute Details
#api_host ⇒ Object (readonly)
Returns the value of attribute api_host.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def api_host @api_host end |
#config_root_path ⇒ Object (readonly)
Returns the value of attribute config_root_path.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def config_root_path @config_root_path end |
#data_storage_limit ⇒ Object (readonly)
Returns the value of attribute data_storage_limit.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def data_storage_limit @data_storage_limit end |
#image ⇒ Object (readonly)
Returns the value of attribute image.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def image @image end |
#log_files ⇒ Object (readonly)
Returns the value of attribute log_files.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def log_files @log_files end |
#max_logs_storage ⇒ Object (readonly)
Returns the value of attribute max_logs_storage.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def max_logs_storage @max_logs_storage end |
#scripts ⇒ Object (readonly)
Returns the value of attribute scripts.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def scripts @scripts end |
#secret_key ⇒ Object (readonly)
Returns the value of attribute secret_key.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def secret_key @secret_key end |
#server_port ⇒ Object (readonly)
Returns the value of attribute server_port.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def server_port @server_port end |
#service_id ⇒ Object (readonly)
Returns the value of attribute service_id.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def service_id @service_id end |
#systemctl ⇒ Object (readonly)
Returns the value of attribute systemctl.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def systemctl @systemctl end |
#watch_system_load ⇒ Object (readonly)
Returns the value of attribute watch_system_load.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def watch_system_load @watch_system_load end |
#watcher_period ⇒ Object (readonly)
Returns the value of attribute watcher_period.
6 7 8 |
# File 'lib/adminix/config.rb', line 6 def watcher_period @watcher_period end |
Instance Method Details
#config_file(file) ⇒ Object
208 209 210 |
# File 'lib/adminix/config.rb', line 208 def config_file(file) "#{config_root_path}/#{file}" end |
#create_config_root_if_not_exists ⇒ Object
203 204 205 206 |
# 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 ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# 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? ⇒ Boolean
187 188 189 |
# File 'lib/adminix/config.rb', line 187 def credentials_defined? !@service_id.nil? && !@secret_key.nil? end |
#export_credentials ⇒ Object
149 150 151 152 153 154 155 156 |
# 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.("File #{file} created") end |
#input(key) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/adminix/config.rb', line 158 def input(key) case key when :service_id Helpers::Output.('Please enter your service ID') @service_id = STDIN.gets.chomp when :secret_key Helpers::Output.('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 ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# 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 ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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 ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# 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? ⇒ Boolean
191 192 193 |
# File 'lib/adminix/config.rb', line 191 def watch_logs? @watch_logs == true end |
#watch_system_load? ⇒ Boolean
195 196 197 |
# File 'lib/adminix/config.rb', line 195 def watch_system_load? @watch_system_load == true end |
#web_debugger_enabled? ⇒ Boolean
199 200 201 |
# File 'lib/adminix/config.rb', line 199 def web_debugger_enabled? @web_debugger_enabled == true end |