forked from nautobot/nautobot-app-golden-config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
359 lines (291 loc) · 11.7 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
"""Tasks for use with Invoke."""
import os
from invoke import task
PYTHON_VER = os.getenv("PYTHON_VER", "3.7")
NAUTOBOT_VER = os.getenv("NAUTOBOT_VER", "1.0.1")
NAUTOBOT_SRC_URL = os.getenv("NAUTOBOT_SRC_URL", f"https://github.com/nautobot/nautobot/archive/{NAUTOBOT_VER}.tar.gz")
# Name of the docker image/container
NAME = os.getenv("IMAGE_NAME", "nautobot-golden-config")
PWD = os.getcwd()
COMPOSE_FILE = "development/docker-compose.yml"
COMPOSE_OVERRIDE = "docker-compose.override.yml"
BUILD_NAME = "nautobot_golden_config"
DEFAULT_ENV = {
"NAUTOBOT_VER": NAUTOBOT_VER,
"PYTHON_VER": PYTHON_VER,
"NAUTOBOT_SRC_URL": NAUTOBOT_SRC_URL,
}
COMPOSE_APPEND = ""
if os.path.isfile(COMPOSE_OVERRIDE):
COMPOSE_APPEND = f"-f {COMPOSE_OVERRIDE}"
COMPOSE_COMMAND = f"docker-compose -f {COMPOSE_FILE} {COMPOSE_APPEND} -p {BUILD_NAME}"
environment = DEFAULT_ENV
# ------------------------------------------------------------------------------
# BUILD
# ------------------------------------------------------------------------------
@task
def build(context, nautobot_ver=NAUTOBOT_VER, python_ver=PYTHON_VER):
"""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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} build",
env=DEFAULT_ENV,
)
# ------------------------------------------------------------------------------
# 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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
print("Starting Nautobot .. ")
context.run(
f"{COMPOSE_COMMAND} up",
env=DEFAULT_ENV,
)
@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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
print("Starting Nautobot in detached mode.. ")
context.run(
f"{COMPOSE_COMMAND} up -d",
env=DEFAULT_ENV,
)
@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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
print("Stopping Nautobot .. ")
context.run(
f"{COMPOSE_COMMAND} down",
env=DEFAULT_ENV,
)
@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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} down",
env=DEFAULT_ENV,
)
context.run(
f"docker volume rm -f {BUILD_NAME}_pgdata_nautobot_golden_config",
env=DEFAULT_ENV,
)
# ------------------------------------------------------------------------------
# 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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} run nautobot nautobot-server nbshell",
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} run nautobot bash",
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} run nautobot nautobot-server createsuperuser --username {user}",
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
context.run(
f"{COMPOSE_COMMAND} up -d postgres",
env=DEFAULT_ENV,
)
if name:
context.run(
f"{COMPOSE_COMMAND} run nautobot nautobot-server makemigrations nautobot_golden_config --name {name}",
env=DEFAULT_ENV,
)
else:
context.run(
f"{COMPOSE_COMMAND} run nautobot nautobot-server makemigrations nautobot_golden_config",
env=DEFAULT_ENV,
)
context.run(
f"{COMPOSE_COMMAND} down",
env=DEFAULT_ENV,
)
# ------------------------------------------------------------------------------
# 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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
docker = f"{COMPOSE_COMMAND} run --entrypoint='' nautobot "
context.run(
f'{docker} sh -c "nautobot-server test nautobot_golden_config"',
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
docker = f"{COMPOSE_COMMAND} run --entrypoint='' 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/*' | "
'PYTHONPATH=/source/development DJANGO_SETTINGS_MODULE=nautobot_config xargs pylint"',
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
docker = f"{COMPOSE_COMMAND} run --entrypoint='' nautobot "
context.run(
f'{docker} sh -c "cd /source && black --check --diff ."',
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
docker = f"{COMPOSE_COMMAND} run --entrypoint='' 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=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
docker = f"{COMPOSE_COMMAND} run --entrypoint='' nautobot "
context.run(
f'{docker} sh -c "cd /source && bandit --recursive ./ --configfile .bandit.yml"',
env=DEFAULT_ENV,
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
"""
DEFAULT_ENV[NAUTOBOT_VER] = nautobot_ver
DEFAULT_ENV[PYTHON_VER] = python_ver
# Sorted loosely from fastest to slowest
print("Running black...")
black(context, nautobot_ver=nautobot_ver, python_ver=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 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!")