From 398a1ea03514df38087ffc01bb934bd5e0e1f503 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Fri, 10 May 2013 15:53:49 +0300
Subject: [PATCH 1/4] add poldek package manager, based on pacman

---
 library/packaging/poldek | 145 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 145 insertions(+)
 create mode 100644 library/packaging/poldek

diff --git a/library/packaging/poldek b/library/packaging/poldek
new file mode 100644
index 0000000..a4a33a8
--- /dev/null
+++ b/library/packaging/poldek
@@ -0,0 +1,145 @@
+#!/usr/bin/python -tt
+# -*- coding: utf-8 -*-
+
+# (c) 2013, Elan Ruusamäe <glen@pld-linux.org>
+#
+# This file is part of Ansible
+#
+# Ansible is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Ansible is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Ansible.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+
+import traceback
+import os
+
+DOCUMENTATION = '''
+---
+module: poldek
+short_description: Manages packages with the I(poldek) package manager
+description:
+     - Will install, upgrade, remove, and list packages with the I(poldek) package manager.
+version_added: "1.1"
+options:
+  name:
+    description:
+      - package name, or package specifier with version, like C(name-1.0).
+    required: true
+    default: null
+    aliases: []
+  state:
+    description:
+      - whether to install (C(present), C(latest)), or remove (C(absent)) a package.
+    required: false
+    choices: [ "present", "latest", "absent" ]
+    default: "present"
+  update_cache:
+    description:
+      - update the package database first
+    required: false
+    default: "no"
+    choices: [ "yes", "no" ]
+
+examples:
+   - code: poldek name=apache state=installed
+   - code: poldek name=apache state=absent
+   - code: poldek name=apache state=installed, update_cache=yes"
+      description: update the package database and install apache (apache will be the updated if a newer version exists) 
+notes: []
+# informational: requirements for nodes
+requirements: [ poldek , rpm ]
+author: Elan Ruusamäe
+'''
+
+RPM_PATH = "/bin/rpm"
+POLDEK_PATH = "/usr/bin/poldek"
+
+def query_package(module, name, state="installed"):
+    # rpm -q returns 0 if the package is installed,
+    # 1 if it is not installed
+    if state == "installed":
+        rc = os.system("%s -q %s" % (RPM_PATH, name))
+        if rc == 0:
+            return True
+
+        return False
+
+def update_package_db(module):
+    rc = os.system("%s --up -q" % POLDEK_PATH)
+    if rc != 0:
+        module.fail_json(msg="could not update package db")
+
+def remove_packages(module, packages):
+    remove_c = 0
+    # Using a for loop in case of error, we can report the package that failed
+    for package in packages:
+        # Query the package first, to see if we even need to remove
+        if not query_package(module, package):
+            continue
+
+        rc = os.system("%s -e %s --noask > /dev/null" % (POLDEK_PATH, package))
+        if rc != 0:
+            module.fail_json(msg="failed to remove %s" % (package))
+
+        remove_c += 1
+
+    if remove_c > 0:
+        module.exit_json(changed=True, msg="removed %s package(s)" % remove_c)
+
+    module.exit_json(changed=False, msg="package(s) already absent")
+
+def install_packages(module, packages):
+    install_c = 0
+    for package in packages:
+        if query_package(module, package):
+            continue
+
+        rc = os.system("%s -u %s --noask > /dev/null" % (POLDEK_PATH, package))
+        if rc != 0:
+            module.fail_json(msg="failed to install %s" % (package))
+
+        install_c += 1
+
+    if install_c > 0:
+        module.exit_json(changed=True, msg="installed %s package(s)" % (install_c))
+
+    module.exit_json(changed=False, msg="package(s) already installed")
+
+def main():
+    module = AnsibleModule(
+        argument_spec = dict(
+        state = dict(default="installed", choices=["installed", "absent"]),
+        update_cache = dict(default="yes", aliases=["update-cache"], type='bool'),
+        name = dict(aliases=["pkg"], required=True))
+    )
+
+    if not os.path.exists(POLDEK_PATH):
+        module.fail_json(msg="cannot find poldek, looked for %s" % (POLDEK_PATH))
+
+    p = module.params
+
+    if p["update_cache"] == "yes":
+        update_package_db(module)
+
+    pkgs = p["name"].split(",")
+
+    if p["state"] == "installed":
+        install_packages(module, pkgs)
+
+    elif p["state"] == "absent":
+        remove_packages(module, pkgs)
+
+# this is magic, see lib/ansible/module_common.py
+#<<INCLUDE_ANSIBLE_MODULE_COMMON>>
+
+main()
-- 
1.8.1.6


From ce2262ee9204742d0926393000531125b3bdbd2b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sat, 11 May 2013 14:02:01 +0300
Subject: [PATCH 2/4] fix documentation parse error

test:
$ ansible-doc poldek
---
 library/packaging/poldek | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/library/packaging/poldek b/library/packaging/poldek
index a4a33a8..244fee1 100644
--- a/library/packaging/poldek
+++ b/library/packaging/poldek
@@ -53,8 +53,8 @@ options:
 examples:
    - code: poldek name=apache state=installed
    - code: poldek name=apache state=absent
-   - code: poldek name=apache state=installed, update_cache=yes"
-      description: update the package database and install apache (apache will be the updated if a newer version exists) 
+   - code: poldek name=apache state=installed, update_cache=yes
+     description: update the package database and install apache (apache will be the updated if a newer version exists) 
 notes: []
 # informational: requirements for nodes
 requirements: [ poldek , rpm ]
-- 
1.8.1.6


From fedb5c529b2e2d4008578c8211114a4569340200 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sat, 11 May 2013 14:38:48 +0300
Subject: [PATCH 3/4] version added can be 1.2 if any

---
 library/packaging/poldek | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/library/packaging/poldek b/library/packaging/poldek
index 244fee1..fb2e487 100644
--- a/library/packaging/poldek
+++ b/library/packaging/poldek
@@ -29,7 +29,7 @@ module: poldek
 short_description: Manages packages with the I(poldek) package manager
 description:
      - Will install, upgrade, remove, and list packages with the I(poldek) package manager.
-version_added: "1.1"
+version_added: "1.2"
 options:
   name:
     description:
-- 
1.8.1.6


From d3a98c43f0542df68422dcbcda04e6e1e0895992 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Elan=20Ruusam=C3=A4e?= <glen@delfi.ee>
Date: Sat, 11 May 2013 16:25:23 +0300
Subject: [PATCH 4/4] find "poldek" as ansible_pkg_mgr

yes, this needs to be before yum
---
 library/system/setup | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/library/system/setup b/library/system/setup
index 3ff29f4..acc09fc 100644
--- a/library/system/setup
+++ b/library/system/setup
@@ -113,7 +113,8 @@ class Facts(object):
     # A list of dicts.  If there is a platform with more than one
     # package manager, put the preferred one last.  If there is an
     # ansible module, use that as the value for the 'name' key.
-    PKG_MGRS = [ { 'path' : '/usr/bin/yum',         'name' : 'yum' },
+    PKG_MGRS = [ { 'path' : '/usr/bin/poldek',      'name' : 'poldek' },
+                 { 'path' : '/usr/bin/yum',         'name' : 'yum' },
                  { 'path' : '/usr/bin/apt-get',     'name' : 'apt' },
                  { 'path' : '/usr/bin/zypper',      'name' : 'zypper' },
                  { 'path' : '/usr/bin/pacman',      'name' : 'pacman' },
-- 
1.8.1.6

