/** * Azure Container Instance sample for managing container instances with Azure File Share mount. * - Create an Azure container instance using Docker image "seanmckenna/aci-hellofiles" with a mount to a new file share * - Retrieve container log content * - Delete the container group resource */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string aciName = SdkContext.RandomResourceName("acisample", 20); string shareName = SdkContext.RandomResourceName("fileshare", 20); string containerImageName = "seanmckenna/aci-hellofiles"; string volumeMountName = "aci-helloshare"; try { //============================================================= // Create a container group with one container instance of default CPU core count and memory size // using public Docker image "seanmckenna/aci-hellofiles" which mounts the file share created previously // as read/write shared container volume. IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithNewAzureFileShareVolume(volumeMountName, shareName) .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(80) .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .Attach() .WithDnsPrefix(aciName) .Create(); Utilities.Print(containerGroup); SdkContext.DelayProvider.Delay(20000); Utilities.Log("Container instance IP address: " + containerGroup.IPAddress); //============================================================= // Check the container instance logs containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); string logContent = containerGroup.GetLogContent(aciName); Utilities.Log($"Logs for container instance: {aciName}\n{logContent}"); //============================================================= // Remove the container group azure.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
public static async Task <ContainerGroupStatus> Run( [ActivityTrigger] ContainerGroupInfo containerGroupInfo, ILogger log) { var(resourceGroupName, containerGroupName) = (containerGroupInfo.ResourceGroupName, containerGroupInfo.ContainerGroupName); log.LogInformation($"Checking status {resourceGroupName}/{containerGroupName}"); var azure = await AzHelpers.GetAzure(); IContainerGroup containerGroup = await azure.ContainerGroups.GetByResourceGroupAsync(resourceGroupName, containerGroupName); if (containerGroup == null) { throw new Exception($"Container Instance is NOT found for { containerGroupName } in { resourceGroupName }"); } var containerGroupStatus = new ContainerGroupStatus() { State = containerGroup.State, Id = containerGroup.Id, Name = containerGroup.Name, ResourceGroupName = containerGroup.ResourceGroupName, Containers = containerGroup.Containers.Values.Select(c => new ContainerInstanceStatus() { Name = c.Name, CurrentState = c.InstanceView?.CurrentState, RestartCount = c.InstanceView?.RestartCount, }).ToArray() }; return(containerGroupStatus); }
public void WaitForContainerGroupToEnterState(IAzure azure, string containerGroupName, string state) { var hasAllReachedState = false; while (!hasAllReachedState) { IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(this.configuration.ResourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(Constants.Delay); } var containersReachedState = 0; foreach (var container in containerGroup.Containers.Values) { this.logger.Log(LogLevel.Information, $"Waiting for container '{container.Name}' to reach state: '{state}'"); if (container.InstanceView.CurrentState.State == state) { containersReachedState++; this.logger.Log(LogLevel.Debug, $"Container '{container.Name}' has reached state: '{state}'"); } } if (containersReachedState == containerGroup.Containers.Count) { hasAllReachedState = true; } } }
public void DeleteContainerGroup(IAzure azure, string containerGroupName) { IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(this.configuration.ResourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(Constants.Delay); } this.logger.Log(LogLevel.Information, $"Deleting container group '{containerGroupName}'"); try { azure.ContainerGroups.DeleteById(containerGroup.Id); } catch (Exception ex) { this.logger.Log(LogLevel.Error, $"Error deleting container group '{containerGroupName}': {ex.Message}"); if (ExceptionHandler.IsFatal(ex)) { throw; } } }
private bool TryGetContainerInstance(IAzure azure, out string containerId, out IContainerGroup containerGroup) { var seleniumContainerGroups = azure.ContainerGroups.ListByResourceGroup("seleniumContainerRegistry"); containerGroup = seleniumContainerGroups.FirstOrDefault(); containerId = containerGroup?.Id; return(containerGroup != null); }
private static async Task CreateContainerGroupWithPolling(TaskLogger taskLogger, IAzure azure, string azureRegion, MyAppParameters myAppParameters, Dictionary <string, string> envVariables) { string message; // Create the container group using a fire-and-forget task Task.Run(() => azure.ContainerGroups.Define(myAppParameters.AgentName) .WithRegion(azureRegion) .WithExistingResourceGroup(myAppParameters.ResourceGroupName) .WithLinux() .WithPublicImageRegistryOnly() .WithoutVolume() .DefineContainerInstance(myAppParameters.AgentName) .WithImage("microsoft/vsts-agent") .WithoutPorts() .WithEnvironmentVariables(envVariables) .Attach() .CreateAsync() ); // Poll for the container group IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(myAppParameters.ResourceGroupName, myAppParameters.AgentName); await taskLogger.Log(".").ConfigureAwait(false); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); var i = 18000; // wait for 5 hrs // Poll until the container group is running while (containerGroup.State != "Running" && i > 0) { message = $"Container group state: {containerGroup.Refresh().State}"; await taskLogger.Log(message).ConfigureAwait(false); Thread.Sleep(1000); i--; } if (containerGroup.State != "Running") { var errorMessage = $"Container group: {myAppParameters.AgentName} not moved to Running state even after 5hrs. Please check container status in Azure portal."; throw new Exception(errorMessage); } message = $"Container group: {myAppParameters.AgentName} in resource group '{myAppParameters.ResourceGroupName}' created with image 'microsoft/vsts-agent'. Container group state: {containerGroup.Refresh().State}"; await taskLogger.Log(message).ConfigureAwait(false); }
/// <summary> /// Creates a container group with a single container asynchronously, and /// polls its status until its state is 'Running'. /// </summary> /// <param name="azure">An authenticated IAzure object.</param> /// <param name="resourceGroupName">The name of the resource group in which to create the container group.</param> /// <param name="containerGroupName">The name of the container group to create.</param> /// <param name="containerImage">The container image name and tag, for example 'microsoft\aci-helloworld:latest'.</param> private static void CreateContainerGroupWithPolling(IAzure azure, string resourceGroupName, string containerGroupName, string containerImage) { Console.WriteLine($"\nCreating container group '{containerGroupName}'..."); // Get the resource group's region IResourceGroup resGroup = azure.ResourceGroups.GetByName(resourceGroupName); Region azureRegion = resGroup.Region; // Create the container group using a fire-and-forget task Task.Run(() => azure.ContainerGroups.Define(containerGroupName) .WithRegion(azureRegion) .WithExistingResourceGroup(resourceGroupName) .WithLinux() .WithPublicImageRegistryOnly() .WithoutVolume() .DefineContainerInstance(containerGroupName + "-1") .WithImage(containerImage) .WithExternalTcpPort(80) .WithCpuCoreCount(1.0) .WithMemorySizeInGB(1) .Attach() .WithDnsPrefix(containerGroupName) .CreateAsync() ); // Poll for the container group IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(resourceGroupName, containerGroupName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); // Poll until the container group is running while (containerGroup.State != "Running") { Console.WriteLine($"Container group state: {containerGroup.Refresh().State}"); Thread.Sleep(1000); } Console.WriteLine($"\nOnce DNS has propagated, container group '{containerGroup.Name}' will be reachable at http://{containerGroup.Fqdn}"); }
private async Task RunStateVerify(IContainerGroup containerGroup, int delay, ILogger log) { await Task.Run(() => { while (containerGroup.State != "Running" || string.IsNullOrEmpty(containerGroup.IPAddress)) { var currentState = containerGroup.Refresh().State; log.LogWarning($"Polling for state of container group {containerGroup.Id}:{currentState}:{containerGroup.IPAddress}"); SdkContext.DelayProvider.Delay(delay); } log.LogError($"Got state result for container group {containerGroup.Id} with state:{containerGroup.State} and Ip: {containerGroup.IPAddress}"); }); }
private static void DeleteContainerGroup(IAzure azure, string resourceGroupName, string containerGroupName) { IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(resourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(1000); } Logger.Log($"Deleting container group '{containerGroupName}'..."); azure.ContainerGroups.DeleteById(containerGroup.Id); }
public void GetContainerLogs(IAzure azure, string containerGroupName) { IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(this.configuration.ResourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(Constants.Delay); } foreach (var container in containerGroup.Containers.Values) { // Print the container's logs this.logger.Log(LogLevel.Debug, $"Logs for container '{container.Name}':"); this.logger.Log(LogLevel.Debug, containerGroup.GetLogContent(container.Name)); this.logger.Log(LogLevel.Debug, $"End logs for container '{container.Name}'"); } }
private static void RestartContainers(IAzure azure, string resourceGroupName, string containerGroupName) { IContainerGroup containerGroup = null; while (containerGroup == null) { Console.Write("."); containerGroup = azure.ContainerGroups.GetByResourceGroup(resourceGroupName, containerGroupName); if (containerGroup != null) { _logger.Info("Restarting: " + resourceGroupName + "/" + containerGroupName); Thread.Sleep(10000); ContainerGroupsOperationsExtensions.StartAsync( containerGroup.Manager.Inner.ContainerGroups, containerGroup.ResourceGroupName, containerGroup.Name).GetAwaiter().GetResult(); SdkContext.DelayProvider.Delay(8000); } } }
/// <summary> /// Gets the specified container group and then prints a few of its properties and their values. /// </summary> /// <param name="azure">An authenticated IAzure object.</param> /// <param name="resourceGroupName">The name of the resource group containing the container group.</param> /// <param name="containerGroupName">The name of the container group whose details should be printed.</param> private static void PrintContainerGroupDetails(IAzure azure, string resourceGroupName, string containerGroupName) { Console.Write($"\nGetting container group details for container group '{containerGroupName}'..."); IContainerGroup containerGroup = null; while (containerGroup == null) { Console.Write("."); containerGroup = azure.ContainerGroups.GetByResourceGroup(resourceGroupName, containerGroupName); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); Console.WriteLine(containerGroup.Name); Console.WriteLine("--------------------------------"); Console.WriteLine($"State: {containerGroup.State}"); Console.WriteLine($"FQDN: {containerGroup.Fqdn}"); Console.WriteLine($"IP: {containerGroup.IPAddress}"); Console.WriteLine($"Region: {containerGroup.RegionName}"); }
//private Action<string> _createContainerInstance = DefaultCreateContainerImage; public void CreateContainerImage(object taskId) { // Configure some environment variables in the container which the // wordcount.py or other script can read to modify its behavior. Dictionary <string, string> envVars = new Dictionary <string, string> { { "TASKID", taskId.ToString() }, { "DELETEDURATION", "1" } }; IWithFirstContainerInstance withFirstContainerInstance = ContainerInstanceSingleton.Instance.ContainerGroup; IContainerGroup containerGroup = withFirstContainerInstance .DefineContainerInstance(taskId.ToString()) .WithImage(ContainerImageName) .WithExternalTcpPort(80) .WithCpuCoreCount(1.0) .WithMemorySizeInGB(2) .WithEnvironmentVariables(envVars) .Attach() .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .WithDnsPrefix(ContainerGroupName) .Create(); }
public static async Task Run( [ActivityTrigger] ContainerGroupInfo containerGroupInfo, ILogger log) { var(resourceGroupName, containerGroupName) = (containerGroupInfo.ResourceGroupName, containerGroupInfo.ContainerGroupName); var azure = await AzHelpers.GetAzure(); IContainerGroup containerGroup = await azure.ContainerGroups.GetByResourceGroupAsync(resourceGroupName, containerGroupName); if (containerGroup == null) { throw new Exception($"Container Instance is NOT found for {resourceGroupName}/{containerGroupName}"); } await containerGroup.StopAsync(); await Task.Delay(5000); log.LogInformation($"starting container: {resourceGroupName}/{containerGroupName} ...."); await azure.ContainerGroups.StartAsync(resourceGroupName, containerGroupName); }
/** * Azure Container Instance sample for managing container instances. * - Create an Azure container group with two container instances using Docker images "microsoft/aci-helloworld" and "microsoft/aci-tutorial-sidecar" * - Retrieve container log content * - Delete the container group resource */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string aciName = SdkContext.RandomResourceName("acisample", 20); string containerImageName1 = "microsoft/aci-helloworld"; string containerImageName2 = "microsoft/aci-tutorial-sidecar"; try { //============================================================= // Create a container group with two container instances IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithoutVolume() .DefineContainerInstance(aciName + "-1") .WithImage(containerImageName1) .WithExternalTcpPort(80) .WithCpuCoreCount(.5) .WithMemorySizeInGB(.75) .Attach() .DefineContainerInstance(aciName + "-2") .WithImage(containerImageName2) .WithoutPorts() .WithCpuCoreCount(.5) .WithMemorySizeInGB(.75) .Attach() .Create(); Utilities.Print(containerGroup); SdkContext.DelayProvider.Delay(20000); Utilities.Log("Container instance IP address: " + containerGroup.IPAddress); //============================================================= // Check the container instance logs containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); string logContent = containerGroup.GetLogContent(aciName + "-1"); Utilities.Log($"Logs for container instance: {aciName}-1\n{logContent}"); logContent = containerGroup.GetLogContent(aciName + "-2"); Utilities.Log($"Logs for container instance: {aciName}-2\n{logContent}"); //============================================================= // Remove the container group azure.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
/** * Azure Container Instance sample for managing container groups with private image repositories. * - Create an Azure Container Registry to be used for holding the Docker images * - If a local Docker engine cannot be found, create a Linux virtual machine that will host a Docker engine * to be used for this sample * - Use Docker DotNet to create a Docker client that will push an image to Azure Container Registry * - Create a new container group with one container instance from the image that was pushed in the registry */ public static void RunSample(IAzure azure) { string rgName = SdkContext.RandomResourceName("rgACI", 15); string acrName = SdkContext.RandomResourceName("acr", 20); string aciName = SdkContext.RandomResourceName("acisample", 20); string saName = SdkContext.RandomResourceName("sa", 20); string dockerImageName = "microsoft/aci-helloworld"; string dockerImageTag = "latest"; string dockerContainerName = "sample-hello"; try { //============================================================= // Create an Azure Container Registry to store and manage private Docker container images Utilities.Log("Creating an Azure Container Registry"); IRegistry azureRegistry = azure.ContainerRegistries.Define(acrName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithBasicSku() .WithRegistryNameAsAdminUser() .Create(); Utilities.Print(azureRegistry); var acrCredentials = azureRegistry.GetCredentials(); //============================================================= // Create a Docker client that will be used to push/pull images to/from the Azure Container Registry using (DockerClient dockerClient = DockerUtils.CreateDockerClient(azure, rgName, region)) { var pullImgResult = dockerClient.Images.PullImage( new Docker.DotNet.Models.ImagesPullParameters() { Parent = dockerImageName, Tag = dockerImageTag }, new Docker.DotNet.Models.AuthConfig()); Utilities.Log("List Docker images for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); var listImages = dockerClient.Images.ListImages( new Docker.DotNet.Models.ImagesListParameters() { All = true }); foreach (var img in listImages) { Utilities.Log("\tFound image " + img.RepoTags[0] + " (id:" + img.ID + ")"); } var createContainerResult = dockerClient.Containers.CreateContainer( new Docker.DotNet.Models.CreateContainerParameters() { Name = dockerContainerName, Image = dockerImageName + ":" + dockerImageTag }); Utilities.Log("List Docker containers for: " + dockerClient.Configuration.EndpointBaseUri.AbsoluteUri); var listContainers = dockerClient.Containers.ListContainers( new Docker.DotNet.Models.ContainersListParameters() { All = true }); foreach (var container in listContainers) { Utilities.Log("\tFound container " + container.Names[0] + " (id:" + container.ID + ")"); } //============================================================= // Commit the new container string privateRepoUrl = azureRegistry.LoginServerUrl + "/" + dockerContainerName; Utilities.Log("Commiting image at: " + privateRepoUrl); var commitContainerResult = dockerClient.Miscellaneous.CommitContainerChanges( new Docker.DotNet.Models.CommitContainerChangesParameters() { ContainerID = dockerContainerName, RepositoryName = privateRepoUrl, Tag = dockerImageTag }); //============================================================= // Push the new Docker image to the Azure Container Registry var pushImageResult = dockerClient.Images.PushImage(privateRepoUrl, new Docker.DotNet.Models.ImagePushParameters() { ImageID = privateRepoUrl, Tag = dockerImageTag }, new Docker.DotNet.Models.AuthConfig() { Username = acrCredentials.Username, Password = acrCredentials.AccessKeys[AccessKeyType.Primary], ServerAddress = azureRegistry.LoginServerUrl }); //============================================================= // Create a container group with one container instance of default CPU core count and memory size // using public Docker image "microsoft/aci-helloworld" and mounts a new file share as read/write // shared container volume. IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPrivateImageRegistry(azureRegistry.LoginServerUrl, acrCredentials.Username, acrCredentials.AccessKeys[AccessKeyType.Primary]) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(privateRepoUrl) .WithExternalTcpPort(80) .Attach() .Create(); Utilities.Print(containerGroup); //============================================================= // Check the container instance logs SdkContext.DelayProvider.Delay(15000); string logContent = containerGroup.GetLogContent(aciName); Utilities.Log($"Logs for container instance: {aciName}\n{logContent}"); } } finally { try { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } catch (Exception) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
public void ContainerInstanceWithPublicIpAddressWithUserAssignedMSI() { using (var context = FluentMockContext.Start(GetType().FullName)) { var rgName = TestUtilities.GenerateName("rgaci"); var cgName = TestUtilities.GenerateName("aci"); var msiManager = TestHelper.CreateMsiManager(); var containerInstanceManager = TestHelper.CreateContainerInstanceManager(); var resourceManager = TestHelper.CreateResourceManager(); string identityName1 = TestUtilities.GenerateName("msi-id"); string identityName2 = TestUtilities.GenerateName("msi-id"); IList <string> dnsServers = new List <string>(); dnsServers.Add("dnsServer1"); IContainerGroup containerGroup = null; IIdentity createdIdentity = msiManager.Identities .Define(identityName1) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .WithAccessToCurrentResourceGroup(BuiltInRole.Reader) .Create(); Microsoft.Azure.Management.Msi.Fluent.Identity.Definition.IWithCreate creatableIdentity = msiManager.Identities .Define(identityName2) .WithRegion(Region.USWest) .WithExistingResourceGroup(rgName) .WithAccessToCurrentResourceGroup(BuiltInRole.Contributor); try { containerGroup = containerInstanceManager.ContainerGroups.Define(cgName) .WithRegion(Region.USEast) .WithExistingResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithEmptyDirectoryVolume("emptydir1") .DefineContainerInstance("tomcat") .WithImage("tomcat") .WithExternalTcpPort(8080) .WithCpuCoreCount(1) .WithEnvironmentVariable("ENV1", "value1") .Attach() .DefineContainerInstance("nginx") .WithImage("nginx") .WithExternalTcpPort(80) .WithEnvironmentVariableWithSecuredValue("ENV2", "securedValue1") .Attach() .WithExistingUserAssignedManagedServiceIdentity(createdIdentity) .WithNewUserAssignedManagedServiceIdentity(creatableIdentity) .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .WithDnsPrefix(cgName) .WithTag("tag1", "value1") .Create(); Assert.Equal(cgName, containerGroup.Name); Assert.Equal("Linux", containerGroup.OSType.Value); Assert.Equal(0, containerGroup.ImageRegistryServers.Count); Assert.Equal(1, containerGroup.Volumes.Count); Assert.NotNull(containerGroup.Volumes["emptydir1"]); Assert.NotNull(containerGroup.IPAddress); Assert.True(containerGroup.IsIPAddressPublic); Assert.Equal(2, containerGroup.ExternalTcpPorts.Length); Assert.Equal(2, containerGroup.ExternalPorts.Count); Assert.Empty(containerGroup.ExternalUdpPorts); Assert.Equal(8080, containerGroup.ExternalTcpPorts[0]); Assert.Equal(80, containerGroup.ExternalTcpPorts[1]); Assert.Equal(2, containerGroup.Containers.Count); Container tomcatContainer = containerGroup.Containers["tomcat"]; Assert.NotNull(tomcatContainer); Container nginxContainer = containerGroup.Containers["nginx"]; Assert.NotNull(nginxContainer); Assert.Equal("tomcat", tomcatContainer.Name); Assert.Equal("tomcat", tomcatContainer.Image); Assert.Equal(1.0, tomcatContainer.Resources.Requests.Cpu); Assert.Equal(1.5, tomcatContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, tomcatContainer.Ports.Count); Assert.Equal(8080, tomcatContainer.Ports[0].Port); Assert.Null(tomcatContainer.VolumeMounts); Assert.Null(tomcatContainer.Command); Assert.NotNull(tomcatContainer.EnvironmentVariables); Assert.Equal(1, tomcatContainer.EnvironmentVariables.Count); Assert.Equal("nginx", nginxContainer.Name); Assert.Equal("nginx", nginxContainer.Image); Assert.Equal(1.0, nginxContainer.Resources.Requests.Cpu); Assert.Equal(1.5, nginxContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, nginxContainer.Ports.Count); Assert.Equal(80, nginxContainer.Ports[0].Port); Assert.Null(nginxContainer.VolumeMounts); Assert.Null(nginxContainer.Command); Assert.NotNull(nginxContainer.EnvironmentVariables); Assert.Equal(cgName, containerGroup.DnsPrefix); Assert.True(containerGroup.Tags.ContainsKey("tag1")); Assert.Equal(ContainerGroupRestartPolicy.Never, containerGroup.RestartPolicy); Assert.True(containerGroup.IsManagedServiceIdentityEnabled); Assert.Null(containerGroup.SystemAssignedManagedServiceIdentityPrincipalId); Assert.Equal(ResourceIdentityType.UserAssigned, containerGroup.ManagedServiceIdentityType); // Ensure the "User Assigned (External) MSI" id can be retrieved from the virtual machine IReadOnlyCollection <string> emsiIds = containerGroup.UserAssignedManagedServiceIdentityIds; Assert.NotNull(emsiIds); Assert.Equal(2, emsiIds.Count); IContainerGroup containerGroup2 = containerInstanceManager.ContainerGroups.GetByResourceGroup(rgName, cgName); var containerGroupList = containerInstanceManager.ContainerGroups.ListByResourceGroup(rgName); Assert.True(containerGroupList.Count() > 0); Assert.NotNull(containerGroupList.First().State); containerGroup.Refresh(); var containerOperationsList = containerInstanceManager.ContainerGroups.ListOperations(); Assert.True(containerOperationsList.Count() > 0); containerGroup.Update() .WithoutTag("tag1") .WithTag("tag2", "value2") .Apply(); Assert.False(containerGroup.Tags.ContainsKey("tag1")); Assert.True(containerGroup.Tags.ContainsKey("tag2")); containerInstanceManager.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { resourceManager.ResourceGroups.BeginDeleteByName(rgName); } catch { } } } }
private static void Print(IContainerGroup containerGroup, ILogger log) { var info = new StringBuilder().Append("Container Group: ").Append(containerGroup.Id) .Append("Name: ").Append(containerGroup.Name) .Append("\n\tResource group: ").Append(containerGroup.ResourceGroupName) .Append("\n\tRegion: ").Append(containerGroup.RegionName) .Append("\n\tTags: ").Append(containerGroup.Tags) .Append("\n\tOS type: ").Append(containerGroup.OSType.Value); if (containerGroup.IPAddress != null) { info.Append("\n\tPublic IP address: ").Append(containerGroup.IPAddress); info.Append("\n\tExternal TCP ports:"); foreach (int port in containerGroup.ExternalTcpPorts) { info.Append(" ").Append(port); } info.Append("\n\tExternal UDP ports:"); foreach (int port in containerGroup.ExternalUdpPorts) { info.Append(" ").Append(port); } } if (containerGroup.ImageRegistryServers.Count > 0) { info.Append("\n\tPrivate Docker image registries:"); foreach (string server in containerGroup.ImageRegistryServers) { info.Append(" ").Append(server); } } if (containerGroup.Volumes.Count > 0) { info.Append("\n\tVolume mapping: "); foreach (var entry in containerGroup.Volumes) { info.Append("\n\t\tName: ").Append(entry.Key).Append(" -> ").Append(entry.Value.AzureFile.ShareName); } } if (containerGroup.Containers.Count > 0) { info.Append("\n\tContainer instances: "); foreach (var entry in containerGroup.Containers) { var container = entry.Value; info.Append("\n\t\tName: ").Append(entry.Key).Append(" -> ").Append(container.Image); info.Append("\n\t\t\tResources: "); info.Append(container.Resources.Requests.Cpu).Append(" CPUs "); info.Append(container.Resources.Requests.MemoryInGB).Append(" GB"); info.Append("\n\t\t\tPorts:"); foreach (var port in container.Ports) { info.Append(" ").Append(port.Port); } if (container.VolumeMounts != null) { info.Append("\n\t\t\tVolume mounts:"); foreach (var volumeMount in container.VolumeMounts) { info.Append(" ").Append(volumeMount.Name).Append("->").Append(volumeMount.MountPath); } } if (container.Command != null) { info.Append("\n\t\t\tStart commands:"); foreach (var command in container.Command) { info.Append("\n\t\t\t\t").Append(command); } } if (container.EnvironmentVariables != null) { info.Append("\n\t\t\tENV vars:"); foreach (var envVar in container.EnvironmentVariables) { info.Append("\n\t\t\t\t").Append(envVar.Name).Append("=").Append(envVar.Value); } } } } log.LogInformation(info.ToString()); }
public static Task Start(IContainerGroup containerGroup, ILogger log) { log.LogInformation("Starting " + containerGroup.Name); return(ContainerGroupsOperationsExtensions.StartAsync(containerGroup.Manager.Inner.ContainerGroups, containerGroup.ResourceGroupName, containerGroup.Name)); }
public static void Main(string[] args) { #if NETCOREAPP3_0 Console.Write("CoFlows CE - NetCoreApp 3.0... "); #endif #if NET461 Console.Write("CoFlows CE - Net Framework 461... "); #endif Console.Write("Python starting... "); PythonEngine.Initialize(); Code.InitializeCodeTypes(new Type[] { typeof(QuantApp.Engine.WorkSpace), typeof(Jint.Native.Array.ArrayConstructor) }); var config_env = Environment.GetEnvironmentVariable("coflows_config"); var config_file = Environment.GetEnvironmentVariable("config_file"); if (string.IsNullOrEmpty(config_file)) { config_file = "quantapp_config.json"; } JObject config = string.IsNullOrEmpty(config_env) ? (JObject)JToken.ReadFrom(new JsonTextReader(File.OpenText(@"mnt/" + config_file))) : (JObject)JToken.Parse(config_env); workspace_name = config["Workspace"].ToString(); hostName = config["Server"]["Host"].ToString(); var secretKey = config["Server"]["SecretKey"].ToString(); ssl_cert = config["Server"]["SSL"]["Cert"].ToString(); ssl_password = config["Server"]["SSL"]["Password"].ToString(); var sslFlag = !string.IsNullOrWhiteSpace(ssl_cert); useJupyter = config["Jupyter"].ToString().ToLower() == "true"; var connectionString = config["Database"].ToString(); var cloudHost = config["Cloud"]["Host"].ToString(); var cloudKey = config["Cloud"]["SecretKey"].ToString(); var cloudSSL = config["Cloud"]["SSL"].ToString(); if (args != null && args.Length > 0 && args[0] == "lab") { Connection.Client.Init(hostName, sslFlag); if (!Connection.Client.Login(secretKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); var pargs = new string[] { "-m", "ipykernel_launcher.py", "-f", args[1] }; Console.Write("Starting lab... "); Python.Runtime.Runtime.Py_Main(pargs.Length, pargs); Console.WriteLine("started lab... "); } //Cloud else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "deploy") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("Starting cloud deployment... "); Code.UpdatePackageFile(workspace_name); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var res = Connection.Client.PublishPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "build") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud build... "); Code.UpdatePackageFile(workspace_name); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var res = Connection.Client.BuildPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + res); } else if (args != null && args.Length > 2 && args[0] == "cloud" && args[1] == "query") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.WriteLine("CoFlows Cloud query... "); var queryID = args[2]; var funcName = args.Length > 3 ? args[3] : null; var parameters = args.Length > 4 ? args.Skip(4).ToArray() : null; var pkg = Code.ProcessPackageFile(workspace_name); Console.WriteLine("Workspace: " + pkg.Name); Console.WriteLine("Query ID: " + queryID); Console.WriteLine("Function Name: " + funcName); if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { Console.WriteLine("Parameter[" + i + "]: " + parameters[i]); } } var(code_name, code) = pkg.Queries.Where(entry => entry.ID == queryID).Select(entry => (entry.Name as string, entry.Content as string)).FirstOrDefault(); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var result = Connection.Client.Execute(code, code_name, pkg.ID, queryID, funcName, parameters); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.WriteLine("Result: "); Console.WriteLine(result); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "log") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteLog(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "remove") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteRemove(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 1 && args[0] == "cloud" && args[1] == "restart") { Console.WriteLine("Cloud Host: " + cloudHost); Console.WriteLine("Cloud SSL: " + cloudSSL); Connection.Client.Init(cloudHost, cloudSSL.ToLower() == "true"); if (!Connection.Client.Login(cloudKey)) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("server connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("CoFlows Cloud log... "); var res = Connection.Client.RemoteRestart(workspace_name); Console.WriteLine("Result: "); Console.WriteLine(res); } else if (args != null && args.Length > 0 && args[0] == "server") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("QuantApp Server " + DateTime.Now); Console.WriteLine("DB Connected"); Console.WriteLine("Local deployment"); if (string.IsNullOrEmpty(config_env)) { var pkg = Code.ProcessPackageFile(workspace_name); Code.ProcessPackageJSON(pkg); SetDefaultWorkSpaces(new string[] { pkg.ID }); Console.WriteLine(pkg.Name + " started"); } else { Console.WriteLine("Empty server..."); } #if NETCOREAPP3_0 if (!sslFlag) { Init(new string[] { "--urls", "http://*:80" }); } else { Init(args); } #endif #if NET461 Init(new string[] { "--urls", "http://*:80" }); #endif Task.Factory.StartNew(() => { while (true) { // Console.WriteLine(DateTime.Now.ToString()); System.Threading.Thread.Sleep(1000); } }); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit); _closing.WaitOne(); } //Local else if (args != null && args.Length > 1 && args[0] == "local" && args[1] == "build") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("DB Connected"); Console.WriteLine("Local build"); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (string.IsNullOrEmpty(res)) { Console.WriteLine("Success!!!"); } else { Console.WriteLine(res); } } else if (args != null && args.Length > 2 && args[0] == "local" && args[1] == "query") { PythonEngine.BeginAllowThreads(); Databases(connectionString); Console.WriteLine("Local Query " + DateTime.Now); Console.WriteLine("DB Connected"); Console.WriteLine("CoFlows Local query... "); var queryID = args[2]; var funcName = args.Length > 3 ? args[3] : null; var parameters = args.Length > 4 ? args.Skip(4).ToArray() : null; Console.WriteLine("QueryID: " + queryID); Console.WriteLine("FuncName: " + funcName); Console.WriteLine("Parameters: " + parameters); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); Code.ProcessPackageJSON(pkg); if (parameters != null) { for (int i = 0; i < parameters.Length; i++) { Console.WriteLine("Parameter[" + i + "]: " + parameters[i]); } } var(code_name, code) = pkg.Queries.Where(entry => entry.ID == queryID).Select(entry => (entry.Name as string, entry.Content as string)).FirstOrDefault(); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); // var wb = wb_res.FirstOrDefault() as CodeData; var codes = new List <Tuple <string, string> >(); codes.Add(new Tuple <string, string>(code_name, code)); var result = QuantApp.Engine.Utils.ExecuteCodeFunction(false, codes, funcName, parameters); //var result = Connection.Client.Execute(code, code_name, pkg.ID, queryID, funcName, parameters); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.WriteLine("Result: "); Console.WriteLine(result); } //Azure Container Instance else if (args != null && args.Length > 1 && args[0] == "azure" && args[1] == "deploy") { PythonEngine.BeginAllowThreads(); Console.WriteLine(); Console.WriteLine("Azure Container Instance start..."); var t0 = DateTime.Now; Console.WriteLine("Started: " + t0); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (string.IsNullOrEmpty(res)) { Console.WriteLine("Build Success!!!"); } else { Console.WriteLine(res); } AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(config["AzureContainerInstance"]["AuthFile"].ToString()); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); string rgName = pkg.ID.ToLower() + "-rg"; string aciName = pkg.Name.ToLower(); string containerImageName = "coflows/ce"; Console.WriteLine("Container Name: " + aciName); Console.WriteLine("Resource Group Name: " + rgName); try { Console.WriteLine("Cleaning Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); IResourceGroup resGroup = azure.ResourceGroups.GetByName(rgName); while (resGroup != null) { resGroup = azure.ResourceGroups.GetByName(rgName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); Console.WriteLine("Cleaned Resource Group: " + rgName); } catch (Exception e) { Console.WriteLine(); Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } Region region = Region.Create(config["AzureContainerInstance"]["Region"].ToString()); Console.WriteLine("Region: " + region); if (config["AzureContainerInstance"]["Gpu"] != null && config["AzureContainerInstance"]["Gpu"]["Cores"].ToString() != "" && config["AzureContainerInstance"]["Gpu"]["Cores"].ToString() != "0" && config["AzureContainerInstance"]["Gpu"]["SKU"].ToString() != "") { Console.WriteLine("Creating a GPU container..."); Task.Run(() => azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() // .WithNewAzureFileShareVolume(volumeMountName, shareName) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(sslFlag ? 443 : 80) // .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .WithCpuCoreCount(Int32.Parse(config["AzureContainerInstance"]["Cores"].ToString())) .WithMemorySizeInGB(Int32.Parse(config["AzureContainerInstance"]["Mem"].ToString())) .WithGpuResource( Int32.Parse(config["AzureContainerInstance"]["Gpu"]["Cores"].ToString()), config["AzureContainerInstance"]["Gpu"]["SKU"].ToString().ToLower() == "k80" ? GpuSku.K80 : config["AzureContainerInstance"]["Gpu"]["SKU"].ToString().ToLower() == "p100" ? GpuSku.P100 : GpuSku.V100 ) .WithEnvironmentVariables(new Dictionary <string, string>() { { "coflows_config", File.ReadAllText(@"mnt/" + config_file) }, }) .WithStartingCommandLine("dotnet", "QuantApp.Server.quant.lnx.dll", "server") .Attach() .WithDnsPrefix(config["AzureContainerInstance"]["Dns"].ToString()) .CreateAsync() ); } else { Console.WriteLine("Creating a standard container..."); Task.Run(() => azure.ContainerGroups.Define(aciName) .WithRegion(region) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() // .WithNewAzureFileShareVolume(volumeMountName, shareName) .WithoutVolume() .DefineContainerInstance(aciName) .WithImage(containerImageName) .WithExternalTcpPort(sslFlag ? 443 : 80) // .WithVolumeMountSetting(volumeMountName, "/aci/logs/") .WithCpuCoreCount(Int32.Parse(config["AzureContainerInstance"]["Cores"].ToString())) .WithMemorySizeInGB(Int32.Parse(config["AzureContainerInstance"]["Mem"].ToString())) .WithEnvironmentVariables(new Dictionary <string, string>() { { "coflows_config", File.ReadAllText(@"mnt/" + config_file) }, }) .WithStartingCommandLine("dotnet", "QuantApp.Server.quant.lnx.dll", "server") .Attach() .WithDnsPrefix(config["AzureContainerInstance"]["Dns"].ToString()) .CreateAsync() ); } // Poll for the container group IContainerGroup containerGroup = null; while (containerGroup == null) { containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } var lastContainerGroupState = containerGroup.Refresh().State; Console.WriteLine(); Console.WriteLine($"Container group state: {containerGroup.Refresh().State}"); // Poll until the container group is running while (containerGroup.State != "Running") { var containerGroupState = containerGroup.Refresh().State; if (containerGroupState != lastContainerGroupState) { Console.WriteLine(); Console.WriteLine(containerGroupState); lastContainerGroupState = containerGroupState; } Console.Write("."); System.Threading.Thread.Sleep(1000); } Console.WriteLine(); Console.WriteLine("Container instance IP address: " + containerGroup.IPAddress); Console.WriteLine("Container instance Ports: " + string.Join(",", containerGroup.ExternalTcpPorts)); string serverUrl = config["AzureContainerInstance"]["Dns"].ToString() + "." + config["AzureContainerInstance"]["Region"].ToString().ToLower() + ".azurecontainer.io"; Console.WriteLine("Container instance DNS Prefix: " + serverUrl); SdkContext.DelayProvider.Delay(10000); Connection.Client.Init(serverUrl, sslFlag); if (!Connection.Client.Login(config["Server"]["SecretKey"].ToString())) { throw new Exception("CoFlows Not connected!"); } Connection.Client.Connect(); Console.Write("Container connected! "); QuantApp.Kernel.M.Factory = new MFactory(); Console.Write("Starting azure deployment... "); Code.UpdatePackageFile(workspace_name); var resDeploy = Connection.Client.PublishPackage(workspace_name); var t1 = DateTime.Now; Console.WriteLine("Ended: " + t1 + " taking " + (t1 - t0)); Console.Write("Result: " + resDeploy); } else if (args != null && args.Length > 1 && args[0] == "azure" && args[1] == "remove") { PythonEngine.BeginAllowThreads(); Console.WriteLine("Azure Container Instance remove start"); var pkg = Code.ProcessPackageFile(Code.UpdatePackageFile(workspace_name)); var res = Code.BuildRegisterPackage(pkg); if (!string.IsNullOrEmpty(res)) { Console.WriteLine(res); } AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(config["AzureContainerInstance"]["AuthFile"].ToString()); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) .Authenticate(credentials) .WithDefaultSubscription(); string rgName = pkg.ID.ToLower() + "-rg"; try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.BeginDeleteByName(rgName); IResourceGroup resGroup = azure.ResourceGroups.GetByName(rgName); while (resGroup != null) { resGroup = azure.ResourceGroups.GetByName(rgName); Console.Write("."); SdkContext.DelayProvider.Delay(1000); } Console.WriteLine(); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (Exception) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } } else { Console.WriteLine("Wrong argument"); } }
public void ContainerInstanceCRD() { using (var context = FluentMockContext.Start(GetType().FullName)) { var rgName = TestUtilities.GenerateName("rgaci"); var cgName = TestUtilities.GenerateName("aci"); var containerInstanceManager = TestHelper.CreateContainerInstanceManager(); var resourceManager = TestHelper.CreateResourceManager(); IContainerGroup containerGroup = null; try { containerGroup = containerInstanceManager.ContainerGroups.Define(cgName) .WithRegion(Region.USEast) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithEmptyDirectoryVolume("emptydir1") .DefineContainerInstance("tomcat") .WithImage("tomcat") .WithExternalTcpPort(8080) .WithCpuCoreCount(1) .WithEnvironmentVariable("ENV1", "value1") .Attach() .DefineContainerInstance("nginx") .WithImage("nginx") .WithExternalTcpPort(80) .WithEnvironmentVariableWithSecuredValue("ENV2", "securedValue1") .Attach() .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .WithDnsPrefix(cgName) .WithTag("tag1", "value1") .Create(); Assert.Equal(cgName, containerGroup.Name); Assert.Equal("Linux", containerGroup.OSType.Value); Assert.Equal(0, containerGroup.ImageRegistryServers.Count); Assert.Equal(1, containerGroup.Volumes.Count); Assert.NotNull(containerGroup.Volumes["emptydir1"]); Assert.NotNull(containerGroup.IPAddress); Assert.True(containerGroup.IsIPAddressPublic); Assert.Equal(2, containerGroup.ExternalTcpPorts.Length); Assert.Equal(2, containerGroup.ExternalPorts.Count); Assert.Empty(containerGroup.ExternalUdpPorts); Assert.Equal(8080, containerGroup.ExternalTcpPorts[0]); Assert.Equal(80, containerGroup.ExternalTcpPorts[1]); Assert.Equal(2, containerGroup.Containers.Count); Container tomcatContainer = containerGroup.Containers["tomcat"]; Assert.NotNull(tomcatContainer); Container nginxContainer = containerGroup.Containers["nginx"]; Assert.NotNull(nginxContainer); Assert.Equal("tomcat", tomcatContainer.Name); Assert.Equal("tomcat", tomcatContainer.Image); Assert.Equal(1.0, tomcatContainer.Resources.Requests.Cpu); Assert.Equal(1.5, tomcatContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, tomcatContainer.Ports.Count); Assert.Equal(8080, tomcatContainer.Ports[0].Port); Assert.Null(tomcatContainer.VolumeMounts); Assert.Null(tomcatContainer.Command); Assert.NotNull(tomcatContainer.EnvironmentVariables); Assert.Empty(tomcatContainer.EnvironmentVariables); Assert.Equal("nginx", nginxContainer.Name); Assert.Equal("nginx", nginxContainer.Image); Assert.Equal(1.0, nginxContainer.Resources.Requests.Cpu); Assert.Equal(1.5, nginxContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, nginxContainer.Ports.Count); Assert.Equal(80, nginxContainer.Ports[0].Port); Assert.Null(nginxContainer.VolumeMounts); Assert.Null(nginxContainer.Command); Assert.NotNull(nginxContainer.EnvironmentVariables); Assert.Empty(nginxContainer.EnvironmentVariables); Assert.Equal(cgName, containerGroup.DnsPrefix); Assert.True(containerGroup.Tags.ContainsKey("tag1")); Assert.Equal(ContainerGroupRestartPolicy.Never, containerGroup.RestartPolicy); IContainerGroup containerGroup2 = containerInstanceManager.ContainerGroups.GetByResourceGroup(rgName, cgName); var containerGroupList = containerInstanceManager.ContainerGroups.ListByResourceGroup(rgName); Assert.True(containerGroupList.Count() > 0); Assert.NotNull(containerGroupList.First().State); containerGroup.Refresh(); var containerOperationsList = containerInstanceManager.ContainerGroups.ListOperations(); Assert.Equal(10, containerOperationsList.Count()); containerGroup.Update() .WithoutTag("tag1") .WithTag("tag2", "value2") .Apply(); Assert.False(containerGroup.Tags.ContainsKey("tag1")); Assert.True(containerGroup.Tags.ContainsKey("tag2")); containerInstanceManager.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { resourceManager.ResourceGroups.BeginDeleteByName(rgName); } catch { } } } }
public void ContainerInstanceWithPrivateIpAddress() { using (var context = FluentMockContext.Start(GetType().FullName)) { var rgName = TestUtilities.GenerateName("rgaci"); var cgName = TestUtilities.GenerateName("aci"); string logAnalyticsWorkspaceId = "REPLACE WITH YOUR LOG ANALYTICS WORKSPACE ID"; string logAnalyticsWorkspaceKey = "REPLACE WITH YOUR LOG ANALYTICS WORKSPACE KEY"; string networkProfileSubscriptionId = "REPLACE WITH YOUR NETWORK PROFILE SUBSCRIPTION ID"; string networkProfileResourceGroupName = "REPLACE WITH YOUR NETWORK PROFILE RESOURCE GROUP NAME"; string networkProfileName = "REPLACE WITH YOUR NETWORK PROFILE NAME"; var containerInstanceManager = TestHelper.CreateContainerInstanceManager(); var resourceManager = TestHelper.CreateResourceManager(); IList <string> dnsServerNames = new List <string>(); dnsServerNames.Add("dnsServer1"); IContainerGroup containerGroup = null; try { containerGroup = containerInstanceManager.ContainerGroups.Define(cgName) .WithRegion(Region.USWest) .WithNewResourceGroup(rgName) .WithLinux() .WithPublicImageRegistryOnly() .WithEmptyDirectoryVolume("emptydir1") .DefineContainerInstance("tomcat") .WithImage("tomcat") .WithExternalTcpPort(8080) .WithCpuCoreCount(1) .WithEnvironmentVariable("ENV1", "value1") .Attach() .DefineContainerInstance("nginx") .WithImage("nginx") .WithExternalTcpPort(80) .WithEnvironmentVariableWithSecuredValue("ENV2", "securedValue1") .Attach() .WithSystemAssignedManagedServiceIdentity() .WithSystemAssignedIdentityBasedAccessToCurrentResourceGroup(BuiltInRole.Contributor) .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .WithLogAnalytics(logAnalyticsWorkspaceId, "isabellaTest") .WithNetworkProfileId(networkProfileSubscriptionId, networkProfileResourceGroupName, networkProfileName) .WithDnsConfiguration(dnsServerNames, "dnsSearchDomains", "dnsOptions") .WithTag("tag1", "value1") .Create(); Assert.Equal(cgName, containerGroup.Name); Assert.Equal("Linux", containerGroup.OSType.Value); Assert.Equal(0, containerGroup.ImageRegistryServers.Count); Assert.Equal(1, containerGroup.Volumes.Count); Assert.NotNull(containerGroup.Volumes["emptydir1"]); Assert.NotNull(containerGroup.IPAddress); Assert.True(containerGroup.IsIPAddressPrivate); Assert.Equal(2, containerGroup.ExternalTcpPorts.Length); Assert.Equal(2, containerGroup.ExternalPorts.Count); Assert.Empty(containerGroup.ExternalUdpPorts); Assert.Equal(8080, containerGroup.ExternalTcpPorts[0]); Assert.Equal(80, containerGroup.ExternalTcpPorts[1]); Assert.Equal(2, containerGroup.Containers.Count); Container tomcatContainer = containerGroup.Containers["tomcat"]; Assert.NotNull(tomcatContainer); Container nginxContainer = containerGroup.Containers["nginx"]; Assert.NotNull(nginxContainer); Assert.Equal("tomcat", tomcatContainer.Name); Assert.Equal("tomcat", tomcatContainer.Image); Assert.Equal(1.0, tomcatContainer.Resources.Requests.Cpu); Assert.Equal(1.5, tomcatContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, tomcatContainer.Ports.Count); Assert.Equal(8080, tomcatContainer.Ports[0].Port); Assert.Null(tomcatContainer.VolumeMounts); Assert.Null(tomcatContainer.Command); Assert.NotNull(tomcatContainer.EnvironmentVariables); Assert.Equal(1, tomcatContainer.EnvironmentVariables.Count); Assert.Equal("nginx", nginxContainer.Name); Assert.Equal("nginx", nginxContainer.Image); Assert.Equal(1.0, nginxContainer.Resources.Requests.Cpu); Assert.Equal(1.5, nginxContainer.Resources.Requests.MemoryInGB); Assert.Equal(1, nginxContainer.Ports.Count); Assert.Equal(80, nginxContainer.Ports[0].Port); Assert.Null(nginxContainer.VolumeMounts); Assert.Null(nginxContainer.Command); Assert.NotNull(nginxContainer.EnvironmentVariables); Assert.True(containerGroup.Tags.ContainsKey("tag1")); Assert.Equal(ContainerGroupRestartPolicy.Never, containerGroup.RestartPolicy); Assert.True(containerGroup.IsManagedServiceIdentityEnabled); Assert.Equal(ResourceIdentityType.SystemAssigned, containerGroup.ManagedServiceIdentityType); Assert.Equal(logAnalyticsWorkspaceId, containerGroup.LogAnalytics.WorkspaceId); Assert.Equal("/subscriptions/" + networkProfileSubscriptionId + "/resourceGroups/" + networkProfileResourceGroupName + "/providers/Microsoft.Network/networkProfiles/" + networkProfileName, containerGroup.NetworkProfileId); Assert.Equal("dnsServer1", containerGroup.DnsConfig.NameServers[0]); Assert.Equal("dnsSearchDomains", containerGroup.DnsConfig.SearchDomains); Assert.Equal("dnsOptions", containerGroup.DnsConfig.Options); IContainerGroup containerGroup2 = containerInstanceManager.ContainerGroups.GetByResourceGroup(rgName, cgName); var containerGroupList = containerInstanceManager.ContainerGroups.ListByResourceGroup(rgName); Assert.True(containerGroupList.Count() > 0); Assert.NotNull(containerGroupList.First().State); containerGroup.Refresh(); var containerOperationsList = containerInstanceManager.ContainerGroups.ListOperations(); Assert.True(containerOperationsList.Count() > 0); containerGroup.Update() .WithoutTag("tag1") .WithTag("tag2", "value2") .Apply(); Assert.False(containerGroup.Tags.ContainsKey("tag1")); Assert.True(containerGroup.Tags.ContainsKey("tag2")); containerInstanceManager.ContainerGroups.DeleteById(containerGroup.Id); } finally { try { resourceManager.ResourceGroups.BeginDeleteByName(rgName); } catch { } } } }
private bool TryCreateRandomSeleniumContainerInstance(PerformContext context, IAzure azure, Region azureRegion, IRegistry azureRegistry, IRegistryCredentials acrCredentials, string privateRepoUrl, out IContainerGroup containerGroup, out string randomContainerInstance) { try { var randomContainerGroupName = SdkContext.RandomResourceName(containerGroupName, 63); randomContainerInstance = SdkContext.RandomResourceName(seleniumRepositoryName + "-instance-", 63); containerGroup = azure.ContainerGroups.Define(randomContainerGroupName) .WithRegion(azureRegion) .WithExistingResourceGroup(resourceGroupName) .WithLinux() .WithPrivateImageRegistry(azureRegistry.LoginServerUrl, acrCredentials.Username, acrCredentials.AccessKeys[AccessKeyType.Primary]) .WithoutVolume() .DefineContainerInstance(randomContainerInstance) .WithImage(privateRepoUrl) .WithExternalTcpPort(4444) .Attach() .WithDnsPrefix(randomContainerInstance) .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .Create(); return(true); } catch (Exception ex) { context.WriteLine(ex.Message); containerGroup = null; randomContainerInstance = null; return(false); } }
public static Task Stop(IContainerGroup containerGroup, ILogger log) { log.LogInformation("Stopping " + containerGroup.Name); return(containerGroup.StopAsync()); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal("", //Azure Id "", // Azure secret "", // Azure Tenant AzureEnvironment.AzureGlobalCloud); Console.WriteLine("credentials done"); var azure = Azure .Configure() .Authenticate(credentials) .WithDefaultSubscription(); string containerGroupName = ""; //name of the container group Console.WriteLine("azure config"); Console.WriteLine("start ACI"); IContainerGroup containerGroup = azure.ContainerGroups.Define(containerGroupName) .WithRegion(Region.USWest2) .WithExistingResourceGroup("newdotnet") .WithLinux() .WithPrivateImageRegistry("newdotcr.azurecr.io", "newdotcr", "") //password sent in teams .WithoutVolume() .DefineContainerInstance(containerGroupName) .WithImage("newdotcr.azurecr.io/newdotcr") .WithExternalTcpPort(80) .WithCpuCoreCount(1) .WithMemorySizeInGB(2.5) .WithEnvironmentVariable("SOURCECONTROL", "GitHub") .WithEnvironmentVariable("TEMPLATE_NAME", "Classlib") .WithEnvironmentVariable("GITHUB_NAME", "") //user name .WithEnvironmentVariable("VSTS_NAME", " ") .WithEnvironmentVariable("REPO", "") // repo name .WithEnvironmentVariable("EMAIL", "") //email .WithEnvironmentVariable("TOKENENGINE", "") // set breakpoint in startup to grab token .WithEnvironmentVariable("DESCRIPTION", " ") .WithEnvironmentVariable("USE_TRAVIS", "false") .WithEnvironmentVariable("ENCRYPTION_ENABLED", "false") .WithEnvironmentVariable("USE_KEYVAULT", "false") .WithEnvironmentVariable("KEYVAULT_NAME", " ") .WithEnvironmentVariable("AD_ID", " ") .WithEnvironmentVariable("AD_SECRET", " ") .WithEnvironmentVariable("AES_KEY", " ") .WithEnvironmentVariable("AES_IV", " ") .WithEnvironmentVariable("HMAC_KEY", " ") .WithEnvironmentVariable("BASEURL", " ")// set up ngrok .WithEnvironmentVariable("PROJECTID", "1") .Attach() .WithRestartPolicy(ContainerGroupRestartPolicy.Never) .Create(); Console.WriteLine("ACI Created"); //Console.WriteLine(containerGroup.ToString()); Console.WriteLine($" Exit before refresh: {containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode.ToString()}"); int i = 0; while (containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode != 0) { Console.WriteLine($" Exit: {containerGroup.Inner.Containers[0].InstanceView.CurrentState.ExitCode.ToString()}"); containerGroup.Refresh(); Console.WriteLine(containerGroup.Inner.Containers[0].InstanceView.CurrentState.DetailStatus); System.Threading.Thread.Sleep(3000); i++; } Console.WriteLine(containerGroup.GetLogContent("refresh4")); Console.WriteLine(containerGroup.Inner.Containers[0].InstanceView.CurrentState.DetailStatus); Console.WriteLine(i); Console.WriteLine("done"); while (true) { } }
public static async Task CreateContainer(TaskLogger taskLogger, MyAppParameters myAppParameters) { // Get credentials var credentials = SdkContext.AzureCredentialsFactory .FromServicePrincipal(myAppParameters.AzureSubscriptionClientId, myAppParameters.AzureSubscriptionClientSecret, myAppParameters.TenantId, AzureEnvironment.AzureGlobalCloud); // Authenticate with Azure var azure = Azure.Configure() .Authenticate(credentials) .WithDefaultSubscription(); var message = $"Authenticated with azure"; await taskLogger.Log(message).ConfigureAwait(false); message = $"Getting resource group '{myAppParameters.ResourceGroupName}' details."; await taskLogger.Log(message).ConfigureAwait(false); // Check resource group exist or not. IResourceGroup resourceGroup = azure.ResourceGroups.GetByName(myAppParameters.ResourceGroupName); if (resourceGroup == null) { message = $"Resource group '{myAppParameters.ResourceGroupName}' not found."; throw new Exception(message); } // Check container group exist or not. IContainerGroup containerGroup = azure.ContainerGroups.GetByResourceGroup(myAppParameters.ResourceGroupName, myAppParameters.AgentName); if (containerGroup != null) { message = $"Already container group with name '{myAppParameters.AgentName}' exist in resource group '{myAppParameters.ResourceGroupName}'."; throw new Exception(message); } message = $"Creating container group '{myAppParameters.AgentName}' in resource group '{myAppParameters.ResourceGroupName}' with container image 'microsoft/vsts-agent:latest' ..."; await taskLogger.Log(message).ConfigureAwait(false); message = $"This will take more than 15 mins... You can check container creating logs in Azure portal."; await taskLogger.Log(message).ConfigureAwait(false); var azureRegion = resourceGroup.Region; var env = new Dictionary <string, string> { { "VSTS_ACCOUNT", myAppParameters.PipelineAccountName }, { "VSTS_TOKEN", myAppParameters.PATToken }, // This PAT token used to configure the agent. This PAT token should have permission to configure the agent else container moves to running state without configuring the agent { "VSTS_AGENT", myAppParameters.AgentName }, { "VSTS_POOL", myAppParameters.AgentPoolName } }; // Create container group with image. await CreateContainerWithAsync(taskLogger, azure, resourceGroup.RegionName, myAppParameters, env); // You can use below function to CreateContainerGroup With Polling // await CreateContainerGroupWithPolling(taskLogger, azure, resourceGroup.RegionName, myAppParameters, env); message = $"Azure pipeline agent container running.."; await taskLogger.Log(message).ConfigureAwait(false); }