#!/usr/bin/env bash # creates a writeable docker instance # exit if a command fails set -o errexit # exit if required variables are not set set -o nounset # return the exit status of the final command before a failure set -o pipefail # check for dependencies script_commands="gotty tmux" missing_counter=0 for needed_command in ${script_commands}; do if ! hash "${needed_command}" >/dev/null 2>&1; then printf "Command not found in PATH: %s\n" "${needed_command}" >&2 ((missing_counter++)) fi done if ((missing_counter > 0)); then printf "Minimum %d commands are missing in PATH, aborting.\n" "${missing_counter}" >&2 exit 1 fi # make sure exactly one argument was passed if [ "$#" -eq 2 ]; then distro="$1" command="$2" else echo -e "\nUsage: $0 \n" exit 1 fi ## configure gotty # listen port listen_port="${listen_port:-"1337"}" # web login credentials gotty_user="${gotty_user:-"admin"}" gotty_pass="${gotty_pass:-"fnord"}" # tab name gotty_title="${gotty_title:-"Try Me!"}" ## configure docker # cores cpu_limit="${cpu_limit:-"1"}" # memory mem_limit="${mem_limit:-"128m"}" # memory+swap swap_limit="${swap_limit:-"256m"}" # hostname host_name="${host_name:-"tryme"}" # kill any existing tmux session, and start a new one tmux has-session -t tryme && tmux kill-session -t tryme && echo "Killed old tmux session..." >/dev/null 2>&1 tmux -2 new-session -d -s tryme echo "Created new tmux session..." # expose an ephemeral container via gotty tmux rename-window -t tryme:0 'GoTTY' tmux select-window -t tryme:0 tmux send-keys "gotty --title-format "${gotty_title}" --credential "${gotty_user}":"${gotty_pass}" --port "${listen_port}" -w -- docker run --interactive --tty --rm --hostname="${host_name}" --cpus="${cpu_limit}" --memory="${mem_limit}" -- "${distro}" "${command}"" C-m echo "Launched GoTTY..." # attach to the new tmux session tmux -2 attach-session -t tryme