From c33dddfa87d81859c4b01e84947e2bebcec36f0d Mon Sep 17 00:00:00 2001 From: Sam Ruby Date: Sat, 22 Jul 2023 09:45:38 -0400 Subject: [PATCH] update fly.toml (if it exists) with swap instead of docker entrypoint --- lib/generators/dockerfile_generator.rb | 25 ++++++++++++++++++- .../templates/docker-entrypoint.erb | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/generators/dockerfile_generator.rb b/lib/generators/dockerfile_generator.rb index 0501fec..9ee1d5c 100644 --- a/lib/generators/dockerfile_generator.rb +++ b/lib/generators/dockerfile_generator.rb @@ -286,7 +286,7 @@ def generate_app fly_attach_consul end - if File.exist?("fly.toml") && (fly_processes || !options.prepare) + if File.exist?("fly.toml") && (fly_processes || !options.prepare || options.swap) if File.stat("fly.toml").size > 0 template "fly.toml.erb", "fly.toml" else @@ -1086,6 +1086,29 @@ def fly_make_toml end end + if options.swap + suffixes = { + "kib" => 1024, + "k" => 1024, + "kb" => 1000, + "mib" => 1048576, + "m" => 1048576, + "mb" => 100000, + "gib" => 1073741824, + "g" => 1073741824, + "gb" => 100000000, + } + + pattern = Regexp.new("^(\\d+)(#{suffixes.keys.join('|')})?$", "i") + + match = pattern.match(options.swap) + + if match + size = ((match[1].to_i * (suffixes[match[2]] || 1)) / 1048576.0).round + toml += "swap_size_mb = #{size}" + end + end + toml end end diff --git a/lib/generators/templates/docker-entrypoint.erb b/lib/generators/templates/docker-entrypoint.erb index 720b021..6f1b34c 100755 --- a/lib/generators/templates/docker-entrypoint.erb +++ b/lib/generators/templates/docker-entrypoint.erb @@ -1,6 +1,6 @@ #!/bin/bash -e -<% if options.swap -%> +<% if options.swap && !File.exist?("fly.toml")-%> <% if run_as_root? or using_passenger? -%> <% @space = "" -%> <% else -%>