mirror of
https://github.com/ansible/awx.git
synced 2026-03-21 19:07:39 -02:30
Add command to export custom inventory scripts to tar file
This commit is contained in:
36
awx/main/management/commands/export_custom_scripts.py
Normal file
36
awx/main/management/commands/export_custom_scripts.py
Normal file
@@ -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))
|
||||||
@@ -108,4 +108,4 @@ def delete_custom_inv_source(apps, schema_editor):
|
|||||||
src_ct = deletions['main.InventorySource']
|
src_ct = deletions['main.InventorySource']
|
||||||
if src_ct:
|
if src_ct:
|
||||||
logger.info('Deleted {} custom inventory script updates.'.format(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')
|
||||||
|
|||||||
Reference in New Issue
Block a user