Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 872e1cd4 authored by Frederico SEQUEIRA's avatar Frederico SEQUEIRA
Browse files

:hammer: Add possibility to add file and handle errors in entrypoint

parent 62733b09
No related branches found
No related tags found
1 merge request!140V3 custom script distribution
Pipeline #280218 passed
...@@ -27,7 +27,7 @@ migrate_func () { ...@@ -27,7 +27,7 @@ migrate_func () {
echo "⚙️ Building search index (lazy)" echo "⚙️ Building search index (lazy)"
./manage.py reindex --lazy ./manage.py reindex --lazy
echo "⚙️ Initialising data" echo "⚙️ Initialising data"
./manage.py runscript universal_init_data.InitializeJsonDataScript --data '{"file_path":"/opt/netbox/netbox/scripts/data/init_data.json"}' --commit ./manage.py runscript universal_init_data.InitializeJsonDataScript --data '{"file_path":"/opt/netbox/netbox/scripts/data/init_data.json"}' --commit || true
fi fi
} }
......
import json import json
import os import os
from django.contrib.auth.models import Group from django.contrib.auth.models import Group
from extras.scripts import Script, StringVar from extras.scripts import Script, StringVar, FileVar
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
class InitializeJsonDataScript(Script): class InitializeJsonDataScript(Script):
file_path = StringVar(description="Path to init data file") file_path = StringVar(description="Path to init data file")
json_file = FileVar(description="Upload a CSV file with roles configuration")
class Meta: class Meta:
name = "Initialize Data in NetBox" name = "Initialize Data in NetBox"
description = "Add data to NetBox" description = "Add data to NetBox via file or path"
field_order = ['file_path'] field_order = ['file_path','json_file']
def __init__(self):
super().__init__()
self.file_path.field_attrs['required'] = False
self.json_file.field_attrs['required'] = False
def run(self, data, commit): def run(self, data, commit):
file_path = data['file_path'] file_path = data.get('file_path',None)
json_file = data.get('json_file',None)
if not os.path.exists(file_path): if file_path:
self.log_failure(f"File not Found: {file_path}") if not os.path.exists(file_path):
return self.log_failure(f"File not Found: {file_path}")
return
if not(json_file) and file_path:
try:
with open(file_path, "r") as file:
json_data = json.load(file)
objects = json_data
except json.JSONDecodeError as e:
self.log_failure(f"Failed to parse file: {e}")
return
except Exception as e:
self.log_failure(f"Error reading file: {e}")
return
elif not(file_path) and json_file:
print("json_file",json_file)
json_data= json_file.read().decode("utf-8")
objects = json_data
try: else:
with open(file_path, "r") as file: self.log_failure("Please specify the path to the data file the data file itself")
json_data = json.load(file)
objects = json_data
except json.JSONDecodeError as e:
self.log_failure(f"Failed to parse file: {e}")
return
except Exception as e:
self.log_failure(f"Error reading file: {e}")
return return
# Process Data # Process Data
for obj_data in objects: for obj_data in objects:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment