support environment variables mostly everywhere,

useful for docker/systemd stuff

also makes logfiles flush to disk per line by default;
can be disabled for a small performance gain with --no-logflush
This commit is contained in:
ed 2023-11-30 10:22:52 +00:00
parent 0b9b8dbe72
commit c9fd26255b
8 changed files with 51 additions and 23 deletions

View file

@ -10,7 +10,14 @@
e2dsa # enable file indexing and filesystem scanning e2dsa # enable file indexing and filesystem scanning
e2ts # and enable multimedia indexing e2ts # and enable multimedia indexing
ansi # and colors in log messages ansi # and colors in log messages
# i: 127.0.0.1 # only allow connections from localhost
# disable logging to stdout/journalctl and log to a file instead;
# $LOGS_DIRECTORY is usually /var/log/copyparty (comes from systemd)
# and copyparty replaces %Y-%m%d with Year-MonthDay, so the
# full path will be something like /var/log/copyparty/2023-1130.txt
# (note: enable compression by adding .xz at the end)
q, lo: $LOGS_DIRECTORY/%Y-%m%d.log
# p: 80,443,3923 # listen on 80/443 as well (requires CAP_NET_BIND_SERVICE) # p: 80,443,3923 # listen on 80/443 as well (requires CAP_NET_BIND_SERVICE)
# i: 127.0.0.1 # only allow connections from localhost (reverse-proxies) # i: 127.0.0.1 # only allow connections from localhost (reverse-proxies)

View file

@ -13,7 +13,9 @@
# systemctl daemon-reload && systemctl enable --now copyparty # systemctl daemon-reload && systemctl enable --now copyparty
# #
# if it fails to start, first check this: systemctl status copyparty # if it fails to start, first check this: systemctl status copyparty
# then try starting it while viewing logs: journalctl -fan 100 # then try starting it while viewing logs:
# journalctl -fan 100
# tail -Fn 100 /var/log/copyparty/$(date +%Y-%m%d.log)
# #
# you may want to: # you may want to:
# - change "User=copyparty" and "/var/lib/copyparty/" to another user # - change "User=copyparty" and "/var/lib/copyparty/" to another user
@ -27,10 +29,6 @@
# python disabling line-buffering, so messages are out-of-order: # python disabling line-buffering, so messages are out-of-order:
# https://user-images.githubusercontent.com/241032/126040249-cb535cc7-c599-4931-a796-a5d9af691bad.png # https://user-images.githubusercontent.com/241032/126040249-cb535cc7-c599-4931-a796-a5d9af691bad.png
# #
# unless you add -q to disable logging, you may want to remove the
# following line to allow buffering (slightly better performance):
# Environment=PYTHONUNBUFFERED=x
#
######################################################################## ########################################################################
######################################################################## ########################################################################
@ -77,6 +75,11 @@ RestrictNamespaces=true
RestrictRealtime=true RestrictRealtime=true
RestrictSUIDSGID=true RestrictSUIDSGID=true
## create a directory for logfiles;
## this defines $LOGS_DIRECTORY which is used in copyparty.conf
##
LogsDirectory=copyparty
## finally, start copyparty and give it the config file: ## finally, start copyparty and give it the config file:
## ##
ExecStart=/usr/bin/python3 /usr/local/bin/copyparty-sfx.py -c /etc/copyparty.conf ExecStart=/usr/bin/python3 /usr/local/bin/copyparty-sfx.py -c /etc/copyparty.conf

View file

@ -1093,6 +1093,7 @@ def add_logging(ap):
ap2.add_argument("-lo", metavar="PATH", type=u, help="logfile, example: \033[32mcpp-%%Y-%%m%%d-%%H%%M%%S.txt.xz") ap2.add_argument("-lo", metavar="PATH", type=u, help="logfile, example: \033[32mcpp-%%Y-%%m%%d-%%H%%M%%S.txt.xz")
ap2.add_argument("--no-ansi", action="store_true", default=not VT100, help="disable colors; same as environment-variable NO_COLOR") ap2.add_argument("--no-ansi", action="store_true", default=not VT100, help="disable colors; same as environment-variable NO_COLOR")
ap2.add_argument("--ansi", action="store_true", help="force colors; overrides environment-variable NO_COLOR") ap2.add_argument("--ansi", action="store_true", help="force colors; overrides environment-variable NO_COLOR")
ap2.add_argument("--no-logflush", action="store_true", help="don't flush the logfile after each write; tiny bit faster")
ap2.add_argument("--no-voldump", action="store_true", help="do not list volumes and permissions on startup") ap2.add_argument("--no-voldump", action="store_true", help="do not list volumes and permissions on startup")
ap2.add_argument("--log-tdec", metavar="N", type=int, default=3, help="timestamp resolution / number of timestamp decimals") ap2.add_argument("--log-tdec", metavar="N", type=int, default=3, help="timestamp resolution / number of timestamp decimals")
ap2.add_argument("--log-badpwd", metavar="N", type=int, default=1, help="log failed login attempt passwords: 0=terse, 1=plaintext, 2=hashed") ap2.add_argument("--log-badpwd", metavar="N", type=int, default=1, help="log failed login attempt passwords: 0=terse, 1=plaintext, 2=hashed")

View file

@ -414,7 +414,7 @@ class VFS(object):
hist = flags.get("hist") hist = flags.get("hist")
if hist and hist != "-": if hist and hist != "-":
zs = "{}/{}".format(hist.rstrip("/"), name) zs = "{}/{}".format(hist.rstrip("/"), name)
flags["hist"] = os.path.expanduser(zs) if zs.startswith("~") else zs flags["hist"] = os.path.expandvars(os.path.expanduser(zs))
return flags return flags
@ -947,9 +947,7 @@ class AuthSrv(object):
if vp is not None and ap is None: if vp is not None and ap is None:
ap = ln ap = ln
if ap.startswith("~"): ap = os.path.expandvars(os.path.expanduser(ap))
ap = os.path.expanduser(ap)
ap = absreal(ap) ap = absreal(ap)
self._l(ln, 2, "bound to filesystem-path [{}]".format(ap)) self._l(ln, 2, "bound to filesystem-path [{}]".format(ap))
self._map_volume(ap, vp, mount, daxs, mflags) self._map_volume(ap, vp, mount, daxs, mflags)
@ -1259,9 +1257,7 @@ class AuthSrv(object):
if vflag == "-": if vflag == "-":
pass pass
elif vflag: elif vflag:
if vflag.startswith("~"): vflag = os.path.expandvars(os.path.expanduser(vflag))
vflag = os.path.expanduser(vflag)
vol.histpath = uncyg(vflag) if WINDOWS else vflag vol.histpath = uncyg(vflag) if WINDOWS else vflag
elif self.args.hist: elif self.args.hist:
for nch in range(len(hid)): for nch in range(len(hid)):

View file

@ -406,6 +406,7 @@ class SMB(object):
smbserver.os.path.abspath = self._hook smbserver.os.path.abspath = self._hook
smbserver.os.path.expanduser = self._hook smbserver.os.path.expanduser = self._hook
smbserver.os.path.expandvars = self._hook
smbserver.os.path.getatime = self._hook smbserver.os.path.getatime = self._hook
smbserver.os.path.getctime = self._hook smbserver.os.path.getctime = self._hook
smbserver.os.path.getmtime = self._hook smbserver.os.path.getmtime = self._hook

View file

@ -411,13 +411,14 @@ class SvcHub(object):
if not vl: if not vl:
continue continue
vl = [os.path.expanduser(x) if x.startswith("~") else x for x in vl] vl = [os.path.expandvars(os.path.expanduser(x)) for x in vl]
setattr(al, k, vl) setattr(al, k, vl)
for k in "lo hist ssl_log".split(" "): for k in "lo hist ssl_log".split(" "):
vs = getattr(al, k) vs = getattr(al, k)
if vs and vs.startswith("~"): if vs:
setattr(al, k, os.path.expanduser(vs)) vs = os.path.expandvars(os.path.expanduser(vs))
setattr(al, k, vs)
for k in "sus_urls nonsus_urls".split(" "): for k in "sus_urls nonsus_urls".split(" "):
vs = getattr(al, k) vs = getattr(al, k)
@ -522,6 +523,7 @@ class SvcHub(object):
import lzma import lzma
lh = lzma.open(fn, "wt", encoding="utf-8", errors="replace", preset=0) lh = lzma.open(fn, "wt", encoding="utf-8", errors="replace", preset=0)
self.args.no_logflush = True
else: else:
lh = open(fn, "wt", encoding="utf-8", errors="replace") lh = open(fn, "wt", encoding="utf-8", errors="replace")
except: except:
@ -751,7 +753,24 @@ class SvcHub(object):
(zd.hour * 100 + zd.minute) * 100 + zd.second, (zd.hour * 100 + zd.minute) * 100 + zd.second,
zd.microsecond // self.log_div, zd.microsecond // self.log_div,
) )
self.logf.write("@%s [%s\033[0m] %s\n" % (ts, src, msg))
if c and not self.args.no_ansi:
if isinstance(c, int):
msg = "\033[3%sm%s\033[0m" % (c, msg)
elif "\033" not in c:
msg = "\033[%sm%s\033[0m" % (c, msg)
else:
msg = "%s%s\033[0m" % (c, msg)
if "\033" in src:
src += "\033[0m"
if "\033" in msg:
msg += "\033[0m"
self.logf.write("@%s [%s] %s\n" % (ts, src, msg))
if not self.args.no_logflush:
self.logf.flush()
now = time.time() now = time.time()
if now >= self.next_day: if now >= self.next_day:
@ -827,6 +846,8 @@ class SvcHub(object):
if self.logf: if self.logf:
self.logf.write(msg) self.logf.write(msg)
if not self.args.no_logflush:
self.logf.flush()
def pr(self, *a: Any, **ka: Any) -> None: def pr(self, *a: Any, **ka: Any) -> None:
try: try:

View file

@ -882,7 +882,9 @@ class Up2k(object):
try: try:
if bos.makedirs(histpath): if bos.makedirs(histpath):
hidedir(histpath) hidedir(histpath)
except: except Exception as ex:
t = "failed to initialize volume '/%s': %s"
self.log(t % (vpath, ex), 1)
return None return None
try: try:

View file

@ -2717,8 +2717,7 @@ def _parsehook(
"capture": cap, "capture": cap,
} }
if cmd.startswith("~"): cmd = os.path.expandvars(os.path.expanduser(cmd))
cmd = os.path.expanduser(cmd)
return chk, fork, jtxt, wait, sp_ka, cmd return chk, fork, jtxt, wait, sp_ka, cmd
@ -2857,9 +2856,7 @@ def loadpy(ap: str, hot: bool) -> Any:
depending on what other inconveniently named files happen depending on what other inconveniently named files happen
to be in the same folder to be in the same folder
""" """
if ap.startswith("~"): ap = os.path.expandvars(os.path.expanduser(ap))
ap = os.path.expanduser(ap)
mdir, mfile = os.path.split(absreal(ap)) mdir, mfile = os.path.split(absreal(ap))
mname = mfile.rsplit(".", 1)[0] mname = mfile.rsplit(".", 1)[0]
sys.path.insert(0, mdir) sys.path.insert(0, mdir)