exedev 0.1.0

Official Rust SDK for exe.dev: create VMs, run commands on them, and run every exe.dev command
Documentation
// Code generated by "go run ./cmd/gensdkrs" in the exe.dev repository. DO NOT EDIT.

//! The SDK's own commands on a VM: POSIX shell scripts run with sh, so that
//! they work whatever the user's login shell is. They are the Python SDK's
//! (cmd/gensdkpy/templates/_wire.py), verbatim; cmd/gensdkrs's tests check.

/// Processes started by spawn(). Each has a directory on the VM, named for
/// it, holding its pid (which is its process group), the boot it started in,
/// its output, and once it ends, its exit status. A monitor process waits for
/// it and writes the exit status.
macro_rules! processes {
    () => {
        r#"base=${XDG_STATE_HOME:-$HOME/.local/state}/exedev/processes
# Sets n to the number in file $1, or to "" if it is missing, empty, or
# garbled, as a VM that stopped before writing it out can leave it.
num() { { n=$(cat "$1"); } 2>/dev/null; case $n in ''|*[!0-9]*) n=;; esac; }
# Prints the process in directory $1 as "PID running", "PID exited STATUS",
# or "PID lost", where PID is 0 if unknown; fails if there is none, or it is
# still starting.
state() {
  [ -d "$1" ] || return
  num "$1/pid"; p=${n:-0}
  num "$1/exit"; if [ -n "$n" ]; then echo "$p exited $n"; return; fi
  if [ "$(cat "$1/boot" 2>/dev/null)" = "$(cat /proc/sys/kernel/random/boot_id 2>/dev/null)" ]; then
    [ "$p" != 0 ] || return
    if kill -0 "$(cat "$1/monitor" 2>/dev/null)" 2>/dev/null; then echo "$p running"; return; fi
    num "$1/exit"; if [ -n "$n" ]; then echo "$p exited $n"; return; fi
  fi
  echo "$p lost"
}
"#
    };
}

/// Runs the command line $1 for run(): in a login shell, so that the profile
/// sets PATH (~/.local/bin, ...) as it does in a terminal, and in a session of
/// its own. When the SSH session ends early (a timeout, a dropped future, a
/// lost connection), the VM kills only this script's shell; the watchdog then
/// kills everything the command started.
pub(crate) const RUN_SCRIPT: &str = r#"s=${SHELL:-/bin/sh}
command -v setsid >/dev/null 2>&1 || exec "$s" -lc "$1"
setsid "$s" -lc "$1" &
p=$!
parent() { read -r st </proc/$$/stat && st=${st##*) } && set -- $st && pp=$2; }
parent 2>/dev/null; p0=$pp
(while parent && [ "$pp" = "$p0" ]; do sleep 0.25 || sleep 1; done; kill -KILL -$p) >/dev/null 2>&1 &
w=$!
wait $p
c=$?
kill $w 2>/dev/null
exit $c"#;

/// Flushes the VM's recent writes to its disk. Only its disk: a plain sync
/// also waits on network and FUSE mounts, which can hang.
pub(crate) const FLUSH_SCRIPT: &str = r#"sync -f / 2>/dev/null || sync"#;

/// A command line too long for one request is uploaded first, then run as
/// this, with {path} replaced: the script reads and deletes the file, then
/// runs what it read.
pub(crate) const UPLOADED_COMMAND: &str = r#"c=$(cat {path}) && rm -f {path} && eval "$c""#;

/// Reports exe-setup.service's state, whether a setup script is in place,
/// and on failure the end of the script's output. See setup_state.
pub(crate) const SETUP_PROBE: &str = r#"if [ -e /exe.dev/setup ]; then echo Setup=present; else echo Setup=absent; fi
if [ ! -d /run/systemd/system ]; then
  if [ -e /etc/systemd/system/exe-setup.service ]; then echo State=booting; else echo State=unsupported; fi
  exit 0
fi
props=$(systemctl show exe-setup.service -p LoadState -p ActiveState -p Job -p Result -p ExecMainCode -p ExecMainStatus -p ExecMainExitTimestampMonotonic -p ConditionResult -p ConditionTimestampMonotonic 2>/dev/null) || { echo State=booting; exit 0; }
echo "$props"
echo "SystemState=$(systemctl is-system-running 2>/dev/null)"
if [ "$(systemctl show exe-setup.service -p ActiveState --value)" = failed ]; then
  echo ---
  journalctl -b _SYSTEMD_UNIT=exe-setup.service _TRANSPORT=stdout -o cat --no-pager -n 50 2>&1
fi"#;

/// $1 path, $2 base64 data, or "" to read the parts in directory $5;
/// $3 mode or ""; $4 "gz" if the data is gzipped.
pub(crate) const WRITE_SCRIPT: &str = r#"f=$1 data=$2 mode=$3 gz=$4 parts=$5
data() { if [ -n "$parts" ]; then cat "$parts"/*; else printf %s "$data"; fi; }
case $f in */*) mkdir -p "${f%/*}" || exit;; esac
if [ -n "$gz" ]; then data | base64 -d | gzip -dc > "$f"; else data | base64 -d > "$f"; fi
c=$?
if [ -n "$parts" ]; then rm -rf "$parts"; fi
if [ $c = 0 ] && [ -n "$mode" ]; then chmod "$mode" "$f" || c=$?; fi
exit $c"#;

/// $1 directory, $2 part number, $3 base64 data.
pub(crate) const PART_SCRIPT: &str = r#"mkdir -p "$1" && printf %s "$3" > "$1/$2""#;

/// $1 path. Exits 2 if it is missing, 3 if it is a directory.
pub(crate) const READ_SCRIPT: &str = r#"if [ -d "$1" ]; then exit 3; fi
if [ ! -e "$1" ]; then exit 2; fi
exec cat "$1""#;

/// Prints D and a gzipped tar of a directory's contents, or F and a file.
pub(crate) const DOWNLOAD_SCRIPT: &str = r#"if [ -d "$1" ]; then printf D && exec tar -czf - -C "$1" .; fi
if [ ! -e "$1" ]; then exit 2; fi
printf F && exec cat "$1""#;

/// $1 archive, $2 directory to extract it into.
pub(crate) const UNTAR_SCRIPT: &str = r#"mkdir -p "$2" && tar -xzf "$1" -C "$2"; c=$?; rm -f "$1"; exit $c"#;

/// $1 name, $2 command line. Prints the pid; exits 3 if the name is taken
/// by a running process.
pub(crate) const SPAWN_SCRIPT: &str = concat!(
    processes!(),
    r#"d=$base/$1
case $(state "$d") in *" running") exit 3;; esac
rm -rf "$d" && mkdir -p "$d" || exit
cat /proc/sys/kernel/random/boot_id > "$d/boot" 2>/dev/null
printf '%s\n' "$2" > "$d/command"
setsid sh -c 'echo $$ > "$1/monitor"
setsid "${SHELL:-/bin/sh}" -lc "$2" < /dev/null > "$1/stdout" 2> "$1/stderr" &
echo $! > "$1/pid.new" && mv "$1/pid.new" "$1/pid"
wait $!
echo $? > "$1/exit.new" && mv "$1/exit.new" "$1/exit" && sync "$1/exit" "$1"' sh "$d" "$2" < /dev/null > /dev/null 2>&1 &
i=0
while [ ! -f "$d/pid" ] && [ $i -lt 100 ]; do sleep 0.05; i=$((i+1)); done
sync "$d/pid" "$d/boot" "$d/command" "$d/monitor" "$d" 2>/dev/null
cat "$d/pid""#
);

/// $1 name. Prints the state; exits 2 if there is no such process.
pub(crate) const STATE_SCRIPT: &str = concat!(processes!(), r#"state "$base/$1" || exit 2"#);

/// Prints each process as "NAME PID STATE...".
pub(crate) const LIST_SCRIPT: &str = concat!(
    processes!(),
    r#"for d in "$base"/*; do
  if s=$(state "$d"); then echo "${d##*/} $s"; fi
done"#
);

/// $1 name, $2 nonce, $3 "1" to print the output. Waits for the process to
/// end, then writes its output and exits with its status. Prints "NONCE
/// missing" or "NONCE lost" instead when there is no process, or it ended
/// without a status.
pub(crate) const WAIT_SCRIPT: &str = concat!(
    processes!(),
    r#"d=$base/$1
while s=$(state "$d") && [ "${s#* }" = running ]; do sleep 0.5; done
case $s in
"") printf '%s missing' "$2"; exit 0;;
*" lost") printf '%s lost' "$2"; exit 0;;
esac
if [ -n "$3" ]; then cat "$d/stdout"; cat "$d/stderr" >&2; fi
exit "${s##* }""#
);

/// $1 name, $2 lines from the end, or "" for all.
pub(crate) const LOGS_SCRIPT: &str = concat!(
    processes!(),
    r#"d=$base/$1
[ -f "$d/pid" ] || exit 2
if [ -n "$2" ]; then
  tail -n "$2" "$d/stdout" && tail -n "$2" "$d/stderr" >&2
else
  cat "$d/stdout" && cat "$d/stderr" >&2
fi"#
);

/// $1 name, $2 signal name. Signals the process group, if it is running.
pub(crate) const KILL_SCRIPT: &str = concat!(
    processes!(),
    r#"s=$(state "$base/$1") || exit 2
case $s in *" running") kill -"$2" -"${s%% *}" 2>/dev/null || kill -"$2" "${s%% *}";; esac"#
);