Module alternatives: support RedHat-based OSes

RedHat-based OSes have a version of update-alternatives which comes from
the chkconfig package and does not support the --query parameter. Work
around that.
pull/18777/head
Aurélien Bompard 10 years ago committed by Matt Clay
parent 2d5c2dabe8
commit 27afdc9566

@ -75,13 +75,14 @@ def main():
current_path = None current_path = None
all_alternatives = [] all_alternatives = []
os_family = None
(rc, query_output, query_error) = module.run_command( (rc, query_output, query_error) = module.run_command(
[UPDATE_ALTERNATIVES, '--query', name] [UPDATE_ALTERNATIVES, '--query', name]
) )
# Gather the current setting and all alternatives from the query output. # Gather the current setting and all alternatives from the query output.
# Query output should look something like this: # Query output should look something like this on Debian systems:
# Name: java # Name: java
# Link: /usr/bin/java # Link: /usr/bin/java
@ -102,6 +103,7 @@ def main():
# java.1.gz /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz # java.1.gz /usr/lib/jvm/java-7-openjdk-amd64/jre/man/man1/java.1.gz
if rc == 0: if rc == 0:
os_family = "Debian"
for line in query_output.splitlines(): for line in query_output.splitlines():
split_line = line.split(':') split_line = line.split(':')
if len(split_line) == 2: if len(split_line) == 2:
@ -113,13 +115,27 @@ def main():
all_alternatives.append(value) all_alternatives.append(value)
elif key == 'Link' and not link: elif key == 'Link' and not link:
link = value link = value
elif rc == 2:
os_family = "RedHat"
# This is the version of update-alternatives that is shipped with
# chkconfig on RedHat-based systems. Try again with the right options.
(rc, query_output, query_error) = module.run_command(
[UPDATE_ALTERNATIVES, '--list']
)
for line in query_output.splitlines():
line_name, line_mode, line_path = line.strip().split("\t")
if line_name != name:
continue
current_path = line_path
break
if current_path != path: if current_path != path:
if module.check_mode: if module.check_mode:
module.exit_json(changed=True, current_path=current_path) module.exit_json(changed=True, current_path=current_path)
try: try:
# install the requested path if necessary # install the requested path if necessary
if path not in all_alternatives: # (unsupported on the RedHat version)
if path not in all_alternatives and os_family == "Debian":
module.run_command( module.run_command(
[UPDATE_ALTERNATIVES, '--install', link, name, path, str(DEFAULT_LINK_PRIORITY)], [UPDATE_ALTERNATIVES, '--install', link, name, path, str(DEFAULT_LINK_PRIORITY)],
check_rc=True check_rc=True

Loading…
Cancel
Save