Class: Adminix::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/adminix/config.rb

Constant Summary collapse

DEFAULT_HOST =
'https://api.adminix.io'.freeze
DEFAULT_WEBSOCKET_HOST =
'wss://api.adminix.io/websocket'.freeze
DEFAULT_SETUP_SERVER_PORT =
'8080'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/adminix/config.rb', line 14

def initialize
  @host = ENV['ADMINIX_HOST'] || DEFAULT_HOST
  @setup_server_port = ENV['ADMINIX_SETUP_SERVER_PORT'] || DEFAULT_SETUP_SERVER_PORT
  @commands = []
  @watcher_sync_period = 10
  @logs_sync_period = 3
  @websocket_path = ENV['ADMINIX_WEBSOCKET_HOST'] || DEFAULT_WEBSOCKET_HOST
  @mode = 'classic'
  @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'
  }
  @watch_log_files = []

  @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

#commandsObject

Returns the value of attribute commands.



12
13
14
# File 'lib/adminix/config.rb', line 12

def commands
  @commands
end

#daemonObject

Returns the value of attribute daemon.



12
13
14
# File 'lib/adminix/config.rb', line 12

def daemon
  @daemon
end

#hostObject

Returns the value of attribute host.



12
13
14
# File 'lib/adminix/config.rb', line 12

def host
  @host
end

#logs_sync_periodObject

Returns the value of attribute logs_sync_period.



12
13
14
# File 'lib/adminix/config.rb', line 12

def logs_sync_period
  @logs_sync_period
end

#modeObject

Returns the value of attribute mode.



12
13
14
# File 'lib/adminix/config.rb', line 12

def mode
  @mode
end

#scriptsObject

Returns the value of attribute scripts.



12
13
14
# File 'lib/adminix/config.rb', line 12

def scripts
  @scripts
end

#secret_keyObject

Returns the value of attribute secret_key.



12
13
14
# File 'lib/adminix/config.rb', line 12

def secret_key
  @secret_key
end

#service_idObject

Returns the value of attribute service_id.



12
13
14
# File 'lib/adminix/config.rb', line 12

def service_id
  @service_id
end

#setup_server_portObject

Returns the value of attribute setup_server_port.



12
13
14
# File 'lib/adminix/config.rb', line 12

def setup_server_port
  @setup_server_port
end

#watch_log_filesObject

Returns the value of attribute watch_log_files.



12
13
14
# File 'lib/adminix/config.rb', line 12

def watch_log_files
  @watch_log_files
end

#watcher_sync_periodObject

Returns the value of attribute watcher_sync_period.



12
13
14
# File 'lib/adminix/config.rb', line 12

def watcher_sync_period
  @watcher_sync_period
end

#websocket_pathObject

Returns the value of attribute websocket_path.



12
13
14
# File 'lib/adminix/config.rb', line 12

def websocket_path
  @websocket_path
end

Instance Method Details

#credentials_defined?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/adminix/config.rb', line 76

def credentials_defined?
  !service_id.nil? && !secret_key.nil?
end

#creds_file_exists?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/adminix/config.rb', line 72

def creds_file_exists?
  File.exists?(@creds_path)
end

#export_credentialsObject



64
65
66
67
68
69
70
# File 'lib/adminix/config.rb', line 64

def export_credentials
  create_config_root_if_not_exists

  open(@creds_path, 'w') do |f|
    f.puts(credentials.to_json)
  end
end

#read_local_configObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/adminix/config.rb', line 37

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'

    scripts = data['scripts'] || {}
    @scripts = {
      watcher_start: scripts['watcher_start'],
      watcher_stop: scripts['watcher_stop'],
      process_start: scripts['process_start'],
      process_stop: scripts['process_stop']
    }

    @watch_log_files = data['watch_logs'] || []
  end
end