Module: Satorix::CI::Deploy::Flynn::EnvironmentVariables

Included in:
Satorix::CI::Deploy::Flynn
Defined in:
lib/satorix/CI/deploy/flynn/environment_variables.rb

Instance Method Summary collapse

Instance Method Details

#adjust_env_varsObject



9
10
11
12
13
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 9

def adjust_env_vars
  ensure_required_env_vars_are_defined unless ENV["SATORIX_#{ current_branch }_ENFORCE_ENV_VAR_DEFINITION"] == 'false'
  env_unset
  env_set
end

#aeev_key_exists?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 16

def aeev_key_exists?
  !(run_command(%w[flynn env], quiet: true) =~ /#{ exported_aeevs_key }=/mi).nil?
end

#aeev_prefixObject



21
22
23
24
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 21

def aeev_prefix
  # AEEV - Application Exportable Environment Variable
  "AEEV_#{ current_branch }_"
end

#ci_provided_env_varsObject



27
28
29
30
31
32
33
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 27

def ci_provided_env_vars
  {}.tap do |vars|
    ENV.each do |key, value|
      vars[key.sub(aeev_prefix, '')] = value.sub('***EMPTY_STRING***', '') if key.start_with?(aeev_prefix)
    end
  end
end

#current_flynn_keysObject



36
37
38
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 36

def current_flynn_keys
  aeev_key_exists? ? run_command(['flynn', 'env', 'get', exported_aeevs_key], quiet: true).split : []
end

#desired_env_varsObject



41
42
43
44
45
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 41

def desired_env_vars
  ci_provided_env_vars.tap do |vars|
    vars[exported_aeevs_key] = (vars.keys << exported_aeevs_key).sort.join("\n")
  end
end

#ensure_required_env_vars_are_defined(configuration_file: 'config/secrets.yml') ⇒ Object



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
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 48

def ensure_required_env_vars_are_defined(configuration_file: 'config/secrets.yml')
  # TODO : handle new Rails secrets methods, like encrypted secrets
  if File.file?(configuration_file)
    secrets = YAML.load_file(configuration_file)

    all_secrets = {}
    all_secrets.merge!(secrets['shared']) if secrets['shared']
    all_secrets.merge!(secrets['production']) if secrets['production']

    required = all_secrets.to_yaml.scan(/ENV\[['"](?<var>[A-Z0-9_.]+)['"]\]/).flatten.uniq
    set = ci_provided_env_vars.keys

    missing = required - set

    if missing.empty?
      log "All required environment variables from #{ configuration_file } have been defined."
    else
      log_error "Environment variables specified in #{ configuration_file } were not defined for the #{ current_branch.downcase } branch."
      log_error "\nPlease define the following variables in your dashboard:\n#{ missing.join("\n") }\n"
      log_error_and_abort 'Missing required environment variables.'
    end
  else
    log "No #{ configuration_file } exists, skipping environment variable enforcement."
  end
end

#env_setObject



75
76
77
78
79
80
81
82
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 75

def env_set
  if env_vars_to_set.empty?
    log 'No new environment variables to set.'
  else
    keys_and_values = env_vars_to_set.map { |k, v| "#{ k }=#{ v }" }
    run_command(['flynn', 'env', 'set', keys_and_values].flatten, filtered_text: env_vars_to_set.values)
  end
end

#env_unsetObject



85
86
87
88
89
90
91
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 85

def env_unset
  if env_vars_to_unset.empty?
    log 'No existing environment variables to unset.'
  else
    run_command(['flynn', 'env', 'unset', env_vars_to_unset])
  end
end

#env_varsObject



94
95
96
97
98
99
100
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 94

def env_vars
  @_env_vars ||= begin
                   {}.tap do |vars|
                     current_flynn_keys.each { |key| vars[key] = run_command(['flynn', 'env', 'get', key], quiet: true).chomp }
                   end
                 end
end

#env_vars_to_setObject



103
104
105
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 103

def env_vars_to_set
  @_env_vars_to_set ||= desired_env_vars.reject { |k, v| env_vars.key?(k) && env_vars[k] == v }
end

#env_vars_to_unsetObject



108
109
110
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 108

def env_vars_to_unset
  @_env_vars_to_unset ||= env_vars.keys - desired_env_vars.keys
end

#exported_aeevs_keyObject



113
114
115
# File 'lib/satorix/CI/deploy/flynn/environment_variables.rb', line 113

def exported_aeevs_key
  'AEEV_KEYS'
end