This way it is not possible for the puppet device application to modify the current host it is running on, as only resource compatible with remote network device can be applied (all the other will be skipped). The only resource type appliable on both host and network device is "schedule".
Signed-off-by: Brice Figureau <[email protected]> --- lib/puppet/application/device.rb | 2 +- lib/puppet/resource/catalog.rb | 1 + lib/puppet/transaction.rb | 4 ++- lib/puppet/type.rb | 28 +++++++++++++++++ lib/puppet/type/interface.rb | 2 + lib/puppet/type/schedule.rb | 2 + lib/puppet/type/vlan.rb | 2 + spec/integration/transaction_spec.rb | 56 ++++++++++++++++++++++++++++++++++ spec/unit/resource/catalog_spec.rb | 1 + spec/unit/transaction_spec.rb | 18 +++++++++++ spec/unit/type/interface_spec.rb | 5 +++ spec/unit/type/schedule_spec.rb | 8 +++++ spec/unit/type/vlan_spec.rb | 5 +++ 13 files changed, 132 insertions(+), 2 deletions(-) diff --git a/lib/puppet/application/device.rb b/lib/puppet/application/device.rb index 9dc6b26..df5bac2 100644 --- a/lib/puppet/application/device.rb +++ b/lib/puppet/application/device.rb @@ -188,7 +188,7 @@ Licensed under the Apache 2.0 License require 'puppet/configurer' configurer = Puppet::Configurer.new - report = configurer.run + report = configurer.run(:network_device => true) rescue => detail puts detail.backtrace if Puppet[:trace] Puppet.err detail.to_s diff --git a/lib/puppet/resource/catalog.rb b/lib/puppet/resource/catalog.rb index 98c2965..a6cff9b 100644 --- a/lib/puppet/resource/catalog.rb +++ b/lib/puppet/resource/catalog.rb @@ -133,6 +133,7 @@ class Puppet::Resource::Catalog < Puppet::SimpleGraph transaction.report = options[:report] if options[:report] transaction.tags = options[:tags] if options[:tags] transaction.ignoreschedules = true if options[:ignoreschedules] + transaction.for_network_device = options[:network_device] transaction.add_times :config_retrieval => self.retrieval_duration || 0 diff --git a/lib/puppet/transaction.rb b/lib/puppet/transaction.rb index d7845fb..3728a2f 100644 --- a/lib/puppet/transaction.rb +++ b/lib/puppet/transaction.rb @@ -12,7 +12,7 @@ class Puppet::Transaction require 'puppet/transaction/resource_harness' require 'puppet/resource/status' - attr_accessor :component, :catalog, :ignoreschedules + attr_accessor :component, :catalog, :ignoreschedules, :for_network_device attr_accessor :configurator # The report, once generated. @@ -339,6 +339,8 @@ class Puppet::Transaction resource.warning "Skipping because of failed dependencies" elsif resource.virtual? resource.debug "Skipping because virtual" + elsif resource.appliable_to_device? ^ for_network_device + resource.debug "Skipping #{resource.appliable_to_device? ? 'device' : 'host'} resources because running on a #{for_network_device ? 'device' : 'host'}" else return false end diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index c0e5d39..656b8f2 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -116,6 +116,26 @@ class Type ens end + def self.apply_to_device + @apply_to = :device + end + + def self.apply_to_host + @apply_to = :host + end + + def self.apply_to_all + @apply_to = :both + end + + def self.apply_to + @apply_to ||= :host + end + + def self.can_apply_to(target) + [ target == :device ? :device : :host, :both ].include?(apply_to) + end + # Deal with any options passed into parameters. def self.handle_param_options(name, options) # If it's a boolean parameter, create a method to test the value easily @@ -1895,6 +1915,14 @@ class Type def virtual?; !!@virtual; end def exported?; !!@exported; end + + def appliable_to_device? + self.class.can_apply_to(:device) + end + + def appliable_to_host? + self.class.can_apply_to(:host) + end end end diff --git a/lib/puppet/type/interface.rb b/lib/puppet/type/interface.rb index 7560a05..d3b5cb0 100644 --- a/lib/puppet/type/interface.rb +++ b/lib/puppet/type/interface.rb @@ -10,6 +10,8 @@ Puppet::Type.newtype(:interface) do interface mode (access or trunking, native vlan and encapsulation), switchport characteristics (speed, duplex)." + apply_to_device + ensurable do defaultvalues diff --git a/lib/puppet/type/schedule.rb b/lib/puppet/type/schedule.rb index 5fb008f..f60f96f 100755 --- a/lib/puppet/type/schedule.rb +++ b/lib/puppet/type/schedule.rb @@ -43,6 +43,8 @@ module Puppet This will cause resources to be applied every 30 minutes by default. " + apply_to_all + newparam(:name) do desc "The name of the schedule. This name is used to retrieve the schedule when assigning it to an object: diff --git a/lib/puppet/type/vlan.rb b/lib/puppet/type/vlan.rb index 6708ea4..e39daf9 100644 --- a/lib/puppet/type/vlan.rb +++ b/lib/puppet/type/vlan.rb @@ -5,6 +5,8 @@ Puppet::Type.newtype(:vlan) do @doc = "This represents a router or switch vlan." + apply_to_device + ensurable newparam(:name) do diff --git a/spec/integration/transaction_spec.rb b/spec/integration/transaction_spec.rb index 41a1eba..78d62fc 100755 --- a/spec/integration/transaction_spec.rb +++ b/spec/integration/transaction_spec.rb @@ -75,6 +75,62 @@ describe Puppet::Transaction do transaction.evaluate end + it "should not apply device resources on normal host" do + catalog = Puppet::Resource::Catalog.new + resource = Puppet::Type.type(:interface).new :name => "FastEthernet 0/1" + catalog.add_resource resource + + transaction = Puppet::Transaction.new(catalog) + transaction.for_network_device = false + + transaction.expects(:apply).never.with(resource, nil) + + transaction.evaluate + transaction.resource_status(resource).should be_skipped + end + + it "should not apply host resources on device" do + catalog = Puppet::Resource::Catalog.new + resource = Puppet::Type.type(:file).new :path => "/foo/bar", :backup => false + catalog.add_resource resource + + transaction = Puppet::Transaction.new(catalog) + transaction.for_network_device = true + + transaction.expects(:apply).never.with(resource, nil) + + transaction.evaluate + transaction.resource_status(resource).should be_skipped + end + + it "should apply device resources on device" do + catalog = Puppet::Resource::Catalog.new + resource = Puppet::Type.type(:interface).new :name => "FastEthernet 0/1" + catalog.add_resource resource + + transaction = Puppet::Transaction.new(catalog) + transaction.for_network_device = true + + transaction.expects(:apply).with(resource, nil) + + transaction.evaluate + transaction.resource_status(resource).should_not be_skipped + end + + it "should apply resources appliable on host and device on a device" do + catalog = Puppet::Resource::Catalog.new + resource = Puppet::Type.type(:schedule).new :name => "test" + catalog.add_resource resource + + transaction = Puppet::Transaction.new(catalog) + transaction.for_network_device = true + + transaction.expects(:apply).with(resource, nil) + + transaction.evaluate + transaction.resource_status(resource).should_not be_skipped + end + # Verify that one component requiring another causes the contained # resources in the requiring component to get refreshed. it "should propagate events from a contained resource through its container to its dependent container's contained resources" do diff --git a/spec/unit/resource/catalog_spec.rb b/spec/unit/resource/catalog_spec.rb index ae65aa9..ebf523e 100755 --- a/spec/unit/resource/catalog_spec.rb +++ b/spec/unit/resource/catalog_spec.rb @@ -592,6 +592,7 @@ describe Puppet::Resource::Catalog, "when compiling" do Puppet::Transaction.stubs(:new).returns(@transaction) @transaction.stubs(:evaluate) @transaction.stubs(:add_times) + @transaction.stubs(:for_network_device=) Puppet.settings.stubs(:use) end diff --git a/spec/unit/transaction_spec.rb b/spec/unit/transaction_spec.rb index 4157e58..d7788c0 100755 --- a/spec/unit/transaction_spec.rb +++ b/spec/unit/transaction_spec.rb @@ -270,6 +270,24 @@ describe Puppet::Transaction do @resource.stubs(:virtual?).returns true @transaction.should be_skip(@resource) end + + it "should skip device only resouce on normal host" do + @resource.stubs(:appliable_to_device?).returns true + @transaction.for_network_device = false + @transaction.should be_skip(@resource) + end + + it "should not skip device only resouce on remote device" do + @resource.stubs(:appliable_to_device?).returns true + @transaction.for_network_device = true + @transaction.should_not be_skip(@resource) + end + + it "should skip host resouce on device" do + @resource.stubs(:appliable_to_device?).returns false + @transaction.for_network_device = true + @transaction.should be_skip(@resource) + end end describe "when determining if tags are missing" do diff --git a/spec/unit/type/interface_spec.rb b/spec/unit/type/interface_spec.rb index 68f4c76..12ba225 100755 --- a/spec/unit/type/interface_spec.rb +++ b/spec/unit/type/interface_spec.rb @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Type.type(:interface) do + it "should have a 'name' parameter'" do Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1")[:name].should == "FastEthernet 0/1" end @@ -15,6 +16,10 @@ describe Puppet::Type.type(:interface) do Puppet::Type.type(:interface).attrtype(:ensure).should == :property end + it "should be applied on device" do + Puppet::Type.type(:interface).new(:name => "FastEthernet 0/1").should be_appliable_to_device + end + [:description, :speed, :duplex, :native_vlan, :encapsulation, :mode, :allowed_trunk_vlans, :etherchannel, :ipaddress].each do |p| it "should have a #{p} property" do Puppet::Type.type(:interface).attrtype(p).should == :property diff --git a/spec/unit/type/schedule_spec.rb b/spec/unit/type/schedule_spec.rb index 7599411..33064ca 100755 --- a/spec/unit/type/schedule_spec.rb +++ b/spec/unit/type/schedule_spec.rb @@ -36,6 +36,14 @@ describe Puppet::Type.type(:schedule) do describe Puppet::Type.type(:schedule) do include ScheduleTesting + it "should apply to device" do + @schedule.should be_appliable_to_device + end + + it "should apply to host" do + @schedule.should be_appliable_to_host + end + it "should default to :distance for period-matching" do @schedule[:periodmatch].should == :distance end diff --git a/spec/unit/type/vlan_spec.rb b/spec/unit/type/vlan_spec.rb index 2983a58..3bee14b 100755 --- a/spec/unit/type/vlan_spec.rb +++ b/spec/unit/type/vlan_spec.rb @@ -3,6 +3,7 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe Puppet::Type.type(:vlan) do + it "should have a 'name' parameter'" do Puppet::Type.type(:vlan).new(:name => "200")[:name].should == "200" end @@ -11,6 +12,10 @@ describe Puppet::Type.type(:vlan) do Puppet::Type.type(:vlan).new(:name => "200", :device_url => :device)[:device_url].should == :device end + it "should be applied on device" do + Puppet::Type.type(:vlan).new(:name => "200").should be_appliable_to_device + end + it "should have an ensure property" do Puppet::Type.type(:vlan).attrtype(:ensure).should == :property end -- 1.7.2.1 -- You received this message because you are subscribed to the Google Groups "Puppet Developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.
