Beispiel #1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req, TraceWriter log)
        {
            var payload = await req.Content.ReadAsStringAsync();

            var nodes = JsonConvert.DeserializeObject <Request>(payload);

            if (nodes.nodes == null || nodes.nodes.Length == 0)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, "Nodes payload not found."));
            }

            List <string> deallocatedVMs = new List <string>();

            foreach (var node in nodes.nodes)
            {
                log.Info($"Reported {node.nodeId} has {node.rooms} rooms");
                //get the state of the corresponding VM
                var vm = await TableStorageHelper.Instance.GetVMByID(node.nodeId.Trim());

                if (node.rooms == 0)
                {
                    if (vm == null)
                    {
                        log.Error($"VM {vm.VMID} not found in DB");
                        continue;
                    }
                    if (vm.State == VMState.MarkedForDeallocation)
                    {
                        deallocatedVMs.Add(vm.VMID);

                        //VM has zero rooms and marked for deallocation
                        //it's fate is sealed, bye bye! :)
                        await AzureAPIHelper.DeallocateVMAsync(vm.VMID, vm.ResourceGroup);

                        //mark it with the deallocating state in table storage
                        //VMMonitor Function will be called when it is finally deallocated
                        vm.State = VMState.Deallocating;
                    }
                }
                vm.RoomsNumber = node.rooms;
                await TableStorageHelper.Instance.ModifyVMDetailsAsync(vm);
            }

            var resp = req.CreateResponse(HttpStatusCode.OK);

            resp.Content = new StringContent(JsonConvert.SerializeObject(new { VMsInDeallocatingState = deallocatedVMs }));
            return(resp);
        }
Beispiel #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "node/deallocate")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("MarkVMForDeallocation Function was called");
            // Get POST body
            dynamic data = JsonConvert.DeserializeObject(await req.Content.ReadAsStringAsync());

            // Set name to query string or body data
            string vmName = data?.vmName;

            //trim it just in case
            vmName = vmName.Trim();

            var vm = await TableStorageHelper.Instance.GetVMByID(vmName);

            if (vm == null)
            {
                return(req.CreateErrorResponse(HttpStatusCode.BadRequest, $"VM {vmName} not found"));
            }

            //set the state of the requested VM as MarkedForDeallocation
            vm.State = VMState.MarkedForDeallocation;

            //however, if there are no games running on this VM, deallocate it immediately
            if (vm.RoomsNumber == 0)
            {
                log.Info($"VM with ID {vmName} has zero game rooms so it will be deallocated immediately");
                await AzureAPIHelper.DeallocateVMAsync(vm.VMID, vm.ResourceGroup);

                vm.State = VMState.Deallocating;
            }

            //modify the VM state in the DB with the new value
            await TableStorageHelper.Instance.ModifyVMDetailsAsync(vm);

            return(vmName == null
                ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a vmName in the request body")
                : req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(vm)));
        }