Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
spara committed Nov 4, 2020
1 parent e97d91b commit ee0433e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
16 changes: 10 additions & 6 deletions azure-nextgen-py-minecraft-server/__main__.py
Expand Up @@ -3,15 +3,11 @@
import provisioners
import pulumi
import base64
from provisioners import ConnectionArgs
from pulumi import Config, Output, export
from pulumi_azure_nextgen.compute import latest as compute
from pulumi_azure_nextgen.network import latest as network
from pulumi_azure_nextgen.resources import latest as resources

# If keyName is provided, an existing KeyPair is used, else if publicKey is provided a new KeyPair
# derived from the publicKey is created.

# Get the config ready to go.
config = Config()
key_name = config.get('keyName')
Expand All @@ -29,10 +25,12 @@ def decode_key(key):
private_key = config.require_secret('privateKey').apply(decode_key)
private_key_passphrase = config.get_secret('privateKeyPassphrase')

# Create a resource group to hold project resources.
resource_group = resources.ResourceGroup("server-rg",
resource_group_name="minecraft",
location=location)

# Create a virtual network resource.
net = network.VirtualNetwork(
"server-network",
resource_group_name=resource_group.name,
Expand All @@ -47,6 +45,7 @@ def decode_key(key):
)]
)

# Create a public IP to enable access on the Internet.
public_ip = network.PublicIPAddress(
"server-ip",
resource_group_name=resource_group.name,
Expand All @@ -55,6 +54,7 @@ def decode_key(key):
public_ip_allocation_method="Dynamic"
)

# Create the network interface for the server.
network_iface = network.NetworkInterface(
"server-nic",
resource_group_name=resource_group.name,
Expand All @@ -68,8 +68,10 @@ def decode_key(key):
)]
)

# Create path to store ssh keys as a string.
ssh_path= "".join(["/home/",admin_username,"/.ssh/authorized_keys"])

# Create the virtual machine.
server = compute.VirtualMachine(
"server-vm",
resource_group_name= resource_group.name,
Expand Down Expand Up @@ -113,28 +115,30 @@ def decode_key(key):
),
)

# Get IP address as an output.
combined_output = Output.all(server.id, public_ip.name, resource_group.name)
public_ip_addr = combined_output.apply(
lambda lst: network.get_public_ip_address(
public_ip_address_name=lst[1],
resource_group_name=lst[2]))

# Create connection object to server.
conn = provisioners.ConnectionArgs(
host= public_ip_addr.ip_address,
username=admin_username,
private_key=private_key,
private_key_passphrase=private_key_passphrase,
)

# Copy a config file to our server.
# Copy install script to server.
cp_config = provisioners.CopyFile('config',
conn=conn,
src='install.sh',
dest='install.sh',
opts=pulumi.ResourceOptions(depends_on=[server]),
)

# # Execute a basic command on our server.
# Execute install script on server.
install = provisioners.RemoteExec('install',
conn=conn,
commands=['sudo chmod 755 install.sh && sudo ./install.sh'],
Expand Down
1 change: 1 addition & 0 deletions azure-nextgen-py-minecraft-server/install.sh
Expand Up @@ -43,6 +43,7 @@ printf 'ExecStart=/usr/bin/java -Xmx2048M -Xms512M -XX:+UseG1GC -jar server.jar
printf 'ExecStop=/opt/minecraft/tools/mcrcon/mcrcon -H 127.0.0.1 -P 25575 -p strongpassword stop\n' >> /etc/systemd/system/minecraft.service
printf '[Install]\nWantedBy=multi-user.target\n' >> /etc/systemd/system/minecraft.service

# start the service
systemctl daemon-reload
systemctl start minecraft
systemctl enable minecraft

0 comments on commit ee0433e

Please sign in to comment.