Created
February 17, 2020 14:54
-
-
Save naari3/ff9ed7c95a7334cd8160125d07af9308 to your computer and use it in GitHub Desktop.
mc-cheap
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
#!/bin/bash -xe | |
# | |
# See: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html | |
# | |
# Make sure both volumes have been created AND attached to this instance ! | |
# | |
# We do not need a loop counter in the "until" statements below because | |
# there is a 5 minute limit on the CreationPolicy for this EC2 instance already. | |
EC2_INSTANCE_ID=$(curl -s http://instance-data/latest/meta-data/instance-id) | |
EC2_VOLUME_ID="vol-009aa0da0297b5c25" | |
region=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed -e 's/.$//') | |
MC_VERSION="1.15.1" | |
###################################################################### | |
# Volume /dev/sdh (which will get created as /dev/xvdh on Amazon Linux) | |
aws ec2 attach-volume --device /dev/sdh --region ${region} \ | |
--instance-id ${EC2_INSTANCE_ID} --volume-id vol-009aa0da0297b5c25 | |
DATA_STATE="unknown" | |
until [ "${DATA_STATE}" == "attached" ]; do | |
DATA_STATE=$(aws ec2 describe-volumes \ | |
--region ${region} \ | |
--filters \ | |
Name=attachment.instance-id,Values=${EC2_INSTANCE_ID} \ | |
Name=attachment.device,Values=/dev/sdh \ | |
--query Volumes[].Attachments[].State \ | |
--output text) | |
sleep 5 | |
done | |
# Format /dev/xvdh if it does not contain a partition yet | |
if [ "$(file -b -s /dev/sdh)" == "data" ]; then | |
mkfs -t ext4 /dev/sdh | |
fi | |
mkdir -p /minecraft | |
mount /dev/sdh /minecraft | |
if test ! -d /minecraft/server; then | |
mkdir -p /minecraft/server | |
fi | |
chown -R ec2-user:ec2-user /minecraft | |
# Persist the volume in /etc/fstab so it gets mounted again | |
echo '/dev/sdh /minecraft ext4 defaults,nofail 0 2' | sudo tee -a /etc/fstab | |
docker run -d -it \ | |
-e EULA=TRUE -e VERSION=${MC_VERSION} -e INIT_MEMORY=1G -e MAX_MEMORY=16G \ | |
-p 25565:25565 \ | |
-v /minecraft/server:/data \ | |
--log-driver=awslogs \ | |
--log-opt awslogs-region=ap-northeast-1 \ | |
--log-opt awslogs-group=mc-cheap \ | |
--log-opt awslogs-stream=minecraft \ | |
--name mc \ | |
itzg/minecraft-server | |
# set route53 | |
R53_ZONE_NAME=mc.naari3.net. | |
R53_HOSTNAME=mcserver | |
HOSTED_ZONEID=/hostedzone/Z3GZ6G254FYNY3 | |
R53_CALLER="${R53_ZONE_NAME}-`date +%Y%m%d-%H%M%S`" | |
R53_COMMENT="dynamic dns record set" | |
MY_DDNS_RECORD=${R53_ZONE_NAME} | |
MY_PUBLIC_IP=`curl -s ifconfig.me` | |
if [ -z $MY_PUBLIC_IP ] ; then | |
echo "failed to get global IP address" | |
exit 1 | |
fi | |
cat <<EOT > /tmp/recordset.json | |
{ | |
"Comment": "create A record", | |
"Changes": [ | |
{ | |
"Action": "UPSERT", | |
"ResourceRecordSet": { | |
"Name": "${MY_DDNS_RECORD}", | |
"Type": "A", | |
"TTL": 60, | |
"ResourceRecords": [ | |
{ | |
"Value": "${MY_PUBLIC_IP}" | |
} | |
] | |
} | |
} | |
] | |
} | |
EOT | |
aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONEID --change-batch file:///tmp/recordset.json | |
rm -rf /tmp/recordset.json | |
# players check | |
cat << 'BASH' > /server_players_check.sh | |
#!/bin/bash -xe | |
player_count=$(docker exec mc bash -c "if [[ \`mcstatus localhost status | grep players:\` =~ ^players:\ ([0-9]+) ]]; then | |
count=\${BASH_REMATCH[1]}; | |
echo \$count; | |
fi") | |
if [ ${player_count} == "0" ]; then | |
if test -e /tmp/until_no_man; then | |
aws autoscaling update-auto-scaling-group --region=ap-northeast-1 --auto-scaling-group-name=mc-cheap --desired-capacity=0 | |
else | |
touch /tmp/until_no_man | |
fi | |
else | |
rm -f /tmp/until_no_man | |
fi | |
BASH | |
chmod +x /server_players_check.sh | |
echo "*/15 * * * * /server_players_check.sh" > /tmp/rootcron | |
crontab -u root /tmp/rootcron | |
rm /tmp/rootcron | |
# create tag | |
aws ec2 create-tags --resources ${EC2_INSTANCE_ID} --tags Key=minecraft-status,Value=starting --region ${region} | |
cat << BASH > /minecraft_server_status_check.sh | |
#!/bin/bash -xe | |
while true; do | |
status=\$(if [[ \`docker ps -f "name=mc" --format "{{.Status}}"\` =~ \\((.+?)\\) ]]; then | |
status=\${BASH_REMATCH[1]}; | |
echo \$status; | |
fi) | |
if [ "\${status}" == "healthy" ]; then | |
aws ec2 create-tags --resources ${EC2_INSTANCE_ID} --tags Key=minecraft-status,Value=healthy --region ${region} | |
fi | |
sleep 10s | |
done | |
BASH | |
chmod +x /minecraft_server_status_check.sh | |
bash /minecraft_server_status_check.sh & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment