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=
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
import os
import hmac
import pack
from pack import Pack
class App:
def __init__(self):
self.pack = pack.Pack()
#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):
json = await request.json()
text = await request.read()
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':
@ -23,12 +28,15 @@ class App:
self.pack.collate()
self.pack.compress()
self.pack.hash()
print(status)
return web.Response(status=200)
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('GITHUB_WEBHOOK_SECRET')
secret_key = os.environ.get('WEBHOOK_SECRET')
if not header_signature:
return 404
@ -47,7 +55,7 @@ class App:
return 200
if __name__ == '__main__':
app = App()
app = App()

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

@ -1,77 +1,76 @@
import os
import re
import shutil
import zipfile
from shutil import make_archive
import hashlib
from dotenv import load_dotenv
from b2sdk.v1 import InMemoryAccountInfo
from b2sdk.v1 import B2Api
class Pack:
def __init__(self):
load_dotenv()
self.repourl = os.environ.get("RESOURCE_PACK_URL")
command = "git clone " + self.repourl + " resourcepack"
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
b2_api = B2Api(info)
b2_api.authorize_account("production", os.environ.get("APPLICATION_KEY_ID"),
os.environ.get("APPLICATION_KEY"))
self.bucket = b2_api.get_bucket_by_name(os.environ.get("BUCKET"))
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(self.dir)
shutil.rmtree("/files/resourcepack")
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:
if os.path.exists("pack"):
shutil.rmtree("pack")
os.mkdir("pack")
shutil.copytree("resourcepack/assets", "pack/assets")
shutil.copy("resourcepack/pack.mcmeta", "pack/pack.mcmeta")
shutil.copy("resourcepack/pack.png", "pack/pack.png")
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:
if not os.path.exists("files"):
os.mkdir("files")
os.system("zip -r files/pack.zip pack")
shutil.rmtree("pack")
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:
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:
with open("/files/hash", 'w') as hashfile:
hashfile.write(self.hash)
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",
file_name="pack.zip")
bucket.upload_local_file(
local_file="files/hash",
self.bucket.upload_local_file(
local_file="/files/hash",
file_name="hash")
if __name__ == "__main__":
from dotenv import load_dotenv
load_dotenv()
pack = Pack()
pack.pull()
pack.collate()
pack.compress()
pack.hash()
if __name__ == "__main__":
print("Pack")
pack = Pack()
# pack.pull()
# pack.collate()
# pack.compress()
# pack.hash()
# pack.upload()
#pack = None