Module: Kaboom::Config

Includes:
Utils, Thor::Actions
Included in:
Backup, CLI
Defined in:
lib/kaboom/helpers/config.rb

Constant Summary collapse

DESTINATIONS =
Dir.glob("./config/deploy.*.yml").map { |file| file.match(/deploy\.(\w+)\.yml/)[1].to_sym }.freeze

Instance Method Summary collapse

Methods included from Utils

#confirm_proceed, #confirm_production_action, #say_and_exit, #validate_destination

Instance Method Details

#database_config(destination) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/kaboom/helpers/config.rb', line 80

def database_config(destination)
  config = YAML.load_file('config/database.yml')[destination]
  {
    name: ERB.new(config['database']).result,
    username: ERB.new(config['username']).result.presence || 'rails',
    password: ERB.new(config['password']).result
  }
end

#load_credentials(destination) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/kaboom/helpers/config.rb', line 54

def load_credentials(destination)
  credentials_key_file = "config/credentials/#{destination}.key"
  credentials_file = "config/credentials/#{destination}.yml.enc"
  
  unless File.exist?(credentials_file) && File.exist?(credentials_key_file)
    say_and_exit "Credentials file #{credentials_file} not found, make sure it is present", :red
  end

  key = [File.read(credentials_key_file)].pack("H*")
  encrypted_credentials = File.binread(credentials_file)
  
  cipher = OpenSSL::Cipher.new('aes-128-gcm')
  encrypted_data, iv, auth_tag = encrypted_credentials.split("--".freeze).map { |v| Base64.strict_decode64(v) }
  
  cipher.decrypt
  cipher.key = key
  cipher.iv = iv
  cipher.auth_tag = auth_tag
  cipher.auth_data = ""
  
  decrypted_data = cipher.update(encrypted_data)
  decrypted_data << cipher.final
  
  return YAML.safe_load(Marshal.load(decrypted_data), symbolize_names: true)
end

#load_env(destination) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kaboom/helpers/config.rb', line 38

def load_env(destination)
  env_file = ".env.#{destination}"
  if File.exist?(env_file)
    ENV.update(Dotenv::Environment.new(env_file))
  else

    say_and_exit "Environment file #{env_file} not found, run 'kamal envify -d destination'", :red
  end

  destination_clear_envs = YAML.load_file("config/deploy.#{destination}.yml")&.dig('env', 'clear') || {}
  clear_envs = YAML.load_file("config/deploy.yml")&.dig('env', 'clear') || {}
  merged_clear_envs = clear_envs.merge(destination_clear_envs)

  ENV.update(merged_clear_envs.transform_values(&:to_s))
end

#validate_repository!Object



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

def validate_repository!
  unless Dir.exist?('.git')
    say_and_exit "Error: This command must be run in a Git repository", :red
  end

  unless File.exist?('Gemfile') && File.exist?('config/application.rb') && File.exist?('config/routes.rb')
    say_and_exit "Error: This command must be run in a root folder of a Rails application", :red
  end

  unless File.exist?('config/credentials/development.key')
    say_and_exit "Error: development.key credentials file not found (source it from 1Password)", :red
  end

  if DESTINATIONS.empty? && !File.exist?('./config/deploy.yml')
    say_and_exit "Error: No deploy configuration files found in config directory.", :red
  end

  if Bundler::Dsl.evaluate('Gemfile', nil, {})&.ruby_version&.versions&.first != RUBY_VERSION
    say "Warning: Ruby version specified in the project's Gemfile is not the same as the currently running Ruby version.", :yellow
  end
end