From 5f39b6276de57a1597afe5663e5cc850e68bb6b2 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Fri, 16 Apr 2021 12:34:52 -0400 Subject: [PATCH] Add command to export custom inventory scripts to tar file --- .../commands/export_custom_scripts.py | 36 +++++++++++++++++++ awx/main/migrations/_inventory_source.py | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 awx/main/management/commands/export_custom_scripts.py diff --git a/awx/main/management/commands/export_custom_scripts.py b/awx/main/management/commands/export_custom_scripts.py new file mode 100644 index 0000000000..281fc886b3 --- /dev/null +++ b/awx/main/management/commands/export_custom_scripts.py @@ -0,0 +1,36 @@ +import tempfile +import tarfile +import stat +import os + +from awx.main.models.inventory import CustomInventoryScript + +from django.core.management.base import BaseCommand +from django.utils.text import slugify + + +class Command(BaseCommand): + + help = 'Export custom inventory scripts into a tarfile.' + + def add_arguments(self, parser): + parser.add_argument('--filename', dest='filename', type=str, default='custom_scripts.tar', help='Filename of the output tar file') + + def handle(self, **options): + tar_filename = options.get('filename') + + with tempfile.TemporaryDirectory() as tmpdirname: + with tarfile.open(tar_filename, "w") as tar: + + for cis in CustomInventoryScript.objects.all(): + # naming convention similar to project paths + slug_name = slugify(str(cis.name)).replace(u'-', u'_') + script_filename = u'_%d__%s' % (int(cis.pk), slug_name) + script_path = os.path.join(tmpdirname, script_filename) + + with open(script_path, 'w') as f: + f.write(cis.script) + os.chmod(script_path, stat.S_IRWXU) + tar.add(script_path, arcname=script_filename) + + print('Dump of old custom inventory scripts at {}'.format(tar_filename)) diff --git a/awx/main/migrations/_inventory_source.py b/awx/main/migrations/_inventory_source.py index b12cfa0918..e6a65a82d4 100644 --- a/awx/main/migrations/_inventory_source.py +++ b/awx/main/migrations/_inventory_source.py @@ -108,4 +108,4 @@ def delete_custom_inv_source(apps, schema_editor): src_ct = deletions['main.InventorySource'] if src_ct: logger.info('Deleted {} custom inventory script updates.'.format(src_ct)) - logger.warning('Custom inventory scripts have been removed, see awx-manage XXXXX') + logger.warning('Custom inventory scripts have been removed, see awx-manage export_custom_scripts')