-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathtasks.py
252 lines (199 loc) · 9.22 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
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-onboarding")
PWD = os.getcwd()
COMPOSE_FILE = "development/docker-compose.yml"
BUILD_NAME = "netbox_onboarding"
# ------------------------------------------------------------------------------
# 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
"""
result = 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):
"""Start NetBox and its dependencies in debug mode."""
print(f"Starting Netbox .. ")
result = 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):
"""Start NetBox and its dependencies in detached mode."""
print(f"Starting Netbox in detached mode.. ")
result = 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):
"""Stop NetBox and its dependencies."""
print(f"Stopping Netbox .. ")
result = 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):
"""Destroy all containers and volumes."""
result = context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": NETBOX_VER, "PYTHON_VER": PYTHON_VER},
)
result = context.run(
f"docker volume rm -f {BUILD_NAME}_pgdata_netbox_onboarding",
env={"NETBOX_VER": NETBOX_VER, "PYTHON_VER": PYTHON_VER},
)
# ------------------------------------------------------------------------------
# ACTIONS
# ------------------------------------------------------------------------------
@task
def nbshell(context):
"""Launch a nbshell session."""
result = 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):
"""Launch a bash shell inside the running NetBox container."""
result = 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"):
"""Create a new user in django (default: admin), will prompt for password"""
result = 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):
"""Run Make Migration in Django"""
result = context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up -d postgres",
env={"NETBOX_VER": NETBOX_VER, "PYTHON_VER": PYTHON_VER},
)
result = 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},
)
result = 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 pytest(context, name=NAME, python_ver=PYTHON_VER):
# """This will run pytest for the specified name and Python version.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# # pty is set to true to properly run the docker commands due to the invocation process of docker
# # https://docs.pyinvoke.org/en/latest/api/runners.html - Search for pty for more information
# # Install python module
# DOCKER = f"docker run -it -v {PWD}:/local {name}-{python_ver}:latest"
# context.run(f"{DOCKER} /bin/bash -c 'poetry install && pytest -vv'", pty=True)
# @task
# def pylint(context, name=NAME, python_ver=PYTHON_VER):
# """Run pytest for the specified name and Python version.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# 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 && find . -name '*.py' | PYTHONPATH=/opt/netbox/netbox xargs pylint --load-plugins pylint_django --persistent=n\"", pty=True)
# @task
# def black(context, name=NAME, python_ver=PYTHON_VER):
# """This will run black to check that Python files adherence to black standards.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# # pty is set to true to properly run the docker commands due to the invocation process of docker
# # https://docs.pyinvoke.org/en/latest/api/runners.html - Search for pty for more information
# DOCKER = f"docker run -it -v {PWD}:/local {name}-{python_ver}:latest"
# context.run(f"{DOCKER} black --check --diff .", pty=True)
# @task
# def pylama(context, name=NAME, python_ver=PYTHON_VER):
# """This will run pylama for the specified name and Python version.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# # pty is set to true to properly run the docker commands due to the invocation process of docker
# # https://docs.pyinvoke.org/en/latest/api/runners.html - Search for pty for more information
# DOCKER = f"docker run -it -v {PWD}:/local {name}-{python_ver}:latest"
# context.run(f"{DOCKER} pylama .", pty=True)
# @task
# def pydocstyle(context, name=NAME, python_ver=PYTHON_VER):
# """This will run pydocstyle to validate docstring formatting adheres to NTC defined standards.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# # pty is set to true to properly run the docker commands due to the invocation process of docker
# # https://docs.pyinvoke.org/en/latest/api/runners.html - Search for pty for more information
# DOCKER = f"docker run -it -v {PWD}:/local {name}-{python_ver}:latest"
# context.run(f"{DOCKER} pydocstyle .", pty=True)
# @task
# def bandit(context, name=NAME, python_ver=PYTHON_VER):
# """This will run bandit to validate basic static code security analysis.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# # pty is set to true to properly run the docker commands due to the invocation process of docker
# # https://docs.pyinvoke.org/en/latest/api/runners.html - Search for pty for more information
# DOCKER = f"docker run -it -v {PWD}:/local {name}-{python_ver}:latest"
# context.run(f"{DOCKER} bandit --recursive ./ --configfile .bandit.yml", pty=True)
# @task
# def tests(context, name=NAME, python_ver=PYTHON_VER):
# """This will run all tests for the specified name and Python version.
# Args:
# context (obj): Used to run specific commands
# name (str): Used to name the docker image
# python_ver (str): Will use the Python version docker image to build from
# """
# print("Running pytest...")
# pytest(context, NAME, python_ver)
# print("Running black...")
# black(context, NAME, python_ver)
# print("Running pylama...")
# pylama(context, NAME, python_ver)
# print("Running yamllint...")
# yamllint(context, NAME, python_ver)
# print("Running pydocstyle...")
# pydocstyle(context, NAME, python_ver)
# print("Running bandit...")
# bandit(context, NAME, python_ver)
# print("All tests have passed!")