Class: Dory::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/dory/config.rb

Class Method Summary collapse

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/dory/config.rb', line 99

def self.debug?
  self.settings[:dory][:debug]
end

.default_filenameObject



20
21
22
# File 'lib/dory/config.rb', line 20

def self.default_filename
  "#{Dir.home}/.dory.yml"
end

.default_settingsObject



67
68
69
# File 'lib/dory/config.rb', line 67

def self.default_settings
  YAML.load(self.default_yaml).with_indifferent_access
end

.default_yamlObject



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
57
58
59
60
61
62
63
64
65
# File 'lib/dory/config.rb', line 28

def self.default_yaml
  %q(---
    dory:
      # Be careful if you change the settings of some of
      # these services.  They may not talk to each other
      # if you change IP Addresses.
      # For example, resolv expects a nameserver listening at
      # the specified address.  dnsmasq normally does this,
      # but if you disable dnsmasq, it
      # will make your system look for a name server that
      # doesn't exist.
      dnsmasq:
        enabled: true
        domains:               # array of domains that will be resolved to the specified address
          - domain: docker     # you can set '#' for a wilcard
            address: 127.0.0.1 # return for queries against the domain
        container_name: dory_dnsmasq
        port: 53  # port to listen for dns requests on.  must be 53 on linux. can be anything that's open on macos
        # kill_others: kill processes bound to the port we need (see previous setting 'port')
        #   Possible values:
        #     ask (prompt about killing each time. User can accept/reject)
        #     yes|true (go aheand and kill without asking)
        #     no|false (don't kill, and don't even ask)
        kill_others: ask
        service_start_delay: 5  # seconds to wait after restarting systemd services
      nginx_proxy:
        enabled: true
        container_name: dory_dinghy_http_proxy
        https_enabled: true
        ssl_certs_dir: ''  # leave as empty string to use default certs
        port: 80           # port 80 is default for http
        tls_port: 443      # port 443 is default for https
      resolv:
        enabled: true
        nameserver: 127.0.0.1
        port: 53  # port where the nameserver listens. On linux it must be 53
  ).split("\n").map{|s| s.sub(' ' * 8, '')}.join("\n")
end

.filenameObject



24
25
26
# File 'lib/dory/config.rb', line 24

def self.filename
  self.find_config_file(Dir.pwd)
end

.find_config_file(starting_dir) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/dory/config.rb', line 10

def self.find_config_file(starting_dir)
  if self.has_config_file?(starting_dir)
    "#{starting_dir}/.dory.yml"
  elsif starting_dir == "/"
    return self.default_filename
  else
    return self.find_config_file(File.dirname(starting_dir)) # recurse up to /
  end
end

.has_config_file?(dir) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/dory/config.rb', line 6

def self.has_config_file?(dir)
  File.exist?("#{dir}/.dory.yml")
end

.settings(filename = self.filename) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dory/config.rb', line 71

def self.settings(filename = self.filename)
  if File.exist?(filename)
    defaults = self.default_settings.dup
    config_file_settings = YAML.load_file(filename).with_indifferent_access
    [:dnsmasq, :nginx_proxy, :resolv].each do |service|
      defaults[:dory][service].merge!(config_file_settings[:dory][service] || {})
    end
    defaults[:dory][:debug] = config_file_settings[:dory][:debug]
    defaults
  else
    self.default_settings
  end
end

.upgrade(old_hash) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dory/config.rb', line 103

def self.upgrade(old_hash)
  newsettings = old_hash.dup

  # If there's a single domain and address, upgrade to the array format
  if newsettings[:dory][:dnsmasq][:domain]
    newsettings[:dory][:dnsmasq][:domains] = [{
      domain: newsettings[:dory][:dnsmasq][:domain],
      address: newsettings[:dory][:dnsmasq][:address] || '127.0.0.1'
    }]
    newsettings[:dory][:dnsmasq].delete(:domain)
    newsettings[:dory][:dnsmasq].delete(:address)
  end

  # Add the option to skip prompts
  unless newsettings[:dory][:dnsmasq][:kill_others]
    newsettings[:dory][:dnsmasq][:kill_others] = 'ask'
  end

  unless newsettings[:dory][:dnsmasq][:service_start_delay]
    newsettings[:dory][:dnsmasq][:service_start_delay] = 5
  end

  # add settings for nginx proxy port
  unless newsettings[:dory][:nginx_proxy][:port]
    newsettings[:dory][:nginx_proxy][:port] = 80
  end
  unless newsettings[:dory][:nginx_proxy][:tls_port]
    newsettings[:dory][:nginx_proxy][:tls_port] = 443
  end

  newsettings
end

.upgrade_settings_file(filename = self.filename) ⇒ Object



95
96
97
# File 'lib/dory/config.rb', line 95

def self.upgrade_settings_file(filename = self.filename)
  self.write_settings(self.upgrade(self.settings), filename, is_yaml: false)
end

.write_default_settings_file(filename = self.filename) ⇒ Object



91
92
93
# File 'lib/dory/config.rb', line 91

def self.write_default_settings_file(filename = self.filename)
  self.write_settings(self.default_yaml, filename, is_yaml: true)
end

.write_settings(settings, filename = self.filename, is_yaml: false) ⇒ Object



85
86
87
88
89
# File 'lib/dory/config.rb', line 85

def self.write_settings(settings, filename = self.filename, is_yaml: false)
  settings = settings.to_yaml unless is_yaml
  settings.gsub!(/\s*!ruby\/hash:ActiveSupport::HashWithIndifferentAccess/, '')
  File.write(filename, settings)
end