From b1183bfe3904b496a52363467d4a1d9fc13c9501 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sat, 25 May 2013 17:39:31 +0300
Subject: [PATCH 1/5] add poldek package manager support

---
 lib/chef/provider/package/poldek.rb | 92 +++++++++++++++++++++++++++++++++++++
 lib/chef/providers.rb               |  1 +
 lib/chef/resource/poldek_package.rb | 34 ++++++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 lib/chef/provider/package/poldek.rb
 create mode 100644 lib/chef/resource/poldek_package.rb

diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
new file mode 100644
index 0000000..2d53b2d
--- /dev/null
+++ b/lib/chef/provider/package/poldek.rb
@@ -0,0 +1,92 @@
+#
+# Author:: Elan Ruusamäe (glen@pld-linux.org)
+# Copyright:: Copyright (c) 2013 Elan Ruusamäe
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/provider/package'
+require 'chef/mixin/shell_out'
+require 'chef/resource/package'
+require 'chef/mixin/get_source_from_package'
+
+class Chef
+  class Provider
+    class Package
+      class Poldek < Chef::Provider::Package
+        include Chef::Mixin::ShellOut
+        attr_accessor :is_virtual_package
+
+        def load_current_resource
+            Chef::Log.debug("#{@new_resource} loading current resource")
+            @current_resource = Chef::Resource::Package.new(@new_resource.name)
+            @current_resource.package_name(@new_resource.package_name)
+            @current_resource.version(nil)
+            check_package_state(@new_resource.package_name)
+            @current_resource # modified by check_package_state
+        end
+
+        def check_package_state(name)
+            Chef::Log.debug("#{@new_resource} checking package #{name}")
+            installed = false
+            @current_resource.version(nil)
+
+            out = shell_out!("rpm -q #{name}", :env => nil, :returns => [0,1])
+            if out.stdout
+                Chef::Log.debug("rpm STDOUT: #{out.stdout}");
+                version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
+                if version
+                    @current_resource.version(version)
+                    installed = true
+                end
+            end
+
+            if !installed
+                out = shell_out!("poldek -q --uniq --skip-installed --cmd 'ls #{name}'", :env => nil, :returns => [0,1])
+                if out.stdout
+                    Chef::Log.debug("poldek STDOUT: #{out.stdout}");
+                    version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
+                    if version
+                        @candidate_version = version
+                    end
+                end
+            end
+
+            return installed
+        end
+
+        def install_package(name, version)
+            Chef::Log.debug("#{@new_resource} installing package #{name}-#{version}")
+            package = "#{name}-#{version}"
+            out = shell_out!("poldek --noask --up -u #{package}", :env => nil)
+        end
+
+        def upgrade_package(name, version)
+            Chef::Log.debug("#{@new_resource} upgrading package #{name}-#{version}")
+            install_package(name, version)
+        end
+
+        def remove_package(name, version)
+            Chef::Log.debug("#{@new_resource} removing package #{name}-#{version}")
+            package = "#{name}-#{version}"
+            out = shell_out!("poldek --noask -e #{package}", :env => nil)
+        end
+
+        def purge_package(name, version)
+            remove_package(name, version)
+        end
+      end
+    end
+  end
+end
diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
index aa14b92..c732022 100644
--- a/lib/chef/providers.rb
+++ b/lib/chef/providers.rb
@@ -58,6 +58,7 @@
 require 'chef/provider/package/macports'
 require 'chef/provider/package/pacman'
 require 'chef/provider/package/portage'
+require 'chef/provider/package/poldek'
 require 'chef/provider/package/rpm'
 require 'chef/provider/package/rubygems'
 require 'chef/provider/package/yum'
diff --git a/lib/chef/resource/poldek_package.rb b/lib/chef/resource/poldek_package.rb
new file mode 100644
index 0000000..cdc8f92
--- /dev/null
+++ b/lib/chef/resource/poldek_package.rb
@@ -0,0 +1,34 @@
+#
+# Author:: Elan Ruusamäe (glen@pld-linux.org)
+# Copyright:: Copyright (c) 2013 Elan Ruusamäe
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/resource/package'
+require 'chef/provider/package/poldek'
+
+class Chef
+  class Resource
+    class PoldekPackage < Chef::Resource::Package
+
+      def initialize(name, run_context=nil)
+        super
+        @resource_name = :poldek_package
+        @provider = Chef::Provider::Package::Poldek
+      end
+
+    end
+  end
+end
-- 
1.8.1.6


From 5b18f968a73a0cfbbae84428ddc9d6b7a171f6ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sun, 26 May 2013 13:49:56 +0300
Subject: [PATCH 2/5] pass options to poldek commands

---
 lib/chef/provider/package/poldek.rb | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
index 2d53b2d..9ea2d20 100644
--- a/lib/chef/provider/package/poldek.rb
+++ b/lib/chef/provider/package/poldek.rb
@@ -53,7 +53,7 @@ def check_package_state(name)
             end
 
             if !installed
-                out = shell_out!("poldek -q --uniq --skip-installed --cmd 'ls #{name}'", :env => nil, :returns => [0,1])
+                out = shell_out!("poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{name}'", :env => nil, :returns => [0,1])
                 if out.stdout
                     Chef::Log.debug("poldek STDOUT: #{out.stdout}");
                     version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
@@ -69,7 +69,7 @@ def check_package_state(name)
         def install_package(name, version)
             Chef::Log.debug("#{@new_resource} installing package #{name}-#{version}")
             package = "#{name}-#{version}"
-            out = shell_out!("poldek --noask --up -u #{package}", :env => nil)
+            out = shell_out!("poldek --noask --up #{expand_options(@new_resource.options)} -u #{package}", :env => nil)
         end
 
         def upgrade_package(name, version)
@@ -80,7 +80,7 @@ def upgrade_package(name, version)
         def remove_package(name, version)
             Chef::Log.debug("#{@new_resource} removing package #{name}-#{version}")
             package = "#{name}-#{version}"
-            out = shell_out!("poldek --noask -e #{package}", :env => nil)
+            out = shell_out!("poldek --noask #{expand_options(@new_resource.options)} -e #{package}", :env => nil)
         end
 
         def purge_package(name, version)
-- 
1.8.1.6


From f5b99716d14bc4129d666ccb3f44de74cef1fdeb Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sun, 26 May 2013 13:50:26 +0300
Subject: [PATCH 3/5] poldek ls returns 255 if package is not found

---
 lib/chef/provider/package/poldek.rb | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
index 9ea2d20..bf3c850 100644
--- a/lib/chef/provider/package/poldek.rb
+++ b/lib/chef/provider/package/poldek.rb
@@ -53,7 +53,7 @@ def check_package_state(name)
             end
 
             if !installed
-                out = shell_out!("poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{name}'", :env => nil, :returns => [0,1])
+                out = shell_out!("poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{name}'", :env => nil, :returns => [0,1,255])
                 if out.stdout
                     Chef::Log.debug("poldek STDOUT: #{out.stdout}");
                     version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
-- 
1.8.1.6


From 6419a0dcc09dc41892d74fa6f9318ae8f41c238c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sun, 26 May 2013 21:15:04 +0300
Subject: [PATCH 4/5] run package indexes sync once (not often) per chef run

---
 lib/chef/provider/package/poldek.rb | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
index bf3c850..e868908 100644
--- a/lib/chef/provider/package/poldek.rb
+++ b/lib/chef/provider/package/poldek.rb
@@ -69,7 +69,8 @@ def check_package_state(name)
         def install_package(name, version)
             Chef::Log.debug("#{@new_resource} installing package #{name}-#{version}")
             package = "#{name}-#{version}"
-            out = shell_out!("poldek --noask --up #{expand_options(@new_resource.options)} -u #{package}", :env => nil)
+            update_indexes
+            out = shell_out!("poldek --noask #{expand_options(@new_resource.options)} -u #{package}", :env => nil)
         end
 
         def upgrade_package(name, version)
@@ -86,6 +87,17 @@ def remove_package(name, version)
         def purge_package(name, version)
             remove_package(name, version)
         end
+
+        @@updated = false
+        private
+        def update_indexes()
+            if @@updated
+                return
+            end
+            Chef::Log.debug("#{@new_resource} updating package indexes")
+            shell_out!("poldek --up #{expand_options(@new_resource.options)}", :env => nil)
+            @@updated = true
+        end
       end
     end
   end
-- 
1.8.1.6


From bccaf2747e9d68bbb6b04f08d2a4b28900dcaa25 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sat, 13 Jul 2013 15:10:27 +0300
Subject: [PATCH 5/5] add candidate_version method

this helps for package action => upgrade to see new versions
---
 lib/chef/provider/package/poldek.rb | 38 +++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/lib/chef/provider/package/poldek.rb b/lib/chef/provider/package/poldek.rb
index e868908..efba524 100644
--- a/lib/chef/provider/package/poldek.rb
+++ b/lib/chef/provider/package/poldek.rb
@@ -33,16 +33,17 @@ def load_current_resource
             @current_resource = Chef::Resource::Package.new(@new_resource.name)
             @current_resource.package_name(@new_resource.package_name)
             @current_resource.version(nil)
-            check_package_state(@new_resource.package_name)
+            check_package_state
             @current_resource # modified by check_package_state
         end
 
-        def check_package_state(name)
-            Chef::Log.debug("#{@new_resource} checking package #{name}")
+        def check_package_state()
+            Chef::Log.debug("#{@new_resource} checking package #{@new_resource.package_name}")
+
             installed = false
             @current_resource.version(nil)
 
-            out = shell_out!("rpm -q #{name}", :env => nil, :returns => [0,1])
+            out = shell_out!("rpm -q #{@new_resource.package_name}", :env => nil, :returns => [0,1])
             if out.stdout
                 Chef::Log.debug("rpm STDOUT: #{out.stdout}");
                 version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
@@ -52,18 +53,27 @@ def check_package_state(name)
                 end
             end
 
-            if !installed
-                out = shell_out!("poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{name}'", :env => nil, :returns => [0,1,255])
-                if out.stdout
-                    Chef::Log.debug("poldek STDOUT: #{out.stdout}");
-                    version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
-                    if version
-                        @candidate_version = version
-                    end
+            return installed
+        end
+
+        def candidate_version
+            Chef::Log.debug("poldek check candidate version for #{@new_resource.package_name}");
+            return @candidate_version if @candidate_version
+
+            update_indexes
+            cmd = "poldek -q --uniq --skip-installed #{expand_options(@new_resource.options)} --cmd 'ls #{@new_resource.package_name}'"
+            out = shell_out!(cmd, :env => nil, :returns => [0,1,255])
+            if out.stdout
+                Chef::Log.debug("poldek STDOUT: #{out.stdout}");
+                version = out.stdout[/^#{@new_resource.package_name}-(.+)/, 1]
+                if version
+                    @candidate_version = version
                 end
             end
-
-            return installed
+            unless @candidate_version
+                raise Chef::Exceptions::Package, "poldek does not have a version of package #{@new_resource.package_name}"
+            end
+            @candidate_version
         end
 
         def install_package(name, version)
-- 
1.8.1.6

