Class: Adminix::Config
- Inherits:
-
Object
- Object
- Adminix::Config
- Includes:
- Singleton
- Defined in:
- lib/adminix/config.rb
Constant Summary collapse
- 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
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#daemon ⇒ Object
Returns the value of attribute daemon.
-
#error_notifier_enabled ⇒ Object
readonly
Returns the value of attribute error_notifier_enabled.
-
#host ⇒ Object
Returns the value of attribute host.
-
#image_sname ⇒ Object
readonly
Returns the value of attribute image_sname.
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#logs_enabled ⇒ Object
Returns the value of attribute logs_enabled.
-
#logs_sync_period ⇒ Object
Returns the value of attribute logs_sync_period.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#password ⇒ Object
Returns the value of attribute password.
-
#scripts ⇒ Object
Returns the value of attribute scripts.
-
#secret_key ⇒ Object
Returns the value of attribute secret_key.
-
#service_id ⇒ Object
Returns the value of attribute service_id.
-
#setup_server_port ⇒ Object
Returns the value of attribute setup_server_port.
-
#watch_log_files ⇒ Object
Returns the value of attribute watch_log_files.
-
#watcher_sync_period ⇒ Object
Returns the value of attribute watcher_sync_period.
-
#websocket_path ⇒ Object
Returns the value of attribute websocket_path.
-
#working_dir ⇒ Object
Returns the value of attribute working_dir.
Instance Method Summary collapse
- #credentials_defined? ⇒ Boolean
- #creds_file_exists? ⇒ Boolean
- #export_credentials ⇒ Object
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #read_local_config ⇒ Object
- #sync_remote_config ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
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 |
# File 'lib/adminix/config.rb', line 18 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 |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def commands @commands end |
#daemon ⇒ Object
Returns the value of attribute daemon.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def daemon @daemon end |
#error_notifier_enabled ⇒ Object (readonly)
Returns the value of attribute error_notifier_enabled.
16 17 18 |
# File 'lib/adminix/config.rb', line 16 def error_notifier_enabled @error_notifier_enabled end |
#host ⇒ Object
Returns the value of attribute host.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def host @host end |
#image_sname ⇒ Object (readonly)
Returns the value of attribute image_sname.
16 17 18 |
# File 'lib/adminix/config.rb', line 16 def image_sname @image_sname end |
#iv ⇒ Object
Returns the value of attribute iv.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def iv @iv end |
#logs_enabled ⇒ Object
Returns the value of attribute logs_enabled.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def logs_enabled @logs_enabled end |
#logs_sync_period ⇒ Object
Returns the value of attribute logs_sync_period.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def logs_sync_period @logs_sync_period end |
#mode ⇒ Object
Returns the value of attribute mode.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def mode @mode end |
#password ⇒ Object
Returns the value of attribute password.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def password @password end |
#scripts ⇒ Object
Returns the value of attribute scripts.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def scripts @scripts end |
#secret_key ⇒ Object
Returns the value of attribute secret_key.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def secret_key @secret_key end |
#service_id ⇒ Object
Returns the value of attribute service_id.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def service_id @service_id end |
#setup_server_port ⇒ Object
Returns the value of attribute setup_server_port.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def setup_server_port @setup_server_port end |
#watch_log_files ⇒ Object
Returns the value of attribute watch_log_files.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def watch_log_files @watch_log_files end |
#watcher_sync_period ⇒ Object
Returns the value of attribute watcher_sync_period.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def watcher_sync_period @watcher_sync_period end |
#websocket_path ⇒ Object
Returns the value of attribute websocket_path.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def websocket_path @websocket_path end |
#working_dir ⇒ Object
Returns the value of attribute working_dir.
15 16 17 |
# File 'lib/adminix/config.rb', line 15 def working_dir @working_dir end |
Instance Method Details
#credentials_defined? ⇒ Boolean
117 118 119 |
# File 'lib/adminix/config.rb', line 117 def credentials_defined? !service_id.nil? && !secret_key.nil? end |
#creds_file_exists? ⇒ Boolean
113 114 115 |
# File 'lib/adminix/config.rb', line 113 def creds_file_exists? File.exists?(@creds_path) end |
#export_credentials ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/adminix/config.rb', line 105 def export_credentials create_config_root_if_not_exists open(@creds_path, 'w') do |f| f.puts(credentials.to_json) end end |
#read_local_config ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/adminix/config.rb', line 46 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 |
#sync_remote_config ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/adminix/config.rb', line 79 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 |