env_configuration 0.1.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.env +5 -5
- data/.gitignore +1 -1
- data/Gemfile.lock +104 -104
- data/README.md +268 -289
- data/env_configuration.gemspec +47 -47
- data/lib/env_configuration.rb +27 -27
- data/lib/env_configuration/adapter/aws_ssm_parameter_store_adapter.rb +88 -91
- data/lib/env_configuration/adapter/base.rb +14 -14
- data/lib/env_configuration/adapter/dot_env_adapter.rb +25 -25
- data/lib/env_configuration/adapter/yaml_adapter.rb +34 -34
- data/lib/env_configuration/aws_ssm_parameter_store_writer.rb +43 -48
- data/lib/env_configuration/configuration.rb +41 -41
- data/lib/env_configuration/configurator.rb +36 -36
- data/lib/env_configuration/version.rb +3 -3
- metadata +2 -2
data/env_configuration.gemspec
CHANGED
@@ -1,47 +1,47 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require "env_configuration/version"
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "env_configuration"
|
8
|
-
spec.version = EnvConfiguration::VERSION
|
9
|
-
spec.authors = ["Channa Ly"]
|
10
|
-
spec.email = ["channa.info@gmail.com"]
|
11
|
-
|
12
|
-
spec.summary = %q{
|
13
|
-
A gem to config env variable in development in dotenv, yaml, aws ssm parameter store, heroku in ruby and rails project.
|
14
|
-
}
|
15
|
-
spec.description = %q{
|
16
|
-
This gem aims to load config to ENV varaible easily. Currently configuration can be done by a .env, a yaml file or by loading from
|
17
|
-
AWS System Manager Parameter Store which is recommended for production deployment.
|
18
|
-
|
19
|
-
}
|
20
|
-
spec.homepage = "https://github.com/channainfo/env_configuration"
|
21
|
-
spec.license = "MIT"
|
22
|
-
|
23
|
-
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
24
|
-
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
25
|
-
# if spec.respond_to?(:metadata)
|
26
|
-
# spec.metadata["allowed_push_host"] = "http://mygemserver.com"
|
27
|
-
# else
|
28
|
-
# raise "RubyGems 2.0 or newer is required to protect against " \
|
29
|
-
# "public gem pushes."
|
30
|
-
# end
|
31
|
-
|
32
|
-
# Specify which files should be added to the gem when it is released.
|
33
|
-
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
34
|
-
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
35
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
36
|
-
end
|
37
|
-
spec.bindir = "exe"
|
38
|
-
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
39
|
-
spec.require_paths = ["lib"]
|
40
|
-
|
41
|
-
spec.add_development_dependency "bundler", "~> 1.16"
|
42
|
-
spec.add_development_dependency "rake", "~> 10.0"
|
43
|
-
spec.add_development_dependency "rspec", "~> 3.0"
|
44
|
-
|
45
|
-
spec.add_dependency "aws-sdk-ssm"
|
46
|
-
spec.add_dependency "dotenv-rails", "2.7.4"
|
47
|
-
end
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "env_configuration/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "env_configuration"
|
8
|
+
spec.version = EnvConfiguration::VERSION
|
9
|
+
spec.authors = ["Channa Ly"]
|
10
|
+
spec.email = ["channa.info@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{
|
13
|
+
A gem to config env variable in development in dotenv, yaml, aws ssm parameter store, heroku in ruby and rails project.
|
14
|
+
}
|
15
|
+
spec.description = %q{
|
16
|
+
This gem aims to load config to ENV varaible easily. Currently configuration can be done by a .env, a yaml file or by loading from
|
17
|
+
AWS System Manager Parameter Store which is recommended for production deployment.
|
18
|
+
|
19
|
+
}
|
20
|
+
spec.homepage = "https://github.com/channainfo/env_configuration"
|
21
|
+
spec.license = "MIT"
|
22
|
+
|
23
|
+
# # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
24
|
+
# # to allow pushing to a single host or delete this section to allow pushing to any host.
|
25
|
+
# if spec.respond_to?(:metadata)
|
26
|
+
# spec.metadata["allowed_push_host"] = "http://mygemserver.com"
|
27
|
+
# else
|
28
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
29
|
+
# "public gem pushes."
|
30
|
+
# end
|
31
|
+
|
32
|
+
# Specify which files should be added to the gem when it is released.
|
33
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
34
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
35
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
36
|
+
end
|
37
|
+
spec.bindir = "exe"
|
38
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
39
|
+
spec.require_paths = ["lib"]
|
40
|
+
|
41
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
42
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
43
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
44
|
+
|
45
|
+
spec.add_dependency "aws-sdk-ssm"
|
46
|
+
spec.add_dependency "dotenv-rails", "2.7.4"
|
47
|
+
end
|
data/lib/env_configuration.rb
CHANGED
@@ -1,27 +1,27 @@
|
|
1
|
-
require "env_configuration/version"
|
2
|
-
require "env_configuration/configurator"
|
3
|
-
require "env_configuration/configuration"
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
1
|
+
require "env_configuration/version"
|
2
|
+
require "env_configuration/configurator"
|
3
|
+
require "env_configuration/configuration"
|
4
|
+
|
5
|
+
module EnvConfiguration
|
6
|
+
class Error < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :configuration
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def self.configuration
|
14
|
+
@configuration ||= Configuration.new
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.configure
|
18
|
+
yield(self.configuration)
|
19
|
+
self.configuration
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset_configuration
|
23
|
+
@configuration = Configuration.new
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
@@ -1,91 +1,88 @@
|
|
1
|
-
module EnvConfiguration
|
2
|
-
module Adapter
|
3
|
-
class AwsSsmParameterStoreAdapter < Base
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
name
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
end
|
91
|
-
end
|
1
|
+
module EnvConfiguration
|
2
|
+
module Adapter
|
3
|
+
class AwsSsmParameterStoreAdapter < Base
|
4
|
+
|
5
|
+
def initialize(options={})
|
6
|
+
super(options)
|
7
|
+
|
8
|
+
end
|
9
|
+
|
10
|
+
def client
|
11
|
+
aws_options = {
|
12
|
+
access_key_id: access_key_id,
|
13
|
+
secret_access_key: secret_access_key,
|
14
|
+
region: region,
|
15
|
+
}
|
16
|
+
@client ||= Aws::SSM::Client.new(@options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def access_key_id
|
20
|
+
@options[:access_key_id] || EnvConfiguration.configuration.aws_access_key_id
|
21
|
+
end
|
22
|
+
|
23
|
+
def secret_access_key
|
24
|
+
@options[:secret_access_key] || EnvConfiguration.configuration.aws_secret_access_key
|
25
|
+
end
|
26
|
+
|
27
|
+
def region
|
28
|
+
@options[:region] || EnvConfiguration.configuration.aws_region
|
29
|
+
end
|
30
|
+
|
31
|
+
def path
|
32
|
+
path_value = @options[:path] || EnvConfiguration.configuration.aws_path
|
33
|
+
raise ":path options is required for example /staging" if path_value.nil?
|
34
|
+
path_value
|
35
|
+
end
|
36
|
+
|
37
|
+
def load
|
38
|
+
# fetch_configs do |item|
|
39
|
+
# ENV["#{item[:name]}"] = item[:value]
|
40
|
+
# end
|
41
|
+
|
42
|
+
configs = fetch_configs
|
43
|
+
ENV.update(configs)
|
44
|
+
configs
|
45
|
+
end
|
46
|
+
|
47
|
+
def fetch_configs(&block)
|
48
|
+
next_token = nil
|
49
|
+
result = {}
|
50
|
+
|
51
|
+
while(true)
|
52
|
+
|
53
|
+
fetch_options = {
|
54
|
+
path: path,
|
55
|
+
with_decryption: false,
|
56
|
+
recursive: true,
|
57
|
+
max_results: 10
|
58
|
+
}
|
59
|
+
|
60
|
+
fetch_options[:next_token] = next_token if next_token
|
61
|
+
|
62
|
+
response = client.get_parameters_by_path(fetch_options)
|
63
|
+
|
64
|
+
items = sanitize_configs(response.parameters, &block)
|
65
|
+
result.merge!(items)
|
66
|
+
|
67
|
+
next_token = response.next_token
|
68
|
+
break if next_token.nil?
|
69
|
+
end
|
70
|
+
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
74
|
+
def sanitize_configs(parameters, &block)
|
75
|
+
items = {}
|
76
|
+
parameters.each do |param|
|
77
|
+
name = param.name.gsub("#{path}/", '')
|
78
|
+
value = param.value
|
79
|
+
item = {name: name, value: value}
|
80
|
+
items[name] = value
|
81
|
+
yield(item) if block_given?
|
82
|
+
end
|
83
|
+
items
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
@@ -1,15 +1,15 @@
|
|
1
|
-
module EnvConfiguration
|
2
|
-
module Adapter
|
3
|
-
attr_accessor :options
|
4
|
-
|
5
|
-
class Base
|
6
|
-
def initialize(options={})
|
7
|
-
@options = options.clone
|
8
|
-
end
|
9
|
-
|
10
|
-
def update_env_variable_with(configs)
|
11
|
-
ENV.update(configs)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
1
|
+
module EnvConfiguration
|
2
|
+
module Adapter
|
3
|
+
attr_accessor :options
|
4
|
+
|
5
|
+
class Base
|
6
|
+
def initialize(options={})
|
7
|
+
@options = options.clone
|
8
|
+
end
|
9
|
+
|
10
|
+
def update_env_variable_with(configs)
|
11
|
+
ENV.update(configs)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
15
|
end
|
@@ -1,25 +1,25 @@
|
|
1
|
-
require 'dotenv'
|
2
|
-
|
3
|
-
module EnvConfiguration
|
4
|
-
module Adapter
|
5
|
-
class DotEnvAdapter < Base
|
6
|
-
|
7
|
-
def initialize(options={})
|
8
|
-
super(options)
|
9
|
-
end
|
10
|
-
|
11
|
-
def file
|
12
|
-
@options[:dot_env_file] || EnvConfiguration.configuration.dot_env_file
|
13
|
-
end
|
14
|
-
|
15
|
-
def load
|
16
|
-
p file
|
17
|
-
if file.nil?
|
18
|
-
Dotenv.load
|
19
|
-
else
|
20
|
-
Dotenv.load(file)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
1
|
+
require 'dotenv'
|
2
|
+
|
3
|
+
module EnvConfiguration
|
4
|
+
module Adapter
|
5
|
+
class DotEnvAdapter < Base
|
6
|
+
|
7
|
+
def initialize(options={})
|
8
|
+
super(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def file
|
12
|
+
@options[:dot_env_file] || EnvConfiguration.configuration.dot_env_file
|
13
|
+
end
|
14
|
+
|
15
|
+
def load
|
16
|
+
p file
|
17
|
+
if file.nil?
|
18
|
+
Dotenv.load
|
19
|
+
else
|
20
|
+
Dotenv.load(file)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,34 +1,34 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module EnvConfiguration
|
4
|
-
module Adapter
|
5
|
-
class YamlAdapter < Base
|
6
|
-
|
7
|
-
def initialize(options = {})
|
8
|
-
super(options)
|
9
|
-
end
|
10
|
-
|
11
|
-
def section
|
12
|
-
section_value = @options[:section] || EnvConfiguration.configuration.yaml_section
|
13
|
-
raise ":section in the options{} is required, for example :staging, :test, :production" if section_value.nil?
|
14
|
-
section_value
|
15
|
-
end
|
16
|
-
|
17
|
-
def yaml_file
|
18
|
-
yaml_file_value = @options[:yaml_file] || EnvConfiguration.configuration.yaml_file
|
19
|
-
raise ":yaml_file in the options{} is required, for example config/application.yml" if yaml_file_value.nil?
|
20
|
-
yaml_file_value
|
21
|
-
end
|
22
|
-
|
23
|
-
def load
|
24
|
-
configs = fetch_configs
|
25
|
-
update_env_variable_with(configs)
|
26
|
-
configs
|
27
|
-
end
|
28
|
-
|
29
|
-
def fetch_configs
|
30
|
-
::YAML.load_file(yaml_file)[section]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module EnvConfiguration
|
4
|
+
module Adapter
|
5
|
+
class YamlAdapter < Base
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
super(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def section
|
12
|
+
section_value = @options[:section] || EnvConfiguration.configuration.yaml_section
|
13
|
+
raise ":section in the options{} is required, for example :staging, :test, :production" if section_value.nil?
|
14
|
+
section_value
|
15
|
+
end
|
16
|
+
|
17
|
+
def yaml_file
|
18
|
+
yaml_file_value = @options[:yaml_file] || EnvConfiguration.configuration.yaml_file
|
19
|
+
raise ":yaml_file in the options{} is required, for example config/application.yml" if yaml_file_value.nil?
|
20
|
+
yaml_file_value
|
21
|
+
end
|
22
|
+
|
23
|
+
def load
|
24
|
+
configs = fetch_configs
|
25
|
+
update_env_variable_with(configs)
|
26
|
+
configs
|
27
|
+
end
|
28
|
+
|
29
|
+
def fetch_configs
|
30
|
+
::YAML.load_file(yaml_file)[section]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|