Debian Codegraph @

interactive-notebook/stap-jupyter-container.in

raw
1 #!/bin/bash
2 # A launcher/builder/publisher for isystemtap containers
3 # Copyright (C) 2023 Red Hat Inc.
4 #
5 # This file is part of systemtap, and is free software. You can
6 # redistribute it and/or modify it under the terms of the GNU General
7 # Public License (GPL); either version 2, or (at your option) any
8 # later version.
9 set -e
10
11 usage() { echo "Usage: stap-jupyter-container [--repo REPOSITORY] [--image IMAGE] [--tag TAG] [--keyname KEYNAME] --{run, pull, build, publish, remove}" 1>&2; exit 1; }
12
13 NOTEBOOK_DIR="@prefix@/share/systemtap/interactive-notebook"
14 declare -A options=( ["--repo"]="quay.io" ["--image"]="systemtap/isystemtap" ["--tag"]="latest" ["--keyname"]="id_rsa" )
15
16 while (( "$#" )); do
17 # Parse the mode
18 if [[ $1 == --*(run|pull|build|publish|remove) ]]; then
19 if [ -z "$MODE" ]; then
20 MODE=$1;
21 else
22 echo The mode may only be defined once; usage
23 fi
24 # Parse options
25 elif [[ $1 == --*(repo|image|tag|keyname)* ]]; then
26 flag=$1
27 if [[ $flag == --*=* ]]; then
28 arg=`echo $flag | awk -F "=" '{print $2}'`
29 flag=`echo $flag | awk -F "=" '{print $1}'`
30 if [ -z "$arg" ]; then echo "$flag cannot have an empty argument"; usage; fi
31 else
32 if [ $# -lt 2 ]; then echo "$flag cannot have an empty argument"; usage; fi
33 arg="$2"
34 shift
35 fi
36 options[$flag]=$arg
37 else
38 echo $1 is not a valid argument; usage
39 fi
40 shift
41 done
42
43
44 if [ "$MODE" = '--run' ]; then
45 HOST_USER=`whoami`
46 # The container user
47 NB_USER="jovyan"
48
49 HOST_SSH_DIR=$HOME/.ssh
50 PRV_KEY_FILE=$HOST_SSH_DIR/${options[--keyname]}
51 PUB_KEY_FILE=$HOST_SSH_DIR/${options[--keyname]}.pub
52 if [ ! -f $PUB_KEY_FILE -o ! -f $PRV_KEY_FILE ]; then
53 echo "A ssh public-private key pair is required! Create one with ssh-keygen";
54 echo "If using --keyname ${options[--keyname]} they must be called ${options[--keyname]} and ${options[--keyname]}.pub"
55 exit 1
56 fi
57
58 # Each run of the container will generate a tmpdir in /tmp
59 CONTAINER_TMP_DIR=`mktemp -d "/tmp/systemtap_jupyter_container_XXXXXXXXXX"`
60 chmod 777 $CONTAINER_TMP_DIR
61 echo "Using tempdir $CONTAINER_TMP_DIR"
62 CONTAINER_SSH_DIR=$CONTAINER_TMP_DIR/.ssh
63 LOCALHOST=127.0.0.1
64
65 # We store the private key & a config file in this .ssh dir which will be mounted
66 echo "Setting up local .ssh directory $CONTAINER_SSH_DIR for ssh from container to localhost. Your ssh keys will never be saved in the container"
67 mkdir -m 700 $CONTAINER_SSH_DIR/
68 echo -e "Host $LOCALHOST\n\tStrictHostKeyChecking no\n\tUser $HOST_USER\n" > $CONTAINER_SSH_DIR/config
69 chmod 600 $CONTAINER_SSH_DIR/config
70 cp -pv $PRV_KEY_FILE $CONTAINER_SSH_DIR
71
72 # If the public key is not in authorized_keys (on the host), add it
73 if ! grep -Fxq "`cat $PUB_KEY_FILE`" $HOST_SSH_DIR/authorized_keys 2> /dev/null
74 then
75 # Since the keys exist in HOST_SSH_DIR its safe to assume the directory exists and is well formed
76 cat $PUB_KEY_FILE >> $HOST_SSH_DIR/authorized_keys
77 chmod 600 $HOST_SSH_DIR/authorized_keys
78 fi
79
80 # Extract the constants.py values, since the locally installed version of stap
81 # is what the container needs (tr removes whitespace)
82 STAP_VERSION_DEF=`grep VERSION $NOTEBOOK_DIR/isystemtap/constants.py | tr -d " \t\n\r"`
83 STAP_PREFIX_DEF=`grep PREFIX $NOTEBOOK_DIR/isystemtap/constants.py | tr -d " \t\n\r"`
84 STAP_PKGDATADIR_DEF=`grep PKGDATADIR $NOTEBOOK_DIR/isystemtap/constants.py | tr -d " \t\n\r"`
85 # We also unpack the static dir containing the examples since
86 # we need to mount that
87 STAP_PKGDATADIR=`echo "$STAP_PKGDATADIR_DEF" | tr -d "' " | awk -F "=" '{print $2}'`
88
89 run_args=(
90 # We need privileged network access to freely ssh/run websockets, etc.
91 --privileged \
92 --net=host \
93 # MOUNTED VOLUMES ----------------------
94 # Monitor mode interactions occur in /proc/systemtap/MODULE_NAME
95 -v /proc:/proc:rw \
96 # The directory where we will find the examples, tapsets, etc.
97 # If installed correctly it is the parent of NOTEBOOK_DIR
98 -v $STAP_PKGDATADIR:$STAP_PKGDATADIR \
99 -v $CONTAINER_SSH_DIR:/home/$NB_USER/.ssh \
100 # ENVIRONMENT VARIABLES ------------------
101 -e $STAP_VERSION_DEF \
102 -e $STAP_PREFIX_DEF \
103 -e $STAP_PKGDATADIR_DEF
104 )
105
106 if [ $EUID -ne 0 ]; then
107 # When running as a regular user we remap so that the container id 0 (which is the podman caller) maps
108 # to the u/gid of jovyan the container user
109 NB_UID=1000
110 NB_GID=100
111 SUBUID_SIZE=$(( $(podman info --format "{{ range .Host.IDMappings.UIDMap }}+{{.Size }}{{end }}" ) - 1 ))
112 SUBGID_SIZE=$(( $(podman info --format "{{ range .Host.IDMappings.GIDMap }}+{{.Size }}{{end }}" ) - 1 ))
113
114 run_args+=(
115 --user $NB_UID:$NB_GID \
116 --uidmap $NB_UID:0:1 --uidmap 0:1:$NB_UID --uidmap $(($NB_UID+1)):$(($NB_UID+1)):$(($SUBUID_SIZE-$NB_UID)) \
117 --gidmap $NB_GID:0:1 --gidmap 0:1:$NB_GID --gidmap $(($NB_GID+1)):$(($NB_GID+1)):$(($SUBGID_SIZE-$NB_GID)) \
118 # It's convinient to be able to access the host's home directory and working directory
119 -v $PWD:/home/$NB_USER/working_dir \
120 -v $HOME:/home/$NB_USER/${HOST_USER}_home_dir
121 )
122 else
123 # When running as root we create a workdir so we can share data with the user (in the temp dir)
124 echo Creating a shared host-container working directory
125 WORKING_DIR=$CONTAINER_TMP_DIR/workdir
126 mkdir -v $WORKING_DIR
127
128 run_args+=(
129 -v $WORKING_DIR:/home/$NB_USER/working_dir \
130 --user root \
131 -e CHOWN_EXTRA="/home/$NB_USER/.ssh",/home/$NB_USER/working_dir \
132 -e CHOWN_EXTRA_OPTS="-R"
133 )
134 fi
135
136 podman run -it --rm --name isystemtap "${run_args[@]}" "${options[--repo]}/${options[--image]}:${options[--tag]}"
137 elif [ "$MODE" = '--pull' ]; then
138 podman pull "${options[--repo]}/${options[--image]}:${options[--tag]}"
139 elif [ "$MODE" = '--build' ]; then
140 # Build the image
141 podman build \
142 --file "$NOTEBOOK_DIR/container/Dockerfile" \
143 --format="docker" \
144 --tag="${options[--image]}:${options[--tag]}" \
145 "$NOTEBOOK_DIR/.."
146 elif [ "$MODE" = '--publish' ]; then
147 podman login $REPO
148 podman push "${options[--image]}" "${options[--repo]}/${options[--image]}:${options[--tag]}"
149 elif [ "$MODE" = '--remove' ]; then
150 podman image rm "${options[--repo]}/${options[--image]}:${options[--tag]}"
151 else
152 usage;
153 fi