forked from nautobot/nautobot-app-device-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
375 lines (312 loc) · 13.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
"""Tasks for use with Invoke.
(c) 2020-2021 Network To Code
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
from invoke import task
PYTHON_VER = os.getenv("PYTHON_VER", "3.7")
NAUTOBOT_VER = os.getenv("NAUTOBOT_VER", "1.0.0b4")
# Name of the docker image/container
NAME = os.getenv("IMAGE_NAME", "nautobot-device-onboarding")
PWD = os.getcwd()
COMPOSE_FILE = "development/docker-compose.yml"
BUILD_NAME = "nautobot_device_onboarding"
# ------------------------------------------------------------------------------
# BUILD
# ------------------------------------------------------------------------------
@task
def build(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER, nocache=False, forcerm=False):
"""Build all docker images.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot version to use to build the container
python_ver (str): Will use the Python version docker image to build from
nocache (bool): Do not use cache when building the image
forcerm (bool): Always remove intermediate containers
"""
command = "build"
if nocache:
command += " --no-cache"
if forcerm:
command += " --force-rm"
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} {command}",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# START / STOP / DEBUG
# ------------------------------------------------------------------------------
@task
def debug(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Start Nautobot and its dependencies in debug mode.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Starting Nautobot .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
@task
def start(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Start Nautobot and its dependencies in detached mode.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Starting Nautobot in detached mode.. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up -d",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
@task
def stop(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Stop Nautobot and its dependencies.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot version to use to build the container
python_ver (str): Will use the Python version docker image to build from
"""
print("Stopping Nautobot .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
@task
def destroy(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Destroy all containers and volumes.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
context.run(
f"docker volume rm -f {BUILD_NAME}_pgdata_nautobot_device_onboarding",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# ACTIONS
# ------------------------------------------------------------------------------
@task
def nbshell(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Launch a nbshell session.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot nautobot-server nbshell",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def cli(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Launch a bash shell inside the running Nautobot container.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot bash",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def create_user(context, user="admin", nautobot_ver=NAUTOBOT_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
nautobot_ver (str): Nautobot 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 nautobot nautobot-server createsuperuser --username {user}",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def makemigrations(context, name="", nautobot_ver=NAUTOBOT_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
nautobot_ver (str): Nautobot 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={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
if name:
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run nautobot nautobot-server makemigrations --name {name}",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
else:
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run nautobot nautobot-server makemigrations",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
)
# ------------------------------------------------------------------------------
# TESTS / LINTING
# ------------------------------------------------------------------------------
@task
def unittest(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run Django unit tests for the plugin.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f'{docker} sh -c "nautobot-server test nautobot_device_onboarding"',
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def pylint(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run pylint code analysis.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
# 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 pylint\"",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def black(context, nautobot_ver=NAUTOBOT_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
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f'{docker} sh -c "cd /source && black --check --diff ."',
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def blacken(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run black to format Python files to adhere to its style standards.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f'{docker} sh -c "cd /source && black ."',
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def pydocstyle(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run pydocstyle to validate docstring formatting adheres to NTC defined standards.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
# 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={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def flake8(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""This will run flake8 for the specified name and Python version.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f"{docker} sh -c \"cd /source && find . -name '*.py' | xargs flake8\"",
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def yamllint(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run yamllint to validate formatting adheres to NTC defined YAML standards.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f'{docker} sh -c "cd /source && yamllint ."',
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def bandit(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run bandit to validate basic static code security analysis.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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 nautobot"
context.run(
f'{docker} sh -c "cd /source && bandit --configfile .bandit.yml --recursive ./"',
env={"NAUTOBOT_VER": nautobot_ver, "PYTHON_VER": python_ver},
pty=True,
)
@task
def tests(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""Run all tests for this plugin.
Args:
context (obj): Used to run specific commands
nautobot_ver (str): Nautobot 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, nautobot_ver=nautobot_ver, python_ver=python_ver)
print("Running yamllint...")
yamllint(context, NAME, python_ver)
print("Running bandit...")
bandit(context, nautobot_ver=nautobot_ver, python_ver=python_ver)
# print("Running pydocstyle...")
# pydocstyle(context, nautobot_ver=nautobot_ver, python_ver=python_ver)
print("Running flake8...")
flake8(context, nautobot_ver=nautobot_ver, python_ver=python_ver)
# print("Running pylint...")
# pylint(context, nautobot_ver=nautobot_ver, python_ver=python_ver)
print("Running unit tests...")
unittest(context, nautobot_ver=nautobot_ver, python_ver=python_ver)
print("All tests have passed!")