Skip to content

Commit 9312244

Browse files
wokalskiastro
authored andcommitted
Add support for remote sudo
1 parent 6690d5e commit 9312244

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

nixos-modules/microvm/ssh-deploy.nix

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,33 @@ in
9797
installOnHost = pkgs.writeShellScriptBin "microvm-install-on-host" ''
9898
set -eou pipefail
9999
100+
USAGE="Usage: $0 root@<host> [--use-remote-sudo]"
101+
100102
HOST="$1"
101103
if [[ -z "$HOST" ]]; then
102-
echo "Usage: $0 root@<host>"
104+
echo $USAGE
103105
exit 1
104106
fi
105107
shift
108+
SSH_CMD="bash"
109+
if [ $# -gt 0 ]; then
110+
if [ "$1" == "--use-remote-sudo" ]; then
111+
SSH_CMD="sudo bash"
112+
shift
113+
else
114+
echo "$USAGE"
115+
exit 1
116+
fi
117+
fi
118+
106119
107120
echo "Copying derivations to $HOST"
108121
nix copy --no-check-sigs --to "ssh-ng://$HOST" \
109122
--derivation \
110123
"${paths.closureInfoDrv}^out" \
111124
"${paths.runnerDrv}^out"
112125
113-
ssh "$HOST" -- bash -e <<__SSH__
126+
ssh "$HOST" -- $SSH_CMD -e <<__SSH__
114127
set -eou pipefail
115128
116129
echo "Initializing MicroVM ${hostName} if necessary"
@@ -160,14 +173,26 @@ in
160173
pkgs.writeShellScriptBin "microvm-switch" ''
161174
set -eou pipefail
162175
176+
USAGE="Usage: $0 root@<target> [--use-remote-sudo]"
177+
163178
TARGET="$1"
164179
if [[ -z "$TARGET" ]]; then
165-
echo "Usage: $0 root@<target>"
180+
echo "$USAGE"
166181
exit 1
167182
fi
168183
shift
184+
SSH_CMD="bash"
185+
if [ $# -gt 0 ]; then
186+
if [ "$1" == "--use-remote-sudo" ]; then
187+
SSH_CMD="sudo bash"
188+
shift
189+
else
190+
echo "$USAGE"
191+
exit 1
192+
fi
193+
fi
169194
170-
ssh "$TARGET" bash -e <<__SSH__
195+
ssh "$TARGET" $SSH_CMD -e <<__SSH__
171196
set -eou pipefail
172197
173198
hostname=\$(cat /etc/hostname)
@@ -193,14 +218,21 @@ in
193218
shift
194219
TARGET="$1"
195220
shift
196-
if [[ -z "$HOST" || -z "$TARGET" ]]; then
197-
echo "Usage: $0 root@<host> root@<target> switch"
221+
OPTS="$@"
222+
if [ $# -gt 0 ]; then
223+
if [ "$1" == "--use-remote-sudo" ]; then
224+
OPTS="$1"
225+
shift
226+
fi
227+
fi
228+
if [[ -z "$HOST" || -z "$TARGET" || $# -gt 0 ]]; then
229+
echo "Usage: $0 root@<host> root@<target> [--use-remote-sudo] switch"
198230
exit 1
199231
fi
200232
201-
${lib.getExe installOnHost} "$HOST"
233+
${lib.getExe installOnHost} "$HOST" $OPTS
202234
${if canSwitchViaSsh
203-
then ''${lib.getExe sshSwitch} "$TARGET"''
235+
then ''${lib.getExe sshSwitch} "$TARGET" $OPTS''
204236
else ''ssh "$HOST" -- systemctl restart "microvm@${hostName}.service"''
205237
}
206238
'';

0 commit comments

Comments
 (0)