Esempio n. 1
0
 public InstanceRequest(
     InstanceLocator instance,
     string machineType,
     string imageFamily,
     IEnumerable <Metadata.ItemsData> metadata)
 {
     this.computeService = TestProject.CreateComputeService();
     this.Locator        = instance;
     this.machineType    = machineType;
     this.imageFamily    = imageFamily;
     this.metadata       = metadata;
 }
        public static async Task <InstanceLocator> CreateOrStartInstanceAsync(
            string name,
            string machineType,
            string imageFamily,
            IEnumerable <Metadata.ItemsData> metadataItems)
        {
            var computeEngine = TestProject.CreateComputeService();

            var metadata = new Metadata()
            {
                Items = new List <Metadata.ItemsData>(metadataItems.ToList())
            };

            // Add metdata that marks this instance as temporary.
            metadata.Add("type", "auto-cleanup");
            metadata.Add("ttl", "120"); // minutes

            var locator = new InstanceLocator(
                TestProject.ProjectId,
                TestProject.Zone,
                name);

            try
            {
                TraceSources.Common.TraceVerbose(
                    "Trying to create new instance {0}...", name);

                await computeEngine.Instances.Insert(
                    new Apis.Compute.v1.Data.Instance()
                {
                    Name        = name,
                    MachineType = $"zones/{locator.Zone}/machineTypes/{machineType}",
                    Disks       = new[]
                    {
                        new AttachedDisk()
                        {
                            AutoDelete       = true,
                            Boot             = true,
                            InitializeParams = new AttachedDiskInitializeParams()
                            {
                                SourceImage = imageFamily
                            }
                        }
                    },
                    Metadata          = metadata,
                    NetworkInterfaces = new[]
                    {
                        new NetworkInterface()
                        {
                            AccessConfigs = new []
                            {
                                new AccessConfig()
                            }
                        }
                    },
                    Scheduling = new Scheduling()
                    {
                        Preemptible = true
                    }
                },
                    locator.ProjectId,
                    locator.Zone).ExecuteAsync();

                await AwaitInstanceCreatedAndReady(
                    computeEngine.Instances,
                    locator);

                return(locator);
            }
            catch (GoogleApiException e) when(e.Error != null && e.Error.Code == 409)
            {
                // Instance already exists - make sure it's running then.
                var instance = await computeEngine.Instances
                               .Get(
                    locator.ProjectId,
                    locator.Zone,
                    locator.Name)
                               .ExecuteAsync();

                if (instance.Status == "RUNNING" ||
                    instance.Status == "PROVISIONING" ||
                    instance.Status == "STAGING")
                {
                    TraceSources.Common.TraceVerbose(
                        "Instance {0} exists and is running...", locator.Name);

                    await AwaitInstanceCreatedAndReady(
                        computeEngine.Instances,
                        locator);

                    return(locator);
                }
                else if (instance.Status == "TERMINATED")
                {
                    TraceSources.Common.TraceVerbose(
                        "Instance {0} exists, but is TERMINATED, starting...", locator.Name);

                    // Reapply metadata.
                    await computeEngine.Instances.AddMetadataAsync(
                        locator,
                        metadata,
                        CancellationToken.None);

                    await computeEngine.Instances.Start(
                        locator.ProjectId,
                        locator.Zone,
                        locator.Name)
                    .ExecuteAsync();

                    await AwaitInstanceCreatedAndReady(
                        computeEngine.Instances,
                        locator);

                    return(locator);
                }
                else
                {
                    TraceSources.Common.TraceError(
                        "Creating instance {0} failed, current status is {1}",
                        locator.Name,
                        instance.Status);
                    TraceSources.Common.TraceError(e);
                    throw;
                }
            }
        }
Esempio n. 3
0
        private async Task CreateOrStartInstanceAsync()
        {
            var computeEngine = TestProject.CreateComputeService();

            try
            {
                var instance = await computeEngine.Instances
                               .Get(
                    this.Locator.ProjectId,
                    this.Locator.Zone,
                    this.Locator.Name)
                               .ExecuteAsync();

                if (instance.Status == "STOPPED")
                {
                    await computeEngine.Instances.Start(
                        this.Locator.ProjectId,
                        this.Locator.Zone,
                        this.Locator.Name)
                    .ExecuteAsync();
                }

                await AwaitInstanceCreatedAndReady();
            }
            catch (Exception)
            {
                var metadata = new List <Metadata.ItemsData>(this.metadata.ToList());

                // Add metdata that marks this instance as temporary.
                metadata.Add(new Metadata.ItemsData()
                {
                    Key   = "type",
                    Value = "auto-cleanup"
                });
                metadata.Add(new Metadata.ItemsData()
                {
                    Key   = "ttl",
                    Value = "120" // minutes
                });

                await computeEngine.Instances.Insert(
                    new Apis.Compute.v1.Data.Instance()
                {
                    Name        = this.Locator.Name,
                    MachineType = $"zones/{this.Locator.Zone}/machineTypes/{this.machineType}",
                    Disks       = new[]
                    {
                        new AttachedDisk()
                        {
                            AutoDelete       = true,
                            Boot             = true,
                            InitializeParams = new AttachedDiskInitializeParams()
                            {
                                SourceImage = this.imageFamily
                            }
                        }
                    },
                    Metadata = new Metadata()
                    {
                        Items = metadata
                    },
                    NetworkInterfaces = new[]
                    {
                        new NetworkInterface()
                        {
                            AccessConfigs = new []
                            {
                                new AccessConfig()
                            }
                        }
                    }
                },
                    this.Locator.ProjectId,
                    this.Locator.Zone).ExecuteAsync();

                await AwaitInstanceCreatedAndReady();
            }
        }