Exemple #1
0
        private static HttpResponseMessage ValidateRequest(HttpRequestMessage req, NodeParameters nodeParams, TraceWriter log)
        {
            if (string.IsNullOrEmpty(nodeParams.Size) || string.IsNullOrEmpty(nodeParams.Region))
            {
                log.Error("Missing size or region params");
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, new System.ArgumentException("Node parameters <region, size> are required")));
            }

            if (string.IsNullOrEmpty(nodeParams.Image) &&
                string.IsNullOrEmpty(ConfigurationManager.AppSettings["GAMESERVER_VM_IMAGE"]?.ToString()))
            {
                log.Error("Missing image param");
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, new System.ArgumentException("Image for vm should be specified when deploying")));
            }

            if (string.IsNullOrEmpty(nodeParams.PortRange) &&
                string.IsNullOrEmpty(ConfigurationManager.AppSettings["GAMESERVER_PORT_RANGE"]?.ToString()))
            {
                log.Error("Missing port range");
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, new System.ArgumentException("Port range for game server should be specified when deploying")));
            }

            return(null);
        }
Exemple #2
0
        private static async Task DeployNodeAsync(string vmName, NodeParameters nodeParams, string sshKey, string deploymentTemplate, TraceWriter log)
        {
            log.Info("Creating VM");
            var appSettings = ConfigurationManager.AppSettings;

            string vmImage = !string.IsNullOrEmpty(nodeParams.Image)
                                ? nodeParams.Image
                                : appSettings["GAMESERVER_VM_IMAGE"].ToString();

            string portRange = !string.IsNullOrEmpty(nodeParams.PortRange)
                                ? nodeParams.PortRange
                                : appSettings["GAMESERVER_PORT_RANGE"].ToString();

            var parameters = JsonConvert.SerializeObject(new Dictionary <string, Dictionary <string, object> > {
                { "vmName", new Dictionary <string, object> {
                      { "value", vmName }
                  } },
                { "location", new Dictionary <string, object> {
                      { "value", nodeParams.Region }
                  } },
                { "virtualMachineSize", new Dictionary <string, object> {
                      { "value", nodeParams.Size }
                  } },
                { "adminUserName", new Dictionary <string, object> {
                      { "value", appSettings["VM_ADMIN_NAME"]?.ToString() ?? "default_gs_admin" }
                  } },
                { "adminPublicKey", new Dictionary <string, object> {
                      { "value", sshKey }
                  } },
                { "gameServerPortRange", new Dictionary <string, object> {
                      { "value", portRange }
                  } },
                { "reportGameroomsUrl", new Dictionary <string, object> {
                      { "value", appSettings["REPORT_GAMEROOMS_URL"]?.ToString() }
                  } },
                { "vmImage", new Dictionary <string, object> {
                      { "value", vmImage }
                  } },
            });

            log.Info($"Creating VM: {vmName}");

            // not awaiting here intentionally, since we want to return response ASAP
            var deploymentTask = AzureMgmtCredentials.Instance.Azure.Deployments
                                 .Define($"NodeDeployment{System.Guid.NewGuid().ToString()}")
                                 .WithExistingResourceGroup(nodeParams.ResourceGroup)
                                 .WithTemplate(deploymentTemplate)
                                 .WithParameters(parameters)
                                 .WithMode(DeploymentMode.Incremental)
                                 .CreateAsync();

            // when don't want to await completion of ARM deployment since it can take up to 5 mins
            // so let's give it few seconds to start and return in case we have deployment exception
            // like service principal error
            await Task.WhenAny(deploymentTask, Task.Delay(1000));

            if (deploymentTask.IsCompleted && deploymentTask.IsFaulted)
            {
                throw deploymentTask.Exception;
            }
        }