diff --git a/lib/itamae/recipe.rb b/lib/itamae/recipe.rb index 64d6178a..ddff83de 100644 --- a/lib/itamae/recipe.rb +++ b/lib/itamae/recipe.rb @@ -42,11 +42,12 @@ def find_recipe_in_gem(recipe) end end - def initialize(runner, path) + def initialize(runner, path, options = {}) @runner = runner @path = path @delayed_notifications = [] @children = RecipeChildren.new + @options = options end def dir @@ -54,7 +55,7 @@ def dir end def load(vars = {}) - context = EvalContext.new(self, vars) + context = EvalContext.new(self, vars, @options) context.instance_eval(File.read(path), path, 1) end @@ -62,20 +63,20 @@ def run(options = {}) show_banner Itamae.logger.with_indent do - @children.run(options) - run_delayed_notifications(options) + @children.run(@options) + run_delayed_notifications end end private - def run_delayed_notifications(options) + def run_delayed_notifications @delayed_notifications.uniq! do |notification| [notification.action, notification.action_resource] end while notification = @delayed_notifications.shift - notification.run(options) + notification.run(@options) end end @@ -84,8 +85,9 @@ def show_banner end class EvalContext - def initialize(recipe, vars) + def initialize(recipe, vars, options = {}) @recipe = recipe + @options = options vars.each do |k, v| define_singleton_method(k) { v } @@ -109,7 +111,7 @@ def method_missing(*args, &block) super end - resource = klass.new(@recipe, name, &block) + resource = klass.new(@recipe, name, @options, &block) @recipe.children << resource end @@ -133,7 +135,7 @@ def include_recipe(target) return end - recipe = Recipe.new(runner, path) + recipe = Recipe.new(runner, path, @options) @recipe.children << recipe recipe.load end @@ -155,7 +157,7 @@ class RecipeFromDefinition < Recipe attr_accessor :definition def load(vars = {}) - context = EvalContext.new(self, vars) + context = EvalContext.new(self, vars, @options) context.instance_eval(&@definition.class.definition_block) end diff --git a/lib/itamae/recipe_children.rb b/lib/itamae/recipe_children.rb index 947ab691..a39c46be 100644 --- a/lib/itamae/recipe_children.rb +++ b/lib/itamae/recipe_children.rb @@ -57,7 +57,7 @@ def run(options) self.each do |resource| case resource when Resource::Base - resource.run(nil, dry_run: options[:dry_run]) + resource.run when Recipe resource.run(options) end diff --git a/lib/itamae/resource/base.rb b/lib/itamae/resource/base.rb index 461387ff..482d77e7 100644 --- a/lib/itamae/resource/base.rb +++ b/lib/itamae/resource/base.rb @@ -101,10 +101,11 @@ def define_attribute(name, options) attr_reader :notifications attr_reader :updated - def initialize(recipe, resource_name, &block) + def initialize(recipe, resource_name, options = {}, &block) clear_current_attributes @recipe = recipe @resource_name = resource_name + @options = options @updated = false EvalContext.new(self).tap do |context| @@ -133,11 +134,11 @@ def run(specific_action = nil, options = {}) end [specific_action || attributes.action].flatten.each do |action| - run_action(action, options) + run_action(action) end - verify unless options[:dry_run] - notify(options) if updated? + verify unless @options[:dry_run] + notify if updated? end @updated = false @@ -166,7 +167,7 @@ def resource_type alias_method :current, :current_attributes - def run_action(action, options) + def run_action(action, options = {}) original_attributes = @attributes # preserve and restore later @current_action = action @@ -187,12 +188,12 @@ def run_action(action, options) show_differences method_name = "action_#{action}" - if options[:dry_run] + if @options[:dry_run] unless respond_to?(method_name) Itamae.logger.error "action #{action.inspect} is unavailable" end else - public_send(method_name, options) + public_send(method_name, @options) end updated! if different? @@ -327,7 +328,7 @@ def updated? @updated end - def notify(options) + def notify(options = {}) (notifications + recipe.children.subscribing(self)).each do |notification| message = "Notifying #{notification.action} to #{notification.action_resource.resource_type} resource '#{notification.action_resource.resource_name}'" @@ -346,7 +347,7 @@ def notify(options) if notification.delayed? @recipe.delayed_notifications << notification elsif notification.immediately? - notification.run(options) + notification.run(@options) end end end diff --git a/lib/itamae/resource/file.rb b/lib/itamae/resource/file.rb index 9809a1b2..51ebdc81 100644 --- a/lib/itamae/resource/file.rb +++ b/lib/itamae/resource/file.rb @@ -20,9 +20,11 @@ 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 @@ -163,4 +165,3 @@ def send_tempfile end end end - diff --git a/lib/itamae/runner.rb b/lib/itamae/runner.rb index 0793e4b9..f7b835f2 100644 --- a/lib/itamae/runner.rb +++ b/lib/itamae/runner.rb @@ -49,7 +49,7 @@ def load_recipes(paths) expanded_path = gem_path if gem_path end - recipe = Recipe.new(self, expanded_path) + recipe = Recipe.new(self, expanded_path, @options) children << recipe recipe.load end @@ -91,4 +91,3 @@ def create_node end end end - diff --git a/spec/unit/lib/itamae/resource/base_spec.rb b/spec/unit/lib/itamae/resource/base_spec.rb index 551282a7..ff246013 100644 --- a/spec/unit/lib/itamae/resource/base_spec.rb +++ b/spec/unit/lib/itamae/resource/base_spec.rb @@ -122,10 +122,12 @@ class TestResource < Itamae::Resource::Base end context 'with dry_run' do + subject(:dry_run_resource) { described_class.new(recipe, "name", dry_run: true) } + context 'when specified action is unavailable' do it 'logs error' do expect(Itamae.logger).to receive(:error).with(/action :name is unavailable/) - subject.run(nil, dry_run: true) + subject.run end end end