forked from Thue/rfwadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·174 lines (145 loc) · 8.48 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/bin/bash
#If a previous installation exists, this script installs a new code version while preserverving configuration and data
### Settings ###
#You can also use WEBINTERFACE_DIR=/var/www/ to put the web interface in the www root
if [ -d /var/www/html ]; then
#centos has webroot /var/www/html
WEBINTERFACE_DIR="/var/www/html/rfwadmin"
else
#Ubuntu has webroot /var/www
WEBINTERFACE_DIR="/var/www/rfwadmin"
fi
#rfwadmin runs as the web server user. If $WEBSERVER_USER is empty, this script will try to guess the web server user from the owner of the process currently using port 80
WEBSERVER_USER=
PATH_BASE="/var/lib/minecraft"
#Symlink /var/lib/minecraft/web and /var/lib/minecraft/minecraft.sh
#back to fsroot . Useful for rfwadmin development if symlinking to a
#git checkout
SYMLINK_CODE=0
### Checks ###
function error_exit
{
PROGNAME=$(basename $0)
echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
exit 1
}
if [ "`whoami`" != "root" ]; then
echo "Script must be run as root"
exit 1
fi
#determine web server user by looking at who is using port 80
if [ -z "$WEBSERVER_USER" ]; then
WEBSERVER_USER=$(ps axho user,comm|grep -E "httpd|apache"|uniq|grep -v "root"|awk 'END {if ($1) print $1}')
echo "Using '$WEBSERVER_USER' as the HTTP server user, which will also run the Minecraft server."
fi
#See if what we got looks remotely like a unix username
if ! [[ "$WEBSERVER_USER" =~ ^[a-zA-Z_][0-9a-zA-Z_-]*$ ]] ; then
error_exit "error: HTTP server user ($WEBSERVER_USER) looks wrong"
fi
### Customize files to install ###
#If there is already stuff in /servers, then we are updating an old installation, and don't want to overwrite with a blank server configuration
if [ ! -d "$PATH_BASE/servers" ] || [ ! "$(ls -A $PATH_BASE/servers)" ]; then
CONFIGURE_SERVER="1"
else
CONFIGURE_SERVER="0"
echo "Not configuring any new Minecraft servers, since there already seem to be a configured server in $PATH_BASE/servers . Delete that directory and rerun the install script if you want to configure a new server."
fi
#Configure a default server if no previous configuration exists
if [ $CONFIGURE_SERVER == "1" ]; then
LATEST_SERVER_VERSION=`wget --quiet -O - https://s3.amazonaws.com/Minecraft.Download/versions/versions.json |grep '"release": ' |sed 's/^ \+"release": "\(.\+\)".\+$/\1/'`
PATTERN='^[0-9.]+$'
if [[ ! $LATEST_SERVER_VERSION =~ $PATTERN ]] ; then
error_exit "Failed to parse latest Minecraft server version from https://s3.amazonaws.com/Minecraft.Download/versions/versions.json"
fi
LATEST_SERVER_BINARY=minecraft_server.${LATEST_SERVER_VERSION}.jar
DOWNLOAD_URL="https://s3.amazonaws.com/Minecraft.Download/versions/${LATEST_SERVER_VERSION}/minecraft_server.${LATEST_SERVER_VERSION}.jar"
#If we are re-running the install script on the same day, no need to re-download the server
if [ ! -f "fsroot/var/lib/minecraft/jars/serverjars/$LATEST_SERVER_BINARY" ]; then
echo "Downloading latest minecraft server jar from Mojang."
wget -O "fsroot/var/lib/minecraft/jars/serverjars/${LATEST_SERVER_BINARY}.tmp" $DOWNLOAD_URL || error_exit "Failed to download minecraft server"
mv "fsroot/var/lib/minecraft/jars/serverjars/${LATEST_SERVER_BINARY}.tmp" "fsroot/var/lib/minecraft/jars/serverjars/${LATEST_SERVER_BINARY}"
fi
cat fsroot/var/lib/minecraft/servers/default/minecraft.sh | sed "s|^FILE_JAR=.*\$|FILE_JAR=\"$PATH_BASE/jars/serverjars/$LATEST_SERVER_BINARY\"|" | sed "s|^PATH_BASE=.*\$|PATH_BASE=\"$PATH_BASE\"|" > fsroot/var/lib/minecraft/servers/default/minecraft.sh.customized
fi
#Maps from before Minecraft 1.2 is not in the "anvil" format. Mojang's
#server will autoconvert them, but if you are repeatably reloading a
#map that is slow. So minecraft_base.sh has a "convert" command to
#convert them once and for all (not available from web interface).
if [ ! -f fsroot/var/lib/minecraft/jars/converter/AnvilConverter.jar -a ! -f $PATH_BASE/jars/converter/AnvilConverter.jar ]; then
echo "Downloading Anvil converter used to convert old maps."
wget -O "fsroot/var/lib/minecraft/jars/converter/Minecraft.AnvilConverter.zip" "http://assets.minecraft.net/12w07a/Minecraft.AnvilConverter.zip"
unzip -q -d "fsroot/var/lib/minecraft/jars/converter" "fsroot/var/lib/minecraft/jars/converter/Minecraft.AnvilConverter.zip" || echo "Failed to extract anvil converter because 'unzip' is not installed. You probably don't need that anyway."
fi
#Configure index.php to use correct PATH_BASE
cat fsroot/var/www/rfwadmin/index.php | sed "s|^\\\$include_base = .*\$|\$include_base = \"$PATH_BASE\";|" > fsroot/var/www/rfwadmin/index.php.customized
#Create a default password (using only unambiguous chars, no 0O1lI...)
PASSWORD=`cat /dev/urandom | tr -dc 23456789abcdefghkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ | head -c${1:-10};`
cat fsroot/var/www/rfwadmin/index.php.customized | sed "s|^\\\$passwords = null;.*\$|\$passwords = Array('${PASSWORD}');|" > fsroot/var/www/rfwadmin/index.php.customized2
mv fsroot/var/www/rfwadmin/index.php.customized2 fsroot/var/www/rfwadmin/index.php.customized
#Configure init script to use correct userid and PATH_BASE
cat fsroot/etc/init.d/minecraft_default.sh | sed "s/^SU_TO_USER=.*$/SU_TO_USER=\"$WEBSERVER_USER\"/" | sed "s|^PATH_BASE=.*\$|PATH_BASE=\"$PATH_BASE\"|" > fsroot/etc/init.d/minecraft_default.sh.customized
### ACTUALLY INSTALL ###
#No changes made outside current directory until this point
#install /var/lib
mkdir -p $PATH_BASE
mkdir -p $PATH_BASE/maps/
mkdir -p $PATH_BASE/jars/converter
mkdir -p $PATH_BASE/jars/plugins
mkdir -p $PATH_BASE/jars/serverjars
mkdir -p $PATH_BASE/servers
cp fsroot/var/lib/minecraft/jars/plugins/README $PATH_BASE/jars/plugins
if [ $SYMLINK_CODE == 1 ]; then
ln -s "`pwd`/fsroot/var/lib/minecraft/minecraft_base.sh" $PATH_BASE
chmod +x $PATH_BASE/minecraft_base.sh
ln -s "`pwd`/fsroot/var/lib/minecraft/web" $PATH_BASE
else
cp fsroot/var/lib/minecraft/minecraft_base.sh $PATH_BASE
chmod +x $PATH_BASE/minecraft_base.sh
cp -r fsroot/var/lib/minecraft/web $PATH_BASE
fi
if [ -f fsroot/var/lib/minecraft/jars/converter/AnvilConverter.jar ]; then
#If downloaded for installation above, then install it
cp fsroot/var/lib/minecraft/jars/converter/AnvilConverter.jar $PATH_BASE/jars/converter
fi
#If no server config exists from previous install, then configure a server
if [ $CONFIGURE_SERVER == "1" ]; then
cp -r fsroot/var/lib/minecraft/servers/default $PATH_BASE/servers
echo "Marking Minecraft EULA as accepted in $PATH_BASE/servers/default/server/eula.txt (otherwise the server won't start)"
mkdir $PATH_BASE/servers/default/backups
mv $PATH_BASE/servers/default/minecraft.sh.customized $PATH_BASE/servers/default/minecraft.sh
chmod +x $PATH_BASE/servers/default/minecraft.sh
cp fsroot/var/lib/minecraft/jars/serverjars/* $PATH_BASE/jars/serverjars
#Install init script
cp fsroot/etc/init.d/minecraft_default.sh.customized /etc/init.d/minecraft_default.sh
chmod +x /etc/init.d/minecraft_default.sh
##Set to stop in ( http://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/runlevels.html ):
#halt
ln -s /etc/init.d/minecraft_default.sh /etc/rc0.d/K01minecraft_default.sh
#single user mode
ln -s /etc/init.d/minecraft_default.sh /etc/rc1.d/K01minecraft_default.sh
#reboot
ln -s /etc/init.d/minecraft_default.sh /etc/rc6.d/K01minecraft_default.sh
#without network
ln -s /etc/init.d/minecraft_default.sh /etc/rc2.d/K01minecraft_default.sh
##set to start in the more or less normal levels
ln -s /etc/init.d/minecraft_default.sh /etc/rc3.d/S99minecraft_default.sh
ln -s /etc/init.d/minecraft_default.sh /etc/rc4.d/S99minecraft_default.sh
ln -s /etc/init.d/minecraft_default.sh /etc/rc5.d/S99minecraft_default.sh
fi
chown -R $WEBSERVER_USER:root $PATH_BASE
#install web interface files
if [ ! -d "$WEBINTERFACE_DIR" ]; then
mkdir --parents "$WEBINTERFACE_DIR"
fi
if [ ! -e "$WEBINTERFACE_DIR"/index.php ]; then
cp -v fsroot/var/www/rfwadmin/index.php.customized "$WEBINTERFACE_DIR/index.php"
echo -e "NOTE: The \033[1mpassword\033[m for the web interface is '\033[1m$PASSWORD\033[m'; it can be changed by editing $WEBINTERFACE_DIR/index.php"
else
if [ ! -e "$WEBINTERFACE_DIR"/rfwadmin_files ]; then
echo "Refusing to overwrite existing $WEBINTERFACE_DIR/index.php file, but I don't think it belongs to rfwadmin. Do a manual 'cp fsroot/var/www/index.php $WEBINTERFACE_DIR/index.php' if you really want to."
fi
fi
if [ ! -e "$WEBINTERFACE_DIR"/rfwadmin_files ]; then
ln -s /var/lib/minecraft/web/visible "$WEBINTERFACE_DIR"/rfwadmin_files
fi
/etc/init.d/minecraft_default.sh start