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://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

Instance Method Summary collapse

Constructor Details

#initializeConfig

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

#commandsObject

Returns the value of attribute commands.



15
16
17
# File 'lib/adminix/config.rb', line 15

def commands
  @commands
end

#daemonObject

Returns the value of attribute daemon.



15
16
17
# File 'lib/adminix/config.rb', line 15

def daemon
  @daemon
end

#error_notifier_enabledObject (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

#hostObject

Returns the value of attribute host.



15
16
17
# File 'lib/adminix/config.rb', line 15

def host
  @host
end

#image_snameObject (readonly)

Returns the value of attribute image_sname.



16
17
18
# File 'lib/adminix/config.rb', line 16

def image_sname
  @image_sname
end

#ivObject

Returns the value of attribute iv.



15
16
17
# File 'lib/adminix/config.rb', line 15

def iv
  @iv
end

#logs_enabledObject

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_periodObject

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

#modeObject

Returns the value of attribute mode.



15
16
17
# File 'lib/adminix/config.rb', line 15

def mode
  @mode
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/adminix/config.rb', line 15

def password
  @password
end

#scriptsObject

Returns the value of attribute scripts.



15
16
17
# File 'lib/adminix/config.rb', line 15

def scripts
  @scripts
end

#secret_keyObject

Returns the value of attribute secret_key.



15
16
17
# File 'lib/adminix/config.rb', line 15

def secret_key
  @secret_key
end

#service_idObject

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_portObject

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_filesObject

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_periodObject

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_pathObject

Returns the value of attribute websocket_path.



15
16
17
# File 'lib/adminix/config.rb', line 15

def websocket_path
  @websocket_path
end

#working_dirObject

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

Returns:

  • (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

Returns:

  • (Boolean)


113
114
115
# File 'lib/adminix/config.rb', line 113

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

#export_credentialsObject



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_configObject



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_configObject



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