Example #1
0
        private Task AwaitReady(ComputeEngine engine, VmInstanceReference instanceRef)
        {
            return(Task.Run(async() =>
            {
                for (int i = 0; i < 60; i++)
                {
                    try
                    {
                        var instance = await engine.Service.Instances.Get(
                            instanceRef.ProjectId, instanceRef.Zone, instanceRef.InstanceName)
                                       .ExecuteAsync();

                        if (await IsReadyAsync(engine, instanceRef, instance))
                        {
                            return;
                        }
                    }
                    catch (Exception)
                    { }

                    await Task.Delay(5 * 1000);
                }

                throw new TimeoutException($"Timeout waiting for {instanceRef} to become ready");
            }));
        }
Example #2
0
        protected virtual async Task <bool> IsReadyAsync(
            ComputeEngine engine,
            VmInstanceReference instanceRef,
            Instance instance)
        {
            var request = engine.Service.Instances.GetGuestAttributes(
                instanceRef.ProjectId,
                instanceRef.Zone,
                instanceRef.InstanceName);

            request.QueryPath = GuestAttributeNamespace + "/";
            var guestAttributes = await request.ExecuteAsync();

            return(guestAttributes
                   .QueryValue
                   .Items
                   .Where(i => i.Namespace__ == GuestAttributeNamespace && i.Key == GuestAttributeKey)
                   .Any());
        }
Example #3
0
        private async Task <VmInstanceReference> GetInstanceAsync(VmInstanceReference vmRef)
        {
            var computeEngine = ComputeEngine.Connect();

            try
            {
                var instance = await computeEngine.Service.Instances
                               .Get(vmRef.ProjectId, vmRef.Zone, vmRef.InstanceName)
                               .ExecuteAsync();

                if (instance.Status == "STOPPED")
                {
                    await computeEngine.Service.Instances.Start(
                        vmRef.ProjectId, vmRef.Zone, vmRef.InstanceName)
                    .ExecuteAsync();
                }

                await AwaitReady(computeEngine, vmRef);
            }
            catch (Exception)
            {
                await computeEngine.Service.Instances.Insert(
                    new Apis.Compute.v1.Data.Instance()
                {
                    Name        = vmRef.InstanceName,
                    MachineType = $"zones/{this.Zone}/machineTypes/{this.MachineType}",
                    Disks       = new []
                    {
                        new AttachedDisk()
                        {
                            AutoDelete       = true,
                            Boot             = true,
                            InitializeParams = new AttachedDiskInitializeParams()
                            {
                                SourceImage = this.ImageFamily
                            }
                        }
                    },
                    Metadata = new Metadata()
                    {
                        Items = this.Metadata.ToList()
                    },
                    NetworkInterfaces = new []
                    {
                        new NetworkInterface()
                        {
                            AccessConfigs = new []
                            {
                                new AccessConfig()
                            }
                        }
                    }
                },
                    vmRef.ProjectId,
                    vmRef.Zone).ExecuteAsync();

                await AwaitReady(computeEngine, vmRef);
            }

            return(vmRef);
        }