Skip to content

Commit

Permalink
Add support for remote sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
wokalski authored and astro committed Nov 1, 2024
1 parent 6690d5e commit 9312244
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions nixos-modules/microvm/ssh-deploy.nix
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,33 @@ in
installOnHost = pkgs.writeShellScriptBin "microvm-install-on-host" ''
set -eou pipefail
USAGE="Usage: $0 root@<host> [--use-remote-sudo]"
HOST="$1"
if [[ -z "$HOST" ]]; then
echo "Usage: $0 root@<host>"
echo $USAGE
exit 1
fi
shift
SSH_CMD="bash"
if [ $# -gt 0 ]; then
if [ "$1" == "--use-remote-sudo" ]; then
SSH_CMD="sudo bash"
shift
else
echo "$USAGE"
exit 1
fi
fi
echo "Copying derivations to $HOST"
nix copy --no-check-sigs --to "ssh-ng://$HOST" \
--derivation \
"${paths.closureInfoDrv}^out" \
"${paths.runnerDrv}^out"
ssh "$HOST" -- bash -e <<__SSH__
ssh "$HOST" -- $SSH_CMD -e <<__SSH__
set -eou pipefail
echo "Initializing MicroVM ${hostName} if necessary"
Expand Down Expand Up @@ -160,14 +173,26 @@ in
pkgs.writeShellScriptBin "microvm-switch" ''
set -eou pipefail
USAGE="Usage: $0 root@<target> [--use-remote-sudo]"
TARGET="$1"
if [[ -z "$TARGET" ]]; then
echo "Usage: $0 root@<target>"
echo "$USAGE"
exit 1
fi
shift
SSH_CMD="bash"
if [ $# -gt 0 ]; then
if [ "$1" == "--use-remote-sudo" ]; then
SSH_CMD="sudo bash"
shift
else
echo "$USAGE"
exit 1
fi
fi
ssh "$TARGET" bash -e <<__SSH__
ssh "$TARGET" $SSH_CMD -e <<__SSH__
set -eou pipefail
hostname=\$(cat /etc/hostname)
Expand All @@ -193,14 +218,21 @@ in
shift
TARGET="$1"
shift
if [[ -z "$HOST" || -z "$TARGET" ]]; then
echo "Usage: $0 root@<host> root@<target> switch"
OPTS="$@"
if [ $# -gt 0 ]; then
if [ "$1" == "--use-remote-sudo" ]; then
OPTS="$1"
shift
fi
fi
if [[ -z "$HOST" || -z "$TARGET" || $# -gt 0 ]]; then
echo "Usage: $0 root@<host> root@<target> [--use-remote-sudo] switch"
exit 1
fi
${lib.getExe installOnHost} "$HOST"
${lib.getExe installOnHost} "$HOST" $OPTS
${if canSwitchViaSsh
then ''${lib.getExe sshSwitch} "$TARGET"''
then ''${lib.getExe sshSwitch} "$TARGET" $OPTS''
else ''ssh "$HOST" -- systemctl restart "microvm@${hostName}.service"''
}
'';
Expand Down

0 comments on commit 9312244

Please sign in to comment.