bankai-docker 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8d314159811ac96d23c11e0d76fc8be0a25725d777842c2dc2712cde62d71e8c
4
+ data.tar.gz: 36069b1bb2fd5776527009ed0bcd504a998ebdc70c81084607b735c82fddd716
5
+ SHA512:
6
+ metadata.gz: 32e12d710314698a8de918bc92b18a6f0c79bfc8e28551e9b0c3fa18c3e63fbe999765a7c08852f9f29020367fa805de9de4377b72da07c698f3ec4b24de23ef
7
+ data.tar.gz: e2cd532fb98f4b6fad37a795a3ea438dfcb93fa3fab8dbe404205e107d5649743fbaf2143c6c9cdbe43f72e3fbc4c8c55cdf1f8e3d5c3f1677d261a2bf821caa
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ coverage/
data/.overcommit.yml ADDED
@@ -0,0 +1,22 @@
1
+ PreCommit:
2
+ AuthorName:
3
+ enabled: false
4
+ RuboCop:
5
+ enabled: true
6
+ on_warn: fail # Treat all warnings as failures
7
+ TrailingWhitespace:
8
+ enabled: true
9
+ exclude:
10
+ - '.licenses/**/*'
11
+
12
+ PrePush:
13
+ BundleAudit:
14
+ enabled: true
15
+ BundleOutdated:
16
+ enabled: true
17
+
18
+ PostCheckout:
19
+ ALL: # Special hook name that customizes all hooks of this type
20
+ quiet: true # Change all post-checkout hooks to only display output on failure
21
+ IndexTags:
22
+ enabled: true # Generate a tags file with `ctags` each time HEAD changes
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.3
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at elct9620@frost.tw. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bankai-docker.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,182 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bankai-docker (0.1.0)
5
+ bankai
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.2.2)
11
+ actionpack (= 5.2.2)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (>= 0.6.1)
14
+ actionmailer (5.2.2)
15
+ actionpack (= 5.2.2)
16
+ actionview (= 5.2.2)
17
+ activejob (= 5.2.2)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.2.2)
21
+ actionview (= 5.2.2)
22
+ activesupport (= 5.2.2)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.2.2)
28
+ activesupport (= 5.2.2)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.2.2)
34
+ activesupport (= 5.2.2)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.2.2)
37
+ activesupport (= 5.2.2)
38
+ activerecord (5.2.2)
39
+ activemodel (= 5.2.2)
40
+ activesupport (= 5.2.2)
41
+ arel (>= 9.0)
42
+ activestorage (5.2.2)
43
+ actionpack (= 5.2.2)
44
+ activerecord (= 5.2.2)
45
+ marcel (~> 0.3.1)
46
+ activesupport (5.2.2)
47
+ concurrent-ruby (~> 1.0, >= 1.0.2)
48
+ i18n (>= 0.7, < 2)
49
+ minitest (~> 5.1)
50
+ tzinfo (~> 1.1)
51
+ arel (9.0.0)
52
+ ast (2.4.0)
53
+ bankai (0.2.2)
54
+ rails (= 5.2.2)
55
+ builder (3.2.3)
56
+ bundler-audit (0.6.1)
57
+ bundler (>= 1.2.0, < 3)
58
+ thor (~> 0.18)
59
+ childprocess (0.9.0)
60
+ ffi (~> 1.0, >= 1.0.11)
61
+ concurrent-ruby (1.1.4)
62
+ crass (1.0.4)
63
+ diff-lcs (1.3)
64
+ docile (1.3.1)
65
+ erubi (1.8.0)
66
+ ffi (1.10.0)
67
+ globalid (0.4.2)
68
+ activesupport (>= 4.2.0)
69
+ i18n (1.6.0)
70
+ concurrent-ruby (~> 1.0)
71
+ iniparse (1.4.4)
72
+ jaro_winkler (1.5.2)
73
+ json (2.2.0)
74
+ loofah (2.2.3)
75
+ crass (~> 1.0.2)
76
+ nokogiri (>= 1.5.9)
77
+ mail (2.7.1)
78
+ mini_mime (>= 0.1.1)
79
+ marcel (0.3.3)
80
+ mimemagic (~> 0.3.2)
81
+ method_source (0.9.2)
82
+ mimemagic (0.3.3)
83
+ mini_mime (1.0.1)
84
+ mini_portile2 (2.4.0)
85
+ minitest (5.11.3)
86
+ nio4r (2.3.1)
87
+ nokogiri (1.10.1)
88
+ mini_portile2 (~> 2.4.0)
89
+ overcommit (0.46.0)
90
+ childprocess (~> 0.6, >= 0.6.3)
91
+ iniparse (~> 1.4)
92
+ parallel (1.14.0)
93
+ parser (2.6.0.0)
94
+ ast (~> 2.4.0)
95
+ powerpack (0.1.2)
96
+ rack (2.0.6)
97
+ rack-test (1.1.0)
98
+ rack (>= 1.0, < 3)
99
+ rails (5.2.2)
100
+ actioncable (= 5.2.2)
101
+ actionmailer (= 5.2.2)
102
+ actionpack (= 5.2.2)
103
+ actionview (= 5.2.2)
104
+ activejob (= 5.2.2)
105
+ activemodel (= 5.2.2)
106
+ activerecord (= 5.2.2)
107
+ activestorage (= 5.2.2)
108
+ activesupport (= 5.2.2)
109
+ bundler (>= 1.3.0)
110
+ railties (= 5.2.2)
111
+ sprockets-rails (>= 2.0.0)
112
+ rails-dom-testing (2.0.3)
113
+ activesupport (>= 4.2.0)
114
+ nokogiri (>= 1.6)
115
+ rails-html-sanitizer (1.0.4)
116
+ loofah (~> 2.2, >= 2.2.2)
117
+ railties (5.2.2)
118
+ actionpack (= 5.2.2)
119
+ activesupport (= 5.2.2)
120
+ method_source
121
+ rake (>= 0.8.7)
122
+ thor (>= 0.19.0, < 2.0)
123
+ rainbow (3.0.0)
124
+ rake (10.5.0)
125
+ rspec (3.8.0)
126
+ rspec-core (~> 3.8.0)
127
+ rspec-expectations (~> 3.8.0)
128
+ rspec-mocks (~> 3.8.0)
129
+ rspec-core (3.8.0)
130
+ rspec-support (~> 3.8.0)
131
+ rspec-expectations (3.8.2)
132
+ diff-lcs (>= 1.2.0, < 2.0)
133
+ rspec-support (~> 3.8.0)
134
+ rspec-mocks (3.8.0)
135
+ diff-lcs (>= 1.2.0, < 2.0)
136
+ rspec-support (~> 3.8.0)
137
+ rspec-support (3.8.0)
138
+ rubocop (0.60.0)
139
+ jaro_winkler (~> 1.5.1)
140
+ parallel (~> 1.10)
141
+ parser (>= 2.5, != 2.5.1.1)
142
+ powerpack (~> 0.1)
143
+ rainbow (>= 2.2.2, < 4.0)
144
+ ruby-progressbar (~> 1.7)
145
+ unicode-display_width (~> 1.4.0)
146
+ ruby-progressbar (1.10.0)
147
+ simplecov (0.16.1)
148
+ docile (~> 1.1)
149
+ json (>= 1.8, < 3)
150
+ simplecov-html (~> 0.10.0)
151
+ simplecov-html (0.10.2)
152
+ sprockets (3.7.2)
153
+ concurrent-ruby (~> 1.0)
154
+ rack (> 1, < 3)
155
+ sprockets-rails (3.2.1)
156
+ actionpack (>= 4.0)
157
+ activesupport (>= 4.0)
158
+ sprockets (>= 3.0.0)
159
+ thor (0.20.3)
160
+ thread_safe (0.3.6)
161
+ tzinfo (1.2.5)
162
+ thread_safe (~> 0.1)
163
+ unicode-display_width (1.4.1)
164
+ websocket-driver (0.7.0)
165
+ websocket-extensions (>= 0.1.0)
166
+ websocket-extensions (0.1.3)
167
+
168
+ PLATFORMS
169
+ ruby
170
+
171
+ DEPENDENCIES
172
+ bankai-docker!
173
+ bundler (~> 1.17)
174
+ bundler-audit
175
+ overcommit
176
+ rake (~> 10.0)
177
+ rspec (~> 3.0)
178
+ rubocop (~> 0.60.0)
179
+ simplecov
180
+
181
+ BUNDLED WITH
182
+ 1.17.3
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ Bankai Docker
2
+ ===
3
+
4
+ This gem is extension for [Bankai](https://github.com/5xruby/bankai) to build production ready docker image.
5
+
6
+ > By the default docker image will have size about 180MB includes Ruby, Node.js, Yarn and Required Gems for Rails
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'bankai-docker'
14
+ ```
15
+
16
+ Then the docker related generator and rake task will added to your project.
17
+
18
+
19
+ ## Usage
20
+
21
+ This gem will try to setup Dockerfile and .dockerignore for your project, so you can direct use this Rake task to build docker image:
22
+
23
+ ```
24
+ rake docker:build
25
+ ```
26
+
27
+ By the default, we will use the `$(whoami)/[RAILS_APP_NAME]` as docker image ex. `5xruby/example` but you can specify it if you want
28
+
29
+ ```
30
+ rake docker:build[mycompany/example]
31
+ ```
32
+
33
+ If you want to use our DSL to define the Dockerfile generate, you can edit `config/docker.rb` to defined it.
34
+
35
+ ```
36
+ # Generate config/docker.rb
37
+ rails generate bankai:docker:install
38
+ # Edit it
39
+ vim config/docker.rb
40
+ ```
41
+
42
+ ## Roadmap
43
+
44
+ * [ ] Auto detect Dockerfile
45
+ * [ ] Auto detect .dockerignore
46
+ * [ ] Auto package install detect from Gemfile
47
+ * [ ] DSL
48
+ * [ ] Gem Install
49
+ * [ ] Specify Ruby/Node Version
50
+ * [ ] Package Install
51
+ * [ ] Rails Commands
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/5xruby/bankai-docker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the Bankai::Docker project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/5xruby/bankai-docker/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,35 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'bankai/docker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'bankai-docker'
7
+ spec.version = Bankai::Docker::VERSION
8
+ spec.authors = %w[5xRuby 蒼時弦也]
9
+ spec.email = ['hi@5xruby.tw', 'contact0@frost.tw']
10
+
11
+ spec.summary = 'The easist docker template for Rails'
12
+ spec.description = 'The easist docker template for Rails'
13
+ spec.homepage = 'https://github.com/5xRuby/bankai-docker'
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the
17
+ # RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`
20
+ .split("\x0")
21
+ .reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.17'
28
+ spec.add_development_dependency 'bundler-audit'
29
+ spec.add_development_dependency 'overcommit'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'rubocop', '~> 0.60.0'
33
+ spec.add_development_dependency 'simplecov'
34
+ spec.add_dependency 'bankai'
35
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'bankai/docker'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,19 @@
1
+ require 'bankai/docker/version'
2
+ require 'bankai/docker/generators/install'
3
+ require 'bankai/docker/railtie'
4
+ require 'bankai/docker/dsl'
5
+ require 'bankai/docker/file'
6
+ require 'bankai/docker/stage'
7
+ require 'bankai/docker/command'
8
+
9
+ module Bankai
10
+ # :nodoc:
11
+ module Docker
12
+ class << self
13
+ def setup(&block)
14
+ @config =
15
+ Bankai::Docker::File.setup(&block)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ # The Rake Builder task
6
+ class Builder
7
+ include Rake::DSL if defined? Rake::DSL
8
+
9
+ IGNORES = ['.git', 'node_modules', 'log/*',
10
+ 'tmp/*', 'public/system', 'public/uploads'].freeze
11
+
12
+ class << self
13
+ def install_tasks
14
+ new.install
15
+ end
16
+ end
17
+
18
+ def initialize
19
+ # TODO: Load name when running task
20
+ @name = File.name
21
+ @layers = File.stages.map(&:first)
22
+ @entrypoint = Tempfile.new('docker-entrypoint')
23
+ @dockerfile = Tempfile.new('Dockerfile')
24
+ @dockerignore = Tempfile.new('.dockerignore')
25
+ @dockerignore_generated = false
26
+
27
+ setup_entrypoint
28
+ setup_dockerfile
29
+ setup_dockerignore
30
+
31
+ at_exit { clear_up }
32
+ end
33
+
34
+ def install
35
+ namespace(:build) { install_build_tasks }
36
+ desc 'Build docker image'
37
+ task :build, [:name] => @layers.map { |name| "build:#{name}" }
38
+ end
39
+
40
+ def setup_entrypoint
41
+ root = Bankai::Docker::Generators::Base.default_source_root
42
+ template = ERB.new(::File.read("#{root}/entrypoint.erb"), nil, '-')
43
+ @entrypoint.write template.result
44
+ @entrypoint.rewind
45
+ path = Rails.root.join('bin', 'docker-entrypoint')
46
+ FileUtils.cp(@entrypoint.path, path)
47
+ FileUtils.chmod 'u+x', path
48
+ end
49
+
50
+ def setup_dockerfile
51
+ @dockerfile.write File.print
52
+ @dockerfile.rewind
53
+ end
54
+
55
+ def setup_dockerignore
56
+ # TODO: Provide DSL to generate it
57
+ ignore_file = Rails.root.join('.dockerignore')
58
+ return if ignore_file.exist?
59
+
60
+ @dockerignore.write IGNORES.join("\n")
61
+ @dockerignore.rewind
62
+ FileUtils.cp(@dockerignore.path, ignore_file)
63
+ @dockerignore_generated = true
64
+ end
65
+
66
+ def clear_up
67
+ entrypoint = Rails.root.join('bin', 'docker-entrypoint')
68
+ entrypoint.unlink if entrypoint.exist?
69
+
70
+ ignore_file = Rails.root.join('.dockerignore')
71
+ return unless ignore_file.exist?
72
+ return unless @dockerignore_generated
73
+
74
+ ignore_file.unlink
75
+ end
76
+
77
+ private
78
+
79
+ def cache_options
80
+ @cache_options = @layers.map do |target|
81
+ "--cache-from #{@name}:#{target}"
82
+ end.join(' ')
83
+ end
84
+
85
+ def install_build_tasks
86
+ @layers.each do |target|
87
+ stage = File.stages[target]
88
+ task target, :name do |_, args|
89
+ File.instance.update_name(args[:name])
90
+ @name ||= File.name
91
+ sh command(stage)
92
+ end
93
+ end
94
+ end
95
+
96
+ def command(stage)
97
+ [
98
+ 'docker build',
99
+ cache_options,
100
+ stage.main? ? '' : "--target #{stage.name}",
101
+ "-t #{stage.tag}",
102
+ '-f - .',
103
+ "< #{@dockerfile.path}"
104
+ ].join(' ')
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ # The docker command
6
+ class Command
7
+ def initialize(type, *args, options: {}, mode: nil)
8
+ @type = type.to_s.upcase
9
+ @args = args
10
+ @options = options
11
+ @mode = mode
12
+ end
13
+
14
+ def to_s
15
+ "#{@type} #{options} #{args}"
16
+ end
17
+
18
+ # TODO: Prevent use getter as helper method name
19
+ def args
20
+ return @args.to_s if @mode == :array
21
+
22
+ @args.join(' ')
23
+ end
24
+
25
+ def options
26
+ @options.map do |key, value|
27
+ "--#{key}=#{value}"
28
+ end.compact.join(' ')
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'dsl/argument'
4
+ require_relative 'dsl/name'
5
+ require_relative 'dsl/commands'
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ module DSL
6
+ # Argument Support
7
+ module Argument
8
+ def argument(name, default = nil)
9
+ @arguments ||= {}
10
+ name = sanitize_argument(name.to_s)
11
+ @arguments[name] = default
12
+ end
13
+
14
+ private
15
+
16
+ def sanitize_argument(name)
17
+ name = name.dup
18
+ name.gsub!(/\s/, '_')
19
+ name.gsub!(/[^\w]/, '')
20
+ name.upcase!
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ module DSL
6
+ # General Command
7
+ module Commands
8
+ def run(*commands)
9
+ command 'run', *commands
10
+ end
11
+
12
+ def workdir(dir)
13
+ command 'workdir', dir
14
+ end
15
+
16
+ def env(env)
17
+ command 'env', env
18
+ end
19
+
20
+ def add(from, to)
21
+ command 'add', from, to
22
+ end
23
+
24
+ def copy(source, destination, from: nil)
25
+ command 'copy', source, destination, options: { from: from }
26
+ end
27
+
28
+ def expose(*ports)
29
+ ports.each do |port|
30
+ command 'expose', port
31
+ end
32
+ end
33
+
34
+ def entrypoint(*params)
35
+ command 'entrypoint', *params, mode: :array
36
+ end
37
+
38
+ def cmd(*params)
39
+ command 'cmd', *params, mode: :array
40
+ end
41
+
42
+ def volume(*volumes)
43
+ command 'volume', *volumes, mode: :array
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ module DSL
6
+ # Argument Support
7
+ module Name
8
+ def name(new_name = nil)
9
+ return @name if new_name.nil?
10
+
11
+ @name = new_name
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+
5
+ module Bankai
6
+ module Docker
7
+ # Docker File generator
8
+ class File
9
+ class << self
10
+ def setup(&block)
11
+ instance.reset
12
+ instance.setup(&block)
13
+ end
14
+
15
+ def name
16
+ instance.name
17
+ end
18
+
19
+ def stages
20
+ instance.stages
21
+ end
22
+
23
+ def default
24
+ root = Bankai::Docker::Generators::Base.default_source_root
25
+ "#{root}/default.rb"
26
+ end
27
+
28
+ def print
29
+ instance.to_s
30
+ end
31
+ end
32
+
33
+ include Singleton
34
+ include DSL::Name
35
+ include DSL::Argument
36
+
37
+ attr_reader :stages
38
+
39
+ def initialize
40
+ reset
41
+ end
42
+
43
+ def setup(&block)
44
+ instance_eval(&block)
45
+ end
46
+
47
+ def reset
48
+ @name = nil
49
+ @stages = {}
50
+ @arguments = {}
51
+ end
52
+
53
+ def stage(name, from: nil, &block)
54
+ name = name.to_sym
55
+ @stages[name] = Stage.new(name, from: from, &block)
56
+ @stages = @stages.sort_by { |_, stage| stage.index }.to_h
57
+ end
58
+
59
+ def main(from: nil, &block)
60
+ @stages[:main] = Stage.new(:main, from: from, &block)
61
+ end
62
+
63
+ def update_name(default = nil)
64
+ app_name = Rails.app_class.parent_name.demodulize.underscore.dasherize
65
+ @name = @name || default || "#{`whoami`.chomp}/#{app_name}"
66
+ end
67
+
68
+ def to_s
69
+ root = Bankai::Docker::Generators::Base.default_source_root
70
+ template = ERB.new(::File.read("#{root}/dockerfile.erb"), nil, '-')
71
+ template.result(binding)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bankai/generators/base'
4
+
5
+ module Bankai
6
+ module Docker
7
+ module Generators
8
+ # :nodoc:
9
+ class Base < Bankai::Generators::Base
10
+ def self.default_source_root
11
+ ::File.expand_path(
12
+ ::File.join('..', '..', '..', '..', 'templates'),
13
+ __dir__
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'base'
4
+
5
+ module Bankai
6
+ module Docker
7
+ module Generators
8
+ # :nodoc:
9
+ class InstallGenerator < Base
10
+ def setup_config
11
+ template 'default.rb', 'config/docker.rb'
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/railtie'
4
+
5
+ module Bankai
6
+ module Docker
7
+ # :nodoc:
8
+ class Railtie < Rails::Railtie
9
+ rake_tasks do
10
+ load 'bankai/docker/tasks/all.rake'
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bankai
4
+ module Docker
5
+ # The multistage
6
+ class Stage
7
+ class << self
8
+ attr_reader :index
9
+
10
+ def next_index
11
+ @index ||= 0
12
+ @index += 1
13
+ end
14
+ end
15
+
16
+ include DSL::Name
17
+ include DSL::Argument
18
+ include DSL::Commands
19
+
20
+ attr_reader :name, :index
21
+
22
+ def initialize(name, from: nil, &block)
23
+ @name = name
24
+ @from = from || "ruby:#{::RUBY_VERSION}-alpine"
25
+ @index = main? ? 999 : Stage.next_index
26
+ @commands = []
27
+ @arguments = {}
28
+ instance_eval(&block)
29
+ end
30
+
31
+ def tag
32
+ return File.name if main?
33
+
34
+ "#{File.name}:#{@name}"
35
+ end
36
+
37
+ def main?
38
+ name == :main
39
+ end
40
+
41
+ def from(new_value = nil)
42
+ return @from if new_value.nil?
43
+
44
+ @from = new_value
45
+ end
46
+
47
+ def command(type, *arguments)
48
+ @commands << Command.new(type, *arguments)
49
+ end
50
+
51
+ def to_s
52
+ root = Bankai::Docker::Generators::Base.default_source_root
53
+ template = ERB.new(::File.read("#{root}/stage.erb"), nil, '-')
54
+ template.result(binding)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bankai/docker'
4
+ require 'bankai/docker/builder'
5
+
6
+ config = Rails.root.join('config', 'docker.rb')
7
+ if config.exist?
8
+ load config
9
+ else
10
+ load Bankai::Docker::File.default
11
+ end
12
+
13
+ namespace :docker do
14
+ load 'bankai/docker/tasks/build.rake'
15
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ Bankai::Docker::Builder.install_tasks
4
+
5
+ desc 'Preview Dockerfile'
6
+ task :preview do
7
+ puts Bankai::Docker::File.print
8
+ end
@@ -0,0 +1,5 @@
1
+ module Bankai
2
+ module Docker
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ # rubocop:disable Metrics/BlockLength
4
+ Bankai::Docker.setup do
5
+ stage :gem do
6
+ run 'apk add --no-cache build-base ca-certificates zlib-dev',
7
+ 'libressl-dev postgresql-dev'
8
+
9
+ run 'mkdir -p /src/app'
10
+
11
+ add 'Gemfile', '/src/app'
12
+ add 'Gemfile.lock', '/src/app'
13
+
14
+ workdir '/src/app'
15
+ run 'bundle install --deployment --without development test ' \
16
+ '--no-cache --clean && rm -rf vendor/bundle/ruby/**/cache'
17
+ end
18
+
19
+ stage :node, from: 'node:10.15.2-alpine' do
20
+ run 'mv /opt/yarn-v${YARN_VERSION} /opt/yarn'
21
+ end
22
+
23
+ main do
24
+ # Requirements
25
+ # node - libstdc++
26
+ # yarn - git
27
+ run 'apk add --no-cache tzdata postgresql-libs libstdc++ libcurl git'
28
+ run 'mkdir -p /src/app'
29
+
30
+ copy '/usr/local/bin/node', '/usr/local/bin/', from: :node
31
+ copy '/opt/yarn', '/opt/yarn', from: :node
32
+
33
+ env 'PATH=/opt/yarn/bin:/src/app/bin:$PATH'
34
+
35
+ add '.', '/src/app'
36
+
37
+ copy '/src/app/vendor/bundle', '/src/app/vendor/bundle', from: :gem
38
+ copy '/usr/local/bundle/config', '/usr/local/bundle/config', from: :gem
39
+
40
+ workdir '/src/app'
41
+
42
+ env 'RAILS_ENV=production'
43
+ run 'rails app:update:bin && rm -rf /src/app/tmp'
44
+
45
+ expose 80
46
+
47
+ entrypoint './bin/docker-entrypoint'
48
+ cmd 'serve'
49
+ end
50
+ end
51
+ # rubocop:enable Metrics/BlockLength
@@ -0,0 +1,7 @@
1
+ <%- @arguments.each do |name, value| -%>
2
+ ARG <%= name %><%= value.nil? ? '' : "=#{value}" %>
3
+ <% end -%>
4
+
5
+ <%- @stages.each do |_, stage| -%>
6
+ <%= stage %>
7
+ <% end -%>
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ command = ARGV.first
5
+ case command
6
+ when 'serve'
7
+ system('rake assets:precompile') unless ENV['RAILS_SERVE_STATIC_FILES'].nil?
8
+ exec('bundle exec puma -p 80')
9
+ else
10
+ exec(ARGV.join(' '))
11
+ end
@@ -0,0 +1,7 @@
1
+ FROM <%= @from %><%= @name == :main ? '' : " AS #{@name}" %>
2
+ <% @arguments.each do |name, value| -%>
3
+ ARG <%= name %><%= value.nil? ? '' : "=#{value}" %>
4
+ <% end %>
5
+ <%- @commands.each do |command| -%>
6
+ <%= command %>
7
+ <% end -%>
metadata ADDED
@@ -0,0 +1,188 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bankai-docker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - 5xRuby
8
+ - 蒼時弦也
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2019-03-12 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.17'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.17'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler-audit
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: overcommit
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rubocop
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - "~>"
89
+ - !ruby/object:Gem::Version
90
+ version: 0.60.0
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - "~>"
96
+ - !ruby/object:Gem::Version
97
+ version: 0.60.0
98
+ - !ruby/object:Gem::Dependency
99
+ name: simplecov
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: bankai
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :runtime
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: The easist docker template for Rails
127
+ email:
128
+ - hi@5xruby.tw
129
+ - contact0@frost.tw
130
+ executables: []
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".gitignore"
135
+ - ".overcommit.yml"
136
+ - ".rspec"
137
+ - ".travis.yml"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - README.md
142
+ - Rakefile
143
+ - bankai-docker.gemspec
144
+ - bin/console
145
+ - bin/setup
146
+ - lib/bankai/docker.rb
147
+ - lib/bankai/docker/builder.rb
148
+ - lib/bankai/docker/command.rb
149
+ - lib/bankai/docker/dsl.rb
150
+ - lib/bankai/docker/dsl/argument.rb
151
+ - lib/bankai/docker/dsl/commands.rb
152
+ - lib/bankai/docker/dsl/name.rb
153
+ - lib/bankai/docker/file.rb
154
+ - lib/bankai/docker/generators/base.rb
155
+ - lib/bankai/docker/generators/install.rb
156
+ - lib/bankai/docker/railtie.rb
157
+ - lib/bankai/docker/stage.rb
158
+ - lib/bankai/docker/tasks/all.rake
159
+ - lib/bankai/docker/tasks/build.rake
160
+ - lib/bankai/docker/version.rb
161
+ - templates/default.rb
162
+ - templates/dockerfile.erb
163
+ - templates/entrypoint.erb
164
+ - templates/stage.erb
165
+ homepage: https://github.com/5xRuby/bankai-docker
166
+ licenses: []
167
+ metadata: {}
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ requirements: []
183
+ rubyforge_project:
184
+ rubygems_version: 2.7.6
185
+ signing_key:
186
+ specification_version: 4
187
+ summary: The easist docker template for Rails
188
+ test_files: []