/// <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}");
        }
Exemplo n.º 2
0
        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);
        }
 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}");
     });
 }
Exemplo n.º 4
0
        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");
            }
        }
Exemplo n.º 5
0
        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 { }
                }
            }
        }
Exemplo n.º 6
0
        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 { }
                }
            }
        }
Exemplo n.º 7
0
        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 { }
                }
            }
        }
Exemplo n.º 8
0
        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)
            {
            }
        }