Skip to content

Commit

Permalink
Implement purge/--pre-cleanup option (#86)
Browse files Browse the repository at this point in the history
* Implement the purge action
* Implement --pre-cleaup option

---------
Signed-off-by: Jose Luis Rivero <[email protected]>
  • Loading branch information
j-rivero authored Apr 12, 2024
1 parent 982bf15 commit badb80c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
22 changes: 19 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,27 @@ jobs:
- name: Test plugins _TEST.py
run: |
python3 plugins/*_TEST.py
- name: Smoke system tests
- name: Smoke system tests for repository module
run: |
sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \
python3 ./gzdev.py repository enable osrf stable
python3 gzdev.py repository list
repo_to_test="osrf"
repo_type_to_test="stable"
# Test --pre-cleanup
sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \
python3 ./gzdev.py repository enable ${repo_to_test} ${repo_type_to_test}
sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \
python3 ./gzdev.py repository --pre-cleanup enable ${repo_to_test} ${repo_type_to_test} >log
grep "/etc/apt/sources.list.d/_gzdev_${repo_to_test}_${repo_type_to_test}.list" log
grep "/usr/share/keyrings/_gzdev_${repo_to_test}_${repo_type_to_test}.gpg" log
# Test the purge action
sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \
python3 ./gzdev.py repository enable ${repo_to_test} ${repo_type_to_test}
sudo env "PYTHONPATH=$pythonLocation/lib/python${{ matrix.python-version}}/site-packages" \
python3 ./gzdev.py repository purge
grep "/etc/apt/sources.list.d/_gzdev_${repo_to_test}_${repo_type_to_test}.list" log
grep "/usr/share/keyrings/_gzdev_${repo_to_test}_${repo_type_to_test}.gpg" log
- name: Smoke system tests for ign-docker module
run: |
python3 gzdev.py ign-docker-env citadel
python3 gzdev.py ign-docker-env dome --linux-distro ubuntu:bionic
python3 gzdev.py ign-docker-env dome --linux-distro ubuntu:focal --vol /tmp:/foo::/tmp:/bar --rocker-args '--dev-helpers'
47 changes: 41 additions & 6 deletions plugins/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Usage:
gzdev repository (ACTION) [<repo-name>] [<repo-type>]
[--project=<project_name>] [--force-linux-distro=<distro>]
[--keyserver=<keyserver>] [--gpg-check]
[--keyserver=<keyserver>] [--gpg-check] [--pre-cleanup]
gzdev repository list
gzdev repository (-h | --help)
gzdev repository --version
Expand All @@ -15,13 +15,15 @@
enable Enable repository in the system
disable Disable repository (if present)
list List repositories enabled
purge Remove all configurations installed by gzdev
Options:
-h --help Show this screen
--version Show gzdev's version
--gpg-check Do run a gpg check for validating the key
downloaded in enable action
(need the gpg binary)
--pre-cleanup Run 'purge' action before proceeding
"""

import distro
Expand All @@ -37,6 +39,9 @@
from docopt import docopt


GZDEV_FILE_PREFIX = '_gzdev_'


def _check_call(cmd):
print('')
print("Invoking '%s'" % ' '.join(cmd))
Expand Down Expand Up @@ -128,13 +133,13 @@ def get_repo_url(repo_name, repo_type, config):


def get_sources_list_file_path(repo_name, repo_type):
filename = '_gzdev_' + repo_name + '_' + repo_type + '.list'
filename = f'{GZDEV_FILE_PREFIX}{repo_name}_{repo_type}.list'
directory = '/etc/apt/sources.list.d'
return directory + '/' + filename


def key_filepath(repo_name, repo_type):
return f"/usr/share/keyrings/_gzdev_{repo_name}_{repo_type}.gpg"
return f"/usr/share/keyrings/{GZDEV_FILE_PREFIX}{repo_name}_{repo_type}.gpg"


def assert_key_in_file(key, key_path):
Expand Down Expand Up @@ -219,13 +224,18 @@ def normalize_args(args):
project = args['--project']
force_linux_distro = args['--force-linux-distro']
gpg_check = args['--gpg_check'] if '--gpg_check' in args else False
pre_cleanup = args['--pre-cleanup'] if '--pre-cleanup' in args else False
if pre_cleanup and action != 'enable':
error('--pre-cleanup is only supported in the "enable" action'
f'(not in {action})')
if force_linux_distro:
linux_distro = force_linux_distro
else:
linux_distro = distro.codename()
if '--keyserver' in args:
if '--keyserver' in args and args['--keyserver']:
warn('--keyserver option is deprecated. It is safe to remove it')
return action, repo_name, repo_type, project, linux_distro, gpg_check
return action, repo_name, repo_type, project, linux_distro, gpg_check, \
pre_cleanup


def validate_input(args):
Expand All @@ -249,7 +259,10 @@ def process_project_install(project, config, linux_distro, gpg_check,


def process_input(args, config):
action, repo_name, repo_type, project, linux_distro, gpg_check = args
action, repo_name, repo_type, project, linux_distro, gpg_check, \
pre_cleanup = args

remove_all_installed() if pre_cleanup else None

if (action == 'enable'):
if project:
Expand All @@ -267,6 +280,28 @@ def process_input(args, config):
gpg_check)
elif (action == 'disable'):
disable_repo(repo_name)
elif (action == 'purge'):
remove_all_installed()


def remove_file_by_pattern(directory, pattern):
for filename in os.listdir(directory):
if pattern.match(filename):
filepath = os.path.join(directory, filename)
try:
os.remove(filepath)
print(f'Removed: {filepath}')
except OSError as e:
print(f'Error: {filepath} - {e}')


def remove_all_installed():
# Remove installed apt directories
remove_file_by_pattern('/etc/apt/sources.list.d/',
re.compile(r'^' + GZDEV_FILE_PREFIX + '(.*)\\.list'))
# Remove installed keys
remove_file_by_pattern('/usr/share/keyrings/',
re.compile(r'^' + GZDEV_FILE_PREFIX + '(.*)\\.gpg'))


def main():
Expand Down

0 comments on commit badb80c

Please sign in to comment.