private static async Task RunSample()
        {
            var azureSubscriptionConfiugration = ACI.ConfigurationFactory.CreateConfigWithActiveDirectoryAppAuth(
                azureSubscriptionId: "a187256a-2ebe-4a23-8bfc-8e194d8eagh7",
                resourceGroup: "MyResourceGroup",
                clientId: "n21d9e2f-1f1c-45cf-q7r7-79c363e5c740",
                clientSecret: "zaTD123lof6JUiiMTUb+bGGldmA8NpIvTEht1w7rylA=",
                tenantId: "32dd593d-c0bb-48e8-8cd1-8521ab9e3b5e",
                imageRegistryServer: "mycontainerregistry.azurecr.io",
                imageRegistryUsername: "******",
                imageRegistryPassword: "******"
                );

            var containerCreationConfiguration = new ACI.ContainerCreationConfiguration()
            {
                //For some reason, container name can't be in Pascal case. Kebab case works.
                ContainerName = "my-container",
                CpuCore       = 2,
                MemoryInGB    = 4,
                ImageName     = "mycontainerregistry.azurecr.io/myimage",
                Port          = 12345,
                Location      = "east us",
                OS            = ACI.ContainerCreationConfiguration.OsType.Windows
            };

            try
            {
                var resourceAccess = new ACI.AciResourceAccess(azureSubscriptionConfiugration);
                var container      = resourceAccess.CreateContainer(containerCreationConfiguration).GetAwaiter().GetResult();
                Console.WriteLine("Container created successfully on ip "
                                  + container.properties.ipAddress.ip);

                //Get container object
                Thread.Sleep(1000);
                container = await resourceAccess.GetContainer("my-container");

                Console.WriteLine("Container status now is: " + container.properties.containers.First()
                                  .properties.instanceView.currentState.state);     //'Waiting' because it's still pulling image

                //Alternatively, we can use 'GetContainerGroupStatus' to get status
                await Task.Delay(TimeSpan.FromMinutes(7));//Wait for image to finish pulling

                var containerStatus = await resourceAccess.GetContainerGroupStatus("my-container");

                Console.WriteLine("Container status now is: " + containerStatus);//ContainerStatus.RUNNING

                //Delete container
                await resourceAccess.DeleteContainer("my-container");

                containerStatus = await resourceAccess.GetContainerGroupStatus("my-container");

                Console.WriteLine("Container status now is: " + containerStatus);//ContainerStatus.DELETED
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error occured: {e}");
            }
        }
示例#2
0
        public async Task <AciContainer> CreateContainer(ContainerCreationConfiguration creationConfig)
        {
            var    subscriptionId        = _configuration.AzureSubscriptionId;
            string imageRegistryServer   = _configuration.ImageRegistryServer;
            string imageRegistryUsername = _configuration.ImageRegistryUsername;
            string imageRegistryPassword = _configuration.ImageRegistryPassword;
            string resourceGroup         = _configuration.ResourceGroup;

            var environmentVariables = creationConfig.EnvironmentVariables ?? new Dictionary <string, string>();
            var containerRequest     = new AciContainer()
            {
                id         = "/subscriptions/" + subscriptionId + "/resourceGroups/" + resourceGroup + "/providers/Microsoft.ContainerInstance/containerGroups/" + creationConfig.ContainerName,
                location   = "east us",
                name       = creationConfig.ContainerName,
                properties = new Properties()
                {
                    containers = new Container[1]
                    {
                        new Container()
                        {
                            name       = creationConfig.ContainerName,
                            properties = new PropertiesOfContainer()
                            {
                                command = new string[0],
                                environmentVariables = environmentVariables.Select(pair =>
                                                                                   new EnvironmentVariable()
                                {
                                    name = pair.Key, value = pair.Value
                                }).ToArray(),
                                image = creationConfig.ImageName,    //"ozcodecontainer.azurecr.io/ozcode",
                                ports = new Port[1]
                                {
                                    new Port()
                                    {
                                        port = creationConfig.Port,
                                    }
                                },
                                resources = new Resources()
                                {
                                    requests = new Requests()
                                    {
                                        cpu        = creationConfig.CpuCore,
                                        memoryInGB = creationConfig.MemoryInGB
                                    }
                                },
                            },
                        },
                    },
                    imageRegistryCredentials = new ImageRegistryCredential[1] {
                        new ImageRegistryCredential()
                        {
                            password = imageRegistryPassword,
                            username = imageRegistryUsername,
                            server   = imageRegistryServer,//"ozcodecontainer.azurecr.io"
                        }
                    },
                    ipAddress = new IpAddress()
                    {
                        ports = new Port2[1]
                        {
                            new Port2()
                            {
                                protocol = "TCP",
                                port     = creationConfig.Port
                            }
                        },
                        type         = "Public",
                        dnsNameLabel = null,
                    },
                    restartPolicy = creationConfig.RestartPolicy.ToString(),
                    osType        = creationConfig.OS.ToString()
                },
                type = "Microsoft.ContainerInstance/containerGroups"
            };

            string url = GetAzureManagementUrl(creationConfig.ContainerName);

            var container = await _restService.SendHttpPutRequest <AciContainer, AciContainer>(url, containerRequest, await GetAccessToken());

            return(container);
        }