Merge pull request 'machines/upload: start to use builtin sudo' (#3697) from builtin-sudo into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/3697
This commit is contained in:
Mic92
2025-05-19 09:30:40 +00:00

View File

@@ -1,12 +1,10 @@
import tarfile import tarfile
from pathlib import Path from pathlib import Path
from shlex import quote
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from clan_lib.errors import ClanError from clan_lib.errors import ClanError
from clan_cli.cmd import Log, RunOpts from clan_cli.cmd import Log, RunOpts
from clan_cli.cmd import run as run_local
from clan_cli.ssh.host import Host from clan_cli.ssh.host import Host
@@ -91,10 +89,6 @@ def upload(
with local_src.open("rb") as f: with local_src.open("rb") as f:
tar.addfile(tarinfo, f) tar.addfile(tarinfo, f)
sudo = ""
if host.user != "root":
sudo = "sudo -- "
cmd = None cmd = None
if local_src.is_dir(): if local_src.is_dir():
cmd = 'install -d -m "$1" "$0" && find "$0" -mindepth 1 -delete && tar -C "$0" -xzf -' cmd = 'install -d -m "$1" "$0" && find "$0" -mindepth 1 -delete && tar -C "$0" -xzf -'
@@ -106,11 +100,11 @@ def upload(
# TODO accept `input` to be an IO object instead of bytes so that we don't have to read the tarfile into memory. # TODO accept `input` to be an IO object instead of bytes so that we don't have to read the tarfile into memory.
with tar_path.open("rb") as f: with tar_path.open("rb") as f:
run_local( host.run(
[ [
*host.ssh_cmd(), "bash",
"--", "-c",
f"{sudo}bash -c {quote(cmd)}", cmd,
str(remote_dest), str(remote_dest),
f"{dir_mode:o}", f"{dir_mode:o}",
], ],
@@ -120,4 +114,5 @@ def upload(
prefix=host.command_prefix, prefix=host.command_prefix,
needs_user_terminal=True, needs_user_terminal=True,
), ),
become_root=True,
) )