From 90ba14d60e160ed5b8063a1703eee5b517765d83 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Mon, 26 Mar 2012 12:49:13 -0700 Subject: [PATCH 1/3] preliminary apt module --- library/apt | 152 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100755 library/apt diff --git a/library/apt b/library/apt new file mode 100755 index 00000000000..e3429875637 --- /dev/null +++ b/library/apt @@ -0,0 +1,152 @@ +#!/usr/bin/python -tt +# (c) 2012, Flowroute LLC +# Written by Matthew Williams +# Based on yum module written by Seth Vidal +# +# This module 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. +# +# This software 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 this software. If not, see . +# + + +import os +import sys +import apt +import shlex +import subprocess + + +try: + import json +except ImportError: + import simplejson as json + +def run_apt(command): + try: + cmd = subprocess.Popen(command, shell=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = cmd.communicate() + except (OSError, IOError), e: + rc = 1 + err = str(e) + out = '' + except: + rc = 1 + err = traceback.format_exc() + out = '' + + if out is None: + out = '' + if err is None: + err = '' + else: + rc = cmd.returncode + + return rc, out, err + +def ensure(state, pkgspec): + cache = apt.Cache() + cache.update() + cache.open(None) + try: + pkg = cache[pkgspec] + except: + msg = "No package matching '%s' is available" % pkgsepc + return {'changed': False, 'failed': True, 'msg': msg} + if state == 'installed': + #check if package is installed + if pkg.is_installed: + return {'changed': False, 'failed': False} + cmd = "apt-get -q -y install '%s'" % pkgspec + rc, out, err = run_apt(cmd) + #pkg.mark_install() + #cache.commit(apt.progress.base.OpProgress()) + + elif state == 'removed': + #check if package is installed + if not pkg.is_installed: + return {'changed': False, 'failed': False} + cmd = "apt-get -q -y remove '%s'" % pkgspec + rc, out, err = run_apt(cmd) + #pkg.mark_delete() + #cache.commit(apt.progress.base.OpProgress()) + else: + return {'failed': True, 'msg': "Unknown state: %s" % state } + + return {'changed': True, 'failed': False} + +def update(args): + #generic update routine + pass + +def remove_only(pkgspec): + # remove this pkg and only this pkg - fail if it will require more to remove + pass + +def main(): + # state=installed pkg=pkgspec + # state=removed pkg=pkgspec + + if len(sys.argv) == 1: + msg = "the apt module requires arguments (-a)" + return 1, msg + + argfile = sys.argv[1] + if not os.path.exists(argfile): + msg = "Argument file not found" + return 1, msg + + args = open(argfile, 'r').read() + items = shlex.split(args) + + if not len(items): + msg = "the apt module requires arguments (-a)" + return 1, msg + + # if nothing else changes - it fails + results = { 'changed':False, + 'failed':True, + 'results':'', + 'errors':'', + 'msg':"; ".join(items) } + params = {} + for x in items: + try: + (k, v) = x.split("=", 1) + except ValueError: + msg = "invalid arguments: %s" % args + return 1, msg + + params[k] = v + + if 'state' in params: + if 'pkg' not in params: + results['msg'] = "No pkg specified" + else: + state = params['state'] + pkgspec = params['pkg'] + + devnull = open(os.devnull, 'w') + results = ensure(state, pkgspec) + print json.dumps(results) + return 0, None + +if __name__ == "__main__": + rc, msg = main() + if rc != 0: # something went wrong emit the msg + print json.dumps({ + "failed" : bool(rc), + "msg" : msg + }) + sys.exit(rc) + + From e85355f0547442334987433fecb8933a744ca52a Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Mon, 26 Mar 2012 13:48:02 -0700 Subject: [PATCH 2/3] cleaned up apt module style --- library/apt | 170 ++++++++++++++++++++++++++-------------------------- 1 file changed, 84 insertions(+), 86 deletions(-) diff --git a/library/apt b/library/apt index e3429875637..e4a6e5e505a 100755 --- a/library/apt +++ b/library/apt @@ -17,20 +17,34 @@ # along with this software. If not, see . # - +try: + import json +except ImportError: + import simplejson as json import os import sys import apt import shlex import subprocess +import traceback +APT = "/usr/bin/apt-get" -try: - import json -except ImportError: - import simplejson as json +def debug(msg): + # ansible ignores stderr, so it's safe to use for debug + print >>sys.stderr, msg + #pass + +def exit_json(rc=0, **kwargs): + print json.dumps(kwargs) + sys.exit(rc) + +def fail_json(**kwargs): + kwargs['failed'] = True + exit_json(rc=1, **kwargs) def run_apt(command): + debug(command) try: cmd = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -51,102 +65,86 @@ def run_apt(command): else: rc = cmd.returncode + debug(err) return rc, out, err -def ensure(state, pkgspec): +def get_cache(): + # TODO: Only update the cache if it's old. cache = apt.Cache() cache.update() cache.open(None) + return cache + +def package_installed(pkgspec): + cache = get_cache() try: - pkg = cache[pkgspec] + pkg = cache[pkgspec] except: - msg = "No package matching '%s' is available" % pkgsepc - return {'changed': False, 'failed': True, 'msg': msg} - if state == 'installed': - #check if package is installed - if pkg.is_installed: - return {'changed': False, 'failed': False} - cmd = "apt-get -q -y install '%s'" % pkgspec - rc, out, err = run_apt(cmd) - #pkg.mark_install() - #cache.commit(apt.progress.base.OpProgress()) - - elif state == 'removed': - #check if package is installed - if not pkg.is_installed: - return {'changed': False, 'failed': False} - cmd = "apt-get -q -y remove '%s'" % pkgspec + fail_json(msg="No package matching '%s' is available" % pkgspec) + return bool(pkg.is_installed) + +def install(pkgspec): + installed = package_installed(pkgspec) + debug("installed: %d" % installed) + if installed: + return False + else: + cmd = "%s -q -y install '%s'" % (APT, pkgspec) rc, out, err = run_apt(cmd) - #pkg.mark_delete() - #cache.commit(apt.progress.base.OpProgress()) + # TODO: Ensure the package was really installed. + return True + +def remove(pkgspec): + installed = package_installed(pkgspec) + debug("installed: %d" % installed) + if not installed: + return False else: - return {'failed': True, 'msg': "Unknown state: %s" % state } + cmd = "%s -q -y remove '%s'" % (APT, pkgspec) + rc, out, err = run_apt(cmd) + # TODO: Ensure the package was really removed. + return True - return {'changed': True, 'failed': False} - def update(args): - #generic update routine + # TODO: generic update routine pass def remove_only(pkgspec): - # remove this pkg and only this pkg - fail if it will require more to remove + # TODO: remove this pkg and only this pkg - fail if it will require more to remove pass -def main(): - # state=installed pkg=pkgspec - # state=removed pkg=pkgspec - - if len(sys.argv) == 1: - msg = "the apt module requires arguments (-a)" - return 1, msg - - argfile = sys.argv[1] - if not os.path.exists(argfile): - msg = "Argument file not found" - return 1, msg - - args = open(argfile, 'r').read() - items = shlex.split(args) - - if not len(items): - msg = "the apt module requires arguments (-a)" - return 1, msg - - # if nothing else changes - it fails - results = { 'changed':False, - 'failed':True, - 'results':'', - 'errors':'', - 'msg':"; ".join(items) } - params = {} - for x in items: - try: - (k, v) = x.split("=", 1) - except ValueError: - msg = "invalid arguments: %s" % args - return 1, msg - - params[k] = v - - if 'state' in params: - if 'pkg' not in params: - results['msg'] = "No pkg specified" - else: - state = params['state'] - pkgspec = params['pkg'] - - devnull = open(os.devnull, 'w') - results = ensure(state, pkgspec) - print json.dumps(results) - return 0, None - -if __name__ == "__main__": - rc, msg = main() - if rc != 0: # something went wrong emit the msg - print json.dumps({ - "failed" : bool(rc), - "msg" : msg - }) - sys.exit(rc) +# =========================================== + +if not os.path.exists(APT): + fail_json(msg="Cannot find apt-get") + +argfile = sys.argv[1] +args = open(argfile, 'r').read() +items = shlex.split(args) + +if not len(items): + fail_json(msg='the module requires arguments -a') + sys.exit(1) + +params = {} +for x in items: + (k, v) = x.split("=") + params[k] = v + +state = params.get('state','installed') +package = params.get('pkg', None) + +if state not in ['installed', 'removed']: + fail_json(msg='invalid state') +if package is None: + fail_json(msg='pkg is required') + +if state == 'installed': + changed = install(package) +elif state == 'removed': + changed = remove(package) +exit_json(changed=changed) +fail_json(name=name, msg='Unexpected position reached') +sys.exit(0) From e2deb94e73cc01358dd6a8bb25db094e47ad25b0 Mon Sep 17 00:00:00 2001 From: Matthew Williams Date: Mon, 26 Mar 2012 13:53:22 -0700 Subject: [PATCH 3/3] trimmed superfluous code from apt module --- library/apt | 2 -- 1 file changed, 2 deletions(-) diff --git a/library/apt b/library/apt index e4a6e5e505a..9e77a8156d4 100755 --- a/library/apt +++ b/library/apt @@ -145,6 +145,4 @@ elif state == 'removed': changed = remove(package) exit_json(changed=changed) -fail_json(name=name, msg='Unexpected position reached') -sys.exit(0)