no sudo inside
This commit is contained in:
parent
4e39381b83
commit
1a5ac06ec0
26
lxcd.py
26
lxcd.py
|
|
@ -1,6 +1,5 @@
|
|||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import grp
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
|
|
@ -117,19 +116,6 @@ def detect_timezone():
|
|||
def detect_locale():
|
||||
return os.environ.get("LANG", "en_US.UTF-8")
|
||||
|
||||
def lxc_prefix():
|
||||
"""Return [] if running inside a Snap, or if the user is root or in the lxd group."""
|
||||
if os.environ.get("SNAP"):
|
||||
return []
|
||||
if os.getuid() == 0:
|
||||
return []
|
||||
try:
|
||||
if grp.getgrnam("lxd").gr_gid in os.getgroups():
|
||||
return []
|
||||
except KeyError:
|
||||
pass
|
||||
return ["sudo"]
|
||||
|
||||
def run_cmd(cmd, check=True):
|
||||
"""Helper to run shell commands."""
|
||||
try:
|
||||
|
|
@ -139,15 +125,15 @@ def run_cmd(cmd, check=True):
|
|||
sys.exit(e.returncode)
|
||||
|
||||
def lxc_run(args, check=True):
|
||||
"""Run an lxc command via run_cmd, auto-adding sudo if needed."""
|
||||
cmd = [*lxc_prefix(), "lxc", *args]
|
||||
"""Run an lxc command via run_cmd."""
|
||||
cmd = ["lxc", *args]
|
||||
if VERBOSE:
|
||||
print(f"+ {' '.join(cmd)}", file=sys.stderr)
|
||||
return run_cmd(cmd, check=check)
|
||||
|
||||
def lxc_subprocess(args, **kwargs):
|
||||
"""Run an lxc command via subprocess.run, auto-adding sudo if needed."""
|
||||
cmd = [*lxc_prefix(), "lxc", *args]
|
||||
"""Run an lxc command via subprocess.run."""
|
||||
cmd = ["lxc", *args]
|
||||
if VERBOSE:
|
||||
print(f"+ {' '.join(cmd)}", file=sys.stderr)
|
||||
return subprocess.run(cmd, **kwargs)
|
||||
|
|
@ -357,7 +343,6 @@ def handle_enter(args):
|
|||
if display:
|
||||
env_prefix += f"DISPLAY={display} "
|
||||
|
||||
prefix = lxc_prefix()
|
||||
shell_cmd = "exec /bin/bash --login"
|
||||
exec_cmd = [
|
||||
"lxc", "exec", args.name,
|
||||
|
|
@ -368,9 +353,6 @@ def handle_enter(args):
|
|||
"sh", "-c", f"env {env_prefix} sh -c '{shell_cmd}'"
|
||||
]
|
||||
|
||||
if prefix:
|
||||
os.execvp("sudo", ["sudo", *exec_cmd])
|
||||
else:
|
||||
os.execvp("lxc", exec_cmd)
|
||||
|
||||
def handle_stop(args):
|
||||
|
|
|
|||
Loading…
Reference in New Issue