From fae8160d0dee2884be991c4669639f80419a284a Mon Sep 17 00:00:00 2001 From: ganmacs Date: Sun, 12 Jul 2015 13:16:16 +0900 Subject: [PATCH] Add options to pre_action to avoid raise error in file edit action when dry-run --- lib/itamae/resource/base.rb | 5 ++--- lib/itamae/resource/directory.rb | 3 +-- lib/itamae/resource/execute.rb | 3 +-- lib/itamae/resource/file.rb | 15 +++++++++------ lib/itamae/resource/gem_package.rb | 3 +-- lib/itamae/resource/git.rb | 3 +-- lib/itamae/resource/link.rb | 2 +- lib/itamae/resource/package.rb | 3 +-- lib/itamae/resource/remote_directory.rb | 2 +- lib/itamae/resource/service.rb | 3 +-- lib/itamae/resource/template.rb | 3 +-- lib/itamae/resource/user.rb | 2 +- 12 files changed, 21 insertions(+), 26 deletions(-) diff --git a/lib/itamae/resource/base.rb b/lib/itamae/resource/base.rb index 20a5a66a..215260af 100644 --- a/lib/itamae/resource/base.rb +++ b/lib/itamae/resource/base.rb @@ -176,7 +176,7 @@ def run_action(action, options) Logger.formatter.with_indent_if(Logger.debug?) do Logger.debug "(in pre_action)" - pre_action + pre_action(options) Logger.debug "(in set_current_attributes)" set_current_attributes @@ -203,7 +203,7 @@ def clear_current_attributes @current_attributes = Hashie::Mash.new end - def pre_action + def pre_action(options = {}) # do nothing end @@ -360,4 +360,3 @@ def verify end end end - diff --git a/lib/itamae/resource/directory.rb b/lib/itamae/resource/directory.rb index d428fa17..5ed03eb2 100644 --- a/lib/itamae/resource/directory.rb +++ b/lib/itamae/resource/directory.rb @@ -9,7 +9,7 @@ class Directory < Base define_attribute :owner, type: String define_attribute :group, type: String - def pre_action + def pre_action(options = {}) case @current_action when :create attributes.exist = true @@ -60,4 +60,3 @@ def action_delete(options) end end end - diff --git a/lib/itamae/resource/execute.rb b/lib/itamae/resource/execute.rb index e617c01e..2ff0efee 100644 --- a/lib/itamae/resource/execute.rb +++ b/lib/itamae/resource/execute.rb @@ -7,7 +7,7 @@ class Execute < Base define_attribute :command, type: String, default_name: true define_attribute :cwd, type: String - def pre_action + def pre_action(options = {}) case @current_action when :run attributes.executed = true @@ -25,4 +25,3 @@ def action_run(options) end end end - diff --git a/lib/itamae/resource/file.rb b/lib/itamae/resource/file.rb index 3d69f121..84f9b3dd 100644 --- a/lib/itamae/resource/file.rb +++ b/lib/itamae/resource/file.rb @@ -11,7 +11,7 @@ class File < Base define_attribute :group, type: String define_attribute :block, type: Proc, default: proc {} - def pre_action + def pre_action(options = {}) case @current_action when :create attributes.exist = true @@ -20,12 +20,16 @@ def pre_action when :edit attributes.exist = true - content = backend.receive_file(attributes.path) - attributes.block.call(content) - attributes.content = content + unless options[:dry_run] + content = backend.receive_file(attributes.path) + attributes.block.call(content) + attributes.content = content + end end - send_tempfile + unless options[:dry_run] + send_tempfile + end end def set_current_attributes @@ -145,4 +149,3 @@ def send_tempfile end end end - diff --git a/lib/itamae/resource/gem_package.rb b/lib/itamae/resource/gem_package.rb index 45adb7dd..1646f97d 100644 --- a/lib/itamae/resource/gem_package.rb +++ b/lib/itamae/resource/gem_package.rb @@ -9,7 +9,7 @@ class GemPackage < Base define_attribute :version, type: String define_attribute :source, type: String - def pre_action + def pre_action(options = {}) case @current_action when :install attributes.installed = true @@ -78,4 +78,3 @@ def install! end end end - diff --git a/lib/itamae/resource/git.rb b/lib/itamae/resource/git.rb index 5f5b3450..0b31b97d 100644 --- a/lib/itamae/resource/git.rb +++ b/lib/itamae/resource/git.rb @@ -11,7 +11,7 @@ class Git < Base define_attribute :revision, type: String define_attribute :recursive, default: false - def pre_action + def pre_action(options = {}) case @current_action when :sync attributes.exist = true @@ -81,4 +81,3 @@ def get_revision(branch) end end end - diff --git a/lib/itamae/resource/link.rb b/lib/itamae/resource/link.rb index 27206734..dc86caa9 100644 --- a/lib/itamae/resource/link.rb +++ b/lib/itamae/resource/link.rb @@ -8,7 +8,7 @@ class Link < Base define_attribute :to, type: String, required: true define_attribute :force, default: false - def pre_action + def pre_action(options = {}) case @current_action when :create attributes.exist = true diff --git a/lib/itamae/resource/package.rb b/lib/itamae/resource/package.rb index b828384e..7e359905 100644 --- a/lib/itamae/resource/package.rb +++ b/lib/itamae/resource/package.rb @@ -8,7 +8,7 @@ class Package < Base define_attribute :version, type: String define_attribute :options, type: String - def pre_action + def pre_action(options = {}) case @current_action when :install attributes.installed = true @@ -41,4 +41,3 @@ def action_remove(action_options) end end end - diff --git a/lib/itamae/resource/remote_directory.rb b/lib/itamae/resource/remote_directory.rb index 5c7d9a3a..b29a2802 100644 --- a/lib/itamae/resource/remote_directory.rb +++ b/lib/itamae/resource/remote_directory.rb @@ -10,7 +10,7 @@ class RemoteDirectory < Base define_attribute :owner, type: String define_attribute :group, type: String - def pre_action + def pre_action(options = {}) directory = ::File.expand_path(attributes.source, ::File.dirname(@recipe.path)) src = ::File.expand_path(directory, ::File.dirname(@recipe.path)) diff --git a/lib/itamae/resource/service.rb b/lib/itamae/resource/service.rb index 631ea423..f35c85de 100644 --- a/lib/itamae/resource/service.rb +++ b/lib/itamae/resource/service.rb @@ -12,7 +12,7 @@ def initialize(*args) @under = attributes.provider ? "_under_#{attributes.provider}" : "" end - def pre_action + def pre_action(options = {}) case @current_action when :start, :restart attributes.running = true @@ -66,4 +66,3 @@ def action_disable(options) end end end - diff --git a/lib/itamae/resource/template.rb b/lib/itamae/resource/template.rb index 9ddffdb4..c83d385e 100644 --- a/lib/itamae/resource/template.rb +++ b/lib/itamae/resource/template.rb @@ -7,7 +7,7 @@ module Resource class Template < RemoteFile define_attribute :variables, type: Hash, default: {} - def pre_action + def pre_action(options = {}) attributes.content = RenderContext.new(self).render_file(source_file) super @@ -50,4 +50,3 @@ def node end end end - diff --git a/lib/itamae/resource/user.rb b/lib/itamae/resource/user.rb index db4f25e3..8b0b9798 100644 --- a/lib/itamae/resource/user.rb +++ b/lib/itamae/resource/user.rb @@ -13,7 +13,7 @@ class User < Base define_attribute :shell, type: String define_attribute :create_home, type: [TrueClass, FalseClass], default: false - def pre_action + def pre_action(options = {}) case @current_action when :create attributes.exist = true