forked from networktocode/ntc-netbox-plugin-metrics-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
303 lines (251 loc) · 10.9 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""Tasks for use with Invoke."""
import os
from invoke import task
PYTHON_VER = os.getenv("PYTHON_VER", "3.7")
NETBOX_VER = os.getenv("NETBOX_VER", "master")
# Name of the docker image/container
NAME = os.getenv("IMAGE_NAME", "ntc-netbox-plugin-metrics-ext")
PWD = os.getcwd()
COMPOSE_FILE = "development/docker-compose.yml"
BUILD_NAME = "netbox_metrics_ext"
# ------------------------------------------------------------------------------
# BUILD
# ------------------------------------------------------------------------------
@task
def build(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Build all docker images.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} build --build-arg netbox_ver={netbox_ver} --build-arg python_ver={python_ver}",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# START / STOP / DEBUG
# ------------------------------------------------------------------------------
@task
def debug(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Start NetBox and its dependencies in debug mode.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Starting Netbox .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
@task
def start(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Start NetBox and its dependencies in detached mode.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Starting Netbox in detached mode.. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up -d",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
@task
def stop(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Stop NetBox and its dependencies.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Stopping Netbox .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
@task
def destroy(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Destroy all containers and volumes.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
context.run(
f"docker volume rm -f {BUILD_NAME}_pgdata_netbox_metrics_ext",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# ACTIONS
# ------------------------------------------------------------------------------
@task
def nbshell(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Launch a nbshell session.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox python manage.py nbshell",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def cli(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Launch a bash shell inside the running NetBox container.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox bash",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def create_user(context, user="admin", netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Create a new user in django (default: admin), will prompt for password.
Args:
context (obj): Used to run specific commands
user (str): name of the superuser to create
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox python manage.py createsuperuser --username {user}",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def makemigrations(context, name="", netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run Make Migration in Django.
Args:
context (obj): Used to run specific commands
name (str): Name of the migration to be created
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up -d postgres",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
if name:
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox python manage.py makemigrations --name {name}",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
else:
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox python manage.py makemigrations",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# TESTS / LINTING
# ------------------------------------------------------------------------------
@task
def unittest(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run Django unit tests for the plugin.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
context.run(
f'{docker} sh -c "python manage.py test netbox_metrics_ext"',
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def pylint(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run pylint code analysis.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
# We exclude the /migrations/ directory since it is autogenerated code
context.run(
f"{docker} sh -c \"cd /source && find . -name '*.py' -not -path '*/migrations/*' | "
'PYTHONPATH=/opt/netbox/netbox xargs pylint"',
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def black(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run black to check that Python files adhere to its style standards.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
context.run(
f'{docker} sh -c "cd /source && black --check --diff ."',
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def pydocstyle(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run pydocstyle to validate docstring formatting adheres to NTC defined standards.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
# We exclude the /migrations/ directory since it is autogenerated code
context.run(
f"{docker} sh -c \"cd /source && find . -name '*.py' -not -path '*/migrations/*' | xargs pydocstyle\"",
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def bandit(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run bandit to validate basic static code security analysis.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
context.run(
f'{docker} sh -c "cd /source && bandit --recursive ./"',
env={"NETBOX_VER": netbox_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def tests(context, netbox_ver=NETBOX_VER, python_ver=PYTHON_VER):
"""Run all tests for this plugin.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
# Sorted loosely from fastest to slowest
print("Running black...")
black(context, netbox_ver=netbox_ver, python_ver=python_ver)
print("Running bandit...")
bandit(context, netbox_ver=netbox_ver, python_ver=python_ver)
print("Running pydocstyle...")
pydocstyle(context, netbox_ver=netbox_ver, python_ver=python_ver)
print("Running pylint...")
pylint(context, netbox_ver=netbox_ver, python_ver=python_ver)
print("Running unit tests...")
unittest(context, netbox_ver=netbox_ver, python_ver=python_ver)
# print("Running yamllint...")
# yamllint(context, NAME, python_ver)
print("All tests have passed!")