scripts.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import inspect
  2. import json
  3. import logging
  4. import os
  5. import yaml
  6. from django import forms
  7. from django.conf import settings
  8. from django.core.validators import RegexValidator
  9. from django.utils import timezone
  10. from django.utils.functional import classproperty
  11. from django.utils.translation import gettext as _
  12. from extras.choices import LogLevelChoices
  13. from extras.models import ScriptModule
  14. from ipam.formfields import IPAddressFormField, IPNetworkFormField
  15. from ipam.validators import MaxPrefixLengthValidator, MinPrefixLengthValidator, prefix_validator
  16. from utilities.forms import add_blank_choice
  17. from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField
  18. from utilities.forms.widgets import DatePicker, DateTimePicker
  19. from .forms import ScriptForm
  20. __all__ = (
  21. 'BaseScript',
  22. 'BooleanVar',
  23. 'ChoiceVar',
  24. 'DateVar',
  25. 'DateTimeVar',
  26. 'FileVar',
  27. 'IntegerVar',
  28. 'IPAddressVar',
  29. 'IPAddressWithMaskVar',
  30. 'IPNetworkVar',
  31. 'MultiChoiceVar',
  32. 'MultiObjectVar',
  33. 'ObjectVar',
  34. 'Script',
  35. 'StringVar',
  36. 'TextVar',
  37. 'get_module_and_script',
  38. )
  39. #
  40. # Script variables
  41. #
  42. class ScriptVariable:
  43. """
  44. Base model for script variables
  45. """
  46. form_field = forms.CharField
  47. def __init__(self, label='', description='', default=None, required=True, widget=None):
  48. # Initialize field attributes
  49. if not hasattr(self, 'field_attrs'):
  50. self.field_attrs = {}
  51. if label:
  52. self.field_attrs['label'] = label
  53. if description:
  54. self.field_attrs['help_text'] = description
  55. if default:
  56. self.field_attrs['initial'] = default
  57. if widget:
  58. self.field_attrs['widget'] = widget
  59. self.field_attrs['required'] = required
  60. def as_field(self):
  61. """
  62. Render the variable as a Django form field.
  63. """
  64. form_field = self.form_field(**self.field_attrs)
  65. if not isinstance(form_field.widget, forms.CheckboxInput):
  66. if form_field.widget.attrs and 'class' in form_field.widget.attrs.keys():
  67. form_field.widget.attrs['class'] += ' form-control'
  68. else:
  69. form_field.widget.attrs['class'] = 'form-control'
  70. return form_field
  71. class StringVar(ScriptVariable):
  72. """
  73. Character string representation. Can enforce minimum/maximum length and/or regex validation.
  74. """
  75. def __init__(self, min_length=None, max_length=None, regex=None, *args, **kwargs):
  76. super().__init__(*args, **kwargs)
  77. # Optional minimum/maximum lengths
  78. if min_length:
  79. self.field_attrs['min_length'] = min_length
  80. if max_length:
  81. self.field_attrs['max_length'] = max_length
  82. # Optional regular expression validation
  83. if regex:
  84. self.field_attrs['validators'] = [
  85. RegexValidator(
  86. regex=regex,
  87. message='Invalid value. Must match regex: {}'.format(regex),
  88. code='invalid'
  89. )
  90. ]
  91. class TextVar(ScriptVariable):
  92. """
  93. Free-form text data. Renders as a <textarea>.
  94. """
  95. form_field = forms.CharField
  96. def __init__(self, *args, **kwargs):
  97. super().__init__(*args, **kwargs)
  98. self.field_attrs['widget'] = forms.Textarea
  99. class IntegerVar(ScriptVariable):
  100. """
  101. Integer representation. Can enforce minimum/maximum values.
  102. """
  103. form_field = forms.IntegerField
  104. def __init__(self, min_value=None, max_value=None, *args, **kwargs):
  105. super().__init__(*args, **kwargs)
  106. # Optional minimum/maximum values
  107. if min_value:
  108. self.field_attrs['min_value'] = min_value
  109. if max_value:
  110. self.field_attrs['max_value'] = max_value
  111. class BooleanVar(ScriptVariable):
  112. """
  113. Boolean representation (true/false). Renders as a checkbox.
  114. """
  115. form_field = forms.BooleanField
  116. def __init__(self, *args, **kwargs):
  117. super().__init__(*args, **kwargs)
  118. # Boolean fields cannot be required
  119. self.field_attrs['required'] = False
  120. class ChoiceVar(ScriptVariable):
  121. """
  122. Select one of several predefined static choices, passed as a list of two-tuples. Example:
  123. color = ChoiceVar(
  124. choices=(
  125. ('#ff0000', 'Red'),
  126. ('#00ff00', 'Green'),
  127. ('#0000ff', 'Blue')
  128. )
  129. )
  130. """
  131. form_field = forms.ChoiceField
  132. def __init__(self, choices, *args, **kwargs):
  133. super().__init__(*args, **kwargs)
  134. # Set field choices, adding a blank choice to avoid forced selections
  135. self.field_attrs['choices'] = add_blank_choice(choices)
  136. class DateVar(ScriptVariable):
  137. """
  138. A date.
  139. """
  140. form_field = forms.DateField
  141. def __init__(self, *args, **kwargs):
  142. super().__init__(*args, **kwargs)
  143. self.form_field.widget = DatePicker()
  144. class DateTimeVar(ScriptVariable):
  145. """
  146. A date and a time.
  147. """
  148. form_field = forms.DateTimeField
  149. def __init__(self, *args, **kwargs):
  150. super().__init__(*args, **kwargs)
  151. self.form_field.widget = DateTimePicker()
  152. class MultiChoiceVar(ScriptVariable):
  153. """
  154. Like ChoiceVar, but allows for the selection of multiple choices.
  155. """
  156. form_field = forms.MultipleChoiceField
  157. def __init__(self, choices, *args, **kwargs):
  158. super().__init__(*args, **kwargs)
  159. # Set field choices
  160. self.field_attrs['choices'] = choices
  161. class ObjectVar(ScriptVariable):
  162. """
  163. A single object within NetBox.
  164. :param model: The NetBox model being referenced
  165. :param query_params: A dictionary of additional query parameters to attach when making REST API requests (optional)
  166. :param context: A custom dictionary mapping template context variables to fields, used when rendering <option>
  167. elements within the dropdown menu (optional)
  168. :param null_option: The label to use as a "null" selection option (optional)
  169. """
  170. form_field = DynamicModelChoiceField
  171. def __init__(self, model, query_params=None, context=None, null_option=None, *args, **kwargs):
  172. super().__init__(*args, **kwargs)
  173. self.field_attrs.update({
  174. 'queryset': model.objects.all(),
  175. 'query_params': query_params,
  176. 'context': context,
  177. 'null_option': null_option,
  178. })
  179. class MultiObjectVar(ObjectVar):
  180. """
  181. Like ObjectVar, but can represent one or more objects.
  182. """
  183. form_field = DynamicModelMultipleChoiceField
  184. class FileVar(ScriptVariable):
  185. """
  186. An uploaded file.
  187. """
  188. form_field = forms.FileField
  189. class IPAddressVar(ScriptVariable):
  190. """
  191. An IPv4 or IPv6 address without a mask.
  192. """
  193. form_field = IPAddressFormField
  194. class IPAddressWithMaskVar(ScriptVariable):
  195. """
  196. An IPv4 or IPv6 address with a mask.
  197. """
  198. form_field = IPNetworkFormField
  199. class IPNetworkVar(ScriptVariable):
  200. """
  201. An IPv4 or IPv6 prefix.
  202. """
  203. form_field = IPNetworkFormField
  204. def __init__(self, min_prefix_length=None, max_prefix_length=None, *args, **kwargs):
  205. super().__init__(*args, **kwargs)
  206. # Set prefix validator and optional minimum/maximum prefix lengths
  207. self.field_attrs['validators'] = [prefix_validator]
  208. if min_prefix_length is not None:
  209. self.field_attrs['validators'].append(
  210. MinPrefixLengthValidator(min_prefix_length)
  211. )
  212. if max_prefix_length is not None:
  213. self.field_attrs['validators'].append(
  214. MaxPrefixLengthValidator(max_prefix_length)
  215. )
  216. #
  217. # Scripts
  218. #
  219. class BaseScript:
  220. """
  221. Base model for custom scripts. User classes should inherit from this model if they want to extend Script
  222. functionality for use in other subclasses.
  223. """
  224. # Prevent django from instantiating the class on all accesses
  225. do_not_call_in_templates = True
  226. class Meta:
  227. pass
  228. def __init__(self):
  229. self.messages = [] # Primary script log
  230. self.tests = {} # Mapping of logs for test methods
  231. self.output = ''
  232. self.failed = False
  233. self._current_test = None # Tracks the current test method being run (if any)
  234. # Initiate the log
  235. self.logger = logging.getLogger(f"netbox.scripts.{self.__module__}.{self.__class__.__name__}")
  236. # Declare the placeholder for the current request
  237. self.request = None
  238. # Compile test methods and initialize results skeleton
  239. for method in dir(self):
  240. if method.startswith('test_') and callable(getattr(self, method)):
  241. self.tests[method] = {
  242. LogLevelChoices.LOG_SUCCESS: 0,
  243. LogLevelChoices.LOG_INFO: 0,
  244. LogLevelChoices.LOG_WARNING: 0,
  245. LogLevelChoices.LOG_FAILURE: 0,
  246. 'log': [],
  247. }
  248. def __str__(self):
  249. return self.name
  250. @classproperty
  251. def module(self):
  252. return self.__module__
  253. @classproperty
  254. def class_name(self):
  255. return self.__name__
  256. @classproperty
  257. def full_name(self):
  258. return f'{self.module}.{self.class_name}'
  259. @classmethod
  260. def root_module(cls):
  261. return cls.__module__.split(".")[0]
  262. # Author-defined attributes
  263. @classproperty
  264. def name(self):
  265. return getattr(self.Meta, 'name', self.__name__)
  266. @classproperty
  267. def description(self):
  268. return getattr(self.Meta, 'description', '')
  269. @classproperty
  270. def field_order(self):
  271. return getattr(self.Meta, 'field_order', None)
  272. @classproperty
  273. def fieldsets(self):
  274. return getattr(self.Meta, 'fieldsets', None)
  275. @classproperty
  276. def commit_default(self):
  277. return getattr(self.Meta, 'commit_default', True)
  278. @classproperty
  279. def job_timeout(self):
  280. return getattr(self.Meta, 'job_timeout', None)
  281. @classproperty
  282. def scheduling_enabled(self):
  283. return getattr(self.Meta, 'scheduling_enabled', True)
  284. @property
  285. def filename(self):
  286. return inspect.getfile(self.__class__)
  287. @property
  288. def source(self):
  289. return inspect.getsource(self.__class__)
  290. @classmethod
  291. def _get_vars(cls):
  292. vars = {}
  293. # Iterate all base classes looking for ScriptVariables
  294. for base_class in inspect.getmro(cls):
  295. # When object is reached there's no reason to continue
  296. if base_class is object:
  297. break
  298. for name, attr in base_class.__dict__.items():
  299. if name not in vars and issubclass(attr.__class__, ScriptVariable):
  300. vars[name] = attr
  301. # Order variables according to field_order
  302. if not cls.field_order:
  303. return vars
  304. ordered_vars = {
  305. field: vars.pop(field) for field in cls.field_order if field in vars
  306. }
  307. ordered_vars.update(vars)
  308. return ordered_vars
  309. def run(self, data, commit):
  310. """
  311. Override this method with custom script logic.
  312. """
  313. # Backward compatibility for legacy Reports
  314. self.pre_run()
  315. self.run_tests()
  316. self.post_run()
  317. def get_job_data(self):
  318. """
  319. Return a dictionary of data to attach to the script's Job.
  320. """
  321. return {
  322. 'log': self.messages,
  323. 'output': self.output,
  324. 'tests': self.tests,
  325. }
  326. #
  327. # Form rendering
  328. #
  329. def get_fieldsets(self):
  330. fieldsets = []
  331. if self.fieldsets:
  332. fieldsets.extend(self.fieldsets)
  333. else:
  334. fields = list(name for name, _ in self._get_vars().items())
  335. fieldsets.append((_('Script Data'), fields))
  336. # Append the default fieldset if defined in the Meta class
  337. exec_parameters = ('_schedule_at', '_interval', '_commit') if self.scheduling_enabled else ('_commit',)
  338. fieldsets.append((_('Script Execution Parameters'), exec_parameters))
  339. return fieldsets
  340. def as_form(self, data=None, files=None, initial=None):
  341. """
  342. Return a Django form suitable for populating the context data required to run this Script.
  343. """
  344. # Create a dynamic ScriptForm subclass from script variables
  345. fields = {
  346. name: var.as_field() for name, var in self._get_vars().items()
  347. }
  348. FormClass = type('ScriptForm', (ScriptForm,), fields)
  349. form = FormClass(data, files, initial=initial)
  350. # Set initial "commit" checkbox state based on the script's Meta parameter
  351. form.fields['_commit'].initial = self.commit_default
  352. # Hide fields if scheduling has been disabled
  353. if not self.scheduling_enabled:
  354. form.fields['_schedule_at'].widget = forms.HiddenInput()
  355. form.fields['_interval'].widget = forms.HiddenInput()
  356. return form
  357. #
  358. # Logging
  359. #
  360. def _log(self, message, obj=None, level=LogLevelChoices.LOG_DEFAULT):
  361. """
  362. Log a message. Do not call this method directly; use one of the log_* wrappers below.
  363. """
  364. if level not in LogLevelChoices.values():
  365. raise ValueError(f"Invalid logging level: {level}")
  366. # A test method is currently active, so log the message using legacy Report logging
  367. if self._current_test:
  368. # Increment the event counter for this level
  369. if level in self.tests[self._current_test]:
  370. self.tests[self._current_test][level] += 1
  371. # Record message (if any) to the report log
  372. if message:
  373. # TODO: Use a dataclass for test method logs
  374. self.tests[self._current_test]['log'].append((
  375. timezone.now().isoformat(),
  376. level,
  377. str(obj) if obj else None,
  378. obj.get_absolute_url() if hasattr(obj, 'get_absolute_url') else None,
  379. str(message),
  380. ))
  381. elif message:
  382. # Record to the script's log
  383. self.messages.append({
  384. 'time': timezone.now().isoformat(),
  385. 'status': level,
  386. 'message': str(message),
  387. 'obj': str(obj) if obj else None,
  388. 'url': obj.get_absolute_url() if hasattr(obj, 'get_absolute_url') else None,
  389. })
  390. # Record to the system log
  391. if obj:
  392. message = f"{obj}: {message}"
  393. self.logger.log(LogLevelChoices.SYSTEM_LEVELS[level], message)
  394. def log_debug(self, message=None, obj=None):
  395. self._log(message, obj, level=LogLevelChoices.LOG_DEBUG)
  396. def log_success(self, message=None, obj=None):
  397. self._log(message, obj, level=LogLevelChoices.LOG_SUCCESS)
  398. def log_info(self, message=None, obj=None):
  399. self._log(message, obj, level=LogLevelChoices.LOG_INFO)
  400. def log_warning(self, message=None, obj=None):
  401. self._log(message, obj, level=LogLevelChoices.LOG_WARNING)
  402. def log_failure(self, message=None, obj=None):
  403. self._log(message, obj, level=LogLevelChoices.LOG_FAILURE)
  404. self.failed = True
  405. #
  406. # Convenience functions
  407. #
  408. def load_yaml(self, filename):
  409. """
  410. Return data from a YAML file
  411. """
  412. try:
  413. from yaml import CLoader as Loader
  414. except ImportError:
  415. from yaml import Loader
  416. file_path = os.path.join(settings.SCRIPTS_ROOT, filename)
  417. with open(file_path, 'r') as datafile:
  418. data = yaml.load(datafile, Loader=Loader)
  419. return data
  420. def load_json(self, filename):
  421. """
  422. Return data from a JSON file
  423. """
  424. file_path = os.path.join(settings.SCRIPTS_ROOT, filename)
  425. with open(file_path, 'r') as datafile:
  426. data = json.load(datafile)
  427. return data
  428. #
  429. # Legacy Report functionality
  430. #
  431. def run_tests(self):
  432. """
  433. Run the report and save its results. Each test method will be executed in order.
  434. """
  435. self.logger.info(f"Running report")
  436. try:
  437. for test_name in self.tests:
  438. self._current_test = test_name
  439. test_method = getattr(self, test_name)
  440. test_method()
  441. self._current_test = None
  442. except Exception as e:
  443. self._current_test = None
  444. self.post_run()
  445. raise e
  446. def pre_run(self):
  447. """
  448. Legacy method for operations performed immediately prior to running a Report.
  449. """
  450. pass
  451. def post_run(self):
  452. """
  453. Legacy method for operations performed immediately after running a Report.
  454. """
  455. pass
  456. class Script(BaseScript):
  457. """
  458. Classes which inherit this model will appear in the list of available scripts.
  459. """
  460. pass
  461. #
  462. # Functions
  463. #
  464. def is_variable(obj):
  465. """
  466. Returns True if the object is a ScriptVariable.
  467. """
  468. return isinstance(obj, ScriptVariable)
  469. def get_module_and_script(module_name, script_name):
  470. module = ScriptModule.objects.get(file_path=f'{module_name}.py')
  471. script = module.scripts.get(name=script_name)
  472. return module, script