-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
227 lines (169 loc) · 6.47 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
"""Invoke Linting Task Automation."""
import os
from invoke import Collection
from invoke import task as invoke_task
namespace = Collection("netopsio")
namespace.configure(
{
"netopsio": {
"project_name": "netopsio",
"local": False,
"compose_dir": os.path.join(os.path.dirname(__file__), "development/"),
"compose_file": "docker-compose.yaml",
}
}
)
def task(function=None, *args, **kwargs):
"""Task decorator to override the default Invoke task decorator."""
def task_wrapper(function=None):
"""Wrapper around invoke.task to add the task to the namespace as well."""
if args or kwargs:
task_func = invoke_task(*args, **kwargs)(function)
else:
task_func = invoke_task(function)
namespace.add_task(task_func)
return task_func
if function:
# The decorator was called with no arguments
return task_wrapper(function)
# The decorator was called with arguments
return task_wrapper
def docker_compose(context, command, **kwargs):
"""Helper function for running a specific docker-compose command with all appropriate parameters and environment.
Args:
context (obj): Used to run specific commands
command (str): Command string to append to the "docker-compose ..." command, such as "build", "up", etc.
**kwargs: Passed through to the context.run() call.
"""
compose_command = f'docker-compose --project-name {context.netopsio.project_name} --project-directory "{context.netopsio.compose_dir}"'
compose_file_path = os.path.join(
context.netopsio.compose_dir, context.netopsio.compose_file
)
compose_command += f' -f "{compose_file_path}"'
compose_command += f" {command}"
# If `service` was passed as a kwarg, add it to the end.
service = kwargs.pop("service", None)
if service is not None:
compose_command += f" {service}"
print(f'Running docker-compose command "{command}"')
return context.run(compose_command, **kwargs)
# Linting Tasks
@task
def isort(context):
"""Execute Python isort."""
context.run("isort .")
@task
def black(context):
"""Execute Python Black."""
context.run("black --check --diff .")
@task
def pylint(context):
"""Execute Python Pylint."""
context.run("pylint netopsio/*")
@task
def flake8(context):
"""Execute Python Flake8."""
context.run("flake8 netopsio/* --ignore=E501 --exclude=*/tests/*")
@task
def linting(context):
"""Execute Linting Tasks."""
isort(context)
black(context)
pylint(context)
flake8(context)
# Testing Tasks
@task
def coverage(context, container="app", percent=80):
"""Execute Coverage Test."""
omit = ["netopsio/asgi.py", "netopsio/wsgi.py", "*/tests*", "manage.py"]
coverage_cmd = "coverage run --source '.' manage.py test"
coverage_report = (
f"coverage report --fail-under={percent} -m --omit={','.join(omit)}"
)
docker_compose(context, f"exec {container} {coverage_cmd}", pty=True)
docker_compose(context, f"exec {container} {coverage_report}", pty=True)
@task
def unittest(context, container="app"):
"""Execute Unit Tests."""
command = "python manage.py test"
docker_compose(context, f"exec {container} {command}", pty=True)
@task
def tests(context):
"""Execute Linting and Testing."""
linting(context)
unittest(context)
coverage(context)
# Docker Tasks
@task(help={"service": "If specified, only affect this service."})
def build(context, service=None):
"""Build NetOps.io and its dependencies."""
print("Building NetOps.io.")
docker_compose(context, "build", service=service)
@task(help={"service": "If specified, only affect this service."})
def start(context, service=None):
"""Start NetOps.io and its dependencies in detached mode."""
print("Starting NetOps.io in detached mode...")
docker_compose(context, "up --detach", service=service)
@task(help={"service": "If specified, only affect this service."})
def restart(context, service=None):
"""Gracefully restart containers."""
print("Restarting NetOps.io.")
docker_compose(context, "restart", service=service)
@task(help={"service": "If specified, only affect this service."})
def stop(context, service=None):
"""Stop NetOps.io and its dependencies."""
print("Stopping NetOps.io.")
if not service:
docker_compose(context, "down")
else:
docker_compose(context, "stop", service=service)
@task
def destroy(context):
"""Destroy all containers and volumes."""
print("Destroying NetOps.io.")
docker_compose(context, "down --volumes")
# Django Tasks
@task(help={"container": "Name of the container to shell into"})
def cli(context, container="app"):
"""Launch a bash shell inside the running NetOps.io container."""
docker_compose(context, f"exec {container} bash", pty=True)
@task(
help={
"user": "Name of the superuser to create. (Default: admin)",
"container": "Name of the container to run the 'createsuperuser' command on. (Default: app)",
}
)
def createsuperuser(context, user="admin", container="app"):
"""Create a new NetOps.io superuser account (default: "admin"), will prompt for password."""
command = f"python manage.py createsuperuser --username {user}"
docker_compose(context, f"exec {container} {command}", pty=True)
@task(
help={
"name": "Use this name for migration file(s). If unspecified, a name will be generated.",
"container": "Name of the container to run the 'makemigrations' command on. (Default: app)",
}
)
def makemigrations(context, name="", container="app"):
"""Perform makemigrations operation in Django."""
command = "python manage.py makemigrations"
if name:
command += f" --name {name}"
docker_compose(context, f"exec {container} {command}", pty=True)
@task(
help={
"container": "Name of the container to run the 'migrate' command on. (Default: app)"
}
)
def migrate(context, container="app"):
"""Perform migrate operation in Django."""
command = "python manage.py migrate"
docker_compose(context, f"exec {container} {command}", pty=True)
@task
def shell(context, container="app"):
"""Enter the Django Shell."""
command = "python manage.py shell"
docker_compose(context, f"exec {container} {command}", pty=True)
@task
def logs(context, container="app"):
"""Enter the Django Shell."""
docker_compose(context, f"logs {container}", pty=True)