mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
7.3 KiB
Python
235 lines
7.3 KiB
Python
12 years ago
|
#!/usr/bin/python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
|
||
|
# (c) 2012, Boyd Adamson <boyd () boydadamson.com>
|
||
|
#
|
||
|
# 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/>.
|
||
|
#
|
||
|
|
||
|
DOCUMENTATION = '''
|
||
|
---
|
||
|
module: svr4pkg
|
||
|
short_description: Manage Solaris SVR4 packages
|
||
|
description:
|
||
|
- Manages SVR4 packages on Solaris 10 and 11.
|
||
|
- These were the native packages on Solaris <= 10 and are available
|
||
|
as a legacy feature in Solaris 11.
|
||
|
- Note that this is a very basic packaging system. It will not enforce
|
||
|
dependencies on install or remove.
|
||
12 years ago
|
version_added: "0.9"
|
||
12 years ago
|
author: Boyd Adamson
|
||
|
options:
|
||
|
name:
|
||
|
description:
|
||
|
- Package name, e.g. C(SUNWcsr)
|
||
|
required: true
|
||
|
|
||
|
state:
|
||
|
description:
|
||
|
- Whether to install (C(present)), or remove (C(absent)) a package.
|
||
12 years ago
|
- If the package is to be installed, then I(src) is required.
|
||
12 years ago
|
- The SVR4 package system doesn't provide an upgrade operation. You need to uninstall the old, then install the new package.
|
||
|
required: true
|
||
|
choices: ["present", "absent"]
|
||
|
|
||
|
src:
|
||
|
description:
|
||
|
- Specifies the location to install the package from. Required when C(state=present).
|
||
12 years ago
|
- "Can be any path acceptable to the C(pkgadd) command's C(-d) option. e.g.: C(somefile.pkg), C(/dir/with/pkgs), C(http:/server/mypkgs.pkg)."
|
||
12 years ago
|
- If using a file or directory, they must already be accessible by the host. See the M(copy) module for a way to get them there.
|
||
12 years ago
|
proxy:
|
||
|
description:
|
||
|
- HTTP[s] proxy to be used if C(src) is a URL.
|
||
11 years ago
|
response_file:
|
||
11 years ago
|
description:
|
||
11 years ago
|
- Specifies the location of a response file to be used if package expects input on install. (added in Ansible 1.4)
|
||
11 years ago
|
required: false
|
||
11 years ago
|
zone:
|
||
|
description:
|
||
|
- Whether to install the package only in the current zone, or install it into all zones.
|
||
|
- The installation into all zones works only if you are working with the global zone.
|
||
|
required: false
|
||
|
default: "all"
|
||
|
choices: ["current", "all"]
|
||
11 years ago
|
version_added: "1.6"
|
||
11 years ago
|
category:
|
||
|
description:
|
||
|
- Install/Remove category instead of a single package.
|
||
|
required: false
|
||
|
choices: ["true", "false"]
|
||
|
version_added: "1.6"
|
||
12 years ago
|
'''
|
||
|
|
||
|
EXAMPLES = '''
|
||
|
# Install a package from an already copied file
|
||
|
- svr4pkg: name=CSWcommon src=/tmp/cswpkgs.pkg state=present
|
||
|
|
||
|
# Install a package directly from an http site
|
||
11 years ago
|
- svr4pkg: name=CSWpkgutil src=http://get.opencsw.org/now state=present zone=current
|
||
12 years ago
|
|
||
11 years ago
|
# Install a package with a response file
|
||
11 years ago
|
- svr4pkg: name=CSWggrep src=/tmp/third-party.pkg response_file=/tmp/ggrep.response state=present
|
||
11 years ago
|
|
||
12 years ago
|
# Ensure that a package is not installed.
|
||
|
- svr4pkg: name=SUNWgnome-sound-recorder state=absent
|
||
11 years ago
|
|
||
|
# Ensure that a category is not installed.
|
||
|
- svr4pkg: name=FIREFOX state=absent category=true
|
||
12 years ago
|
'''
|
||
12 years ago
|
|
||
|
|
||
12 years ago
|
import os
|
||
|
import tempfile
|
||
|
|
||
11 years ago
|
def package_installed(module, name, category):
|
||
12 years ago
|
cmd = [module.get_bin_path('pkginfo', True)]
|
||
|
cmd.append('-q')
|
||
11 years ago
|
if category:
|
||
|
cmd.append('-c')
|
||
12 years ago
|
cmd.append(name)
|
||
12 years ago
|
rc, out, err = module.run_command(' '.join(cmd))
|
||
12 years ago
|
if rc == 0:
|
||
12 years ago
|
return True
|
||
|
else:
|
||
|
return False
|
||
|
|
||
|
def create_admin_file():
|
||
|
(desc, filename) = tempfile.mkstemp(prefix='ansible_svr4pkg', text=True)
|
||
|
fullauto = '''
|
||
|
mail=
|
||
|
instance=unique
|
||
|
partial=nocheck
|
||
|
runlevel=quit
|
||
|
idepend=nocheck
|
||
|
rdepend=nocheck
|
||
|
space=quit
|
||
|
setuid=nocheck
|
||
|
conflict=nocheck
|
||
|
action=nocheck
|
||
|
networktimeout=60
|
||
|
networkretries=3
|
||
|
authentication=quit
|
||
|
keystore=/var/sadm/security
|
||
|
proxy=
|
||
|
basedir=default
|
||
|
'''
|
||
|
os.write(desc, fullauto)
|
||
|
os.close(desc)
|
||
|
return filename
|
||
|
|
||
|
def run_command(module, cmd):
|
||
|
progname = cmd[0]
|
||
|
cmd[0] = module.get_bin_path(progname, True)
|
||
12 years ago
|
return module.run_command(cmd)
|
||
12 years ago
|
|
||
11 years ago
|
def package_install(module, name, src, proxy, response_file, zone, category):
|
||
12 years ago
|
adminfile = create_admin_file()
|
||
11 years ago
|
cmd = [ 'pkgadd', '-n']
|
||
|
if zone == 'current':
|
||
|
cmd += [ '-G' ]
|
||
|
cmd += [ '-a', adminfile, '-d', src ]
|
||
12 years ago
|
if proxy is not None:
|
||
|
cmd += [ '-x', proxy ]
|
||
11 years ago
|
if response_file is not None:
|
||
|
cmd += [ '-r', response_file ]
|
||
11 years ago
|
if category:
|
||
|
cmd += [ '-Y' ]
|
||
12 years ago
|
cmd.append(name)
|
||
|
(rc, out, err) = run_command(module, cmd)
|
||
|
os.unlink(adminfile)
|
||
|
return (rc, out, err)
|
||
|
|
||
11 years ago
|
def package_uninstall(module, name, src, category):
|
||
12 years ago
|
adminfile = create_admin_file()
|
||
11 years ago
|
if category:
|
||
|
cmd = [ 'pkgrm', '-na', adminfile, '-Y', name ]
|
||
|
else:
|
||
|
cmd = [ 'pkgrm', '-na', adminfile, name]
|
||
12 years ago
|
(rc, out, err) = run_command(module, cmd)
|
||
|
os.unlink(adminfile)
|
||
|
return (rc, out, err)
|
||
|
|
||
|
def main():
|
||
|
module = AnsibleModule(
|
||
|
argument_spec = dict(
|
||
|
name = dict(required = True),
|
||
|
state = dict(required = True, choices=['present', 'absent']),
|
||
|
src = dict(default = None),
|
||
11 years ago
|
proxy = dict(default = None),
|
||
11 years ago
|
response_file = dict(default = None),
|
||
11 years ago
|
zone = dict(required=False, default = 'all', choices=['current','all']),
|
||
|
category = dict(default=False, type='bool')
|
||
12 years ago
|
),
|
||
|
supports_check_mode=True
|
||
|
)
|
||
12 years ago
|
state = module.params['state']
|
||
|
name = module.params['name']
|
||
|
src = module.params['src']
|
||
|
proxy = module.params['proxy']
|
||
11 years ago
|
response_file = module.params['response_file']
|
||
11 years ago
|
zone = module.params['zone']
|
||
11 years ago
|
category = module.params['category']
|
||
12 years ago
|
rc = None
|
||
|
out = ''
|
||
|
err = ''
|
||
|
result = {}
|
||
|
result['name'] = name
|
||
|
result['state'] = state
|
||
|
|
||
|
if state == 'present':
|
||
|
if src is None:
|
||
|
module.fail_json(name=name,
|
||
|
msg="src is required when state=present")
|
||
11 years ago
|
if not package_installed(module, name, category):
|
||
12 years ago
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
11 years ago
|
(rc, out, err) = package_install(module, name, src, proxy, response_file, zone, category)
|
||
12 years ago
|
# Stdout is normally empty but for some packages can be
|
||
|
# very long and is not often useful
|
||
|
if len(out) > 75:
|
||
|
out = out[:75] + '...'
|
||
|
|
||
|
elif state == 'absent':
|
||
11 years ago
|
if package_installed(module, name, category):
|
||
12 years ago
|
if module.check_mode:
|
||
|
module.exit_json(changed=True)
|
||
11 years ago
|
(rc, out, err) = package_uninstall(module, name, src, category)
|
||
12 years ago
|
out = out[:75]
|
||
|
|
||
11 years ago
|
# Success, Warning, Interruption, Reboot all, Reboot this return codes
|
||
|
if rc in (0, 2, 3, 10, 20):
|
||
11 years ago
|
result['changed'] = True
|
||
11 years ago
|
# no install nor uninstall, or failed
|
||
11 years ago
|
else:
|
||
12 years ago
|
result['changed'] = False
|
||
11 years ago
|
|
||
|
# Fatal error, Administration, Administration Interaction return codes
|
||
|
if rc in (1, 4 , 5):
|
||
|
result['failed'] = True
|
||
12 years ago
|
else:
|
||
11 years ago
|
result['failed'] = False
|
||
12 years ago
|
|
||
|
if out:
|
||
|
result['stdout'] = out
|
||
|
if err:
|
||
|
result['stderr'] = err
|
||
|
|
||
|
module.exit_json(**result)
|
||
|
|
||
11 years ago
|
# import module snippets
|
||
|
from ansible.module_utils.basic import *
|
||
12 years ago
|
main()
|