mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Force command action to not be executed by the shell unless specifically enabled
This commit is contained in:
committed by
James Cammarata
parent
9730157525
commit
ba0fec4f42
@@ -151,8 +151,8 @@ def main():
|
||||
command = '%s %s' % (virtualenv, env)
|
||||
if site_packages:
|
||||
command += ' --system-site-packages'
|
||||
os.chdir(tempfile.gettempdir())
|
||||
rc_venv, out_venv, err_venv = module.run_command(command)
|
||||
cwd = tempfile.gettempdir()
|
||||
rc_venv, out_venv, err_venv = module.run_command(command, cwd=cwd)
|
||||
|
||||
rc += rc_venv
|
||||
out += out_venv
|
||||
|
||||
@@ -125,10 +125,11 @@ class Npm(object):
|
||||
cmd.append(self.name_version)
|
||||
|
||||
#If path is specified, cd into that path and run the command.
|
||||
cwd = None
|
||||
if self.path:
|
||||
os.chdir(self.path)
|
||||
cwd = self.path
|
||||
|
||||
rc, out, err = self.module.run_command(cmd, check_rc=check_rc)
|
||||
rc, out, err = self.module.run_command(cmd, check_rc=check_rc, cwd=cwd)
|
||||
return out
|
||||
return ''
|
||||
|
||||
|
||||
@@ -90,7 +90,8 @@ def query_package(module, name, state="installed"):
|
||||
# pacman -Q returns 0 if the package is installed,
|
||||
# 1 if it is not installed
|
||||
if state == "installed":
|
||||
rc = os.system("pacman -Q %s" % (name))
|
||||
cmd = "pacman -Q %s" % (name)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
|
||||
if rc == 0:
|
||||
return True
|
||||
@@ -99,7 +100,8 @@ def query_package(module, name, state="installed"):
|
||||
|
||||
|
||||
def update_package_db(module):
|
||||
rc = os.system("pacman -Syy > /dev/null")
|
||||
cmd = "pacman -Syy > /dev/null"
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
|
||||
if rc != 0:
|
||||
module.fail_json(msg="could not update package db")
|
||||
@@ -118,7 +120,8 @@ def remove_packages(module, packages):
|
||||
if not query_package(module, package):
|
||||
continue
|
||||
|
||||
rc = os.system("pacman -%s %s --noconfirm > /dev/null" % (args, package))
|
||||
cmd = "pacman -%s %s --noconfirm > /dev/null" % (args, package)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
|
||||
if rc != 0:
|
||||
module.fail_json(msg="failed to remove %s" % (package))
|
||||
@@ -145,7 +148,8 @@ def install_packages(module, packages, package_files):
|
||||
else:
|
||||
params = '-S %s' % package
|
||||
|
||||
rc = os.system("pacman %s --noconfirm > /dev/null" % (params))
|
||||
cmd = "pacman %s --noconfirm > /dev/null" % (params)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
|
||||
if rc != 0:
|
||||
module.fail_json(msg="failed to install %s" % (package))
|
||||
|
||||
@@ -253,10 +253,10 @@ def main():
|
||||
cmd = '%s --no-site-packages %s' % (virtualenv, env)
|
||||
else:
|
||||
cmd = '%s %s' % (virtualenv, env)
|
||||
os.chdir(tempfile.gettempdir())
|
||||
this_dir = tempfile.gettempdir()
|
||||
if chdir:
|
||||
os.chdir(chdir)
|
||||
rc, out_venv, err_venv = module.run_command(cmd)
|
||||
this_dir = os.path.join(this_dir, chdir)
|
||||
rc, out_venv, err_venv = module.run_command(cmd, cwd=this_dir)
|
||||
out += out_venv
|
||||
err += err_venv
|
||||
if rc != 0:
|
||||
@@ -298,10 +298,11 @@ def main():
|
||||
|
||||
if module.check_mode:
|
||||
module.exit_json(changed=True)
|
||||
os.chdir(tempfile.gettempdir())
|
||||
this_dir = tempfile.gettempdir()
|
||||
if chdir:
|
||||
os.chdir(chdir)
|
||||
rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix)
|
||||
this_dir = os.path.join(this_dir, chdir)
|
||||
|
||||
rc, out_pip, err_pip = module.run_command(cmd, path_prefix=path_prefix, cwd=this_dir)
|
||||
out += out_pip
|
||||
err += err_pip
|
||||
if rc == 1 and state == 'absent' and 'not installed' in out_pip:
|
||||
|
||||
@@ -75,39 +75,13 @@ EXAMPLES = '''
|
||||
import os
|
||||
import re
|
||||
import types
|
||||
import subprocess
|
||||
import ConfigParser
|
||||
import shlex
|
||||
|
||||
|
||||
class CommandException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def run_command(args):
|
||||
'''
|
||||
Convenience method to run a command, specified as a list of arguments.
|
||||
Returns:
|
||||
* tuple - (stdout, stder, retcode)
|
||||
'''
|
||||
|
||||
# Coerce into a string
|
||||
if isinstance(args, str):
|
||||
args = shlex.split(args)
|
||||
|
||||
# Run desired command
|
||||
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT)
|
||||
(stdout, stderr) = proc.communicate()
|
||||
returncode = proc.poll()
|
||||
if returncode != 0:
|
||||
cmd = ' '.join(args)
|
||||
raise CommandException("Command failed (%s): %s\n%s" % (returncode, cmd, stdout))
|
||||
return (stdout, stderr, returncode)
|
||||
|
||||
|
||||
class RegistrationBase (object):
|
||||
def __init__(self, username=None, password=None):
|
||||
class RegistrationBase(object):
|
||||
def __init__(self, module, username=None, password=None):
|
||||
self.module = module
|
||||
self.username = username
|
||||
self.password = password
|
||||
|
||||
@@ -147,9 +121,10 @@ class RegistrationBase (object):
|
||||
|
||||
|
||||
class Rhsm(RegistrationBase):
|
||||
def __init__(self, username=None, password=None):
|
||||
RegistrationBase.__init__(self, username, password)
|
||||
def __init__(self, module, username=None, password=None):
|
||||
RegistrationBase.__init__(self, module, username, password)
|
||||
self.config = self._read_config()
|
||||
self.module = module
|
||||
|
||||
def _read_config(self, rhsm_conf='/etc/rhsm/rhsm.conf'):
|
||||
'''
|
||||
@@ -199,8 +174,8 @@ class Rhsm(RegistrationBase):
|
||||
for k,v in kwargs.items():
|
||||
if re.search(r'^(system|rhsm)_', k):
|
||||
args.append('--%s=%s' % (k.replace('_','.'), v))
|
||||
|
||||
run_command(args)
|
||||
|
||||
self.module.run_command(args, check_rc=True)
|
||||
|
||||
@property
|
||||
def is_registered(self):
|
||||
@@ -216,13 +191,11 @@ class Rhsm(RegistrationBase):
|
||||
os.path.isfile('/etc/pki/consumer/key.pem')
|
||||
|
||||
args = ['subscription-manager', 'identity']
|
||||
try:
|
||||
(stdout, stderr, retcode) = run_command(args)
|
||||
except CommandException, e:
|
||||
return False
|
||||
else:
|
||||
# Display some debug output
|
||||
rc, stdout, stderr = self.module.run_command(args, check_rc=False)
|
||||
if rc == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def register(self, username, password, autosubscribe, activationkey):
|
||||
'''
|
||||
@@ -244,7 +217,7 @@ class Rhsm(RegistrationBase):
|
||||
args.extend(['--password', password])
|
||||
|
||||
# Do the needful...
|
||||
run_command(args)
|
||||
rc, stderr, stdout = self.module.run_command(args, check_rc=True)
|
||||
|
||||
def unsubscribe(self):
|
||||
'''
|
||||
@@ -253,7 +226,7 @@ class Rhsm(RegistrationBase):
|
||||
* Exception - if error occurs while running command
|
||||
'''
|
||||
args = ['subscription-manager', 'unsubscribe', '--all']
|
||||
run_command(args)
|
||||
rc, stderr, stdout = self.module.run_command(args, check_rc=True)
|
||||
|
||||
def unregister(self):
|
||||
'''
|
||||
@@ -262,7 +235,7 @@ class Rhsm(RegistrationBase):
|
||||
* Exception - if error occurs while running command
|
||||
'''
|
||||
args = ['subscription-manager', 'unregister']
|
||||
run_command(args)
|
||||
rc, stderr, stdout = self.module.run_command(args, check_rc=True)
|
||||
|
||||
def subscribe(self, regexp):
|
||||
'''
|
||||
@@ -273,7 +246,7 @@ class Rhsm(RegistrationBase):
|
||||
'''
|
||||
|
||||
# Available pools ready for subscription
|
||||
available_pools = RhsmPools()
|
||||
available_pools = RhsmPools(self.module)
|
||||
|
||||
for pool in available_pools.filter(regexp):
|
||||
pool.subscribe()
|
||||
@@ -284,7 +257,8 @@ class RhsmPool(object):
|
||||
Convenience class for housing subscription information
|
||||
'''
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, module, **kwargs):
|
||||
self.module = module
|
||||
for k,v in kwargs.items():
|
||||
setattr(self, k, v)
|
||||
|
||||
@@ -292,15 +266,20 @@ class RhsmPool(object):
|
||||
return str(self.__getattribute__('_name'))
|
||||
|
||||
def subscribe(self):
|
||||
(stdout, stderr, retcode) = run_command("subscription-manager subscribe --pool %s" % self.PoolId)
|
||||
return True
|
||||
args = "subscription-manager subscribe --pool %s" % self.PoolId
|
||||
rc, stdout, stderr = self.module.run_command(args, check_rc=True)
|
||||
if rc == 0:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
class RhsmPools(object):
|
||||
"""
|
||||
This class is used for manipulating pools subscriptions with RHSM
|
||||
"""
|
||||
def __init__(self):
|
||||
def __init__(self, module):
|
||||
self.module = module
|
||||
self.products = self._load_product_list()
|
||||
|
||||
def __iter__(self):
|
||||
@@ -310,7 +289,8 @@ class RhsmPools(object):
|
||||
"""
|
||||
Loads list of all availaible pools for system in data structure
|
||||
"""
|
||||
(stdout, stderr, retval) = run_command("subscription-manager list --available")
|
||||
args = "subscription-manager list --available"
|
||||
rc, stdout, stderr = self.module.run_command(args, check_rc=True)
|
||||
|
||||
products = []
|
||||
for line in stdout.split('\n'):
|
||||
@@ -326,7 +306,7 @@ class RhsmPools(object):
|
||||
value = value.strip()
|
||||
if key in ['ProductName', 'SubscriptionName']:
|
||||
# Remember the name for later processing
|
||||
products.append(RhsmPool(_name=value, key=value))
|
||||
products.append(RhsmPool(self.module, _name=value, key=value))
|
||||
elif products:
|
||||
# Associate value with most recently recorded product
|
||||
products[-1].__setattr__(key, value)
|
||||
@@ -348,7 +328,7 @@ class RhsmPools(object):
|
||||
def main():
|
||||
|
||||
# Load RHSM configuration from file
|
||||
rhn = Rhsm()
|
||||
rhn = Rhsm(AnsibleModule())
|
||||
|
||||
module = AnsibleModule(
|
||||
argument_spec = dict(
|
||||
@@ -364,6 +344,7 @@ def main():
|
||||
)
|
||||
)
|
||||
|
||||
rhn.module = module
|
||||
state = module.params['state']
|
||||
username = module.params['username']
|
||||
password = module.params['password']
|
||||
|
||||
@@ -72,12 +72,7 @@ EXAMPLES = '''
|
||||
'''
|
||||
|
||||
import sys
|
||||
import os
|
||||
import re
|
||||
import types
|
||||
import subprocess
|
||||
import ConfigParser
|
||||
import shlex
|
||||
import xmlrpclib
|
||||
import urlparse
|
||||
|
||||
@@ -90,75 +85,9 @@ except ImportError, e:
|
||||
module.fail_json(msg="Unable to import up2date_client. Is 'rhn-client-tools' installed?\n%s" % e)
|
||||
|
||||
|
||||
class CommandException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def run_command(args):
|
||||
'''
|
||||
Convenience method to run a command, specified as a list of arguments.
|
||||
Returns:
|
||||
* tuple - (stdout, stder, retcode)
|
||||
'''
|
||||
|
||||
# Coerce into a string
|
||||
if isinstance(args, str):
|
||||
args = shlex.split(args)
|
||||
|
||||
# Run desired command
|
||||
proc = subprocess.Popen(args, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT)
|
||||
(stdout, stderr) = proc.communicate()
|
||||
returncode = proc.poll()
|
||||
if returncode != 0:
|
||||
cmd = ' '.join(args)
|
||||
raise CommandException("Command failed (%s): %s\n%s" % (returncode, cmd, stdout))
|
||||
return (stdout, stderr, returncode)
|
||||
|
||||
|
||||
class RegistrationBase (object):
|
||||
def __init__(self, username=None, password=None):
|
||||
self.username = username
|
||||
self.password = password
|
||||
|
||||
def configure(self):
|
||||
raise NotImplementedError("Must be implemented by a sub-class")
|
||||
|
||||
def enable(self):
|
||||
# Remove any existing redhat.repo
|
||||
redhat_repo = '/etc/yum.repos.d/redhat.repo'
|
||||
if os.path.isfile(redhat_repo):
|
||||
os.unlink(redhat_repo)
|
||||
|
||||
def register(self):
|
||||
raise NotImplementedError("Must be implemented by a sub-class")
|
||||
|
||||
def unregister(self):
|
||||
raise NotImplementedError("Must be implemented by a sub-class")
|
||||
|
||||
def unsubscribe(self):
|
||||
raise NotImplementedError("Must be implemented by a sub-class")
|
||||
|
||||
def update_plugin_conf(self, plugin, enabled=True):
|
||||
plugin_conf = '/etc/yum/pluginconf.d/%s.conf' % plugin
|
||||
if os.path.isfile(plugin_conf):
|
||||
cfg = ConfigParser.ConfigParser()
|
||||
cfg.read([plugin_conf])
|
||||
if enabled:
|
||||
cfg.set('main', 'enabled', 1)
|
||||
else:
|
||||
cfg.set('main', 'enabled', 0)
|
||||
fd = open(plugin_conf, 'rwa+')
|
||||
cfg.write(fd)
|
||||
fd.close()
|
||||
|
||||
def subscribe(self, **kwargs):
|
||||
raise NotImplementedError("Must be implemented by a sub-class")
|
||||
|
||||
|
||||
class Rhn(RegistrationBase):
|
||||
|
||||
def __init__(self, username=None, password=None):
|
||||
def __init__(self, module, username=None, password=None):
|
||||
RegistrationBase.__init__(self, username, password)
|
||||
self.config = self.load_config()
|
||||
|
||||
@@ -271,7 +200,7 @@ class Rhn(RegistrationBase):
|
||||
register_cmd += " --activationkey '%s'" % activationkey
|
||||
# FIXME - support --profilename
|
||||
# FIXME - support --systemorgid
|
||||
run_command(register_cmd)
|
||||
rc, stdout, stderr = self.module.run_command(register_command, check_rc=True)
|
||||
|
||||
def api(self, method, *args):
|
||||
'''
|
||||
@@ -309,14 +238,14 @@ class Rhn(RegistrationBase):
|
||||
Subscribe to requested yum repositories using 'rhn-channel' command
|
||||
'''
|
||||
rhn_channel_cmd = "rhn-channel --user='%s' --password='%s'" % (self.username, self.password)
|
||||
(stdout, stderr, rc) = run_command(rhn_channel_cmd + " --available-channels")
|
||||
rc, stdout, stderr = self.module.run_command(rhn_channel_cmd + " --available-channels", check_rc=True)
|
||||
|
||||
# Enable requested repoid's
|
||||
for wanted_channel in channels:
|
||||
# Each inserted repo regexp will be matched. If no match, no success.
|
||||
for availaible_channel in stdout.rstrip().split('\n'): # .rstrip() because of \n at the end -> empty string at the end
|
||||
if re.search(wanted_repo, available_channel):
|
||||
run_command(rhn_channel_cmd + " --add --channel=%s" % available_channel)
|
||||
rc, stdout, stderr = self.module.run_command(rhn_channel_cmd + " --add --channel=%s" % available_channel, check_rc=True)
|
||||
|
||||
def main():
|
||||
|
||||
@@ -379,4 +308,6 @@ def main():
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
from ansible.module_utils.redhat import *
|
||||
|
||||
main()
|
||||
|
||||
@@ -91,7 +91,8 @@ def query_package(module, name):
|
||||
|
||||
# rpm -q returns 0 if the package is installed,
|
||||
# 1 if it is not installed
|
||||
rc = os.system("rpm -q %s" % (name))
|
||||
cmd = "rpm -q %s" % (name)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
if rc == 0:
|
||||
return True
|
||||
else:
|
||||
@@ -103,13 +104,14 @@ def query_package_provides(module, name):
|
||||
|
||||
# rpm -q returns 0 if the package is installed,
|
||||
# 1 if it is not installed
|
||||
rc = os.system("rpm -q --provides %s >/dev/null" % (name))
|
||||
cmd = "rpm -q --provides %s >/dev/null" % (name)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
return rc == 0
|
||||
|
||||
|
||||
def update_package_db(module):
|
||||
rc = os.system("urpmi.update -a -q")
|
||||
|
||||
cmd = "urpmi.update -a -q"
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
if rc != 0:
|
||||
module.fail_json(msg="could not update package db")
|
||||
|
||||
@@ -123,7 +125,8 @@ def remove_packages(module, packages):
|
||||
if not query_package(module, package):
|
||||
continue
|
||||
|
||||
rc = os.system("%s --auto %s > /dev/null" % (URPME_PATH, package))
|
||||
cmd = "%s --auto %s > /dev/null" % (URPME_PATH, package)
|
||||
rc, stdout, stderr = module.run_command(cmd, check_rc=False)
|
||||
|
||||
if rc != 0:
|
||||
module.fail_json(msg="failed to remove %s" % (package))
|
||||
|
||||
Reference in New Issue
Block a user