-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcreate_minecraft_vm.sh
More file actions
97 lines (77 loc) · 3.14 KB
/
create_minecraft_vm.sh
File metadata and controls
97 lines (77 loc) · 3.14 KB
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
#!/bin/bash
# Here you need to specify these parameters
readonly AZURE_ACCT="rifujita"
readonly RES_LOC="japaneast"
readonly RES_GRP="${AZURE_ACCT}mcrg"
readonly VM_SIZE="Standard_D8s_v3"
readonly VM_NAME="${RES_GRP}vm"
readonly RCON_PASSWORD="testpass"
# Checking if Resource Group exists
echo "Checking Resource Group..."
res=$(az group show -g $RES_GRP -o tsv --query "properties.provisioningState" 2>&1)
if [ "$res" = "Succeeded" ]; then
echo "The Resource Group, ${RES_GRP} has already existed."
exit
fi
# Create Resource Group
echo "Creating Resource Group..."
res=$(az group create -l $RES_LOC -g $RES_GRP -o tsv --query "properties.provisioningState")
if [ "$res" != "Succeeded" ]; then
az group delete --yes --no-wait -g $RES_GRP
exit
fi
# Create VM
echo "Creating VM..."
echo "Please wait for about 5 minutes to complete..."
res=$(az vm create --image Canonical:UbuntuServer:18.04-LTS:latest --size ${VM_SIZE} -g ${RES_GRP} -n ${VM_NAME} \
--os-disk-size-gb 256 --storage-sku Premium_LRS --public-ip-address-dns-name ${VM_NAME} -o tsv --query "powerState")
if [ "$res" != "VM running" ]; then
az group delete --yes --no-wait -g $RES_GRP
exit
fi
echo "Installing docker..."
sleep 30
ssh-keygen -R "${VM_NAME}.${RES_LOC}.cloudapp.azure.com"
ssh -o "StrictHostKeyChecking no" ${AZURE_ACCT}@"${VM_NAME}.${RES_LOC}.cloudapp.azure.com" <<-'EOF'
# On Remote
sudo apt-get -y update
sudo apt-get -y upgrade
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common pwgen apache2-utils
curl -fsSL -o /tmp/gpg https://download.docker.com/linux/ubuntu/gpg
sudo apt-key add /tmp/gpg
rm -f /tmp/gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get -y install docker-ce
sudo usermod -aG docker $USER
sudo systemctl enable docker --now
EOF
echo "Starting container..."
ssh -t -o "StrictHostKeyChecking no" ${AZURE_ACCT}@"${VM_NAME}.${RES_LOC}.cloudapp.azure.com" "RCON_PASSWORD=$(printf %q "$RCON_PASSWORD") \
sudo docker run -itd --restart=always \
-e EULA=true -e ENABLE_RCON=true -e RCON_PASSWORD=$RCON_PASSWORD \
-p 25565:25565 -p 25575:25575 \
-v /var/opt/minecraft/data:/data rioriost/minecraft-server"
# Open Ports
echo "Opening ports..."
res=$(az vm open-port -g $RES_GRP -n $VM_NAME --port 25565 --priority 910 -o tsv --query provisioningState)
if [ "$res" != "Succeeded" ]; then
echo "Please open port 25565 manually."
fi
res=$(az vm open-port -g $RES_GRP -n $VM_NAME --port 25575 --priority 911 -o tsv --query provisioningState)
if [ "$res" != "Succeeded" ]; then
echo "Please open port 25575 manually."
fi
cat << EOF | tee minecraft_vm_settings.txt
Your Minecraft Server has been successfully created!
${VM_NAME}.${RES_LOC}.cloudapp.azure.com
Command to stop the service:
az vm deallocate -g ${RES_GRP} -n ${VM_NAME} --no-wait
Command to start the service again:
az vm start -g ${RES_GRP} -n ${VM_NAME}
Command to delete all the data:
az group delete --yes --no-wait -g ${RES_GRP}
Please consider to use Azure Automation to schedule VM start/stop.
https://docs.microsoft.com/ja-jp/azure/automation/automation-solution-vm-management
ENJOY!!
EOF