s3_assets_deployer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7fafda75c54f7eef90f941621ff314b1c5f0c97d
4
+ data.tar.gz: b0cccf43185b224e75abac884401b5b51958500a
5
+ SHA512:
6
+ metadata.gz: f9afc73012333151eb5d3946ecc8ece9fa443dd018d388c9b8d848873db8e766aba3ffb531be164f24fb34153f8b3f9137bf30119620ce5d7cfe47144864f58a
7
+ data.tar.gz: 6eccc3c128d58294936cc8de53c3126e8c4da76f8689d320fc12bbb87671eef593dc9791c2d26bb6d642ccd90d0b9b60aa892453f671c2b0ffa91fa1676846d5
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Shota Iguchi
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # S3AssetsDeployer
2
+ Asset files deployer for rails application.
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 's3_assets_deployer'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install s3_assets_deployer
19
+ ```
20
+
21
+ ## Usage
22
+
23
+ ### Rails
24
+ Add initializer at `config/iniitializers/s3_assets_deployer.rb`
25
+ ```rb
26
+ AssetsDeployer.configure do |config|
27
+ config.assets.root_path = Rails.root.join('public')
28
+ config.assets.prefix_paths = ['assets', 'packs']
29
+ config.assets.ignore_paths = ['packs/manifest.json']
30
+ # your storage service configurations
31
+ config.s3.region = 'ap-northeast-1'
32
+ config.s3.bucket = 'bucket_name'
33
+ config.s3.prefix_key = 'project_name'
34
+ # credentials
35
+ # Also, you can use environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) or instance profile credentials
36
+ config.s3.credentials[:access_key_id] = 'aws_access_key_id'
37
+ config.s3.credentials[:secret_access_key] = 'aws_secret_access_key'
38
+ end
39
+ ```
40
+
41
+ then you can use this rails command
42
+ ```console
43
+ $ bundle exec rails assets:precompile assets:deploy
44
+ ```
45
+
46
+ ## License
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'bundler/gem_tasks'
@@ -0,0 +1,24 @@
1
+ require 'mime/types'
2
+
3
+ module S3AssetsDeployer
4
+ class AssetFile
5
+ attr_reader :prefix
6
+
7
+ def initialize(prefix, path)
8
+ @prefix = prefix
9
+ @path = path
10
+ end
11
+
12
+ def key
13
+ [prefix, File.basename(@path)].join('/')
14
+ end
15
+
16
+ def body
17
+ File.new(@path)
18
+ end
19
+
20
+ def content_type
21
+ MIME::Types.type_for(@path).first.to_s
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,56 @@
1
+ require 'aws-sdk'
2
+
3
+ module S3AssetsDeployer
4
+ class AwsS3
5
+ def initialize(credentials:, region: nil, bucket: nil, prefix_key: nil)
6
+ @credentials = credentials
7
+ @bucket = bucket
8
+ @prefix_key = prefix_key
9
+ @region = region
10
+ end
11
+
12
+ def upload(files)
13
+ files.each do |file|
14
+ client.put_object(
15
+ bucket: @bucket,
16
+ key: [@prefix_key, file.key].compact.join('/'),
17
+ body: file.body,
18
+ content_type: file.content_type
19
+ )
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def client
26
+ @client ||= Aws::S3::Client.new(client_options)
27
+ end
28
+
29
+ def client_options
30
+ hash = {}
31
+ hash[:credentials] = credentials
32
+ hash[:region] = region if !region.nil? && !region.empty?
33
+ hash
34
+ end
35
+
36
+ def credentials
37
+ if access_key_id && secret_access_key
38
+ Aws::Credentials.new(access_key_id, secret_access_key)
39
+ else
40
+ Aws::InstanceProfileCredentials.new
41
+ end
42
+ end
43
+
44
+ def access_key_id
45
+ @credentials[:access_key_id] || ENV['AWS_ACCESS_KEY_ID']
46
+ end
47
+
48
+ def secret_access_key
49
+ @credentials[:secret_access_key] || ENV['AWS_SECRET_ACCESS_KEY']
50
+ end
51
+
52
+ def region
53
+ @region || ENV['AWS_REGION']
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,27 @@
1
+ module S3AssetsDeployer
2
+ class Configuration
3
+ attr_accessor :assets,
4
+ :s3
5
+
6
+ class AssetsConfiguration < Struct.new(:root_path, :prefix_paths, :ignore_paths)
7
+ def initialize
8
+ self.prefix_paths = ['assets']
9
+ self.ignore_paths = []
10
+ end
11
+ end
12
+
13
+ class S3Configuration < Struct.new(:credentials, :bucket, :prefix_key, :region)
14
+ def initialize
15
+ self.credentials = {}
16
+ end
17
+ end
18
+
19
+ def assets
20
+ @assets ||= AssetsConfiguration.new
21
+ end
22
+
23
+ def s3
24
+ @s3 ||= S3Configuration.new
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require 's3_assets_deployer/asset_file'
2
+
3
+ module S3AssetsDeployer
4
+ class Deployer
5
+ def initialize(storage:, root_path:, prefix_paths:, ignore_paths:)
6
+ @storage = storage
7
+ @root_path = root_path
8
+ @prefix_paths = prefix_paths
9
+ @ignore_paths = ignore_paths.map { |path| File.join(root_path, path) }
10
+ end
11
+
12
+ def run
13
+ @storage.upload(files)
14
+ end
15
+
16
+ private
17
+
18
+ def files
19
+ @files ||= @prefix_paths.flat_map do |prefix|
20
+ Dir.glob(@root_path.join(prefix).join('**', '**')).map do |path|
21
+ AssetFile.new(prefix, path) if Pathname.new(path).file? && !@ignore_paths.include?(path)
22
+ end
23
+ end.compact
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,5 @@
1
+ module S3AssetsDeployer
2
+ class Engine < Rails::Engine
3
+ engine_name 's3_assets_deployer'
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module S3AssetsDeployer
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,35 @@
1
+ require 's3_assets_deployer/deployer'
2
+ require 's3_assets_deployer/configuration'
3
+ require 's3_assets_deployer/aws_s3'
4
+
5
+ module S3AssetsDeployer
6
+ def self.run
7
+ deployer = Deployer.new(
8
+ storage: s3,
9
+ root_path: config.assets.root_path,
10
+ prefix_paths: config.assets.prefix_paths,
11
+ ignore_paths: config.assets.ignore_paths
12
+ )
13
+
14
+ deployer.run
15
+ end
16
+
17
+ def self.config
18
+ @config ||= Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield config
23
+ end
24
+
25
+ def self.s3
26
+ @s3 ||= AwsS3.new(
27
+ credentials: config.s3.credentials,
28
+ bucket: config.s3.bucket,
29
+ prefix_key: config.s3.prefix_key,
30
+ region: config.s3.region
31
+ )
32
+ end
33
+ end
34
+
35
+ require 's3_assets_deployer/engine' if defined?(Rails)
@@ -0,0 +1,5 @@
1
+ namespace :assets do
2
+ task deploy: :environment do
3
+ S3AssetsDeployer.run
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: s3_assets_deployer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shota Iguchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aws-sdk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - shota-iguchi@cookpad.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - lib/s3_assets_deployer.rb
66
+ - lib/s3_assets_deployer/asset_file.rb
67
+ - lib/s3_assets_deployer/aws_s3.rb
68
+ - lib/s3_assets_deployer/configuration.rb
69
+ - lib/s3_assets_deployer/deployer.rb
70
+ - lib/s3_assets_deployer/engine.rb
71
+ - lib/s3_assets_deployer/version.rb
72
+ - lib/tasks/assets.rake
73
+ homepage:
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.6.11
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Rails assets deployer
97
+ test_files: []