From 277838ec2c72d56e8c1b4917468150eed006e8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=92=BC=E6=99=82=E5=BC=A6=E4=B9=9F?= Date: Thu, 22 Dec 2022 23:05:19 +0800 Subject: [PATCH] Add customize command support --- lib/boxing/config.rb | 3 +- lib/boxing/context.rb | 26 ++++++++++++++++ spec/boxing/context_spec.rb | 60 +++++++++++++++++++++++++++++++++++++ templates/Dockerfile.tt | 12 ++------ 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/lib/boxing/config.rb b/lib/boxing/config.rb index e861571..8b0abfa 100644 --- a/lib/boxing/config.rb +++ b/lib/boxing/config.rb @@ -13,7 +13,8 @@ class Config :health_check, :health_check_path, :assets_precompile, :node_version, :build_packages, :runtime_packages, - :revision, :sentry_release + :revision, :sentry_release, + :entrypoint, :command # @since 0.5.0 def initialize(&block) diff --git a/lib/boxing/context.rb b/lib/boxing/context.rb index 2df0915..218627e 100644 --- a/lib/boxing/context.rb +++ b/lib/boxing/context.rb @@ -115,5 +115,31 @@ def mode_of(name) mode |= Package::RUNTIME if config.runtime_packages&.include?(name) mode end + + # Return entrypoint options + # + # @return [Array] + # + # @since 0.9.0 + def entrypoint + return config.entrypoint.map(&:to_s) if config.entrypoint + return ['bin/openbox'] if has?('openbox') + return ['bin/rails'] if has?('rails') + + %w[bundle exec] + end + + # Return command options + # + # @return [Array] + # + # @since 0.9.0 + def command + return config.command.map(&:to_s) if config.command + return ['server'] if has?('openbox') + return ['server', '-b', '0.0.0.0'] if has?('rails') + + ['rackup', '-o', '0.0.0.0'] + end end end diff --git a/spec/boxing/context_spec.rb b/spec/boxing/context_spec.rb index 3411c76..2bbc2a4 100644 --- a/spec/boxing/context_spec.rb +++ b/spec/boxing/context_spec.rb @@ -70,4 +70,64 @@ it { is_expected.to be_truthy } end end + + describe '#entrypoint' do + subject { context.entrypoint } + + it { is_expected.to include('bundle', 'exec') } + + context 'when customized entrypoint' do + before do + config.entrypoint = ['bin/rackup'] + end + + it { is_expected.to include('bin/rackup') } + end + + context 'when openbox exists' do + let(:context) do + described_class.new(config, database, [instance_double('Bundler::Dependency', name: 'openbox', git: nil)]) + end + + it { is_expected.to include('bin/openbox') } + end + + context 'when rails exists' do + let(:context) do + described_class.new(config, database, [instance_double('Bundler::Dependency', name: 'rails', git: nil)]) + end + + it { is_expected.to include('bin/rails') } + end + end + + describe '#command' do + subject { context.command } + + it { is_expected.to include('rackup', '-o', '0.0.0.0') } + + context 'when customized command' do + before do + config.command = ['console'] + end + + it { is_expected.to include('console') } + end + + context 'when openbox exists' do + let(:context) do + described_class.new(config, database, [instance_double('Bundler::Dependency', name: 'openbox', git: nil)]) + end + + it { is_expected.to include('server') } + end + + context 'when rails exists' do + let(:context) do + described_class.new(config, database, [instance_double('Bundler::Dependency', name: 'rails', git: nil)]) + end + + it { is_expected.to include('server', '-b', '0.0.0.0') } + end + end end diff --git a/templates/Dockerfile.tt b/templates/Dockerfile.tt index f0c3dce..60fe8db 100644 --- a/templates/Dockerfile.tt +++ b/templates/Dockerfile.tt @@ -107,13 +107,5 @@ EXPOSE <%= config.port %> <%- if has?('liveness') || config.health_check -%> HEALTHCHECK CMD curl -f http://localhost:<%= config.port %><%= config.health_check_path %> || exit 1 <%- end -%> -<%- if has?('openbox') -%> -ENTRYPOINT ["bin/openbox"] -CMD ["server"] -<%- elsif has?('rails') -%> -ENTRYPOINT ["bin/rails"] -CMD ["server", "-b", "0.0.0.0"] -<%- else -%> -ENTRYPOINT ["bundle", "exec"] -CMD ["rackup", "-o", "0.0.0.0"] -<%- end -%> +ENTRYPOINT <%= entrypoint %> +CMD <%= command %>