Don't use heredoc for sudo wrapping

This commit is contained in:
Marcus Whybrow 2012-11-03 22:20:53 +00:00
parent 2f41ae5552
commit 014ef73ed2
3 changed files with 42 additions and 58 deletions

View File

@ -48,16 +48,16 @@ function install_dependencies() {
# Verifies existence of or adds user for Minecraft server (default "minecraft") # Verifies existence of or adds user for Minecraft server (default "minecraft")
function add_minecraft_user() { function add_minecraft_user() {
install_log "Creating default user '$msm_user'" install_log "Creating default user '$msm_user'"
useradd --system --user-group $msm_user sudo useradd $msm_user || install_error "Couldn't create server user"
} }
# Verifies existence and permissions of msm server directory (default /opt/msm) # Verifies existence and permissions of msm server directory (default /opt/msm)
function create_msm_directories() { function create_msm_directories() {
install_log "Creating MSM directories" install_log "Creating MSM directories"
if [ ! -d "$msm_dir" ]; then if [ ! -d "$msm_dir" ]; then
mkdir -p "$msm_dir" || install_error "Couldn't create directory '$msm_dir'" sudo mkdir -p "$msm_dir" || install_error "Couldn't create directory '$msm_dir'"
fi fi
chown -R $msm_user:$msm_user "$msm_dir" || install_error "Couldn't change file ownership for '$msm_dir'" sudo chown -R $msm_user:$msm_user "$msm_dir" || install_error "Couldn't change file ownership for '$msm_dir'"
} }
# Fetches latest msm.conf, cron job, and init script # Fetches latest msm.conf, cron job, and init script
@ -67,15 +67,15 @@ function download_latest_files() {
fi fi
install_log "Downloading latest MSM configuration file" install_log "Downloading latest MSM configuration file"
wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/msm.conf \ sudo wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/msm.conf \
-O "$dl_dir/msm.conf.orig" || install_error "Couldn't download configuration file" -O "$dl_dir/msm.conf.orig" || install_error "Couldn't download configuration file"
install_log "Downloading latest MSM cron file" install_log "Downloading latest MSM cron file"
wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/cron/msm \ sudo wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/cron/msm \
-O "$dl_dir/msm.cron.orig" || install_error "Couldn't download cron file" -O "$dl_dir/msm.cron.orig" || install_error "Couldn't download cron file"
install_log "Downloading latest MSM version" install_log "Downloading latest MSM version"
wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/init/msm \ sudo wget https://raw.github.com/marcuswhybrow/minecraft-server-manager/latest/init/msm \
-O "$dl_dir/msm.init.orig" || install_error "Couldn't download init file" -O "$dl_dir/msm.init.orig" || install_error "Couldn't download init file"
} }
@ -83,23 +83,23 @@ function download_latest_files() {
function patch_latest_files() { function patch_latest_files() {
# patch config file # patch config file
install_log "Patching MSM configuration file" install_log "Patching MSM configuration file"
sed 's#USERNAME="minecraft"#USERNAME="'$msm_user'"#g' "$dl_dir/msm.conf.orig" | \ sudo sed 's#USERNAME="minecraft"#USERNAME="'$msm_user'"#g' "$dl_dir/msm.conf.orig" | \
sed "s#/opt/msm#$msm_dir#g" >"$dl_dir/msm.conf" sed "s#/opt/msm#$msm_dir#g" >"$dl_dir/msm.conf"
# patch cron file # patch cron file
install_log "Patching MSM cron file" install_log "Patching MSM cron file"
awk '{ if ($0 !~ /^#/) sub(/minecraft/, "'$msm_user'"); print }' \ sudo awk '{ if ($0 !~ /^#/) sub(/minecraft/, "'$msm_user'"); print }' \
"$dl_dir/msm.cron.orig" >"$dl_dir/msm.cron" "$dl_dir/msm.cron.orig" >"$dl_dir/msm.cron"
# patch init file # patch init file
install_log "Patching MSM init file" install_log "Patching MSM init file"
cp "$dl_dir/msm.init.orig" "$dl_dir/msm.init" sudo cp "$dl_dir/msm.init.orig" "$dl_dir/msm.init"
} }
# Installs msm.conf into /etc # Installs msm.conf into /etc
function install_config() { function install_config() {
install_log "Installing MSM configuration file" install_log "Installing MSM configuration file"
install -b -m0644 "$dl_dir/msm.conf" /etc/msm.conf sudo install -b -m0644 "$dl_dir/msm.conf" /etc/msm.conf
if [ ! -e /etc/msm.conf ]; then if [ ! -e /etc/msm.conf ]; then
install_error "Couldn't install configuration file" install_error "Couldn't install configuration file"
fi fi
@ -108,7 +108,7 @@ function install_config() {
# Installs msm.cron into /etc/cron.d # Installs msm.cron into /etc/cron.d
function install_cron() { function install_cron() {
install_log "Installing MSM cron file" install_log "Installing MSM cron file"
install "$dl_dir/msm.cron" /etc/cron.d/msm || install_error "Couldn't install cron file" sudo install "$dl_dir/msm.cron" /etc/cron.d/msm || install_error "Couldn't install cron file"
} }
# Reloads cron service (if necessary) # Reloads cron service (if necessary)
@ -120,10 +120,10 @@ function reload_cron() {
# Installs init script into /etc/init.d # Installs init script into /etc/init.d
function install_init() { function install_init() {
install_log "Installing MSM init file" install_log "Installing MSM init file"
install -b "$dl_dir/msm.init" /etc/init.d/msm || install_error "Couldn't install init file" sudo install -b "$dl_dir/msm.init" /etc/init.d/msm || install_error "Couldn't install init file"
install_log "Making MSM accessible as the command 'msm'" install_log "Making MSM accessible as the command 'msm'"
ln -s /etc/init.d/msm /usr/local/bin/msm sudo ln -s /etc/init.d/msm /usr/local/bin/msm
} }
# Enables init script in default runlevels # Enables init script in default runlevels
@ -135,7 +135,7 @@ function enable_init() {
# Updates rest of MSM using init script updater # Updates rest of MSM using init script updater
function update_msm() { function update_msm() {
install_log "Asking MSM to update itself" install_log "Asking MSM to update itself"
/etc/init.d/msm update --noinput sudo /etc/init.d/msm update --noinput
} }
function install_complete() { function install_complete() {
@ -143,30 +143,18 @@ function install_complete() {
} }
function install_msm() { function install_msm() {
echo "This script requires superuser access to install files to /etc." config_installation
echo "You will be prompted for your password by sudo." add_minecraft_user
update_system_packages
# Clear existing sudo credentials install_dependencies
sudo -k create_msm_directories
download_latest_files
# Run install functions as sudo patch_latest_files
# These functions are defined in another script install_config
# after which this script is sourced install_cron
sudo sh <<SCRIPT reload_cron
config_installation install_init
update_system_packages enable_init
install_dependencies update_msm
add_minecraft_user install_complete
create_msm_directories
download_latest_files
patch_latest_files
install_config
install_cron
reload_cron
install_init
enable_init
update_msm
install_complete
SCRIPT
} }

View File

@ -1,25 +1,23 @@
wget -q https://raw.github.com/marcuswhybrow/minecraft-server-manager/master/installers/common.sh -O /tmp/msmcommon.sh source <(wget -qO- https://raw.github.com/marcuswhybrow/minecraft-server-manager/master/installers/common.sh)
source /tmp/msmcommon.sh
rm -f /tmp/msmcommon.sh
function update_system_packages() { function update_system_packages() {
install_log "Updating sources" install_log "Updating sources"
apt-get update || install_error "Couldn't update package list" sudo apt-get update || install_error "Couldn't update package list"
apt-get upgrade || install_error "Couldn't upgrade packages" sudo apt-get upgrade || install_error "Couldn't upgrade packages"
} }
function install_dependencies() { function install_dependencies() {
install_log "Installing required packages" install_log "Installing required packages"
apt-get install screen rsync zip || install_error "Couldn't install dependencies" sudo apt-get install screen rsync zip || install_error "Couldn't install dependencies"
} }
function reload_cron() { function reload_cron() {
install_log "Reloading cron service" install_log "Reloading cron service"
hash service 2>/dev/null hash service 2>/dev/null
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
service cron reload sudo service cron reload
else else
/etc/init.d/cron reload sudo /etc/init.d/cron reload
fi fi
} }
@ -27,10 +25,10 @@ function enable_init() {
install_log "Enabling automatic startup and shutdown" install_log "Enabling automatic startup and shutdown"
hash insserv 2>/dev/null hash insserv 2>/dev/null
if [[ $? == 0 ]]; then if [[ $? == 0 ]]; then
insserv msm sudo insserv msm
else else
update-rc.d msm defaults sudo update-rc.d msm defaults
fi fi
} }
install_msm install_msm

View File

@ -1,25 +1,23 @@
wget -q https://raw.github.com/marcuswhybrow/minecraft-server-manager/master/installers/common.sh -O /tmp/msmcommon.sh source <(wget -qO- https://raw.github.com/marcuswhybrow/minecraft-server-manager/master/installers/common.sh)
source /tmp/msmcommon.sh
rm -f /tmp/msmcommon.sh
function update_system_packages() { function update_system_packages() {
install_log "Updating sources" install_log "Updating sources"
yum update || install_error "Couldn't update packages" sudo yum update || install_error "Couldn't update packages"
} }
function install_dependencies() { function install_dependencies() {
install_log "Installing required packages" install_log "Installing required packages"
yum install screen rsync zip || install_error "Couldn't install dependencies" sudo yum install screen rsync zip || install_error "Couldn't install dependencies"
} }
function reload_cron() { function reload_cron() {
install_log "Reloading cron service" install_log "Reloading cron service"
service crond reload sudo service crond reload
} }
function enable_init() { function enable_init() {
install_log "Enabling automatic startup and shutdown" install_log "Enabling automatic startup and shutdown"
chkconfig --add msm sudo chkconfig --add msm
} }
install_msm install_msm