Skip to content

Commit 7c3318d

Browse files
committed
#6529 - Adjusted the arguments. Added documentation
1 parent b7c0e8b commit 7c3318d

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

docs/customization/custom-scripts.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,20 @@ http://netbox/api/extras/scripts/example.MyReport/ \
259259
--data '{"data": {"foo": "somevalue", "bar": 123}, "commit": true}'
260260
```
261261

262+
### Via the CLI
263+
264+
Scripts can be run on the CLI by invoking the management command:
265+
266+
```
267+
python3 manage.py runscript [--commit] [--loglevel {debug,info,warning,error,critical}] --script <module>.<script> <data>
268+
```
269+
270+
The required ``--script <module>.<script>`` argument is the report to run where ``<module>`` is the name of the python file in the ``scripts`` directory without the ``.py`` extension and ``<script>`` is the name of the script class in the ``<module>`` to run.
271+
272+
The optional ``--loglevel`` argument is the desired logging level to output to the console.
273+
274+
The optional ``--commit`` argument will commit any changes in the script to the database.
275+
262276
## Example
263277

264278
Below is an example script that creates new objects for a planned site. The user is prompted for three variables:

netbox/extras/management/commands/runscript.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def add_arguments(self, parser):
2828
dest='loglevel',
2929
default='info',
3030
choices=['debug', 'info', 'warning', 'error', 'critical'])
31-
parser.add_argument('--script', help="Script to run", dest='script', required=True)
32-
parser.add_argument('--commit', help="Commit this script to database", dest='commit')
33-
parser.add_argument('--user', help="User script is running as", dest='user')
34-
parser.add_argument('data', help="Data as a JSON blob")
31+
parser.add_argument('--script', help="Script to run", required=True)
32+
parser.add_argument('--commit', help="Commit this script to database", action='store_true')
33+
parser.add_argument('--user', help="User script is running as")
34+
parser.add_argument('data', help="Data as a string encapsulated JSON blob")
3535

3636
def handle(self, *args, **options):
3737
def _run_script():
@@ -68,8 +68,8 @@ def _run_script():
6868
# Params
6969
script = options['script']
7070
loglevel = options['loglevel']
71+
commit = options['commit']
7172
data = json.loads(options['data']) if options['data'] is not None else None
72-
commit = True if options['commit'] in ['1', 'true', 'True'] else False
7373

7474
module, name = script.split('.', 1)
7575

0 commit comments

Comments
 (0)