dockerfile-rails 0.4.8 → 0.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9b601d4f4e8ba2476ebdc5a721aa605f605b54b36cd2ebda0d8959035ed6621
4
- data.tar.gz: 8f87d53ade0657596d6b0915c1523b477d213684019dfb66fd48949a86568186
3
+ metadata.gz: 906eaaf2f4166356a6cece9b347dd58a29324a0e7a3d0f557cfea85054c34816
4
+ data.tar.gz: 0c071eb1fd4da339676916705498c18fb2d5e08e0115b8f47745def8229390e3
5
5
  SHA512:
6
- metadata.gz: cfe3dc014ee68c023c65c8dfe1716ba9d05ac6266bf0b1ef478f73968c974be78303d9b108ef91e475cf81fd9398185d46ee4b13ebb9435ca15357db31a71c8a
7
- data.tar.gz: 25124c64efc2e59bdd00e608c72758ea6996655051f15cf85242b99311c0d04f700d98c13d936d5e752516eaf58f777964c5e113b0203606a7f0a535e6082ccf
6
+ metadata.gz: 66b27141d048276513a4384b6dd88fae87f929d2128a9e6a19187cac6a1d75f751d5e261ccd4f94424ae7ae53ca0713141db34163d04cb997a719731a6e39dde
7
+ data.tar.gz: 7e927389954e205c3cc89e6de75506d85265ac394a749f0b394d9b5263b4de719a5dc935d8ff493f1c1cfdc30854b70f7f5b77f6c48599095a7132f0f050a0cd
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ## Overview
2
2
 
3
- Provides Rails generators to produce Dockerfiles and related files. This is being proposed as the generator to be included in Rails 7.1, and a substantial number of pull requests along those lines have already been merged. This repository contains fixes and features beyond those pull requests. Highlights:
3
+ Provides a Rails generator to produce Dockerfiles and related files. This is being proposed as the generator to be included in Rails 7.1, and a substantial number of pull requests along those lines have already been merged. This repository contains fixes and features beyond those pull requests. Highlights:
4
4
 
5
5
  * Supports all [Rails supported releases](https://guides.rubyonrails.org/maintenance_policy.html), not just Rails 7.1, and likely works with a number of previous releases.
6
6
  * Can be customized using flags on the `generate dockerfile` command, and rerun to produce a custom tailored dockerfile based on detecting the actual features used by your application.
@@ -17,6 +17,8 @@ General options:
17
17
 
18
18
  * `--force` - overwrite existing files
19
19
  * `--ci` - include test gems in deployed image
20
+ * `--bin-cd` - adjust binstubs to set current working directory
21
+ * `--no-prepare` - omit `db:prepare`. Useful for cloud platforms with [release](https://devcenter.heroku.com/articles/release-phase) phases.
20
22
  * `--platform=s` - specify target platform. See [FROM](https://docs.docker.com/engine/reference/builder/#from) for details.
21
23
  * `--cache` - use build caching to speed up builds
22
24
  * `--parallel` - use multi-stage builds to install gems and node modules in parallel
@@ -33,13 +35,15 @@ additional support may be needed:
33
35
  * `--redis` - add redis libraries
34
36
  * `--sqlite3` - add sqlite3 libraries
35
37
 
36
- Optimizations:
38
+ Runtime Optimizations:
37
39
 
38
40
  * `--fullstaq` - use [fullstaq](https://fullstaqruby.org/) [images](https://github.com/evilmartians/fullstaq-ruby-docker) on [quay.io](https://quay.io/repository/evl.ms/fullstaq-ruby?tab=tags&tag=latest)
39
41
  * `--jemalloc` - use [jemalloc](https://jemalloc.net/) memory allocator
40
42
  * `--yjit` - enable [YJIT](https://github.com/ruby/ruby/blob/master/doc/yjit/yjit.md) optimizing compiler
41
43
  * `--swap=n` - allocate swap space. See [falloc options](https://man7.org/linux/man-pages/man1/fallocate.1.html#OPTIONS) for suffixes
42
44
 
45
+ Options are saved between runs into `config/dockerfile.yml`. To invert a boolean options, add or remove a `no-` prefix from the option name.
46
+
43
47
  ## Testing
44
48
 
45
49
  The current testing strategy is to run `rails new` and `generate dockerfile` with various configurations and compare the generated artifacts with expected results. `ARG` values in `Dockerfiles` are masked before comparison.
@@ -55,9 +59,14 @@ To assis with this process, outputs of tests can be captured automatically. Thi
55
59
 
56
60
  ```
57
61
  rake test:capture
58
- TEST_CAPTURE=1 ruby test/test_minimal.rb
59
62
  ```
60
63
 
64
+ If you are running a single test, the following environment variables settings may be helpful:
65
+
66
+ * `RAILS_ENV=TEST` will match the environment used to produce the captured outputs.
67
+ * `TEST_CAPTURE=1` will capture test results.
68
+ * `TEST_KEEP=1` will leave the test app behind for inspection after the test completes.
69
+
61
70
  ## Links
62
71
 
63
72
  Many of the following links relate to the current development status with respect to Rails 7.1 and will be removed once that is resolved.
@@ -4,48 +4,86 @@ require_relative '../dockerfile-rails/scanner.rb'
4
4
  class DockerfileGenerator < Rails::Generators::Base
5
5
  include DockerfileRails::Scanner
6
6
 
7
- class_option :ci, type: :boolean, default: false,
7
+ OPTION_DEFAULTS = OpenStruct.new(
8
+ 'bin-cd' => false,
9
+ 'cache' => false,
10
+ 'ci' => false,
11
+ 'compose' => false,
12
+ 'fullstaq' => false,
13
+ 'jemalloc' => false,
14
+ 'mysql' => false,
15
+ 'parallel' => false,
16
+ 'platform' => nil,
17
+ 'prepare' => true,
18
+ 'redis' => false,
19
+ 'swap' => nil,
20
+ 'yjit' => false,
21
+ )
22
+
23
+ # load defaults from config file
24
+ if File.exist? 'config/dockerfile.yml'
25
+ options = YAML.safe_load_file('config/dockerfile.yml', symbolize_names: true)[:options]
26
+ if options
27
+ OPTION_DEFAULTS.to_h.each do |option, value|
28
+ OPTION_DEFAULTS[option] = options[option] if options.include? option
29
+ end
30
+ end
31
+ end
32
+
33
+ class_option :ci, type: :boolean, default: OPTION_DEFAULTS.ci,
8
34
  desc: 'include test gems in bundle'
9
35
 
10
- class_option :cache, type: :boolean, default: false,
36
+ class_option 'bin-cd', type: :boolean, default: OPTION_DEFAULTS['bin-cd'],
37
+ desc: 'modify binstubs to set working directory'
38
+
39
+ class_option :cache, type: :boolean, default: OPTION_DEFAULTS.cache,
11
40
  desc: 'use build cache to speed up installs'
12
41
 
13
- class_option :parallel, type: :boolean, default: false,
42
+ class_option :prepare, type: :boolean, default: OPTION_DEFAULTS.prepare,
43
+ desc: 'include db:prepare step'
44
+
45
+ class_option :parallel, type: :boolean, default: OPTION_DEFAULTS.parallel,
14
46
  desc: 'use build stages to install gems and node modules in parallel'
15
47
 
16
- class_option :swap, type: :string, default: nil,
48
+ class_option :swap, type: :string, default: OPTION_DEFAULTS.swap,
17
49
  desc: 'allocate swapspace'
18
50
 
19
- class_option :compose, type: :boolean, default: false,
51
+ class_option :compose, type: :boolean, default: OPTION_DEFAULTS.compose,
20
52
  desc: 'generate a docker-compose.yml file'
21
53
 
22
- class_option :redis, type: :boolean, default: false,
54
+ class_option :redis, type: :boolean, default: OPTION_DEFAULTS.redis,
23
55
  desc: 'include redis libraries'
24
56
 
25
- class_option :sqlite3, aliases: '--sqlite', type: :boolean, default: false,
57
+ class_option :sqlite3, aliases: '--sqlite', type: :boolean, default: OPTION_DEFAULTS.sqlite3,
26
58
  desc: 'include sqlite3 libraries'
27
59
 
28
- class_option :postgresql, aliases: '--postgres', type: :boolean, default: false,
60
+ class_option :postgresql, aliases: '--postgres', type: :boolean, default: OPTION_DEFAULTS.postgresql,
29
61
  desc: 'include postgresql libraries'
30
62
 
31
- class_option :mysql, type: :boolean, default: false,
63
+ class_option :mysql, type: :boolean, default: OPTION_DEFAULTS.mysql,
32
64
  desc: 'include mysql libraries'
33
65
 
34
- class_option :platform, type: :string, default: nil,
66
+ class_option :platform, type: :string, default: OPTION_DEFAULTS.platform,
35
67
  desc: 'image platform (example: linux/arm64)'
36
68
 
37
- class_option :jemalloc, type: :boolean, default: false,
69
+ class_option :jemalloc, type: :boolean, default: OPTION_DEFAULTS.jemalloc,
38
70
  desc: 'use jemalloc alternative malloc implementation'
39
71
 
40
- class_option :fullstaq, type: :boolean, default: false,
72
+ class_option :fullstaq, type: :boolean, default: OPTION_DEFAULTS.fullstaq,
41
73
  descr: 'use Fullstaq Ruby image from Quay.io'
42
74
 
43
- class_option :yjit, type: :boolean, default: false,
75
+ class_option :yjit, type: :boolean, default: OPTION_DEFAULTS.yjit,
44
76
  desc: 'enable YJIT optimizing compiler'
45
77
 
46
78
  def generate_app
47
79
  source_paths.push File.expand_path('./templates', __dir__)
48
80
 
81
+ # gather up options for config file
82
+ @dockerfile_config = OPTION_DEFAULTS.dup.to_h.stringify_keys
83
+ options.to_h.each do |option, value|
84
+ @dockerfile_config[option] = value if @dockerfile_config.include? option
85
+ end
86
+
49
87
  scan_rails_app
50
88
 
51
89
  template 'Dockerfile.erb', 'Dockerfile'
@@ -57,6 +95,8 @@ class DockerfileGenerator < Rails::Generators::Base
57
95
  chmod "bin/docker-entrypoint", 0755 & ~File.umask, verbose: false
58
96
 
59
97
  template 'docker-compose.yml.erb', 'docker-compose.yml' if options.compose
98
+
99
+ template 'dockerfile.yml.erb', 'config/dockerfile.yml'
60
100
  end
61
101
 
62
102
  private
@@ -210,6 +250,11 @@ private
210
250
  binfixups.unshift "chmod +x bin/*"
211
251
  end
212
252
 
253
+ # optionally, adjust cwd
254
+ if options['bin-cd']
255
+ binfixups.push %{sed -i '/^#!/aDir.chdir File.expand_path("..", __dir__)' /app/bin/*}
256
+ end
257
+
213
258
  binfixups
214
259
  end
215
260
 
@@ -19,7 +19,7 @@ WORKDIR /rails
19
19
  # Set production environment
20
20
  ENV RAILS_ENV="production" \
21
21
  BUNDLE_PATH="vendor/bundle" \
22
- BUNDLE_WITHOUT="<%= options.ci? ? 'test' : 'development:test' %>"
22
+ BUNDLE_WITHOUT="<%= options.ci? ? 'development' : 'development:test' %>"
23
23
 
24
24
  # Update gems and preinstall the desired version of bundler
25
25
  ARG BUNDLER_VERSION=<%= Bundler::VERSION %>
@@ -93,7 +93,11 @@ RUN bundle exec bootsnap precompile app/ lib/
93
93
 
94
94
  <% end -%>
95
95
  <% unless binfile_fixups.empty? -%>
96
- # Adjust binfiles to be executable on Linux
96
+ <% if options['bin-cd'] and binfile_fixups.length == 1 -%>
97
+ # Adjust binfiles to set current working directory
98
+ <% else -%>
99
+ # Adjust binfiles to be executable on Linux<%= options['bin-cd'] ? ' and set current working directory' : '' %>
100
+ <% end -%>
97
101
  <%= "RUN " + binfile_fixups.join(" && \\\n ") %>
98
102
 
99
103
  <% end -%>
@@ -133,7 +137,11 @@ ENV RAILS_LOG_TO_STDOUT="1" \
133
137
  <% end -%>
134
138
  MALLOC_CONF="dirty_decay_ms:1000,narenas:2,background_thread:true"<% end %>
135
139
 
140
+ <% if options.prepare -%>
136
141
  # Entrypoint prepares the database.
142
+ <% else -%>
143
+ # Entrypoint sets up the container.
144
+ <% end -%>
137
145
  ENTRYPOINT ["/rails/bin/docker-entrypoint"]
138
146
 
139
147
  # Start the server by default, this can be overwritten at runtime
@@ -10,9 +10,14 @@ swapon /swapfile
10
10
  echo 1 > /proc/sys/vm/overcommit_memory
11
11
 
12
12
  <% end -%>
13
+ <% if options.prepare -%>
13
14
  # If running the rails server then create or migrate existing database
14
15
  if [ "${*}" == "./bin/rails server" ]; then
15
16
  ./bin/rails <%= dbprep_command %>
16
17
  fi
17
18
 
19
+ <% elsif !options.swap -%>
20
+ # Add any container initialization steps here
21
+
22
+ <% end -%>
18
23
  exec "${@}"
@@ -0,0 +1,3 @@
1
+ # generated by dockerfile-rails
2
+
3
+ <%= YAML.dump('options' => @dockerfile_config) %>
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dockerfile-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.8
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Ruby
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-22 00:00:00.000000000 Z
11
+ date: 2023-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -44,6 +44,7 @@ files:
44
44
  - lib/generators/templates/_npm_install.erb
45
45
  - lib/generators/templates/docker-compose.yml.erb
46
46
  - lib/generators/templates/docker-entrypoint.erb
47
+ - lib/generators/templates/dockerfile.yml.erb
47
48
  - lib/generators/templates/dockerignore.erb
48
49
  - lib/generators/templates/node-version.erb
49
50
  homepage: https://github.com/rubys/dockerfile-rails