Move to app folder
This commit is contained in:
parent
e96253d542
commit
f5163cd4c2
61
src/main.py
61
src/main.py
|
@ -1,61 +0,0 @@
|
||||||
from aiohttp import web
|
|
||||||
import os
|
|
||||||
import hmac
|
|
||||||
from pack import Pack
|
|
||||||
|
|
||||||
class App:
|
|
||||||
def __init__(self):
|
|
||||||
self.pack = Pack()
|
|
||||||
print("Done")
|
|
||||||
self.app = web.Application()
|
|
||||||
self.app.add_routes([web.post('/update', self.updatePack)])
|
|
||||||
self.app.router.add_static('/files', "./files")
|
|
||||||
print("Started")
|
|
||||||
web.run_app(self.app)
|
|
||||||
|
|
||||||
async def updatePack(self, request):
|
|
||||||
try:
|
|
||||||
json = await request.json()
|
|
||||||
text = await request.read()
|
|
||||||
except:
|
|
||||||
return web.Response(status=500, text="Invalid json or text")
|
|
||||||
|
|
||||||
header_signature = request.headers.get('X-Hub-Signature')
|
|
||||||
print("Hook recieved")
|
|
||||||
if "ref" in json and json["ref"] == 'refs/heads/release':
|
|
||||||
if (status := self.verify_signature(text, header_signature)) == 200:
|
|
||||||
self.pack.pull()
|
|
||||||
self.pack.collate()
|
|
||||||
self.pack.compress()
|
|
||||||
self.pack.hash()
|
|
||||||
self.pack.upload()
|
|
||||||
else:
|
|
||||||
status = 404
|
|
||||||
print(status)
|
|
||||||
return web.Response(status=status)
|
|
||||||
|
|
||||||
def verify_signature(self, request_data, header_signature):
|
|
||||||
# do not store your secret key in your code, pull from environment variable
|
|
||||||
secret_key = os.environ.get('WEBHOOK_SECRET')
|
|
||||||
|
|
||||||
if not header_signature:
|
|
||||||
return 404
|
|
||||||
|
|
||||||
# separate the signature from the sha1 indication
|
|
||||||
sha_name, signature = header_signature.split('=')
|
|
||||||
if sha_name != 'sha1':
|
|
||||||
return 501
|
|
||||||
|
|
||||||
# create a new hmac with the secret key and the request data
|
|
||||||
mac = hmac.new(secret_key.encode(), msg=request_data, digestmod='sha1')
|
|
||||||
|
|
||||||
# verify the digest matches the signature
|
|
||||||
if not hmac.compare_digest(mac.hexdigest(), signature):
|
|
||||||
return 404
|
|
||||||
return 200
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
app = App()
|
|
||||||
|
|
||||||
|
|
||||||
|
|
76
src/pack.py
76
src/pack.py
|
@ -1,76 +0,0 @@
|
||||||
import os
|
|
||||||
import re
|
|
||||||
import shutil
|
|
||||||
from shutil import make_archive
|
|
||||||
import hashlib
|
|
||||||
from b2sdk.v1 import InMemoryAccountInfo
|
|
||||||
from b2sdk.v1 import B2Api
|
|
||||||
|
|
||||||
class Pack:
|
|
||||||
def __init__(self):
|
|
||||||
command = "git clone -b {branch} --single-branch {url} /files/resourcepack" \
|
|
||||||
.format(branch=os.environ.get("BRANCH"), url=os.environ.get("RESOURCE_PACK_URL"))
|
|
||||||
print(command)
|
|
||||||
status = os.system(command)
|
|
||||||
print(status)
|
|
||||||
|
|
||||||
info = InMemoryAccountInfo() # store credentials, tokens and cache in memory
|
|
||||||
self.b2_api = B2Api(info)
|
|
||||||
self.b2_api.authorize_account("production", os.environ.get("B2_ID"),
|
|
||||||
os.environ.get("B2_KEY"))
|
|
||||||
self.bucket = self.b2_api.get_bucket_by_name(os.environ.get("BUCKET"))
|
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
shutil.rmtree("/files/resourcepack")
|
|
||||||
|
|
||||||
def pull(self) -> None:
|
|
||||||
os.system("git -C /files/resourcepack pull origin {}".format(os.environ.get("BRANCH")))
|
|
||||||
|
|
||||||
def collate(self) -> None:
|
|
||||||
if os.path.exists("/files/pack"):
|
|
||||||
shutil.rmtree("/files/pack")
|
|
||||||
os.mkdir("/files/pack")
|
|
||||||
shutil.copytree("/files/resourcepack/assets", "/files/pack/assets")
|
|
||||||
shutil.copy("/files/resourcepack/pack.mcmeta", "/files/pack/pack.mcmeta")
|
|
||||||
shutil.copy("/files/resourcepack/pack.png", "/files/pack/pack.png")
|
|
||||||
|
|
||||||
|
|
||||||
def compress(self) -> None:
|
|
||||||
shutil.make_archive("/files/pack", 'zip', "/files/pack")
|
|
||||||
shutil.rmtree("/files/pack")
|
|
||||||
|
|
||||||
def hash(self):
|
|
||||||
sha1sum = hashlib.sha1()
|
|
||||||
with open("/files/pack.zip", "rb") as pack:
|
|
||||||
block = pack.read(2**16)
|
|
||||||
while len(block) != 0:
|
|
||||||
sha1sum.update(block)
|
|
||||||
block = pack.read(2**16)
|
|
||||||
self.hash = sha1sum.hexdigest()
|
|
||||||
with open("/files/hash", 'w') as hashfile:
|
|
||||||
hashfile.write(self.hash)
|
|
||||||
|
|
||||||
def upload(self):
|
|
||||||
self.b2_api.authorize_account("production", os.environ.get("B2_ID"),
|
|
||||||
os.environ.get("B2_KEY"))
|
|
||||||
self.bucket.upload_local_file(
|
|
||||||
local_file="files/pack.zip",
|
|
||||||
file_name="pack.zip")
|
|
||||||
|
|
||||||
self.bucket.upload_local_file(
|
|
||||||
local_file="/files/hash",
|
|
||||||
file_name="hash")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
print("Pack")
|
|
||||||
pack = Pack()
|
|
||||||
# pack.pull()
|
|
||||||
# pack.collate()
|
|
||||||
# pack.compress()
|
|
||||||
# pack.hash()
|
|
||||||
# pack.upload()
|
|
||||||
#pack = None
|
|
Loading…
Reference in New Issue