deploy files

pull/1/head
Matt C 2 years ago
parent 686d3262f8
commit 5fb567715a

@ -0,0 +1,20 @@
test:
python3 main.py
pip:
sudo pip3 install -r requirements.txt
deploy: pip
cp sample.service new.service
# yes, this exists because I'm too lazy to escape regex + inline 'sed'
python3 sed.py
sudo mv new.service /etc/systemd/system/webforge.service
sudo systemctl daemon-reload
sudo systemctl enable --now webforge.service
undeploy:
sudo systemctl stop webforge.service
sudo systemctl disable webforge.service
sudo rm /etc/systemd/system/webforge.service
sudo systemctl daemon-reload
update:
make undeploy
git pull
make deploy

@ -1,9 +1,10 @@
# Stdlib
import os,yaml
import os, yaml
# Pip
import flask_login
from flask import Flask,render_template,request,redirect,make_response
from flask import Flask, render_template, request, redirect, make_response
from flask_login import login_required
# In-house
from utils import run_command_shell
@ -23,6 +24,7 @@ mlc = mlcmgr()
class User(flask_login.UserMixin):
pass
@login_manager.user_loader
def user_loader(uid):
if not db.check_user_exists(uid):
@ -81,14 +83,56 @@ def logout():
flask_login.logout_user()
return redirect("/")
@app.route("/")
def main():
if not flask_login.current_user.is_authenticated:
pc = "<a href='/login'>Login</a>"
else:
pc = f"<p>Hi, {flask_login.current_user.id}</p>" + render_template("logout.html")
pc = f"<p>Hi, {flask_login.current_user.id}</p>" + render_template(
"logout.html"
) + "<br/><h3>All repos:</h3>" + mlc.html_repo_list()
return render_template("page.html", page_title="Home", content=pc)
@app.route("/repos/<name>")
@login_required
def getrepo(name):
if not mlc.config["has_subdirs"]:
return "This repo doesn't have subdirs. Use: TODO: new handler"
else:
return render_template(
"page.html",
page_title=f"Repo Info - {name}",
content=mlc.html_list_packages(name),
)
@app.route("/packages/<repo>/<name>")
@login_required
def getpackage(repo, name):
if not mlc.config["has_subdirs"]:
return "This repo doesn't have subdirs. Use: TODO: new handler"
else:
return render_template(
"page.html",
page_title=f"Package info - {name}",
content=mlc.pkg_page(name, repo),
)
@app.route("/pkgbuild/<repo>/<name>")
@login_required
def getpkgbuild(repo, name):
if not mlc.config["has_subdirs"]:
return "This repo doesn't have subdirs. Use: TODO: new handler"
else:
return render_template(
"page.html",
page_title=f"PKGBUILD for - {name}",
content=f"<pre><code>{mlc.dump_pkgbuild(name, repo)}</code></pre>",
)
if __name__ == "__main__":
app.run(host="127.0.0.1", port=6969, debug=True)
app.run(host="127.0.0.1", port=6969, debug=True)

@ -1,4 +1,4 @@
import os,yaml,contextlib
import os, yaml, contextlib
import subprocess
@ -21,7 +21,7 @@ class mlcmgr:
subdirs = []
has_subdirs = input("Does your repo have subdirs? (Y/n): ")
if has_subdirs != "n":
newd = 'foo'
newd = "foo"
while newd != "done":
newd = input("Subdir (or 'done'): ")
if newd == "done":
@ -36,7 +36,7 @@ class mlcmgr:
"src": src,
"dst": dst,
"has_subdirs": has_subdirs,
"subdirs": subdirs
"subdirs": subdirs,
}
with open("config.yml", "w") as f:
f.write(yaml.dump(settings))
@ -47,52 +47,116 @@ class mlcmgr:
def init_workspace(self):
if not os.path.exists(self.config["dst"]):
os.system(f"git clone {self.config['src']} {self.config['dst']}")
if not self.config['has_subdirs']:
if not self.config["has_subdirs"]:
os.system(f"cd {self.config['dst']} && mlc init")
else:
for subdir in self.config['subdirs']:
os.system(f"cd {self.config['dst']} && cd {subdir} && mlc init && cd ../")
for subdir in self.config["subdirs"]:
os.system(
f"cd {self.config['dst']} && cd {subdir} && mlc init && cd ../"
)
def pull_all(self):
if self.config['has_subdirs']:
for subdir in self.config['subdirs']:
os.system(f"cd {self.config['dst']} && cd {subdir} && mlc pull && cd ../")
if self.config["has_subdirs"]:
for subdir in self.config["subdirs"]:
os.system(
f"cd {self.config['dst']} && cd {subdir} && mlc pull && cd ../"
)
else:
os.system(f"cd {self.config['dst']} && mlc pull && cd ../")
return "Done"
def get_info(self, subdir=None):
extra = self.config['dst']
extra = self.config["dst"]
if subdir is not None:
extra += "/" + subdir
with pushd(extra):
out = subprocess.check_output(["mlc","info"]).decode('utf-8')
out = subprocess.check_output(["mlc", "info"]).decode("utf-8")
return out
def list_packages(self, subdir=None):
extra = self.config['dst']
extra = self.config["dst"]
if subdir is not None:
extra += "/" + subdir
return os.listdir(extra)
all_files = os.listdir(extra)
bad = ["mlc.toml", "README.md"]
for thing in bad:
if thing in all_files:
all_files.remove(thing)
return all_files
def build(self, package, subdir=None):
p = self.config['dst']
p = self.config["dst"]
if subdir is not None:
p += "/" + subdir
with pushd(p):
os.system(f"mlc build {package}")
def gen_repo(self, subdir=None):
p = self.config['dst']
p = self.config["dst"]
if subdir is not None:
p += "/" + subdir
with pushd(p):
os.system("mlc repo-gen")
def html_list_packages(self, subdir=None):
raw = self.list_packages(subdir)
html = "<ul>"
for pkg in raw:
html += f"<li><p>- <a class='slicklink' href='/packages/{subdir+'/' if subdir is not None else ''}{pkg}'>{pkg}</a></p></li><br/>"
html += "</ul>"
return html
def dump_pkgbuild(self, package, subdir=None):
p = self.config["dst"]
if subdir is not None:
p += "/" + subdir
p += "/" + package
with pushd(p):
return open("PKGBUILD").read()
def pkg_info(self, package, subdir=None):
p = self.config["dst"]
if subdir is not None:
p += "/" + subdir
p += "/" + package
with pushd(p):
pkgbuild = open("PKGBUILD").read()
pkgbuild_lines = pkgbuild.split("\n")
info = {}
for line in pkgbuild_lines:
if "=" in line:
info[line.split("=")[0]] = line.split("=")[1]
return info
def pkg_page(self, package, subdir=None):
info = self.pkg_info(package, subdir)
html = f"<h2>pkgname: <code>{info['pkgname']}</code></h2>"
html += f"<p>Version: <code>{info['pkgver']}</code></p>"
html += f"<p>Rel: <code>{info['pkgrel']}</code></p>"
html += f"<p>Desc: <code>{info['pkgdesc']}</code></p>"
s_url = info["url"].replace("'", "").replace('"',"").replace("$pkgname", info['pkgname'])
html += f"<p>URL: <a class='slicklink' href='{s_url}'>{s_url}</a></p>"
html += f"<p>You can view the whole pkgbuild <a href='/pkgbuild/{subdir}/{package}'>here</a></p>"
return html
def list_repos(self):
if not self.config['has_subdirs']:
return []
else:
return self.config['subdirs']
def html_repo_list(self):
repos = self.list_repos()
html = "<ul>"
for repo in repos:
html += f"<li><p><a class='slicklink' href='/repos/{repo}'>{repo}</a></p></li><br/>"
html += "</ul>"
return html
if __name__ == "__main__":
mlc = mlcmgr()
#mlc.pull_all()
mlc.build("base", "any")
# mlc.pull_all()
# mlc.build("base", "any")
print(mlc.pkg_info("base", "any"))

@ -0,0 +1,13 @@
[Unit]
Description=Localizer gunicorn service
[Service]
# WHO is edited by sed.py to become the current user
User=WHO
Type=simple
# GCPATH is edited by sed.py to point to the user's installation of gunicorn
# PATH becomes the output of `pwd`
ExecStart=GCPATH --chdir PATH --workers=8 --bind=0.0.0.0 main:app
[Install]
WantedBy=multi-user.target

@ -0,0 +1,18 @@
import os, subprocess, getpass
# Feel free to edit this file, but note that `make deploy` expects the service file to be output as `new.service`
# It, of course, is renamed to `localizer.service` as it's moved to `/etc/systemd/system/`
text = open("new.service").read()
text = text.replace(
"GCPATH",
subprocess.check_output(["/usr/bin/bash", "-c", "which gunicorn"])
.decode("utf-8")
.strip(),
)
text = text.replace("PATH", os.getcwd())
text = text.replace("WHO", getpass.getuser())
os.remove("new.service")
with open("new.service", "w") as f:
f.write(text)

@ -1,9 +1,8 @@
import os,yaml
import os, yaml
from passlib.hash import pbkdf2_sha256
class usermgr:
def __init__(self):
if not os.path.exists("db"):
os.makedirs("db")
@ -47,4 +46,4 @@ class usermgr:
self.write_user(uid, obj)
return {"message": "done."}
else:
return {"message": f"error: no such user {uid}"}
return {"message": f"error: no such user {uid}"}

@ -1,4 +1,4 @@
import asyncio,threading
import asyncio, threading
# Maybe add: https://docs.python.org/3/library/shlex.html#shlex.quote ?
async def run_command_shell(command, grc=False):
@ -38,4 +38,4 @@ async def run_command_shell(command, grc=False):
# Return stdout
return result
else:
return process.returncode, result
return process.returncode, result

Loading…
Cancel
Save