-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
224 lines (172 loc) · 4.55 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
# -*- coding: utf-8 -*-
"""
Invoke - Tasks
==============
"""
from __future__ import print_function, unicode_literals
import hashlib
import hmac
import os
import requests
from invoke import task
from invoke.exceptions import Failure
from colour.utilities import message_box
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2018-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '[email protected]'
__status__ = 'Production'
__all__ = [
'APPLICATION_NAME', 'ORG', 'CONTAINER', 'clean', 'formatting',
'docker_build', 'docker_remove', 'docker_run'
]
APPLICATION_NAME = 'Colour - Webhook'
ORG = 'colourscience'
VERSION = '0.1.0'
CONTAINER = APPLICATION_NAME.replace(' ', '').lower()
@task
def clean(ctx, bytecode=False):
"""
Cleans the project.
Parameters
----------
bytecode : bool, optional
Whether to clean the bytecode files, e.g. *.pyc* files.
Returns
-------
bool
Task success.
"""
message_box('Cleaning project...')
patterns = []
if bytecode:
patterns.append('**/*.pyc')
for pattern in patterns:
ctx.run("rm -rf {}".format(pattern))
@task
def formatting(ctx, yapf=False):
"""
Formats the codebase with *Yapf*.
Parameters
----------
ctx : invoke.context.Context
Context.
yapf : bool, optional
Whether to format the codebase with *Yapf*.
Returns
-------
bool
Task success.
"""
if yapf:
message_box('Formatting codebase with "Yapf"...')
ctx.run('yapf -p -i -r .')
@task
def docker_build(ctx):
"""
Builds the *docker* image.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Building "docker" image...')
ctx.run('docker build -t {0}/{1}:latest -t {0}/{1}:v{2} .'.format(
ORG, CONTAINER, VERSION))
@task
def docker_remove(ctx):
"""
Stops and remove the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Stopping "docker" container...')
try:
ctx.run('docker stop {0}'.format(CONTAINER))
except Failure:
pass
message_box('Removing "docker" container...')
try:
ctx.run('docker rm {0}'.format(CONTAINER))
except Failure:
pass
@task(docker_remove, docker_build)
def docker_run(ctx):
"""
Runs the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Running "docker" container...')
ctx.run('docker run -d '
'--name={1} '
'-e GITHUB_WEBHOOK_SECRET=$GITHUB_WEBHOOK_SECRET '
'-p 9010:9000 '
'-v {2}:/etc/colour-webhook/hooks '
'-v {3}:/etc/colour-webhook/commands '
'-v {4}:/mnt/colour-science.org '
'{0}/{1} '
'-verbose '
'-hotreload '
'-template '
'-hooks=/etc/colour-webhook/hooks/hooks.json '.format(
ORG, CONTAINER, os.path.abspath('hooks'),
os.path.abspath('commands'),
os.path.abspath(os.path.join('..', 'colour-science.org'))))
@task
def docker_push(ctx):
"""
Pushes the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Pushing "docker" container...')
ctx.run('docker push {0}/{1}'.format(ORG, CONTAINER))
@task
def emulate_github_webhook(ctx):
"""
Emulates *Github* webhook.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Emulating "Github" webhook...')
github_webhook_secret = os.environ.get('GITHUB_WEBHOOK_SECRET')
with open(os.path.join('resources', 'payload.json'), 'r') as payload_file:
payload_content = payload_file.read()
data = '{{"ref": "{0}"}}'.format(payload_content)
digest = hmac.new(
github_webhook_secret, data, digestmod=hashlib.sha1).hexdigest()
headers = {'X-Hub-Signature': 'sha1={0}'.format(digest)}
print(requests.post(
'http://0.0.0.0:9010/hooks/colour-science.org',
headers=headers,
data=data).text)