Module: Dockable

Defined in:
lib/dockable.rb,
lib/dockable/version.rb

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.load_command(argv, procfile_location: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dockable.rb', line 21

def self.load_command(argv, procfile_location: nil)
  procfile_location = presence(procfile_location) || "Procfile"
  if File.exist?(procfile_location) && argv.size == 1
    commands = YAML.load_file(procfile_location)
    if commands.has_key?(argv[0])
      [ commands[argv[0]].to_s ]
    else
      argv
    end
  elsif argv.empty?
    fail ArgumentError, "No command specified"
  else
    argv
  end
end

.load_environment_variables(bucket:, key:, ssm_prefix:) ⇒ Object



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

def self.load_environment_variables(bucket:, key:, ssm_prefix:)
  if presence(ssm_prefix)
    load_from_ssm(ssm_prefix: ssm_prefix)
  elsif presence(bucket) && presence(key)
    load_from_s3(bucket: bucket, key: key)
  else
    STDERR.puts "Dockable warning: No bucket/key or SSM Prefix found, no environment file loaded."
    {}
  end
end

.load_from_s3(bucket:, key:) ⇒ Object



65
66
67
68
69
70
# File 'lib/dockable.rb', line 65

def self.load_from_s3(bucket:, key:)
  client = Aws::S3::Client.new
  resp   = client.get_object(bucket: bucket, key: key)
  body   = resp.body.read
  YAML.safe_load(body).map { |k, v| [ k.to_s, v.to_s ] }.to_h
end

.load_from_ssm(ssm_prefix:) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dockable.rb', line 53

def self.load_from_ssm(ssm_prefix:)
  client     = Aws::SSM::Client.new
  ssm_prefix = "/#{ssm_prefix}" if ssm_prefix[0] != "/" # get_parameters_by_path requires leading /
  parameters = client.get_parameters_by_path(path: ssm_prefix, with_decryption: true).each.flat_map(&:parameters)
  parameters.map { |param|
    [
      param.name.split("/").last.upcase.gsub(/\W/, "_"),
      param.value.to_s
    ]
  }.to_h
end

.presence(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dockable.rb', line 37

def self.presence(value)
  case value
  when nil, false then nil
  when /\A[[:space:]]*\z/
    nil
  when Array, Hash
    if value.empty?
      nil
    else
      value
    end
  else
    value
  end
end