diff --git a/swarmlab-app/launch.sh b/swarmlab-app/launch.sh new file mode 100755 index 0000000..e44a1e2 --- /dev/null +++ b/swarmlab-app/launch.sh @@ -0,0 +1,187 @@ +#!/usr/bin/env bash + +# Copyright (C) 2018 The noVNC Authors +# Licensed under MPL 2.0 or any later version (see LICENSE.txt) + +usage() { + if [ "$*" ]; then + echo "$*" + echo + fi + echo "Usage: ${NAME} [--listen PORT] [--vnc VNC_HOST:PORT] [--cert CERT] [--ssl-only]" + echo + echo "Starts the WebSockets proxy and a mini-webserver and " + echo "provides a cut-and-paste URL to go to." + echo + echo " --listen PORT Port for proxy/webserver to listen on" + echo " Default: 6080" + echo " --vnc VNC_HOST:PORT VNC server host:port proxy target" + echo " Default: localhost:5900" + echo " --cert CERT Path to combined cert/key file, or just" + echo " the cert file if used with --key" + echo " Default: self.pem" + echo " --key KEY Path to key file, when not combined with cert" + echo " --web WEB Path to web files (e.g. vnc.html)" + echo " Default: ./" + echo " --ssl-only Disable non-https connections." + echo " " + echo " --record FILE Record traffic to FILE.session.js" + echo " " + exit 2 +} + +NAME="$(basename $0)" +REAL_NAME="$(readlink -f $0)" +HERE="$(cd "$(dirname "$REAL_NAME")" && pwd)" +PORT="6080" +VNC_DEST="localhost:5900" +CERT="" +KEY="" +WEB="" +proxy_pid="" +SSLONLY="" +RECORD_ARG="" + +die() { + echo "$*" + exit 1 +} + +cleanup() { + trap - TERM QUIT INT EXIT + trap "true" CHLD # Ignore cleanup messages + echo + if [ -n "${proxy_pid}" ]; then + echo "Terminating WebSockets proxy (${proxy_pid})" + kill ${proxy_pid} + fi +} + +# Process Arguments + +# Arguments that only apply to chrooter itself +while [ "$*" ]; do + param=$1; shift; OPTARG=$1 + case $param in + --listen) PORT="${OPTARG}"; shift ;; + --vnc) VNC_DEST="${OPTARG}"; shift ;; + --cert) CERT="${OPTARG}"; shift ;; + --key) KEY="${OPTARG}"; shift ;; + --web) WEB="${OPTARG}"; shift ;; + --ssl-only) SSLONLY="--ssl-only" ;; + --record) RECORD_ARG="--record ${OPTARG}"; shift ;; + -h|--help) usage ;; + -*) usage "Unknown chrooter option: ${param}" ;; + *) break ;; + esac +done + +# Sanity checks +if bash -c "exec 7<>/dev/tcp/localhost/${PORT}" &> /dev/null; then + exec 7<&- + exec 7>&- + die "Port ${PORT} in use. Try --listen PORT" +else + exec 7<&- + exec 7>&- +fi + +trap "cleanup" TERM QUIT INT EXIT + +# Find vnc.html +if [ -n "${WEB}" ]; then + if [ ! -e "${WEB}/vnc.html" ]; then + die "Could not find ${WEB}/vnc.html" + fi +elif [ -e "$(pwd)/vnc.html" ]; then + WEB=$(pwd) +elif [ -e "${HERE}/../vnc.html" ]; then + WEB=${HERE}/../ +elif [ -e "${HERE}/vnc.html" ]; then + WEB=${HERE} +elif [ -e "${HERE}/../share/novnc/vnc.html" ]; then + WEB=${HERE}/../share/novnc/ +else + die "Could not find vnc.html" +fi + +# Find self.pem +if [ -n "${CERT}" ]; then + if [ ! -e "${CERT}" ]; then + die "Could not find ${CERT}" + fi +elif [ -e "$(pwd)/self.pem" ]; then + CERT="$(pwd)/self.pem" +elif [ -e "${HERE}/../self.pem" ]; then + CERT="${HERE}/../self.pem" +elif [ -e "${HERE}/self.pem" ]; then + CERT="${HERE}/self.pem" +else + echo "Warning: could not find self.pem" +fi + +# Check key file +if [ -n "${KEY}" ]; then + if [ ! -e "${KEY}" ]; then + die "Could not find ${KEY}" + fi +fi + +# try to find websockify (prefer local, try global, then download local) +if [[ -d ${HERE}/websockify ]]; then + WEBSOCKIFY=${HERE}/websockify/run + + if [[ ! -x $WEBSOCKIFY ]]; then + echo "The path ${HERE}/websockify exists, but $WEBSOCKIFY either does not exist or is not executable." + echo "If you intended to use an installed websockify package, please remove ${HERE}/websockify." + exit 1 + fi + + echo "Using local websockify at $WEBSOCKIFY" +else + WEBSOCKIFY_FROMSYSTEM=$(which websockify 2>/dev/null) + WEBSOCKIFY_FROMSNAP=${HERE}/../usr/bin/python2-websockify + [ -f $WEBSOCKIFY_FROMSYSTEM ] && WEBSOCKIFY=$WEBSOCKIFY_FROMSYSTEM + [ -f $WEBSOCKIFY_FROMSNAP ] && WEBSOCKIFY=$WEBSOCKIFY_FROMSNAP + + if [ ! -f "$WEBSOCKIFY" ]; then + echo "No installed websockify, attempting to clone websockify..." + WEBSOCKIFY=${HERE}/websockify/run + git clone https://github.com/novnc/websockify ${HERE}/websockify + + if [[ ! -e $WEBSOCKIFY ]]; then + echo "Unable to locate ${HERE}/websockify/run after downloading" + exit 1 + fi + + echo "Using local websockify at $WEBSOCKIFY" + else + echo "Using installed websockify at $WEBSOCKIFY" + fi +fi + +echo "Starting webserver and WebSockets proxy on port ${PORT}" +echo ${WEBSOCKIFY} > /tmp/1 + +if [ "$SERVERROLE" == "master" ] ; then + ${WEBSOCKIFY} ${SSLONLY} --web ${WEB} ${CERT:+--cert ${CERT}} ${KEY:+--key ${KEY}} --log-file ${HERE}/vnvlog ${PORT} --token-plugin TokenFile --token-source /home/ubuntu/vnc.config.d/ ${RECORD_ARG} & +fi + +proxy_pid="$!" +sleep 1 +if ! ps -p ${proxy_pid} >/dev/null; then + proxy_pid= + echo "Failed to start WebSockets proxy" + exit 1 +fi + +echo -e "\n\nNavigate to this URL:\n" +if [ "x$SSLONLY" == "x" ]; then + echo -e " http://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n" +else + echo -e " https://$(hostname):${PORT}/vnc.html?host=$(hostname)&port=${PORT}\n" +fi + +echo -e "Press Ctrl-C to exit\n\n" + +wait ${proxy_pid} diff --git a/swarmlab-app/startup.sh b/swarmlab-app/startup.sh new file mode 100644 index 0000000..783dd87 --- /dev/null +++ b/swarmlab-app/startup.sh @@ -0,0 +1,192 @@ +#!/bin/bash + +if [ ! -f $HOME/.vnc/passwd ] ; then + + if [ -z "$PASSWORD" ] ; then + PASSWORD=`pwgen -c -n -1 12` + echo -e "PASSWORD = $PASSWORD" > $HOME/password.txt + fi + + echo "$USER:$PASSWORD" | chpasswd + + # Set up vncserver + su $USER -c "mkdir $HOME/.vnc && echo '$PASSWORD' | vncpasswd -f > $HOME/.vnc/passwd && chmod 600 $HOME/.vnc/passwd && touch $HOME/.Xresources" +#vncpasswd -f <<<"write"$'\n'"view" >"$HOME/.vnc/passwd" +vncpasswd -f >"$HOME/.vnc/passwd" < $HOME/ngrok/Ngrok_URL.txt" + esac +fi + +if [ "$SERVERROLE" == "master" ] ; then + IP=$(ifconfig eth1 | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p') + mkdir -p /tmp/vnc1 + echo "s$TASKREPID: $IP:5901" > /tmp/vnc1/mpi$TASKREPID.server + rm -f /home/ubuntu/vnc.config.d/token.list + rm -f /home/ubuntu/vnc.config.d/ls1.conf + + cp /tmp/vnc1/mpi$TASKREPID.server /home/ubuntu/vnc.config.d/token.list + + if [ ! -z "$GIT_TUTOR" ] ; then + mkdir -p $HOME/tutor + cd $HOME/tutor + /usr/bin/git clone $GIT_REPO_TUTOR + fi + + +fi + + + +# --------------------------- +# create scripts +# -------------------------- +if [ "$GIT_TUTOR" ] ; then + +REPO=${GIT_TUTOR##*/} +REPO=${REPO%.git} + +# --------------------------- +# start playground +# -------------------------- + +read -r -d '' VAR < /home/ubuntu/crond/playground-start.sh +chmod +x /home/ubuntu/crond/playground-start.sh + +read -r -d '' VAR < /home/ubuntu/crond/playground.config.js +chmod +x /home/ubuntu/crond/playground.config.js + +# --------------------------- +# sync playground +# -------------------------- +read -r -d '' VAR < /home/ubuntu/crond/playground-sync.sh +chmod +x /home/ubuntu/crond/playground-sync.sh + +# ***************************************************** +# ***************************************************** +# ELSE +# ***************************************************** +# ***************************************************** + +else + +# --------------------------- +# hello world! +# -------------------------- +read -r -d '' VAR < /home/ubuntu/crond/playground-start.sh +chmod +x /home/ubuntu/crond/playground-start.sh + +# --------------------------- +# NO sync playground +# -------------------------- +read -r -d '' VAR < /home/ubuntu/crond/playground-sync.sh +chmod +x /home/ubuntu/crond/playground-sync.sh + +fi + +read -r -d '' VAR < /home/ubuntu/crond/info + + +chown -R $USER.$USER /home/$USER +chmod -R 755 /home/$USER + +/home/ubuntu/crond/playground-start.sh +/home/ubuntu/crond/playground-sync.sh + +ipfs get /ipns/QmQ1R6HEG1tTChyzYdqmt1rSbUMoD4K4oN9Db6DN7g3PpR -o $HOME/bin +/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf diff --git a/swarmlab-app/supervisor.conf b/swarmlab-app/supervisor.conf new file mode 100644 index 0000000..7918ce9 --- /dev/null +++ b/swarmlab-app/supervisor.conf @@ -0,0 +1,32 @@ +[program:apache2] +command=bash -c 'sleep 5 && /usr/sbin/apache2 -DFOREGROUND' +autorestart=true + +[program:sshd] +#command=bash -c 'sleep 5 && /usr/sbin/sshd -D' +command=/usr/sbin/sshd -D +stdout_logfile=/var/log/sshd.log +autorestart=true + +[program:ttyd] +command=bash -c 'sleep 5 && cd /home/ubuntu/project && sudo -u ubuntu ttyd --ssl --ssl-cert /home/ubuntu/ssl-ttyd/ca.crt --ssl-key /home/ubuntu/ssl-ttyd/ca.key -c user:%(ENV_PASSWORD)s -p 8088 -u ubuntu bash' +stdout_logfile=/var/log/ttyd.log +autorestart=true + + +[program:ipfs] +environment=IPFS_PATH=/home/ubuntu/.ipfs +command=ipfs daemon + +[program:vncserver] +command=vncserver -geometry 1600x900 :1 +user=ubuntu + +[program:noVNC] +command=/home/ubuntu/noVNC/utils/launch.sh --vnc localhost:5901 --listen 6080 +user=ubuntu +stdout_logfile=/var/log/novnc.log +redirect_stderr=true +#startretries=10 + +