forked from evolix/ansible-roles
apt: add tools to migrate sources to deb822 format
This commit is contained in:
parent
96a2bbecdd
commit
fa1935e46c
|
@ -13,6 +13,7 @@ The **patch** part changes is incremented if multiple releases happen the same m
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
* apt: add move-apt-keyrings script/tasks
|
* apt: add move-apt-keyrings script/tasks
|
||||||
|
* apt: add tools to migrate sources to deb822 format
|
||||||
* nagios-nrpe: Print pool config path in check_phpfpm_multi output
|
* nagios-nrpe: Print pool config path in check_phpfpm_multi output
|
||||||
* nagios-nrpe: add tasks/files for a wrapper
|
* nagios-nrpe: add tasks/files for a wrapper
|
||||||
* fail2ban: add "Internal login failure" to Dovecot filter
|
* fail2ban: add "Internal login failure" to Dovecot filter
|
||||||
|
|
96
apt/files/deb822-migration.py
Normal file
96
apt/files/deb822-migration.py
Normal file
|
@ -0,0 +1,96 @@
|
||||||
|
#!/bin/env python3
|
||||||
|
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
src_file = sys.argv[1]
|
||||||
|
else:
|
||||||
|
print("You must provide a source file as first argument", file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if not os.access(src_file, os.R_OK):
|
||||||
|
print(src_file, "is not readable", file=sys.stderr)
|
||||||
|
sys.exit(2)
|
||||||
|
|
||||||
|
pattern = re.compile('^(?P<type>deb|deb-src) +(?P<options>\[.+\] ?)*(?P<uri>\w+:\/\/\S+) +(?P<suite>\S+)(?: +(?P<components>.*))?$')
|
||||||
|
|
||||||
|
sources = {}
|
||||||
|
|
||||||
|
def split_options(raw):
|
||||||
|
table = str.maketrans({
|
||||||
|
"[": None,
|
||||||
|
"]": None
|
||||||
|
})
|
||||||
|
options = raw.translate(table).split(' ')
|
||||||
|
|
||||||
|
return options
|
||||||
|
|
||||||
|
with open(src_file,'r') as file:
|
||||||
|
for line in file:
|
||||||
|
matches = re.match(pattern, line)
|
||||||
|
if matches is not None:
|
||||||
|
# print(matches.groupdict())
|
||||||
|
uri = matches['uri']
|
||||||
|
|
||||||
|
options = {}
|
||||||
|
if matches.group('options'):
|
||||||
|
for option in split_options(matches['options']):
|
||||||
|
if "=" in option:
|
||||||
|
key, value = option.split("=")
|
||||||
|
options[key] = value
|
||||||
|
|
||||||
|
if uri in sources:
|
||||||
|
sources[uri]["Types"].add(matches["type"])
|
||||||
|
sources[uri]["URIs"] = matches["uri"]
|
||||||
|
sources[uri]["Suites"].add(matches["suite"])
|
||||||
|
sources[uri]["Components"].update(matches["components"].split(' '))
|
||||||
|
else:
|
||||||
|
source = {
|
||||||
|
"Types": {matches['type']},
|
||||||
|
"URIs": matches['uri'],
|
||||||
|
"Enabled": "yes",
|
||||||
|
}
|
||||||
|
|
||||||
|
if matches.group('suite'):
|
||||||
|
source["Suites"] = set(matches['suite'].split(' '))
|
||||||
|
|
||||||
|
if matches.group('components'):
|
||||||
|
source["Components"] = set(matches['components'].split(' '))
|
||||||
|
|
||||||
|
if "arch" in options:
|
||||||
|
if "Architectures" in source:
|
||||||
|
source["Architectures"].append(options["arch"])
|
||||||
|
else:
|
||||||
|
source["Architectures"] = {options["arch"]}
|
||||||
|
|
||||||
|
if "signed-by" in options:
|
||||||
|
if "Signed-by" in source:
|
||||||
|
source["Signed-by"].append(options["signed-by"])
|
||||||
|
else:
|
||||||
|
source["Signed-by"] = {options["signed-by"]}
|
||||||
|
|
||||||
|
if "lang" in options:
|
||||||
|
if "Languages" in source:
|
||||||
|
source["Languages"].append(options["lang"])
|
||||||
|
else:
|
||||||
|
source["Languages"] = {options["lang"]}
|
||||||
|
|
||||||
|
if "target" in options:
|
||||||
|
if "Targets" in source:
|
||||||
|
source["Targets"].append(options["target"])
|
||||||
|
else:
|
||||||
|
source["Targets"] = {options["target"]}
|
||||||
|
|
||||||
|
sources[uri] = source
|
||||||
|
|
||||||
|
for i, (uri, source) in enumerate(sources.items()):
|
||||||
|
if i > 0:
|
||||||
|
print("")
|
||||||
|
for key, value in source.items():
|
||||||
|
if isinstance(value, str):
|
||||||
|
print("{}: {}".format(key, value) )
|
||||||
|
else:
|
||||||
|
print("{}: {}".format(key, ' '.join(value)) )
|
||||||
|
i += 1
|
48
apt/files/deb822-migration.sh
Normal file
48
apt/files/deb822-migration.sh
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
deb822_migrate_script=$(command -v deb822-migration.py)
|
||||||
|
|
||||||
|
if [ -z "${deb822_migrate_script}" ]; then
|
||||||
|
deb822_migrate_script="./deb822-migration.py"
|
||||||
|
fi
|
||||||
|
if [ ! -x "${deb822_migrate_script}" ]; then
|
||||||
|
>&2 echo "ERROR: '${deb822_migrate_script}' not found or not executable"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
dest_dir="/etc/apt/sources.list.d"
|
||||||
|
rc=0
|
||||||
|
|
||||||
|
migrate_file() {
|
||||||
|
legacy_file=$1
|
||||||
|
deb822_file=$2
|
||||||
|
|
||||||
|
if [ -f "${legacy_file}" ]; then
|
||||||
|
if [ -f "${deb822_file}" ]; then
|
||||||
|
>&2 echo "ERROR: '${deb822_file}' already exists"
|
||||||
|
rc=2
|
||||||
|
else
|
||||||
|
${deb822_migrate_script} "${legacy_file}" > "${deb822_file}"
|
||||||
|
if [ $? -eq 0 ] && [ -f "${deb822_file}" ]; then
|
||||||
|
mv "${legacy_file}" "${legacy_file}.bak"
|
||||||
|
echo "Migrated ${legacy_file} to ${deb822_file} and renamed to ${legacy_file}.bak"
|
||||||
|
else
|
||||||
|
>&2 echo "ERROR: failed to convert '${legacy_file}' to '${deb822_file}'"
|
||||||
|
rc=2
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
>&2 echo "ERROR: '${legacy_file}' not found"
|
||||||
|
rc=2
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
migrate_file "/etc/apt/sources.list" "${dest_dir}/system.sources"
|
||||||
|
|
||||||
|
# shellcheck disable=SC2044
|
||||||
|
for legacy_file in $(find /etc/apt/sources.list.d -mindepth 1 -maxdepth 1 -type f -name '*.list'); do
|
||||||
|
deb822_file=$(basename "${legacy_file}" .list)
|
||||||
|
migrate_file "${legacy_file}" "${dest_dir}/${deb822_file}.sources"
|
||||||
|
done
|
||||||
|
|
||||||
|
exit ${rc}
|
31
apt/tasks/migrate-to-deb822.yml
Normal file
31
apt/tasks/migrate-to-deb822.yml
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
---
|
||||||
|
- include_role:
|
||||||
|
name: evolix/remount-usr
|
||||||
|
|
||||||
|
- name: /usr/share/scripts exists
|
||||||
|
file:
|
||||||
|
dest: /usr/share/scripts
|
||||||
|
mode: "0700"
|
||||||
|
owner: root
|
||||||
|
group: root
|
||||||
|
state: directory
|
||||||
|
tags:
|
||||||
|
- apt
|
||||||
|
|
||||||
|
- name: Migration scripts are installed
|
||||||
|
copy:
|
||||||
|
src: "{{ item }}"
|
||||||
|
dest: "/usr/share/scripts/{{ item }}"
|
||||||
|
force: yes
|
||||||
|
mode: "0755"
|
||||||
|
loop:
|
||||||
|
- deb822-migration.py
|
||||||
|
- deb822-migration.sh
|
||||||
|
tags:
|
||||||
|
- apt
|
||||||
|
|
||||||
|
- name: Exec migration script
|
||||||
|
command: /usr/share/scripts/deb822-migration.sh
|
||||||
|
ignore_errors: yes
|
||||||
|
tags:
|
||||||
|
- apt
|
Loading…
Reference in a new issue