This commit is contained in:
jimmy 2021-06-20 03:04:41 +00:00
parent ef2abe0ad7
commit 371665b8f7
3 changed files with 56 additions and 44 deletions

View File

@ -1,2 +1,7 @@
RESOURCE_PACK_URL= RESOURCE_PACK_URL=
GITHUB_WEBHOOK_SECRET= WEBHOOK_SECRET=
BRANCH=
BUCKET=
B2_ID=
B2_KEY=
GIT_SSH_COMMAND=

26
src/main.py Normal file → Executable file
View File

@ -1,20 +1,25 @@
from aiohttp import web from aiohttp import web
import os import os
import hmac import hmac
import pack from pack import Pack
class App: class App:
def __init__(self): def __init__(self):
self.pack = pack.Pack() #self.pack = Pack()
print("Done")
self.app = web.Application() self.app = web.Application()
self.app.add_routes([web.post('/update', self.updatePack)]) self.app.add_routes([web.post('/update', self.updatePack)])
self.app.router.add_static('/files', "./files") self.app.router.add_static('/files', "./files")
print("Started")
web.run_app(self.app) web.run_app(self.app)
async def updatePack(self, request): async def updatePack(self, request):
json = await request.json() try:
text = await request.read() 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') header_signature = request.headers.get('X-Hub-Signature')
print("Hook recieved") print("Hook recieved")
if "ref" in json and json["ref"] == 'refs/heads/release': if "ref" in json and json["ref"] == 'refs/heads/release':
@ -23,12 +28,15 @@ class App:
self.pack.collate() self.pack.collate()
self.pack.compress() self.pack.compress()
self.pack.hash() self.pack.hash()
print(status) self.pack.upload()
return web.Response(status=200) else:
status = 404
print(status)
return web.Response(status=status)
def verify_signature(self, request_data, header_signature): def verify_signature(self, request_data, header_signature):
# do not store your secret key in your code, pull from environment variable # do not store your secret key in your code, pull from environment variable
secret_key = os.environ.get('GITHUB_WEBHOOK_SECRET') secret_key = os.environ.get('WEBHOOK_SECRET')
if not header_signature: if not header_signature:
return 404 return 404
@ -47,7 +55,7 @@ class App:
return 200 return 200
if __name__ == '__main__': if __name__ == '__main__':
app = App() app = App()

67
src/pack.py Normal file → Executable file
View File

@ -1,77 +1,76 @@
import os import os
import re import re
import shutil import shutil
import zipfile from shutil import make_archive
import hashlib import hashlib
from dotenv import load_dotenv
from b2sdk.v1 import InMemoryAccountInfo from b2sdk.v1 import InMemoryAccountInfo
from b2sdk.v1 import B2Api from b2sdk.v1 import B2Api
class Pack: class Pack:
def __init__(self): def __init__(self):
load_dotenv() command = "git clone -b {branch} --single-branch {url} /files/resourcepack" \
self.repourl = os.environ.get("RESOURCE_PACK_URL") .format(branch=os.environ.get("BRANCH"), url=os.environ.get("RESOURCE_PACK_URL"))
command = "git clone " + self.repourl + " resourcepack" print(command)
status = os.system(command) status = os.system(command)
print(status) print(status)
info = InMemoryAccountInfo() # store credentials, tokens and cache in memory info = InMemoryAccountInfo() # store credentials, tokens and cache in memory
b2_api = B2Api(info) self.b2_api = B2Api(info)
b2_api.authorize_account("production", os.environ.get("APPLICATION_KEY_ID"), self.b2_api.authorize_account("production", os.environ.get("B2_ID"),
os.environ.get("APPLICATION_KEY")) os.environ.get("B2_KEY"))
self.bucket = b2_api.get_bucket_by_name(os.environ.get("BUCKET")) self.bucket = self.b2_api.get_bucket_by_name(os.environ.get("BUCKET"))
def __del__(self): def __del__(self):
shutil.rmtree(self.dir) shutil.rmtree("/files/resourcepack")
def pull(self) -> None: def pull(self) -> None:
os.system("git -C {} pull origin master".format(self.dir)) os.system("git -C /files/resourcepack pull origin {}".format(os.environ.get("BRANCH")))
def collate(self) -> None: def collate(self) -> None:
if os.path.exists("pack"): if os.path.exists("/files/pack"):
shutil.rmtree("pack") shutil.rmtree("/files/pack")
os.mkdir("pack") os.mkdir("/files/pack")
shutil.copytree("resourcepack/assets", "pack/assets") shutil.copytree("/files/resourcepack/assets", "/files/pack/assets")
shutil.copy("resourcepack/pack.mcmeta", "pack/pack.mcmeta") shutil.copy("/files/resourcepack/pack.mcmeta", "/files/pack/pack.mcmeta")
shutil.copy("resourcepack/pack.png", "pack/pack.png") shutil.copy("/files/resourcepack/pack.png", "/files/pack/pack.png")
def compress(self) -> None: def compress(self) -> None:
if not os.path.exists("files"): shutil.make_archive("/files/pack", 'zip', "/files/pack")
os.mkdir("files") shutil.rmtree("/files/pack")
os.system("zip -r files/pack.zip pack")
shutil.rmtree("pack")
def hash(self): def hash(self):
sha1sum = hashlib.sha1() sha1sum = hashlib.sha1()
with open("files/pack.zip", "rb") as pack: with open("/files/pack.zip", "rb") as pack:
block = pack.read(2**16) block = pack.read(2**16)
while len(block) != 0: while len(block) != 0:
sha1sum.update(block) sha1sum.update(block)
block = pack.read(2**16) block = pack.read(2**16)
self.hash = sha1sum.hexdigest() self.hash = sha1sum.hexdigest()
with open("files/hash", 'w') as hashfile: with open("/files/hash", 'w') as hashfile:
hashfile.write(self.hash) hashfile.write(self.hash)
def upload(self): def upload(self):
bucket.upload_local_file( 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", local_file="files/pack.zip",
file_name="pack.zip") file_name="pack.zip")
bucket.upload_local_file( self.bucket.upload_local_file(
local_file="files/hash", local_file="/files/hash",
file_name="hash") file_name="hash")
if __name__ == "__main__": if __name__ == "__main__":
from dotenv import load_dotenv print("Pack")
load_dotenv() pack = Pack()
pack = Pack() # pack.pull()
pack.pull() # pack.collate()
pack.collate() # pack.compress()
pack.compress() # pack.hash()
pack.hash() # pack.upload()
#pack = None #pack = None