-
-
Save bboozzoo/052a1f4b52e4173417960cbe063a11ce to your computer and use it in GitHub Desktop.
minimal snap client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# naive snap replacement | |
set -ex | |
SNAPD_SOCKET=/run/snapd.socket | |
CURL="${CURL:-curl}" | |
CURL_CMD="${CURL} --unix-socket $SNAPD_SOCKET -D- -v" | |
if [[ "$0" == /snap/bin/* ]] || [[ "$0" == /var/lib/snapd/snap/bin/* ]]; then | |
exec "$(readlink -f "$0")" run "$(basename "$0")" "$@" | |
fi | |
cmd="$1" | |
if [ -z "$cmd" ]; then | |
echo "error: sc <install|remove|list> [--purge] [--dangerous] [<snap-name>|<snap-file>]" | |
exit 1 | |
fi | |
case "$cmd" in | |
remove) | |
shift | |
purge="false" | |
if [ "$1" = "--purge" ]; then | |
purge="true" | |
shift 1 | |
fi | |
name="$1" | |
if [ -z "$name" ]; then | |
echo "error: missing snap name" | |
exit 1 | |
fi | |
echo "{\"action\":\"$cmd\",\"purge\": $purge}" | $CURL_CMD \ | |
-X POST -d@- "http://localhost/v2/snaps/$name" | |
;; | |
install) | |
shift | |
dangerous="false" | |
if [ "$1" = "--dangerous" ]; then | |
dangerous="true" | |
shift | |
fi | |
name="$1" | |
if [ -z "$name" ]; then | |
echo "error: missing snap name" | |
exit 1 | |
fi | |
if [ "${name%.snap}" != "$name" ]; then | |
# file upload | |
$CURL -F action=install -F "dangerous=$dangerous" -F "snap=@$name" \ | |
-X POST "http://localhost/v2/snaps" | |
else | |
echo "{\"action\":\"$cmd\",\"dangerous\":\"$dangerous\"}" | $CURL_CMD \ | |
-X POST -d@- "http://localhost/v2/snaps/$name" | |
fi | |
;; | |
list) | |
$CURL_CMD "http://localhost/v2/snaps" | |
exit 0 | |
;; | |
change) | |
shift | |
nr="$1" | |
if [ -z "$nr" ]; then | |
echo "error: missing change ID" | |
exit 1 | |
fi | |
$CURL_CMD "http://localhost/v2/changes/$nr" | tr '[]{}' '\n' | |
;; | |
run) | |
real=/usr/bin/snap.real | |
for arg in "$@"; do | |
case "$arg" in | |
--hook*|--timer*|--shell|--help|--command*) | |
exec "$real" "$@" | |
# noreturn | |
;; | |
esac | |
done | |
# drop "run" | |
shift | |
snapapp="$1" | |
shift | |
if [[ "${snapapp/.*/}" = "$snapapp" ]]; then | |
snapapp="$snapapp.$snapapp" | |
fi | |
SNAP_NAME=${snapapp/.*/} | |
SNAP_REVISION="$(basename "$(readlink "/snap/${SNAP_NAME}/current")")" | |
SNAP=/snap/$SNAP_NAME/$SNAP_REVISION | |
SNAP_COMMON=/var/snap/$SNAP | |
SNAP_DATA=/var/snap/$SNAP_REVISION | |
SNAP_INSTANCE_NAME=$SNAP_NAME | |
SNAP_INSTANCE_KEY= | |
SNAP_VERSION=123 | |
needsbase="$(awk '/^base: .*/ {print $2}' < "$SNAP/meta/snap.yaml")" | |
basearg= | |
if [ -n "$needsbase" ]; then | |
basearg="--base $needsbase" | |
fi | |
export SNAP SNAP_REVISION \ | |
SNAP_DATA SNAP_COMMON \ | |
SNAP_NAME SNAP_INSTANCE_NAME SNAP_INSTANCE_KEY \ | |
SNAP_VERSION | |
#shellcheck disable=SC2086 | |
exec /usr/lib/snapd/snap-confine $basearg "snap.$snapapp" /usr/lib/snapd/snap-exec "$snapapp" "$@" | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment