lib/generators/dockerfile_generator.rb in dockerfile-rails-1.0.17 vs lib/generators/dockerfile_generator.rb in dockerfile-rails-1.0.18

- old
+ new

@@ -9,34 +9,42 @@ 'cache' => false, 'ci' => false, 'compose' => false, 'fullstaq' => false, 'jemalloc' => false, + 'label' => {}, 'mysql' => false, + 'nginx' => false, 'parallel' => false, 'platform' => nil, 'postgresql' => false, 'precompile' => nil, 'prepare' => true, 'redis' => false, + 'root' => false, 'swap' => nil, 'yjit' => false, - 'label' => {}, - 'nginx' => false, ) @@labels = {} + @@packages = {"base" => [], "build" => [], "deploy" => []} # load defaults from config file if File.exist? 'config/dockerfile.yml' options = YAML.safe_load(IO.read('config/dockerfile.yml'), symbolize_names: true)[:options] if options OPTION_DEFAULTS.to_h.each do |option, value| OPTION_DEFAULTS[option] = options[option] if options.include? option end + if options[:packages] + options[:packages].each do |stage, list| + @@packages[stage.to_s] = list + end + end + @@labels = options[:label].stringify_keys if options.include? :label end end class_option :ci, type: :boolean, default: OPTION_DEFAULTS.ci, @@ -91,10 +99,31 @@ desc: 'Add Docker label(s)' class_option :nginx, type: :boolean, default: OPTION_DEFAULTS.nginx, desc: 'Serve static files with nginx' + class_option :root, type: :boolean, default: OPTION_DEFAULTS.root, + desc: 'Run application as root user' + + class_option 'add-base', type: :array, default: [], + desc: 'additional packages to install for both build and deploy' + + class_option 'add-build', type: :array, default: [], + desc: 'additional packages to install for use during build' + + class_option 'add-deploy', aliases: '--add', type: :array, default: [], + desc: 'additional packages to install for deployment' + + class_option 'remove-base', type: :array, default: [], + desc: 'remove from list of base packages' + + class_option 'remove-build', type: :array, default: [], + desc: 'remove from list of build packages' + + class_option 'remove-deploy', aliases: '--remove', type: :array, default: [], + desc: 'remove from list of deploy packages' + def generate_app source_paths.push File.expand_path('./templates', __dir__) # merge options options.label.replace(@@labels.merge(options.label).select {|key, value| value != ''}) @@ -103,10 +132,20 @@ @dockerfile_config = OPTION_DEFAULTS.dup.to_h.stringify_keys options.to_h.each do |option, value| @dockerfile_config[option] = value if @dockerfile_config.include? option end + # apply requested package changes + %w(base build deploy).each do |phase| + @@packages[phase] += options["add-#{phase}"] + @@packages[phase] -= options["remove-#{phase}"] + @@packages[phase].uniq! + @@packages.delete phase if @@packages[phase].empty? + end + + @dockerfile_config['packages'] = @@packages + scan_rails_app Bundler.with_original_env { install_gems } template 'Dockerfile.erb', 'Dockerfile' @@ -167,10 +206,14 @@ else "" end end + def run_as_root? + true # options.root? || options.nginx? + end + def using_node? return @using_node if @using_node != nil @using_node = File.exist? 'package.json' end @@ -213,10 +256,11 @@ end end def base_packages packages = [] + packages += @@packages['base'] if @@packages['base'] if using_execjs? packages += %w(curl unzip) end @@ -246,10 +290,11 @@ end def build_packages # start with the essentials packages = %w(build-essential) + packages += @@packages['build'] if @@packages['build'] # add databases: sqlite3, postgres, mysql packages << 'pkg-config' if options.sqlite3? or @sqlite3 packages << 'libpq-dev' if options.postgresql? or @postgresql packages << 'default-libmysqlclient-dev' if options.mysql? or @mysql @@ -296,9 +341,10 @@ packages.sort.uniq end def deploy_packages packages = [] + packages += @@packages['deploy'] if @@packages['deploy'] # start with databases: sqlite3, postgres, mysql packages << 'libsqlite3-0' if options.sqlite3? or @sqlite3 packages << 'postgresql-client' if options.postgresql? or @postgresql packages << 'default-mysql-client' if options.mysql or @mysql