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] 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

