Last active
July 24, 2020 02:50
-
-
Save jamesalbert/621bf21a97d9a00d2cceafbbf419ad3e to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env bash | |
set -uo pipefail | |
# set -x | |
PATH=$PWD/bin:$PATH | |
RUST_HOME=$HOME/docker-rust | |
SYSV=$(pidof /sbin/init >/dev/null && echo "yes" || echo "no") | |
SYSD=$(pidof systemd >/dev/null && echo "yes" || echo "no") | |
read -r -d '' CONFIG << EOM | |
global.dependencies=git make vim wget | |
debian.package.install=apt install -y | |
debian.package.update=apt update -y | |
debian.group.add=groupadd -g 7878 rustgroup | |
debian.user.add=useradd -m -s /bin/bash -u 7878 | |
debian.user.add.group=usermod -aG | |
debian.user.password.set=passwd | |
debian.user.setShell=usermod --shell /bin/bash | |
debian.dependencies=apt-transport-https ca-certificates curl gnupg-agent software-properties-common | |
debian.package.add.docker.gpg=curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - | |
debian.package.add.docker.repo=add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian \$(lsb_release -cs) stable" | |
centos.package.install=yum install -y | |
centos.package.update=yum check-update -y | |
centos.group.add=groupadd -g 7878 rustgroup | |
centos.user.add=useradd -m -s /bin/bash | |
centos.user.add.group=usermod -aG | |
centos.user.password.set=passwd | |
centos.user.setShell=usermod --shell /bin/bash | |
centos.dependencies=yum-utils https://download.docker.com/linux/centos/7/x86_64/stable/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm | |
centos.package.add.docker.repo=yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo | |
centos.firewall.enabled=firewall-cmd --state | |
centos.firewall.enabledResponse=running | |
centos.firewall.openPort=firewall-cmd --zone=public --permanent --add-port= | |
centos.firewall.reload=firewall-cmd --reload | |
EOM | |
function platform { | |
grep -oP "^ID=\"?\K([\w]+)" /etc/os-release; | |
} | |
function remove-quotes { | |
local quoteless="${1%\"}" | |
echo "${quoteless#\"}" | |
} | |
# throw an error and exit | |
function throw { | |
local message=$1; | |
echo "$message" >&2; | |
exit 1; | |
} | |
function need-sudo { | |
test ! "$USER" == "root"; | |
} | |
# prompt user for yes or no question | |
function prompt { | |
local message=$1; | |
read -p "$message [Y/n]" -n 1 -r; | |
echo; | |
[[ $REPLY =~ ^[Yy]$ ]] && return 0 || return 1; | |
} | |
function config-get { | |
local key="$1" | |
if [[ ! "$key" =~ ^global ]]; then | |
key="$(platform).${key}" | |
fi | |
echo "$CONFIG" | grep -oP "^${key}=\K(.+)" | |
} | |
function package-update { | |
run-sudo "$(config-get package.update)" | |
} | |
function package-install { | |
run-sudo "$(config-get package.install) $*"; | |
} | |
function should-install { | |
! command -v "$1" >/dev/null; | |
} | |
function run-sudo { | |
local command="$*" | |
if need-sudo ; then | |
command="sudo $command" | |
fi | |
eval "$command" | |
} | |
function is-root { | |
test "$(id -u "$USER")" == "0"; | |
} | |
function package-add-docker-repo { | |
if config-get package.add.docker.gpg ; then | |
run-sudo "$(config-get package.add.docker.gpg)" | |
fi | |
run-sudo "$(config-get package.add.docker.repo)" | |
} | |
function install-docker { | |
package-add-docker-repo | |
package-update | |
if should-install containerd ; then | |
package-install containerd.io | |
fi | |
package-install docker-ce docker-ce-cli | |
if [[ "$SYSV" == "yes" ]]; then | |
run-sudo update-rc.d docker enable | |
elif [[ "$SYSD" == "yes" ]]; then | |
run-sudo systemctl restart docker.service | |
run-sudo systemctl enable docker.service | |
fi | |
} | |
function install-golang { | |
wget -P /tmp https://dl.google.com/go/go1.14.3.linux-amd64.tar.gz | |
tar zxvf /tmp/go1.14.3.linux-amd64.tar.gz -C "$HOME" | |
} | |
function is-dependencies-installed { | |
for pkg in $(config-get global.dependencies) ; do | |
if should-install "$pkg" ; then | |
return 1; | |
fi | |
done | |
for pkg in $(config-get dependencies) ; do | |
if should-install "$pkg" ; then | |
return 1; | |
fi | |
done | |
if should-install docker ; then | |
return 1; | |
fi | |
if should-install go ; then | |
return 1; | |
fi | |
return 0; | |
} | |
function is-repo-cloned { | |
test -d "$RUST_HOME"; | |
} | |
function is-system-configured { | |
is-dependencies-installed \ | |
&& is-repo-cloned | |
} | |
function install-dependencies { | |
package-install "$(config-get global.dependencies)" | |
package-install "$(config-get dependencies)" | |
if should-install docker ; then | |
install-docker; | |
fi | |
if ! echo "$PATH" | grep "$HOME/go/bin" ; then | |
PATH="$HOME/go/bin:$PATH" | |
fi | |
if should-install go ; then | |
install-golang; | |
fi | |
printf "dependencies are installed!\n" | |
} | |
function clone-repo { | |
printf "cloning decay.dev/rust-server to %s...\n" "$RUST_HOME" | |
git clone https://github.com/initrode/docker-rust.git "$RUST_HOME" | |
} | |
function configure-firewall { | |
if [[ "$(config-get firewall.enabled)" == "" ]] ; then | |
return 0; | |
fi | |
if [[ "$(run-sudo "$(config-get firewall.enabled)")" == "$(config-get firewall.enabledResponse)" ]]; then | |
run-sudo "$(config-get firewall.openPort)28015/tcp" | |
run-sudo "$(config-get firewall.openPort)28015/udp" | |
run-sudo "$(config-get firewall.openPort)28016/tcp" | |
run-sudo "$(config-get firewall.openPort)8080/tcp" | |
run-sudo "$(config-get firewall.reload)" | |
fi | |
} | |
function rustuser-exists { | |
id -u rustuser | |
} | |
function create-rustuser { | |
run-sudo "$(config-get user.add) rustuser" | |
run-sudo "$(config-get group.add) rustuser" | |
printf "Set a password for your new 'rustuser' user\n" | |
run-sudo "$(config-get user.password.set) rustuser" | |
echo "rustuser ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/rustuser | |
run-sudo "$(config-get user.setShell) rustuser" | |
run-sudo "cp -pr $HOME/.ssh /home/rustuser" | |
run-sudo "chown -R 7878:7878 /home/rustuser" | |
} | |
function system-configure { | |
if ! is-system-configured ; then | |
printf "configuring system...\n" | |
configure-firewall; | |
if ! is-dependencies-installed ; then | |
install-dependencies; | |
fi | |
if ! is-repo-cloned ; then | |
clone-repo; | |
fi | |
cd "$RUST_HOME" || exit; | |
go mod tidy; | |
make build; | |
if echo $PATH | grep -q $RUST_HOME ; then | |
PATH=$RUST_HOME/bin:$PATH | |
fi | |
if ! grep "RUST_HOME" ~/.bash_login ; then | |
echo "export RUST_HOME=$RUST_HOME" >> ~/.bash_login | |
echo "export PATH=\$RUST_HOME/bin:$PATH" >> ~/.bash_login | |
fi | |
if test ! -d $RUST_HOME/.config/docker-rust ; then | |
mkdir -p $RUST_HOME/.config/docker-rust; | |
echo "{}" > $RUST_HOME/.config/docker-rust/config.json; | |
fi | |
fi | |
} | |
function kill-ssh { | |
pgrep -u "$USER" sshd | sort -nr | head -n1 | xargs sudo kill -9 | |
} | |
if ! rustuser-exists ; then | |
create-rustuser; | |
fi | |
if test ! "$USER" == "rustuser" ; then | |
echo "will not run as '$USER'; Log back in with 'ssh rustuser@$(hostname -I | awk '{print $1}')' and try again"; | |
kill-ssh; | |
fi | |
system-configure; | |
echo ' | |
Welcome to your Rust server | |
## rusty | |
### setup | |
# run this if your setting up this server for the first time | |
$ rusty setup | |
### admin | |
# start rust server | |
$ rusty start | |
# stop rust server | |
$ rusty stop | |
# restart rust server | |
$ rusty restart | |
# get rust logs | |
$ rusty logs | |
# configure rust server | |
$ rusty configure | |
# install oxide plugins | |
$ rusty oxide search | |
' | sudo tee /etc/motd | |
cat << EOM | |
Looks like you're all setup. Great! | |
We're going to boot you off now. Log back in and you're all set. | |
Log back in with 'ssh rustuser@$(hostname -I | awk '{print $1}')' | |
EOM | |
run-sudo "$(config-get user.add.group) docker rustuser" | |
kill-ssh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment