marc/pubnix.py
2025-02-18 20:58:05 +01:00

74 lines
2 KiB
Python

import os
import subprocess
PORT = 22
USER = "mspe"
ROOT = "public_html"
PROTOCOL = "https"
DIR = False
class PubNix:
def __init__(
self,
dn,
ssh=None,
port=PORT,
user=USER,
root=ROOT,
dir=DIR,
web=None,
sub=None,
):
self.dn = dn
self.ssh = f"{ssh}.{self.dn}" if ssh else self.dn
self.port = port
self.user = user
self.root = root
self.web = f"{web}.{self.dn}" if web else self.dn
if sub:
self.fqdn = f"{self.user}.{self.web}"
self.context = None
else:
self.fqdn = self.web
self.context = f"~{self.user}"
self.path = os.path.join(self.root, self.fqdn) if dir else self.root
self.target = f"{self.user}@{self.ssh}:{self.path}"
self.url = [f"{PROTOCOL}:", str(), self.fqdn]
if self.context:
self.url.append(self.context)
self.url = "/".join(self.url)
def capturun(self, *args):
return self.run(*args, capture_output=True)
def run(self, *args, **kwargs):
return subprocess.run(
[
"ssh",
"-o",
"LogLevel Error",
"-p",
str(self.port),
f"{self.user}@{self.ssh}",
"--",
*args,
],
**kwargs,
)
def disk_free(self):
process = self.capturun("df", "-h", os.curdir)
return process.stdout.decode("UTF-8").strip()
def os(self):
ps = self.capturun("cat", "/etc/os-release")
if ps.returncode == 0:
for line in ps.stdout.decode("UTF-8").strip().split(os.linesep):
if "PRETTY_NAME" in line:
return line.split("=")[1].split('"')[1]
else:
return self.capturun("uname", "-sr").stdout.decode("UTF-8").strip()
def __str__(self):
return os.linesep.join([self.target, self.url])