Class: Envoku::Adapters::S3

Inherits:
Object
  • Object
show all
Defined in:
lib/envoku/adapters/s3.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(custom_options = {}) ⇒ S3

Returns a new instance of S3


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/envoku/adapters/s3.rb', line 15

def initialize(custom_options = {})
  default_options = {
    filename: nil,
    bucket_name: nil,
    access_key_id: nil,
    secret_access_key: nil,
  }
  @options = OpenStruct.new default_options.merge(custom_options)
  @local_file_name = "/tmp/envoku-#{SecureRandom.hex 16}.env"
  @data = {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options


13
14
15
# File 'lib/envoku/adapters/s3.rb', line 13

def options
  @options
end

Instance Method Details

#get(key) ⇒ Object


56
57
58
# File 'lib/envoku/adapters/s3.rb', line 56

def get(key)
  @data[key]
end

#get_allObject


52
53
54
# File 'lib/envoku/adapters/s3.rb', line 52

def get_all
  @data
end

#loadObject


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/envoku/adapters/s3.rb', line 27

def load
  Envoku.logger.debug("Loading via S3 Adapter")
  apply_environment_options
  return unless options.bucket_name && options.filename && options.access_key_id && options.secret_access_key
  Envoku.logger.debug("Downloading \"#{options.bucket_name}/#{options.filename}\" from S3")
  FileUtils.rm @local_file_name if File.exists? @local_file_name
  return unless clone_s3_file
  @_env_before = ENV.to_h
  Envoku.logger.debug("Applying ENV vars from S3")
  @data = Dotenv.load(@local_file_name) || {}
  @_env_after = ENV.to_h
  # TODO: Abstract the env diff to adapter base
  @_env_after.each do |key, value|
    if !@_env_before.has_key?(key)
      Envoku.logger.debug("- ADD #{key}")
      # @data[key] = value
    elsif @_env_before[key] != value
      Envoku.logger.debug("- MOD #{key}")
      # @data[key] = value
    end
  end
  FileUtils.rm @local_file_name
  ENV['ENVOKU_REFRESHED_AT'] = Time.now.to_s
end

#set(key, value) ⇒ Object


60
61
62
63
# File 'lib/envoku/adapters/s3.rb', line 60

def set(key, value)
  # TODO: Not yet implemented
  @data[key] = value
end