forked from FlxPeters/netbox-plugin-prometheus-sd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
189 lines (163 loc) · 6.08 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
import os
from invoke import task
NETBOX_VER = os.getenv("NETBOX_VER", "latest")
# Name of the docker image/container
NAME = os.getenv("IMAGE_NAME", "netbox-plugin-prometheus")
PWD = os.getcwd()
COMPOSE_FILE = "develop/docker-compose.yml"
DOCKER_FILE = "develop/Dockerfile"
BUILD_NAME = "netbox_prometheus_sd"
# ------------------------------------------------------------------------------
# BUILD
# ------------------------------------------------------------------------------
@task
def build(context, netbox_ver=NETBOX_VER):
"""Build all docker images.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
"""
print(f"Build Netbox image for version {netbox_ver} ...")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} build --build-arg netbox_ver={netbox_ver}",
env={"NETBOX_VER": netbox_ver},
)
# ------------------------------------------------------------------------------
# START / STOP / DEBUG
# ------------------------------------------------------------------------------
@task
def debug(context, netbox_ver=NETBOX_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
"""
print("Starting Netbox .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up",
env={"NETBOX_VER": netbox_ver},
)
@task
def start(context, netbox_ver=NETBOX_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
"""
print("Starting Netbox in detached mode.. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} up -d",
env={"NETBOX_VER": netbox_ver},
)
@task
def stop(context, netbox_ver=NETBOX_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
"""
print("Stopping Netbox .. ")
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": netbox_ver},
)
@task
def destroy(context, netbox_ver=NETBOX_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
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} down",
env={"NETBOX_VER": netbox_ver},
)
context.run(
f"docker volume rm -f {BUILD_NAME}_pgdata_netbox_prometheus_sd",
env={"NETBOX_VER": netbox_ver},
)
# ------------------------------------------------------------------------------
# ACTIONS
# ------------------------------------------------------------------------------
@task
def nbshell(context, netbox_ver=NETBOX_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} exec netbox python manage.py nbshell",
env={"NETBOX_VER": netbox_ver},
pty=True,
)
@task
def cli(context, netbox_ver=NETBOX_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
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} exec netbox bash",
env={"NETBOX_VER": netbox_ver},
pty=True,
)
@task
def create_user(context, user="admin", netbox_ver=NETBOX_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
"""
context.run(
f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} "
f"exec netbox python manage.py createsuperuser --username {user}",
env={"NETBOX_VER": netbox_ver},
pty=True,
)
# ------------------------------------------------------------------------------
# TESTS / LINTING
# ------------------------------------------------------------------------------
@task
def unittest(context, netbox_ver=NETBOX_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
"""
docker = f"docker-compose -f {COMPOSE_FILE} -p {BUILD_NAME} run netbox"
context.run(
f'{docker} sh -c "python manage.py test netbox_prometheus_sd --keepdb"',
env={"NETBOX_VER": netbox_ver},
pty=True,
)
@task
def pylint(context, netbox_ver=NETBOX_VER):
"""Run pylint code analysis.
Args:
context (obj): Used to run specific commands
netbox_ver (str): NetBox version to use to build the container
"""
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 DJANGO_SETTINGS_MODULE=netbox.settings xargs pylint"',
env={"NETBOX_VER": netbox_ver},
pty=True,
)
@task
def tests(context, netbox_ver=NETBOX_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
"""
print("Running linter...")
pylint(context, netbox_ver=netbox_ver)
print("Running unit tests...")
unittest(context, netbox_ver=netbox_ver)
print("All tests have passed!")