#!/usr/bin/env bash # creates a writable shared 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="docker 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:-"1339"}" # web login credentials gotty_user="${gotty_user:-"admin"}" gotty_pass="${gotty_pass:-"fnord"}" # tab name gotty_title="${gotty_title:-"Twitch Plays Sysadmin!"}" ## configure docker # cores cpu_limit="${cpu_limit:-"1"}" # memory mem_limit="${mem_limit:-"512m"}" # memory+swap swap_limit="${swap_limit:-"512m"}" # hostname host_name="${host_name:-"multiplayer"}" # remove old container, if any docker rm -f multiplayer && echo "Removed old docker container..." # kill any existing tmux session, and start a new one tmux has-session -t multiplayer && tmux kill-session -t multiplayer && echo "Killed old tmux session..." >/dev/null 2>&1 tmux -2 new-session -d -s multiplayer echo "Created new tmux session..." # create a tmux session, and run docker in window 1 tmux new-window -t multiplayer:1 -n 'Docker' tmux select-window -t multiplayer:1 tmux send-keys "docker run -it --rm --name=multiplayer --hostname=""${host_name}"" --cpus=""${cpu_limit}"" --memory=""${mem_limit}"" --memory-swap=""${swap_limit}"" -- ""${distro}"" ""${command}""" C-m echo "Created container..." # run gotty in window 0 tmux rename-window -t multiplayer:0 'GoTTY' tmux select-window -t multiplayer:0 tmux send-keys "gotty --title-format "${gotty_title}" --credential "${gotty_user}":"${gotty_pass}" --port "${listen_port}" -w -- docker attach multiplayer" C-m echo "Launched GoTTY..." # attach to the new tmux session to join the fun tmux select-window -t multiplayer:1 tmux -2 attach-session -t multiplayer