al-infrastructure-configuration-storage 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 336af31c8929ceb376f9b97e8696dd1029fc604b55195c5cfed393dac8f335c1
4
+ data.tar.gz: b702b62934327ca41328e02a72d3d34c60e5808df7967843c7753b3db2611bd5
5
+ SHA512:
6
+ metadata.gz: e33538b2bc8a6b50c0c501b832be9ba5b5d62412208cce3d4382796ee49f71b1e1cf98a38a112092c7b5a02459d9970e655d8c1628589ef5ed57b63ac8dc69b0
7
+ data.tar.gz: ef677443e441517ccc039f7534079e88f62a88a7923cba097cdaa6fbac6bbe992da38727030a069ac3ea0d5f4c49f60c3bbacf124541bbc2a1e869265b33b86d
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/settings"
4
+ require "infrastructure/configuration"
5
+
6
+ # ::nodoc::
7
+ module Infrastructure
8
+ class << self
9
+ def configure
10
+ yield config
11
+ end
12
+
13
+ def config
14
+ @config ||= Configuration.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Infrastructure
4
+ # infrastructure settings configuration
5
+ class Configuration
6
+ class << self
7
+ def properties
8
+ @properties ||= []
9
+ end
10
+
11
+ def property(name, env_var_name = nil)
12
+ attr_accessor name
13
+ properties << [name, (env_var_name || name.to_s.upcase)]
14
+ end
15
+ end
16
+
17
+ property :aws_access_key_id
18
+ property :aws_secret_access_key
19
+ property :aws_region
20
+ property :storage, "INFRASTRUCTURE_SETTINGS_STORAGE"
21
+ property :location, "INFRASTRUCTURE_SETTINGS_LOCATION"
22
+
23
+ def initialize(attr = {})
24
+ self.class.properties.map do |property, env_var_name|
25
+ send("#{property}=", attr[property] || ENV[env_var_name])
26
+ end
27
+ end
28
+
29
+ def properties
30
+ self.class.properties.each_with_object({}) { |p, memo| memo[p] = send(p) }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/formatters/bash_source"
4
+ require "infrastructure/formatters/json"
5
+ require "infrastructure/formatters/yaml"
6
+
7
+ module Infrastructure
8
+ # ::nodoc::
9
+ module Formatters
10
+ class << self
11
+ def build(format)
12
+ case format.to_sym
13
+ when :bash
14
+ BashSource.new
15
+ when :json
16
+ JSON.new
17
+ when :yaml
18
+ YAML.new
19
+ else
20
+ raise ArgumentError "Unknown format #{format}"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Infrastructure
4
+ module Formatters
5
+ # base formatter class
6
+ class Base
7
+ class << self
8
+ def format(hash)
9
+ new.format(hash)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/formatters/base"
4
+
5
+ module Infrastructure
6
+ module Formatters
7
+ # transform hashes to bash source format
8
+ class BashSource < Base
9
+ # @param [Hash] hash
10
+ # @return [String]
11
+ def format(hash)
12
+ hash.map do |key, value|
13
+ val = value.gsub(/[']/, "'" => "\\'")
14
+ "export #{key}='#{val}'\n"
15
+ end.join
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/formatters/base"
4
+ require "json"
5
+
6
+ module Infrastructure
7
+ module Formatters
8
+ # transform hashes to YAML
9
+ class JSON < Base
10
+ # @param [Hash] hash
11
+ # @return [String]
12
+ def format(hash)
13
+ ::JSON.pretty_generate(hash)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/formatters/base"
4
+ require "yaml"
5
+
6
+ module Infrastructure
7
+ module Formatters
8
+ # transform hashes to YAML
9
+ class YAML < Base
10
+ # @param [Hash] hash
11
+ # @return [String]
12
+ def format(hash)
13
+ "---\n#{hash.map { |k, v| "#{k}: '#{v.gsub(/['\n]/, "'" => "''", "\n" => '\\n')}'" }.join("\n")}\n"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/loaders/local"
4
+ require "infrastructure/loaders/s3"
5
+
6
+ module Infrastructure
7
+ # ::nodoc::
8
+ module Loaders
9
+ class << self
10
+ def build(config)
11
+ case config.storage.to_s.downcase.to_sym
12
+ when :s3
13
+ S3.new(config)
14
+ when :local
15
+ Local.new(config)
16
+ else
17
+ raise ArgumentError, "Unknown storage type #{config.storage}"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Infrastructure
4
+ module Loaders
5
+ # base loader class
6
+ class Base
7
+ def initialize(config = Infrastructure.config)
8
+ @config = config
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :config
14
+
15
+ def mkdir_unless_exists(dir)
16
+ FileUtils.mkpath(dir) unless File.directory?(dir)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/loaders/base"
4
+ require "fileutils"
5
+
6
+ module Infrastructure
7
+ module Loaders
8
+ # store and retrieve data in file system
9
+ class Local < Base
10
+ # @param [String] to directory to save file to
11
+ # @param [String] name file name
12
+ # @param [String] content file content
13
+ def push(to:, name:, content:)
14
+ path = File.dirname(File.join(to, name))
15
+ mkdir_unless_exists(path)
16
+ File.open(File.join(to, name), "w+") do |f|
17
+ f.write(content)
18
+ end
19
+ end
20
+
21
+ # @param [String] from source directory
22
+ # @param [String] name settings name
23
+ # @param [String] to destination directory
24
+ def pull(from:, name:, to:)
25
+ mkdir_unless_exists(to)
26
+ FileUtils.cp(Dir[File.join(from, "#{name}/#{name}.*")], to)
27
+ end
28
+
29
+ # @param [String] from directory
30
+ # @param [String] name settings name
31
+ def delete(from:, name:)
32
+ FileUtils.rm(Dir[File.join(from, "#{name}/#{name}.*")])
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/loaders/base"
4
+ require "aws-sdk-s3"
5
+
6
+ module Infrastructure
7
+ module Loaders
8
+ # store and retrieve data on S3 bucket
9
+ class S3 < Base
10
+ # @param [String] to bucket name
11
+ # @param [String] name object key
12
+ # @param [String] content
13
+ def push(to:, name:, content:)
14
+ bucket = s3_bucket(to)
15
+ bucket.put_object(key: name, body: content)
16
+ end
17
+
18
+ # @param [String] from bucket name
19
+ # @param [String] name settings name
20
+ # @param [String] to destination directory
21
+ def pull(from:, name:, to:)
22
+ bucket = s3_bucket(from)
23
+ mkdir_unless_exists(to)
24
+ bucket.objects(prefix: "#{name}/#{name}.").each do |obj|
25
+ File.open(File.join(to, obj.key.split("/").last), "w+") do |f|
26
+ f.write(read_object_content(obj))
27
+ end
28
+ end
29
+ end
30
+
31
+ # @param [String] from bucket name
32
+ # @param [String] name settings name
33
+ def delete(from:, name:)
34
+ bucket = s3_bucket(from)
35
+ object_keys = bucket.objects(prefix: "#{name}/#{name}.").map { |obj| { key: obj.key } }
36
+ bucket.delete_objects(delete: { objects: object_keys })
37
+ end
38
+
39
+ private
40
+
41
+ def read_object_content(obj)
42
+ obj.get.body.read
43
+ end
44
+
45
+ def s3_bucket(name)
46
+ s3 = s3_resource
47
+ s3.bucket(name)
48
+ end
49
+
50
+ def s3_resource
51
+ Aws::S3::Resource.new(
52
+ region: config.aws_region,
53
+ credentials: Aws::Credentials.new(config.aws_access_key_id, config.aws_secret_access_key)
54
+ )
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infrastructure/loaders"
4
+ require "infrastructure/formatters"
5
+
6
+ module Infrastructure
7
+ # upload and download settings
8
+ class Settings
9
+ class << self
10
+ def push(*attr)
11
+ new.push(*attr)
12
+ end
13
+
14
+ def pull(*attr)
15
+ new.pull(*attr)
16
+ end
17
+
18
+ def delete(*attr)
19
+ new.delete(*attr)
20
+ end
21
+ end
22
+
23
+ def initialize(config: Infrastructure.config, formatters: Infrastructure::Formatters)
24
+ @config = config
25
+ @loader = Loaders.build(config)
26
+ @formatters = formatters
27
+ end
28
+
29
+ def push(name:, settings:, formats: %i[bash])
30
+ formats.each do |format|
31
+ content = formatters.build(format).format(settings)
32
+ loader.push(to: config.location, name: "#{name}/#{name}.#{format}", content: content)
33
+ end
34
+ end
35
+
36
+ def pull(name:, dir:)
37
+ loader.pull(from: config.location, to: dir, name: name)
38
+ end
39
+
40
+ def delete(name:)
41
+ loader.delete(from: config.location, name: name)
42
+ end
43
+
44
+ private
45
+
46
+ attr_reader :config, :loader, :formatters
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: al-infrastructure-configuration-storage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Pochapsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-s3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.23'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.23'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakefs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.18'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.8'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.60'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.60'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description:
84
+ email: info@agiliumlabs.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - lib/al-infrastructure-configuration-storage.rb
90
+ - lib/infrastructure.rb
91
+ - lib/infrastructure/configuration.rb
92
+ - lib/infrastructure/formatters.rb
93
+ - lib/infrastructure/formatters/base.rb
94
+ - lib/infrastructure/formatters/bash_source.rb
95
+ - lib/infrastructure/formatters/json.rb
96
+ - lib/infrastructure/formatters/yaml.rb
97
+ - lib/infrastructure/loaders.rb
98
+ - lib/infrastructure/loaders/base.rb
99
+ - lib/infrastructure/loaders/local.rb
100
+ - lib/infrastructure/loaders/s3.rb
101
+ - lib/infrastructure/settings.rb
102
+ homepage: https://agiliumlabs.com
103
+ licenses: []
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.7.7
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Save and load infrastructure configuration
125
+ test_files: []