Running custom script during VM creation - VmStartupScript (preview)
Introduction
This feature is in preview. You’re welcome to start using it today and give us your feedback. Instructions on how to connect with us is provided at the end of the article. Note that technical support is limited during preview.
This is an advanced feature that should be used with extreme caution. The running script executes in the Virtual Machine (VM) level with admin (root) privileges. If not used properly, it can potentially disrupt the regular flow of the running game servers, or even prevent them from running at all. The end user is responsible for the content of the script.
How to use VmStartupScript
To use the VmStartupScript feature, you have to provide a custom script, and all the relevant software (optional) you plan to install. The script starts executing when the Virtual Machine is initialized. This operation occurs before the game servers start on every VM. After the script has successfully finished executing, the MPS service proceeds to complete initializing the game servers and delivers them to the StandingBy state. To learn more about the different game server’s state, see Lifecycle of a multiplayer server. To use this feature in an actual production environment, see Recommended developer workflow before starting.Create a script
- Create a file called PF_StartupScript.sh for Linux VMs or PF_StartupScript.ps1 for Windows VMs.
- Add set-up/execution commands in the file. If you need them, here are some commonly used environment variables that you can use in the script. Some actions aren’t supported or would cause the VMs to not start successfully, incurring unwanted charges. For details, see the What is not supported section.
Create and upload the zipped file
- Gather all the relevant software in a folder that your script is planning to use or call. If your script installs third party software, your script can download it during execution, or it can be bundled with it in the zipped file. Skip this step if you aren’t installing anything.
- Create a zipped file (.zip) with the script (.sh or .ps1) you created in the earlier section and the software you gathered in the earlier step, if needed. The script file should be at the root of the zipped file and not inside a directory. Additionally, if your script file isn’t named PF_StartupScript.sh (Linux) or PF_StartupScript.ps1 (Windows), it will not be executed and the game servers will fail to start.
- Upload the zipped file using one of the following methods:
- Use PlayFab Game Manager
- Issue a PUT request with the header {“x-ms-blob-type”: “BlockBlob”} on the URL returned from the GetAssetUploadUrl API call.
- Use the PowerShell cmdlets.
We recommend to include all binaries and assets needed by your script in the zipped file as this will result in faster execution and shorter time for MPS to deliver your game servers. Make sure to include the assets for the relevant platform your game servers will run on. For example, if you use Linux servers, you should include “amd64” Debian/Ubuntu packages.
Apply the custom script to new builds
After uploading the .zip file, use the MPS API to create a new Build after configuring the VmStartupScriptAssetReference property. For instructions, see How to create Builds using the MPS API.- Add the VmStartupScriptConfiguration.VmStartupScriptAssetReference property that includes a reference to the uploaded assets file. This property is part of all the “CreateBuild” related APIs, like CreateBuildWithCustomContainer, CreateBuildWithManagedContainer and CreateBuildWithProcessBasedServer.
- Add a valid value for the VmStartupScriptAssetReference.FileName property. This value must be the same as the name of your assets file, for example vmstartupscriptassets.zip.
- The VmStartupScriptAssetReference.MountPath property must be empty, since isn’t supported for the VmStartupScript feature.
If you set up a value for the MountPath property, the create build operation will fail.
Special considerations
On Linux, do I need to have the PF_StartupScript.sh file marked as executable?
Before MPS runs the script file, it marks it as executable and then converts any Windows line endings (“\r\n”) to Linux ones (“\n”). So, you don’t need to worry about these two things.Environment variables
Here are environment variables that you can use in your startup script.What is not supported
You shouldn’t do these actions from your script, as there is a high chance of disrupting the lifecycle of VM and game servers:- Don’t block during the execution of the startup script. The script must end successfully for the game servers to be created. If you need something to run in the background, you can install it either as a systemd service on Linux or a Windows service.
- Don’t use ports starting from 30000 since they’re used for the game servers or port 56001 as it’s used by VmAgent process (the MPS game server orchestrator executable).
- Don’t modify any of the files on the D: (Windows) or /mnt (Linux) paths as these files are necessary for VmAgent operation (apart from folders that contain editable content, like the
PF_SHARED_CONTENT_FOLDER_VM). - You shouldn’t use GSDK from within the VmStartupScript or an app that is launched by it. GSDK should only be used from GameServers.
- You shouldn’t manually reboot the Virtual Machine as this operation will create challenges in the communication with the MPS Control Plane.
Ports
When you use the VmStartupScript feature, it is possible to request a number of ports to be exposed on each VM. These ports can be used by any programs launched by your script and are different from the ports MPS opens for your game servers.Usage
You can request up to five ports for every VM. For each port, you must specify the protocol (TCP or UDP) and a name. Here is an example of how to request two ports:
For example, for the two ports requested in the above sample script, you should expect to find these environment variables in your VmStartupScript:
Similar to the ports we open for game servers, it is up to you to authenticate clients connecting to your ports. MPS doesn’t provide any authentication mechanism for these ports.
Customers will find that ports that are allocated start from number 20000 and up. However, we recommend that you don’t hardcode this value in your scripts as it might change in the future and always use the environment variables to obtain proper port information.
