diff options
author | Toon Claes <toon@gitlab.com> | 2019-05-09 14:33:51 +0300 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2019-07-01 14:40:21 +0300 |
commit | 9a502f63bc9427e75d2e5316f9a66ba2c9c86534 (patch) | |
tree | 3d4a91ab1a6bb575d81f6cf2713b91eea35fe3b2 | |
parent | da0e12c3932be805f66cbf62f0260b39285beacd (diff) |
Safe render ERB files
Avoid overwriting generated files blindly, and instead show the diff
with the changes and ask the user what to do. Just like `bin/safe-sed`
was introduced in:
https://gitlab.com/gitlab-org/gitlab-development-kit/merge_requests/628
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | Rakefile | 13 | ||||
-rw-r--r-- | gdk-defaults.yml.erb | 2 | ||||
-rw-r--r-- | lib/gdk.rb | 2 | ||||
-rw-r--r-- | lib/gdk/env.rb | 2 | ||||
-rw-r--r-- | lib/gdk/erb_renderer.rb | 54 |
6 files changed, 63 insertions, 12 deletions
@@ -325,7 +325,7 @@ support-setup: .ruby-version foreman Procfile redis gitaly-setup jaeger-setup po gdk.yml: touch $@ -Procfile: gdk.yml gdk-defaults.yml.erb auto_devops_enabled auto_devops_gitlab_port auto_devops_registry_port +Procfile: Procfile.erb gdk.yml gdk-defaults.yml.erb auto_devops_enabled auto_devops_gitlab_port auto_devops_registry_port rake $@ redis: redis/redis.conf @@ -2,23 +2,16 @@ $LOAD_PATH.unshift('.') -require 'lib/gdk/config' +require 'lib/gdk' def config @config ||= GDK::Config.new end -def render_erb(source, target) - str = File.read(source) - result = ERB.new(str).result - - IO.write(target, result) -end - file 'Procfile' => ['Procfile.erb', GDK::Config::FILE, GDK::Defaults::FILE] do |t| - render_erb(t.source, t.name) + GDK::ErbRenderer.new(t.source, t.name).safe_render! end file 'nginx/conf/nginx.conf' => ['nginx/conf/nginx.conf.erb', GDK::Config::FILE, GDK::Defaults::FILE] do |t| - render_erb(t.source, t.name) + GDK::ErbRenderer.new(t.source, t.name).safe_render! end diff --git a/gdk-defaults.yml.erb b/gdk-defaults.yml.erb index de6f0c95f0b..dc881bc4b3e 100644 --- a/gdk-defaults.yml.erb +++ b/gdk-defaults.yml.erb @@ -39,7 +39,7 @@ auto_devops: gitlab: port: <%= read_or_write!('auto_devops_gitlab_port', rand(20000..24999)) %> registry: - port: <%= config.auto_devops.gitlab.port + 5000 %> + port: <%= config.auto_devops.gitlab.port.to_i + 5000 %> geo: enabled: false diff --git a/lib/gdk.rb b/lib/gdk.rb index 2d71e24bbcb..7275c0c40c2 100644 --- a/lib/gdk.rb +++ b/lib/gdk.rb @@ -4,6 +4,8 @@ # part of the gitlab-development-kit gem so that we can iterate faster. require_relative 'gdk/env' +require_relative 'gdk/config' +require_relative 'gdk/erb_renderer' module GDK PROGNAME = 'gdk'.freeze diff --git a/lib/gdk/env.rb b/lib/gdk/env.rb index 8a14d52b3d8..0e2a44eaa32 100644 --- a/lib/gdk/env.rb +++ b/lib/gdk/env.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'pathname' require 'shellwords' diff --git a/lib/gdk/erb_renderer.rb b/lib/gdk/erb_renderer.rb new file mode 100644 index 00000000000..d9ebdadfc82 --- /dev/null +++ b/lib/gdk/erb_renderer.rb @@ -0,0 +1,54 @@ +# frozen_string_literal: true + +require 'erb' +require 'tempfile' + +module GDK + class ErbRenderer + attr_reader :source, :target + + def initialize(source, target) + @source = source + @target = target + end + + def render!(target = @target) + str = File.read(source) + result = ERB.new(str).result + + File.write(target, result) + end + + def safe_render! + temp_file = Tempfile.new(target) + + render!(temp_file.path) + + return File.rename(temp_file.path, target) unless File.exist?(target) + + warn!(temp_file) unless FileUtils.identical?(target, temp_file.path) + ensure + temp_file.close! + end + + private + + def warn!(temp_file) + diff = `git --no-pager diff --no-index --color -u "#{target}" "#{temp_file.path}"` + + puts <<~EOF + ------------------------------------------------------------------------------------------------------------- + Warning: Your `#{target}` is outdated. Below are the changes GDK wanted to apply. + - To automatically update: `rm #{target}` + and re-run `gdk update`. + - To silence this warning (at your own peril): `touch #{target}` + ------------------------------------------------------------------------------------------------------------- + #{diff} + ------------------------------------------------------------------------------------------------------------- + Waiting 5 seconds for previous warning to be noticed...." + ------------------------------------------------------------------------------------------------------------- + EOF + sleep 5 + end + end +end |