Commit 8a9f194a authored by Jean-Michel Elyn's avatar Jean-Michel Elyn
Browse files

graphics: Correct xorg template

parent 24bc803e
Pipeline #3518246 passed with stage
in 19 seconds
......@@ -37,7 +37,7 @@ EndSection
Section "Device"
Identifier "Card0"
Driver "modesetting"
BusID "PCI:{{ r_graphical_pci_port }}"
BusID "PCI:{{ r_graphical_pci_port.stdout }}"
EndSection
Section "Screen"
......
#!/usr/bin/python3
#######################################################################################################################
# DESCRIPTION
#
'''
Module for Ansible: package_manage.
It updates packages, adds required ones and removes banned ones.
Files touched: All packages
'''
#######################################################################################################################
# IMPORTS
#
import json
import os
import re
import subprocess
import sys
import yaml
#######################################################################################################################
# FUNCTIONS
#
#----------------------------------------------------------------------------------------------------------------------
def update_packages(repo):
cmd = 'yum update -y --allowerasing'
if repo != 'enabled_repos':
cmd += ' --enablerepo=' + repo
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(cmd_output, cmd_err) = process.communicate()
if process.returncode != 0 and cmd_err and cmd_err != 'Error: Nothing to do\n':
print(json.dumps({'failed' : True, 'msg' : cmd_err.decode()}))
sys.exit(1)
#----------------------------------------------------------------------------------------------------------------------
def remove_packages(package_set):
global ansible_state
global removed_set
if len(package_set) == 0:
return
ansible_state = 'changed'
removed_set |= package_set
cmd = 'yum remove -y ' + ' '.join(package_set)
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(cmd_output, cmd_error) = process.communicate()
output = cmd_output.decode()
error = cmd_error.decode()
if process.returncode != 0 and error:
print(json.dumps({'failed' : True, 'msg' : error}))
sys.exit(1)
#----------------------------------------------------------------------------------------------------------------------
def install_packages(package_dict, repo):
global ansible_state
global installed_set
if repo not in package_dict or len(package_dict[repo]) == 0:
return
ansible_state = 'changed'
installed_set |= package_dict[repo]
if repo == 'enabled_repos':
repo_option = ''
else:
repo_option = '--enablerepo=' + repo + ' '
cmd = 'yum install -y ' + repo_option + ' '.join(package_dict[repo])
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(cmd_output, cmd_error) = process.communicate()
output = cmd_output.decode()
error = cmd_error.decode()
if process.returncode != 0 and error and not re.match('Error: Nothing to do', error) and not re.match('Non-fatal POSTIN scriptlet failure', error):
print(json.dumps({'failed' : True, 'msg' : error}))
sys.exit(1)
#######################################################################################################################
# GET MODULE ARGUMENTS
#
# Ansible guarantees to provide a file containing JSON arguments
# WANT_JSON : This word tells Ansible to provide arguments in JSON format
with open(sys.argv[1], 'r') as args_stream:
args_dict = json.load(args_stream)
# Check module arguments
if 'enabled_repos' not in args_dict \
or 'required_packages' not in args_dict \
or 'banned_packages' not in args_dict \
or 'arch_list' not in args_dict:
print(json.dumps({'failed': True, 'msg': 'Missing module arguments'}))
sys.exit(1)
#######################################################################################################################
# GET LIST OF INSTALLED PACKAGES
#
# Get list of packages from yum command
cmd = 'yum list installed'
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(cmd_output, cmd_err) = process.communicate()
if process.returncode != 0:
print(json.dumps({'failed' : True, 'msg' : cmd_err}))
sys.exit(1)
if not cmd_output:
print(json.dumps({'failed' : True, 'msg' : 'empty list of installed packages'}))
sys.exit(1)
# Format yum output
installed_package_dict = dict()
installed_package_set = set()
broken_line = ''
is_header = True
enabled_repo_set = set(args_dict['enabled_repos'])
for output_line in cmd_output.decode().splitlines():
# Skip header
if output_line == 'Installed Packages':
is_header = False
continue
if is_header:
continue
# Populate installed_package_dict
package_name = field_list[0]
package_version = re.sub('^[0-9]*:', '', field_list[1])
package_repo = re.sub('^@', '', field_list[2])
if package_repo in enabled_repo_set:
package_repo = 'enabled_repo'
installed_package_dict[package_name] = {'repo': package_repo, 'version': package_version }
# Populate installed_package_set
package_title = re.sub('[.][^.]+$', '', package_name)
package_arch = re.sub('^.*[.]', '', package_name)
full_package_name = package_title + '-' + package_version + '.' + package_arch
installed_package_set.add(full_package_name)
#######################################################################################################################
# UPDATE INSTALLED PACKAGES
#
# First, update packages from enabled repos
update_packages(installed_package_dict['enabled_repos'])
# Then, update packages from other repos, repo by repo
for repo in installed_package_dict:
if repo == 'enabled_repos':
continue
update_packages(repo)
#######################################################################################################################
# REMOVE BANNED PACKAGES
#
# Calculate packages to remove
arch_set = set(args_dict['arch_list'])
banned_package_dict = args_dict['banned_packages']
for package_name in banned_package_dict:
package_ext = re.sub('^.*[.]', '', package_name)
if package_ext in arch_set:
packages_to_remove = package_name
else:
for arch in args_dict['arch_list']:
packages_to_remove_set.add(package_name + '.' + arch)
packages_to_remove_set = set(args_dict['banned_packages'].keys()) & set(args_dict['installed_packages'].keys())
# First, remove banned packages
remove_packages(packages_to_remove_set)
#######################################################################################################################
# INSTALL REQUIRED PACKAGES
#
# Calculate packages to install
packages_to_install_dict = { package: repo for package, repo in args_dict['required_packages'].items() \
if package not in args_dict['installed_packages'] and package not in args_dict['banned_packages'] }
# Format packages to install
packages_to_install_by_repos_dict = dict()
for package, repo in packages_to_install_dict.items():
if not repo or repo == 'None':
repo = 'enabled_repos'
if repo not in packages_to_install_by_repos_dict:
packages_to_install_by_repos_dict[repo] = set()
packages_to_install_by_repos_dict[repo].add(package)
# First, install packages from enabled_repos
install_packages(packages_to_install_by_repos_dict, 'enabled_repos')
# Then, install packages from other repos
for repo in [r for r in packages_to_install_by_repos_dict if r != 'enabled_repos']:
install_packages(packages_to_install_by_repos_dict, repo)
installed_list = sorted(installed_set)
removed_list = sorted(removed_set)
#######################################################################################################################
# RETURN ANSIBLE VALUES
#
print(json.dumps({'ok': True, 'dict': package_dict }))
sys.exit(0)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment