Merge pull request #131 from mbornoz/apt-preferences
authorHunter Haugen <h.haugen@gmail.com>
Tue, 2 Jul 2013 00:46:24 +0000 (17:46 -0700)
committerHunter Haugen <h.haugen@gmail.com>
Tue, 2 Jul 2013 00:46:24 +0000 (17:46 -0700)
apt::pin: handling all apt preferences properties

README.md
manifests/pin.pp
spec/defines/pin_spec.rb
templates/pin.pref.erb

index f9f7ee8321a501a3d0f51f7b1a88f42bcda97291..402d7ab021982244b61a07ab923405c536418cd3 100644 (file)
--- a/README.md
+++ b/README.md
@@ -101,6 +101,16 @@ Adds an apt pin for a certain release.
     apt::pin { 'karmic-updates': priority => 700 }
     apt::pin { 'karmic-security': priority => 700 }
 
+Note you can also specifying more complex pins using distribution properties.
+
+    apt::pin { 'stable':
+      priority        => -10,
+      originator      => 'Debian',
+      release_version => '3.0',
+      component       => 'main',
+      label           => 'Debian'
+    }
+
 ###apt::ppa
 
 Adds a ppa repository using `add-apt-repository`.
index 21cc3ffdefc0294c0c91c37de474971b94b26869..39de3d8f1f688bbf1f79b5903c094558923efb07 100644 (file)
@@ -2,15 +2,19 @@
 # pin a release in apt, useful for unstable repositories
 
 define apt::pin(
-  $ensure     = present,
-  $explanation = "${::caller_module_name}: ${name}",
-  $order      = '',
-  $packages   = '*',
-  $priority   = 0,
-  $release    = '',
-  $origin     = '',
-  $originator = '',
-  $version    = ''
+  $ensure          = present,
+  $explanation     = "${::caller_module_name}: ${name}",
+  $order           = '',
+  $packages        = '*',
+  $priority        = 0,
+  $release         = '', # a=
+  $origin          = '',
+  $version         = '',
+  $codename        = '', # n=
+  $release_version = '', # v=
+  $component       = '', # c=
+  $originator      = '', # o=
+  $label           = ''  # l=
 ) {
 
   include apt::params
@@ -21,16 +25,37 @@ define apt::pin(
     fail('Only integers are allowed in the apt::pin order param')
   }
 
-  if $release != '' {
-    $pin = "release a=${release}"
-  } elsif $origin != '' {
-    $pin = "origin \"${origin}\""
-  } elsif $originator != '' {
-    $pin = "release o=${originator}"
-  } elsif $version != '' {
-    $pin = "version ${version}"
-  } else {
-    $pin = "release a=${name}"
+  $pin_release_array = [
+    $release,
+    $codename,
+    $release_version,
+    $component,
+    $originator,
+    $label] 
+  $pin_release = join($pin_release_array, '')
+
+  # Read the manpage 'apt_preferences(5)', especially the chapter
+  # 'Thea Effect of APT Preferences' to understand the following logic
+  # and the difference between specific and general form
+  if $packages != '*' { # specific form
+
+    if ( $pin_release != '' and ( $origin != '' or $version != '' )) or
+      ( $origin != '' and ( $pin_release != '' or $version != '' )) or
+      ( $version != '' and ( $pin_release != '' or $origin != '' )) {
+      fail('parameters release, origin, and version are mutually exclusive')
+    }
+
+  } else { # general form
+
+    if $version != '' {
+      fail('parameter version cannot be used in general form')
+    }
+
+    if ( $pin_release != '' and $origin != '' ) or
+      ( $origin != '' and $pin_release != '' ) {
+      fail('parmeters release and origin are mutually exclusive')
+    }
+
   }
 
   $path = $order ? {
index 3aaf49cef494e79f3b4fce0c2e2e02ba91d7915d..a4cb1e26e3ad74f0ef67a58b52af2f9119278aa9 100644 (file)
@@ -12,34 +12,77 @@ describe 'apt::pin', :type => :define do
     }
   end
 
-  [ {},
+  [ 
+    { :params  => {},
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: *\nPin: release a=my_pin\nPin-Priority: 0\n"
+    },
     {
-      :packages  => 'apache',
-      :priority  => '1'
+      :params => {
+        :packages => 'apache', 
+        :priority => '1'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: release a=my_pin\nPin-Priority: 1\n"
     },
     {
-      :order     => 50,
-      :packages  => 'apache',
-      :priority  => '1'
+      :params => {
+        :order    => 50, 
+        :packages => 'apache', 
+        :priority => '1'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: release a=my_pin\nPin-Priority: 1\n"
     },
     {
-      :ensure    => 'absent',
-      :packages  => 'apache',
-      :priority  => '1'
+      :params => {
+        :ensure   => 'absent',
+        :packages => 'apache',
+        :priority => '1'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: release a=my_pin\nPin-Priority: 1\n"
     },
     {
-      :packages  => 'apache',
-      :priority  => '1',
-      :release   => 'my_newpin'
-    }
+      :params => {
+        :packages => 'apache',
+        :priority => '1',
+        :release  => 'my_newpin'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: release a=my_newpin\nPin-Priority: 1\n"
+    },
+    {
+      :params => {
+        :packages => 'apache',
+        :priority => '1',
+        :version  => '2.2.16*'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: version 2.2.16*\nPin-Priority: 1\n"
+    },
+    {
+      :params => {
+        :priority => '1',
+        :origin   => 'ftp.de.debian.org'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: *\nPin: origin \"ftp.de.debian.org\"\nPin-Priority: 1\n"
+    },
+    {
+      :params => {
+        :packages        => 'apache',
+        :priority        => '1',  
+        :release         => 'stable',
+        :codename        => 'wheezy',
+        :release_version => '3.0',
+        :component       => 'main',
+        :originator      => 'Debian',
+        :label           => 'Debian'
+      },
+      :content => "# my_pin\nExplanation: : my_pin\nPackage: apache\nPin: release a=stable, n=wheezy, v=3.0, c=main, o=Debian, l=Debian\nPin-Priority: 1\n"
+    },
   ].each do |param_set|
     describe "when #{param_set == {} ? "using default" : "specifying"} define parameters" do
       let :param_hash do
-        default_params.merge(param_set)
+        default_params.merge(param_set[:params])
       end
 
       let :params do
-        param_set
+        param_set[:params]
       end
 
       it { should include_class("apt::params") }
@@ -50,7 +93,7 @@ describe 'apt::pin', :type => :define do
           'owner'   => 'root',
           'group'   => 'root',
           'mode'    => '0644',
-          'content' => "# #{title}\nExplanation: : #{title}\nPackage: #{param_hash[:packages]}\nPin: release a=#{param_hash[:release] || title}\nPin-Priority: #{param_hash[:priority]}\n",
+          'content' => param_set[:content],
         })
       }
     end
index 74df8b79c45b54b36400e7d942da2f67df6c9e4d..62c44c72414b41551ad8334c468ec70ba8ccc80f 100644 (file)
@@ -1,3 +1,20 @@
+<%-
+@pin = "release a=#{@name}" # default value
+if @pin_release.length > 0
+  options = []
+  options.push("a=#{@release}") if @release.length > 0
+  options.push("n=#{@codename}") if @codename.length > 0
+  options.push("v=#{@release_version}") if @release_version.length > 0
+  options.push("c=#{@component}") if @component.length > 0
+  options.push("o=#{@originator}") if @originator.length > 0
+  options.push("l=#{@label}") if @label.length > 0
+  @pin = "release #{options.join(', ')}"
+elsif @version.length > 0
+  @pin = "version #{@version}"
+elsif @origin.length > 0
+  @pin = "origin \"#{@origin}\""
+end
+-%>
 # <%= @name %>
 Explanation: <%= @explanation %>
 Package: <%= @packages %>