-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_data.sh
168 lines (144 loc) · 4.24 KB
/
user_data.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
#!/bin/bash
#
# Install, configure and start a new Minecraft server
# This supports Ubuntu and Amazon Linux 2 flavors of Linux (maybe/probably others but not tested).
set -e
# Determine linux distro
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
# Update OS and install start script
ubuntu_linux_setup() {
export SSH_USER="ubuntu"
export DEBIAN_FRONTEND=noninteractive
/usr/bin/apt-get update
/usr/bin/apt-get -yq install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" default-jre wget awscli
/bin/cat <<"__UPG__" > /etc/apt/apt.conf.d/10periodic
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Download-Upgradeable-Packages "1";
APT::Periodic::AutocleanInterval "7";
APT::Periodic::Unattended-Upgrade "1";
__UPG__
# Init script for starting, stopping
cat <<INIT > /etc/init.d/minecraft
#!/bin/bash
# Short-Description: Minecraft server
start() {
echo "Starting minecraft server from ${mc_root}..."
start-stop-daemon --start --quiet --pidfile ${mc_root}/minecraft.pid -m -b -c $SSH_USER -d ${mc_root} --exec /usr/bin/java -- -Xmx${java_mx_mem} -Xms${java_ms_mem} -jar ${mc_root}/$MINECRAFT_JAR nogui
}
stop() {
echo "Stopping minecraft server..."
start-stop-daemon --stop --pidfile ${mc_root}/minecraft.pid
}
case \$1 in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 5
start
;;
esac
exit 0
INIT
# Start up on reboot
/bin/chmod +x /etc/init.d/minecraft
/usr/sbin/update-rc.d minecraft defaults
}
# Update OS and install start script
amazon_linux_setup() {
export SSH_USER="ec2-user"
/usr/bin/yum install java-1.8.0 yum-cron wget awscli -y
/bin/sed -i -e 's/update_cmd = default/update_cmd = security/'\
-e 's/apply_updates = no/apply_updates = yes/'\
-e 's/emit_via = stdio/emit_via = email/' /etc/yum/yum-cron.conf
chkconfig yum-cron on
service yum-cron start
/usr/bin/yum upgrade -y
cat <<SYSTEMD > /etc/systemd/system/minecraft.service
[Unit]
Description=Minecraft Server
After=network.target
[Service]
Type=simple
User=$SSH_USER
WorkingDirectory=${mc_root}
ExecStart=/usr/bin/java -Xmx${java_mx_mem} -Xms${java_ms_mem} -jar $MINECRAFT_JAR nogui
Restart=on-abort
[Install]
WantedBy=multi-user.target
SYSTEMD
# Start on boot
/usr/bin/systemctl enable minecraft
}
MINECRAFT_JAR="minecraft_server.${mc_version}.jar"
case $OS in
Ubuntu*)
ubuntu_linux_setup
;;
Amazon*)
amazon_linux_setup
;;
*)
echo "$PROG: unsupported OS $OS"
exit 1
esac
# Create mc dir, sync S3 to it and download mc if not already there (from S3)
/bin/mkdir -p ${mc_root}
/usr/bin/aws s3 sync s3://${mc_bucket} ${mc_root}
[[ -e "$MINECRAFT_JAR" ]] || /usr/bin/wget -O ${mc_root}/$MINECRAFT_JAR https://launcher.mojang.com/v1/objects/bb2b6b1aefcd70dfd1892149ac3a215f6c636b07/server.jar
# Cron job to sync data to S3 every five mins
/bin/cat <<CRON > /etc/cron.d/minecraft
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:${mc_root}
*/${mc_backup_freq} * * * * $SSH_USER /usr/bin/aws s3 sync ${mc_root} s3://${mc_bucket}
CRON
# Update minecraft EULA
/bin/cat >${mc_root}/eula.txt<<EULA
#By changing the setting below to TRUE you are indicating your agreement to our EULA (https://account.mojang.com/documents/minecraft_eula).
#Tue Jan 27 21:40:00 UTC 2015
eula=true
EULA
# Not root
/bin/chown -R $SSH_USER ${mc_root}
# Start the server
case $OS in
Ubuntu*)
/etc/init.d/minecraft start
;;
Amazon*)
/usr/bin/systemctl start minecraft
;;
esac
exit 0