コード例 #1
0
ファイル: Table.cs プロジェクト: christianblunden/Simple.Data
 public Table(IAzure azure, string tableName, IfTableDoesNotExist doesNotExistAction)
 {
     _tableName = tableName;
     _autoCreate = doesNotExistAction == IfTableDoesNotExist.CreateIt;
     _azure = azure;
     _tableService = new TableService(_azure);
 }
        public static void RunSample(IAzure azure)
        {
            string resourceGroupName  = SdkContext.RandomResourceName("rgNEMV_", 24);
            string app1Name           = SdkContext.RandomResourceName("webapp1-", 20);
            string app2Name           = SdkContext.RandomResourceName("webapp2-", 20);
            string app3Name           = SdkContext.RandomResourceName("webapp3-", 20);
            string app4Name           = SdkContext.RandomResourceName("webapp4-", 20);
            string app5Name           = SdkContext.RandomResourceName("webapp5-", 20);
            string plan1Name          = SdkContext.RandomResourceName("jplan1_", 15);
            string plan2Name          = SdkContext.RandomResourceName("jplan2_", 15);
            string plan3Name          = SdkContext.RandomResourceName("jplan3_", 15);
            string domainName         = SdkContext.RandomResourceName("jsdkdemo-", 20) + ".com";
            string trafficManagerName = SdkContext.RandomResourceName("jsdktm-", 20);

            try
            {
                //============================================================
                // Purchase a domain (will be canceled for a full refund)

                Utilities.Log("Purchasing a domain " + domainName + "...");

                azure.ResourceGroups.Define(resourceGroupName)
                .WithRegion(Region.USEast2)
                .Create();

                var domain = azure.AppServices.AppServiceDomains.Define(domainName)
                             .WithExistingResourceGroup(resourceGroupName)
                             .DefineRegistrantContact()
                             .WithFirstName("Jon")
                             .WithLastName("Doe")
                             .WithEmail("*****@*****.**")
                             .WithAddressLine1("123 4th Ave")
                             .WithCity("Redmond")
                             .WithStateOrProvince("WA")
                             .WithCountry(CountryISOCode.UnitedStates)
                             .WithPostalCode("98052")
                             .WithPhoneCountryCode(CountryPhoneCode.UnitedStates)
                             .WithPhoneNumber("4258828080")
                             .Attach()
                             .WithDomainPrivacyEnabled(true)
                             .WithAutoRenewEnabled(false)
                             .Create();
                Utilities.Log("Purchased domain " + domain.Name);
                Utilities.Print(domain);

                //============================================================
                // Create a self-singed SSL certificate

                pfxPath = "webapp_" + nameof(ManageLinuxWebAppWithTrafficManager).ToLower() + ".pfx";

                Utilities.Log("Creating a self-signed certificate " + pfxPath + "...");

                Utilities.CreateCertificate(domainName, pfxPath, CERT_PASSWORD);

                //============================================================
                // Create 3 app service plans in 3 regions

                Utilities.Log("Creating app service plan " + plan1Name + " in US West...");

                var plan1 = CreateAppServicePlan(azure, resourceGroupName, plan1Name, Region.USNorthCentral);

                Utilities.Log("Created app service plan " + plan1.Name);
                Utilities.Print(plan1);

                Utilities.Log("Creating app service plan " + plan2Name + " in Europe West...");

                var plan2 = CreateAppServicePlan(azure, resourceGroupName, plan2Name, Region.EuropeWest);

                Utilities.Log("Created app service plan " + plan2.Name);
                Utilities.Print(plan1);

                Utilities.Log("Creating app service plan " + plan3Name + " in Asia South East...");

                var plan3 = CreateAppServicePlan(azure, resourceGroupName, plan3Name, Region.AsiaSouthEast);

                Utilities.Log("Created app service plan " + plan2.Name);
                Utilities.Print(plan1);

                //============================================================
                // Create 5 web apps under these 3 app service plans

                Utilities.Log("Creating web app " + app1Name + "...");

                var app1 = CreateWebApp(azure, domain, resourceGroupName, app1Name, plan1);

                Utilities.Log("Created web app " + app1.Name);
                Utilities.Print(app1);

                Utilities.Log("Creating another web app " + app2Name + "...");
                var app2 = CreateWebApp(azure, domain, resourceGroupName, app2Name, plan2);

                Utilities.Log("Created web app " + app2.Name);
                Utilities.Print(app2);

                Utilities.Log("Creating another web app " + app3Name + "...");
                var app3 = CreateWebApp(azure, domain, resourceGroupName, app3Name, plan3);

                Utilities.Log("Created web app " + app3.Name);
                Utilities.Print(app3);

                Utilities.Log("Creating another web app " + app3Name + "...");
                var app4 = CreateWebApp(azure, domain, resourceGroupName, app4Name, plan1);

                Utilities.Log("Created web app " + app4.Name);
                Utilities.Print(app4);

                Utilities.Log("Creating another web app " + app3Name + "...");
                var app5 = CreateWebApp(azure, domain, resourceGroupName, app5Name, plan1);

                Utilities.Log("Created web app " + app5.Name);
                Utilities.Print(app5);

                //============================================================
                // Create a traffic manager

                Utilities.Log("Creating a traffic manager " + trafficManagerName + " for the web apps...");

                var trafficManager = azure.TrafficManagerProfiles
                                     .Define(trafficManagerName)
                                     .WithExistingResourceGroup(resourceGroupName)
                                     .WithLeafDomainLabel(trafficManagerName)
                                     .WithTrafficRoutingMethod(TrafficRoutingMethod.Weighted)
                                     .DefineAzureTargetEndpoint("endpoint1")
                                     .ToResourceId(app1.Id)
                                     .Attach()
                                     .DefineAzureTargetEndpoint("endpoint2")
                                     .ToResourceId(app2.Id)
                                     .Attach()
                                     .DefineAzureTargetEndpoint("endpoint3")
                                     .ToResourceId(app3.Id)
                                     .Attach()
                                     .Create();

                Utilities.Log("Created traffic manager " + trafficManager.Name);

                //============================================================
                // Scale up the app service plans

                Utilities.Log("Scaling up app service plan " + plan1Name + "...");

                plan1.Update()
                .WithCapacity(plan1.Capacity * 2)
                .Apply();

                Utilities.Log("Scaled up app service plan " + plan1Name);
                Utilities.Print(plan1);

                Utilities.Log("Scaling up app service plan " + plan2Name + "...");

                plan2.Update()
                .WithCapacity(plan2.Capacity * 2)
                .Apply();

                Utilities.Log("Scaled up app service plan " + plan2Name);
                Utilities.Print(plan2);

                Utilities.Log("Scaling up app service plan " + plan3Name + "...");

                plan3.Update()
                .WithCapacity(plan3.Capacity * 2)
                .Apply();

                Utilities.Log("Scaled up app service plan " + plan3Name);
                Utilities.Print(plan3);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + resourceGroupName);
                    azure.ResourceGroups.DeleteByName(resourceGroupName);
                    Utilities.Log("Deleted Resource Group: " + resourceGroupName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #3
0
        /**
         * Azure Compute sample for managing virtual machine extensions. -
         *  - Create a Linux and Windows virtual machine
         *  - Add three users (user names and passwords for windows, SSH keys for Linux)
         *  - Resets user credentials
         *  - Remove a user
         *  - Install MySQL on Linux | something significant on Windows
         *  - Remove extensions
         */
        public static void RunSample(IAzure azure)
        {
            string rgName               = SdkContext.RandomResourceName("rgCOVE", 15);
            string linuxVmName          = SdkContext.RandomResourceName("lVM", 10);
            string windowsVmName        = SdkContext.RandomResourceName("wVM", 10);
            string pipDnsLabelLinuxVM   = SdkContext.RandomResourceName("rgPip1", 25);
            string pipDnsLabelWindowsVM = SdkContext.RandomResourceName("rgPip2", 25);

            try
            {
                //=============================================================
                // Create a Linux VM with root (sudo) user
                Utilities.Log("Creating a Linux VM");

                IVirtualMachine linuxVM = azure.VirtualMachines.Define(linuxVmName)
                                          .WithRegion(Region.USEast)
                                          .WithNewResourceGroup(rgName)
                                          .WithNewPrimaryNetwork("10.0.0.0/28")
                                          .WithPrimaryPrivateIPAddressDynamic()
                                          .WithNewPrimaryPublicIPAddress(pipDnsLabelLinuxVM)
                                          .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer14_04_Lts)
                                          .WithRootUsername(FirstLinuxUserName)
                                          .WithRootPassword(FirstLinuxUserPassword)
                                          .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                                          .Create();

                Utilities.Log("Created a Linux VM:" + linuxVM.Id);
                Utilities.PrintVirtualMachine(linuxVM);

                //=============================================================
                // Add a second sudo user to Linux VM using VMAccess extension

                linuxVM.Update()
                .DefineNewExtension(linuxVmAccessExtensionName)
                .WithPublisher(linuxVmAccessExtensionPublisherName)
                .WithType(linuxVmAccessExtensionTypeName)
                .WithVersion(linuxVmAccessExtensionVersionName)
                .WithProtectedSetting("username", SecondLinuxUserName)
                .WithProtectedSetting("password", SecondLinuxUserPassword)
                .WithProtectedSetting("expiration", SecondLinuxUserExpiration)
                .Attach()
                .Apply();

                Utilities.Log("Added a second sudo user to the Linux VM");

                //=============================================================
                // Add a third sudo user to Linux VM by updating VMAccess extension

                linuxVM.Update()
                .UpdateExtension(linuxVmAccessExtensionName)
                .WithProtectedSetting("username", ThirdLinuxUserName)
                .WithProtectedSetting("password", ThirdLinuxUserPassword)
                .WithProtectedSetting("expiration", ThirdLinuxUserExpiration)
                .Parent()
                .Apply();

                Utilities.Log("Added a third sudo user to the Linux VM");


                //=============================================================
                // Reset ssh password of first user of Linux VM by updating VMAccess extension

                linuxVM.Update()
                .UpdateExtension(linuxVmAccessExtensionName)
                .WithProtectedSetting("username", FirstLinuxUserName)
                .WithProtectedSetting("password", FirstLinuxUserNewPassword)
                .WithProtectedSetting("reset_ssh", "true")
                .Parent()
                .Apply();

                Utilities.Log("Password of first user of Linux VM has been updated");

                //=============================================================
                // Removes the second sudo user from Linux VM using VMAccess extension

                linuxVM.Update()
                .UpdateExtension(linuxVmAccessExtensionName)
                .WithProtectedSetting("remove_user", SecondLinuxUserName)
                .Parent()
                .Apply();

                //=============================================================
                // Install MySQL in Linux VM using CustomScript extension

                linuxVM.Update()
                .DefineNewExtension(LinuxCustomScriptExtensionName)
                .WithPublisher(LinuxCustomScriptExtensionPublisherName)
                .WithType(LinuxCustomScriptExtensionTypeName)
                .WithVersion(LinuxCustomScriptExtensionVersionName)
                .WithMinorVersionAutoUpgrade()
                .WithPublicSetting("fileUris", MySQLLinuxInstallScriptFileUris)
                .WithPublicSetting("commandToExecute", MySqlScriptLinuxInstallCommand)
                .Attach()
                .Apply();

                Utilities.Log("Installed MySql using custom script extension");
                Utilities.PrintVirtualMachine(linuxVM);

                //=============================================================
                // Removes the extensions from Linux VM

                linuxVM.Update()
                .WithoutExtension(LinuxCustomScriptExtensionName)
                .WithoutExtension(linuxVmAccessExtensionName)
                .Apply();
                Utilities.Log("Removed the custom script and VM Access extensions from Linux VM");
                Utilities.PrintVirtualMachine(linuxVM);

                //=============================================================
                // Create a Windows VM with admin user

                Utilities.Log("Creating a Windows VM");

                IVirtualMachine windowsVM = azure.VirtualMachines.Define(windowsVmName)
                                            .WithRegion(Region.USEast)
                                            .WithExistingResourceGroup(rgName)
                                            .WithNewPrimaryNetwork("10.0.0.0/28")
                                            .WithPrimaryPrivateIPAddressDynamic()
                                            .WithNewPrimaryPublicIPAddress(pipDnsLabelWindowsVM)
                                            .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
                                            .WithAdminUsername(firstWindowsUserName)
                                            .WithAdminPassword(firstWindowsUserPassword)
                                            .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                                            .DefineNewExtension(windowsCustomScriptExtensionName)
                                            .WithPublisher(windowsCustomScriptExtensionPublisherName)
                                            .WithType(windowsCustomScriptExtensionTypeName)
                                            .WithVersion(windowsCustomScriptExtensionVersionName)
                                            .WithMinorVersionAutoUpgrade()
                                            .WithPublicSetting("fileUris", mySQLWindowsInstallScriptFileUris)
                                            .WithPublicSetting("commandToExecute", mySqlScriptWindowsInstallCommand)
                                            .Attach()
                                            .Create();

                Utilities.Log("Created a Windows VM:" + windowsVM.Id);
                Utilities.PrintVirtualMachine(windowsVM);

                //=============================================================
                // Add a second admin user to Windows VM using VMAccess extension

                windowsVM.Update()
                .DefineNewExtension(windowsVmAccessExtensionName)
                .WithPublisher(windowsVmAccessExtensionPublisherName)
                .WithType(windowsVmAccessExtensionTypeName)
                .WithVersion(windowsVmAccessExtensionVersionName)
                .WithProtectedSetting("username", secondWindowsUserName)
                .WithProtectedSetting("password", secondWindowsUserPassword)
                .Attach()
                .Apply();

                Utilities.Log("Added a second admin user to the Windows VM");

                //=============================================================
                // Add a third admin user to Windows VM by updating VMAccess extension

                windowsVM.Update()
                .UpdateExtension(windowsVmAccessExtensionName)
                .WithProtectedSetting("username", thirdWindowsUserName)
                .WithProtectedSetting("password", thirdWindowsUserPassword)
                .Parent()
                .Apply();

                Utilities.Log("Added a third admin user to the Windows VM");

                //=============================================================
                // Reset admin password of first user of Windows VM by updating VMAccess extension

                windowsVM.Update()
                .UpdateExtension(windowsVmAccessExtensionName)
                .WithProtectedSetting("username", firstWindowsUserName)
                .WithProtectedSetting("password", firstWindowsUserNewPassword)
                .Parent()
                .Apply();

                Utilities.Log("Password of first user of Windows VM has been updated");

                //=============================================================
                // Removes the extensions from Linux VM

                windowsVM.Update()
                .WithoutExtension(windowsVmAccessExtensionName)
                .Apply();
                Utilities.Log("Removed the VM Access extensions from Windows VM");
                Utilities.PrintVirtualMachine(windowsVM);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (Exception ex)
                {
                    Utilities.Log(ex);
                }
            }
        }
コード例 #4
0
        /**
         * Azure SQL sample for managing SQL Virtual Network Rules
         *  - Create a Virtual Network with two subnets.
         *  - Create a SQL Server along with one virtual network rule.
         *  - Add another virtual network rule in the SQL Server
         *  - Get a virtual network rule.
         *  - Update a virtual network rule.
         *  - List all virtual network rules.
         *  - Delete a virtual network.
         *  - Delete Sql Server
         */
        public static void RunSample(IAzure azure)
        {
            try
            {
                // ============================================================
                // Create a virtual network with two subnets.
                Utilities.Log("Create a virtual network with two subnets: subnet1 and subnet2");

                var virtualNetwork = azure.Networks.Define(vnetName)
                                     .WithRegion(Region.AsiaSouthEast)
                                     .WithNewResourceGroup(rgName)
                                     .WithAddressSpace("192.168.0.0/16")
                                     .DefineSubnet("subnet1")
                                     .WithAddressPrefix("192.168.1.0/24")
                                     .WithAccessFromService(ServiceEndpointType.MicrosoftSql)
                                     .Attach()
                                     .WithSubnet("subnet2", "192.168.2.0/24")
                                     .Create();

                Utilities.Log("Created a virtual network");
                // Print the virtual network details
                Utilities.PrintVirtualNetwork(virtualNetwork);

                // ============================================================
                // Create a SQL Server, with one virtual network rule.
                Utilities.Log("Create a SQL server with one virtual network rule");

                var sqlServer = azure.SqlServers.Define(sqlServerName)
                                .WithRegion(Region.AsiaSouthEast)
                                .WithExistingResourceGroup(rgName)
                                .WithAdministratorLogin(administratorLogin)
                                .WithAdministratorPassword(administratorPassword)
                                .WithoutAccessFromAzureServices()
                                .DefineVirtualNetworkRule("virtualNetworkRule1")
                                .WithSubnet(virtualNetwork.Id, "subnet1")
                                .Attach()
                                .Create();

                Utilities.PrintSqlServer(sqlServer);


                // ============================================================
                // Get the virtual network rule created above.
                var virtualNetworkRule = azure.SqlServers.VirtualNetworkRules
                                         .GetBySqlServer(rgName, sqlServerName, "virtualNetworkRule1");

                Utilities.PrintSqlVirtualNetworkRule(virtualNetworkRule);


                // ============================================================
                // Add new virtual network rules.
                Utilities.Log("adding another virtual network rule in existing SQL Server");
                virtualNetworkRule = sqlServer.VirtualNetworkRules
                                     .Define("virtualNetworkRule2")
                                     .WithSubnet(virtualNetwork.Id, "subnet2")
                                     .IgnoreMissingSqlServiceEndpoint()
                                     .Create();

                Utilities.PrintSqlVirtualNetworkRule(virtualNetworkRule);


                // ============================================================
                // Update a virtual network rules.
                Utilities.Log("Updating an existing virtual network rules in SQL Server.");
                virtualNetworkRule.Update()
                .WithSubnet(virtualNetwork.Id, "subnet1")
                .Apply();

                Utilities.PrintSqlVirtualNetworkRule(virtualNetworkRule);


                // ============================================================
                // List and delete all virtual network rules.
                Utilities.Log("Listing all virtual network rules in SQL Server.");

                var virtualNetworkRules = sqlServer.VirtualNetworkRules.List();
                foreach (var vnetRule in virtualNetworkRules)
                {
                    // Delete the virtual network rule.
                    Utilities.Log("Deleting a virtual network rule");
                    vnetRule.Delete();
                }


                // Delete the SQL Server.
                Utilities.Log("Deleting a Sql Server");
                azure.SqlServers.DeleteById(sqlServer.Id);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (Exception e)
                {
                    Utilities.Log(e);
                }
            }
        }
        /**
         * Azure App Service basic sample for managing function apps.
         *  - Create 3 function apps under the same new app service plan and with the same storage account
         *    - Deploy 1 and 2 via Git a function that calculates the square of a number
         *    - Deploy 3 via Web Deploy
         *    - Enable app level authentication for the 1st function app
         *    - Verify the 1st function app can be accessed with the admin key
         *    - Enable function level authentication for the 2nd function app
         *    - Verify the 2nd function app can be accessed with the function key
         *    - Enable function level authentication for the 3rd function app
         *    - Verify the 3rd function app can be accessed with the function key
         */


        public static void RunSample(IAzure azure)
        {
            // New resources
            string suffix   = ".azurewebsites.net";
            string app1Name = SdkContext.RandomResourceName("webapp1-", 20);
            string app2Name = SdkContext.RandomResourceName("webapp2-", 20);
            string app3Name = SdkContext.RandomResourceName("webapp3-", 20);
            string app1Url  = app1Name + suffix;
            string app2Url  = app2Name + suffix;
            string app3Url  = app3Name + suffix;
            string rgName   = SdkContext.RandomResourceName("rg1NEMV_", 24);

            try {
                //============================================================
                // Create a function app with admin level auth

                Utilities.Log("Creating function app " + app1Name + " in resource group " + rgName + " with admin level auth...");

                IFunctionApp app1 = azure.AppServices.FunctionApps.Define(app1Name)
                                    .WithRegion(Region.USWest)
                                    .WithNewResourceGroup(rgName)
                                    .WithLocalGitSourceControl()
                                    .Create();

                Utilities.Log("Created function app " + app1.Name);
                Utilities.Print(app1);

                //============================================================
                // Create a second function app with function level auth

                Utilities.Log("Creating another function app " + app2Name + " in resource group " + rgName + " with function level auth...");
                IAppServicePlan plan = azure.AppServices.AppServicePlans.GetById(app1.AppServicePlanId);
                IFunctionApp    app2 = azure.AppServices.FunctionApps.Define(app2Name)
                                       .WithExistingAppServicePlan(plan)
                                       .WithExistingResourceGroup(rgName)
                                       .WithExistingStorageAccount(app1.StorageAccount)
                                       .WithLocalGitSourceControl()
                                       .Create();

                Utilities.Log("Created function app " + app2.Name);
                Utilities.Print(app2);

                //============================================================
                // Create a third function app with function level auth

                Utilities.Log("Creating another function app " + app3Name + " in resource group " + rgName + " with function level auth...");
                IFunctionApp app3 = azure.AppServices.FunctionApps.Define(app3Name)
                                    .WithExistingAppServicePlan(plan)
                                    .WithExistingResourceGroup(rgName)
                                    .WithExistingStorageAccount(app1.StorageAccount)
                                    .WithLocalGitSourceControl()
                                    .Create();

                Utilities.Log("Created function app " + app3.Name);
                Utilities.Print(app3);

                //============================================================
                // Deploy to app 1 through Git

                Utilities.Log("Deploying a local function app to " + app1Name + " through Git...");

                IPublishingProfile profile = app1.GetPublishingProfile();
                Utilities.DeployByGit(profile, "square-function-app-admin-auth");

                // warm up
                Utilities.Log("Warming up " + app1Url + "/api/square...");
                Utilities.PostAddress("http://" + app1Url + "/api/square", "625");
                SdkContext.DelayProvider.Delay(5000);
                Utilities.Log("CURLing " + app1Url + "/api/square...");
                Utilities.Log("Square of 625 is " + Utilities.PostAddress("http://" + app1Url + "/api/square?code=" + app1.GetMasterKey(), "625"));

                //============================================================
                // Deploy to app 2 through Git

                Utilities.Log("Deploying a local function app to " + app2Name + " through Git...");

                profile = app2.GetPublishingProfile();
                Utilities.DeployByGit(profile, "square-function-app-function-auth");

                Utilities.Log("Deployment to function app " + app2.Name + " completed");
                Utilities.Print(app2);


                string functionKey = app2.ListFunctionKeys("square").Values.First();

                // warm up
                Utilities.Log("Warming up " + app2Url + "/api/square...");
                Utilities.PostAddress("http://" + app2Url + "/api/square", "725");
                SdkContext.DelayProvider.Delay(5000);
                Utilities.Log("CURLing " + app2Url + "/api/square...");
                Utilities.Log("Square of 725 is " + Utilities.PostAddress("http://" + app2Url + "/api/square?code=" + functionKey, "725"));

                Utilities.Log("Adding a new key to function app " + app2.Name + "...");

                var newKey = app2.AddFunctionKey("square", "newkey", null);

                Utilities.Log("CURLing " + app2Url + "/api/square...");
                Utilities.Log("Square of 825 is " + Utilities.PostAddress("http://" + app2Url + "/api/square?code=" + newKey.Value, "825"));

                //============================================================
                // Deploy to app 3 through web deploy

                Utilities.Log("Deploying a local function app to " + app3Name + " throuh web deploy...");

                app3.Deploy()
                .WithPackageUri("https://github.com/Azure/azure-libraries-for-net/raw/master/Samples/Asset/square-function-app-function-auth.zip")
                .WithExistingDeploymentsDeleted(false)
                .Execute();

                Utilities.Log("Deployment to function app " + app3.Name + " completed");

                Utilities.Log("Adding a new key to function app " + app3.Name + "...");
                app3.AddFunctionKey("square", "newkey", "mysecretkey");

                // warm up
                Utilities.Log("Warming up " + app3Url + "/api/square...");
                Utilities.PostAddress("http://" + app3Url + "/api/square", "925");
                SdkContext.DelayProvider.Delay(5000);
                Utilities.Log("CURLing " + app3Url + "/api/square...");
                Utilities.Log("Square of 925 is " + Utilities.PostAddress("http://" + app3Url + "/api/square?code=mysecretkey", "925"));
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #6
0
        /**
         * Azure App Service basic sample for managing web apps.
         *  - Create a SQL database in a new SQL server
         *  - Create a web app deployed with Project Nami (WordPress's SQL Server variant)
         *      that contains the app settings to connect to the SQL database
         *  - Update the SQL server's firewall rules to allow the web app to access
         *  - Clean up
         */

        public static void RunSample(IAzure azure)
        {
            string appName       = SdkContext.RandomResourceName("webapp1-", 20);
            string appUrl        = appName + Suffix;
            string sqlServerName = SdkContext.RandomResourceName("jsdkserver", 20);
            string sqlDbName     = SdkContext.RandomResourceName("jsdkdb", 20);
            string rgName        = SdkContext.RandomResourceName("rg1NEMV_", 24);

            try
            {
                //============================================================
                // Create a sql server

                Utilities.Log("Creating SQL server " + sqlServerName + "...");

                ISqlServer server = azure.SqlServers.Define(sqlServerName)
                                    .WithRegion(Region.USWest)
                                    .WithNewResourceGroup(rgName)
                                    .WithAdministratorLogin(Admin)
                                    .WithAdministratorPassword(Password)
                                    .Create();

                Utilities.Log("Created SQL server " + server.Name);

                //============================================================
                // Create a sql database for the web app to use

                Utilities.Log("Creating SQL database " + sqlDbName + "...");

                ISqlDatabase db = server.Databases.Define(sqlDbName)
                                  .Create();

                Utilities.Log("Created SQL database " + db.Name);

                //============================================================
                // Create a web app with a new app service plan

                Utilities.Log("Creating web app " + appName + "...");

                IWebApp app = azure.WebApps
                              .Define(appName)
                              .WithRegion(Region.USWest)
                              .WithExistingResourceGroup(rgName)
                              .WithNewWindowsPlan(PricingTier.StandardS1)
                              .WithPhpVersion(PhpVersion.V5_6)
                              .DefineSourceControl()
                              .WithPublicGitRepository("https://github.com/ProjectNami/projectnami")
                              .WithBranch("master")
                              .Attach()
                              .WithAppSetting("ProjectNami.DBHost", server.FullyQualifiedDomainName)
                              .WithAppSetting("ProjectNami.DBName", db.Name)
                              .WithAppSetting("ProjectNami.DBUser", Admin)
                              .WithAppSetting("ProjectNami.DBPass", Password)
                              .Create();

                Utilities.Log("Created web app " + app.Name);
                Utilities.Print(app);

                //============================================================
                // Allow web app to access the SQL server

                Utilities.Log("Allowing web app " + appName + " to access SQL server...");

                Microsoft.Azure.Management.Sql.Fluent.SqlServer.Update.IUpdate update = server.Update();
                foreach (var ip in app.OutboundIPAddresses)
                {
                    update = update.WithNewFirewallRule(ip);
                }
                server = update.Apply();

                Utilities.Log("Firewall rules added for web app " + appName);
                Utilities.PrintSqlServer(server);

                Utilities.Log("Your WordPress app is ready.");
                Utilities.Log("Please navigate to http://" + appUrl + " to finish the GUI setup. Press enter to exit.");
                Utilities.ReadLine();
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #7
0
 protected override void BeforeRender()
 {
     _azure = _azureConnector.Azure();
     _graph = _azureConnector.Graph();
 }
コード例 #8
0
        /**
         * Azure Compute sample for managing virtual machines -
         *  - Create a virtual machine
         *  - Deallocate the virtual machine
         *  - Generalize the virtual machine
         *  - Capture the virtual machine to create a generalized image
         *  - Create a second virtual machine using the generalized image
         *  - Delete the second virtual machine
         *  - Create a new virtual machine by attaching OS disk of deleted VM to it.
         */
        public static void RunSample(IAzure azure)
        {
            string rgName           = SdkContext.RandomResourceName("rgCOMV", 10);
            string linuxVmName1     = SdkContext.RandomResourceName("VM1", 10);
            string linuxVmName2     = SdkContext.RandomResourceName("VM2", 10);
            string linuxVmName3     = SdkContext.RandomResourceName("VM3", 10);
            string publicIpDnsLabel = SdkContext.RandomResourceName("pip", 10);

            try
            {
                //=============================================================
                // Create a Linux VM using an image from PIR (Platform Image Repository)

                Utilities.Log("Creating a Linux VM");

                var linuxVM = azure.VirtualMachines.Define(linuxVmName1)
                              .WithRegion(Region.USWest)
                              .WithNewResourceGroup(rgName)
                              .WithNewPrimaryNetwork("10.0.0.0/28")
                              .WithPrimaryPrivateIPAddressDynamic()
                              .WithNewPrimaryPublicIPAddress(publicIpDnsLabel)
                              .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                              .WithRootUsername(UserName)
                              .WithRootPassword(Password)
                              .WithUnmanagedDisks()
                              .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                              .DefineNewExtension("CustomScriptForLinux")
                              .WithPublisher("Microsoft.OSTCExtensions")
                              .WithType("CustomScriptForLinux")
                              .WithVersion("1.4")
                              .WithMinorVersionAutoUpgrade()
                              .WithPublicSetting("fileUris", ApacheInstallScriptUris)
                              .WithPublicSetting("commandToExecute", ApacheInstallCommand)
                              .Attach()
                              .Create();

                Utilities.Log("Created a Linux VM: " + linuxVM.Id);
                Utilities.PrintVirtualMachine(linuxVM);

                // De-provision the virtual machine
                Utilities.DeprovisionAgentInLinuxVM(linuxVM.GetPrimaryPublicIPAddress().Fqdn, 22, UserName, Password);

                //=============================================================
                // Deallocate the virtual machine
                Utilities.Log("Deallocate VM: " + linuxVM.Id);

                linuxVM.Deallocate();

                Utilities.Log("Deallocated VM: " + linuxVM.Id + "; state = " + linuxVM.PowerState);

                //=============================================================
                // Generalize the virtual machine
                Utilities.Log("Generalize VM: " + linuxVM.Id);

                linuxVM.Generalize();

                Utilities.Log("Generalized VM: " + linuxVM.Id);

                //=============================================================
                // Capture the virtual machine to get a 'Generalized image' with Apache
                Utilities.Log("Capturing VM: " + linuxVM.Id);

                var capturedResultJson = linuxVM.Capture("capturedvhds", "img", true);

                Utilities.Log("Captured VM: " + linuxVM.Id);

                //=============================================================
                // Create a Linux VM using captured image (Generalized image)
                JObject o             = JObject.Parse(capturedResultJson);
                JToken  resourceToken = o.SelectToken("$.resources[?(@.properties.storageProfile.osDisk.image.uri != null)]");
                if (resourceToken == null)
                {
                    throw new Exception("Could not locate image uri under expected section in the capture result -" + capturedResultJson);
                }
                string capturedImageUri = (string)(resourceToken["properties"]["storageProfile"]["osDisk"]["image"]["uri"]);

                Utilities.Log("Creating a Linux VM using captured image - " + capturedImageUri);

                var linuxVM2 = azure.VirtualMachines.Define(linuxVmName2)
                               .WithRegion(Region.USWest)
                               .WithExistingResourceGroup(rgName)
                               .WithNewPrimaryNetwork("10.0.0.0/28")
                               .WithPrimaryPrivateIPAddressDynamic()
                               .WithoutPrimaryPublicIPAddress()
                               .WithStoredLinuxImage(capturedImageUri) // Note: A Generalized Image can also be an uploaded VHD prepared from an on-premise generalized VM.
                               .WithRootUsername(UserName)
                               .WithRootPassword(Password)
                               .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                               .Create();

                Utilities.PrintVirtualMachine(linuxVM2);

                var specializedVhd = linuxVM2.OSUnmanagedDiskVhdUri;
                //=============================================================
                // Deleting the virtual machine
                Utilities.Log("Deleting VM: " + linuxVM2.Id);

                azure.VirtualMachines.DeleteById(linuxVM2.Id); // VM required to be deleted to be able to attach it's
                                                               // OS Disk VHD to another VM (Deallocate is not sufficient)

                Utilities.Log("Deleted VM");

                //=============================================================
                // Create a Linux VM using 'specialized VHD' of previous VM

                Utilities.Log("Creating a new Linux VM by attaching OS Disk vhd - "
                              + specializedVhd
                              + " of deleted VM");

                var linuxVM3 = azure.VirtualMachines.Define(linuxVmName3)
                               .WithRegion(Region.USWest)
                               .WithExistingResourceGroup(rgName)
                               .WithNewPrimaryNetwork("10.0.0.0/28")
                               .WithPrimaryPrivateIPAddressDynamic()
                               .WithoutPrimaryPublicIPAddress()
                               .WithSpecializedOSUnmanagedDisk(specializedVhd, OperatingSystemTypes.Linux) // New user credentials cannot be specified
                               .WithSize(VirtualMachineSizeTypes.StandardD3V2)                             // when attaching a specialized VHD
                               .Create();

                Utilities.PrintVirtualMachine(linuxVM3);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
            }
        }
コード例 #9
0
ファイル: MyApp.cs プロジェクト: Atvaark/vsts-rm-extensions
        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);
        }
コード例 #10
0
        /**
         * Azure Compute sample for managing availability sets -
         *  - Create an availability set
         *  - Create a VM in a new availability set
         *  - Create another VM in the same availability set
         *  - Update the availability set
         *  - Create another availability set
         *  - List availability sets
         *  - Delete an availability set.
         */
        public static void RunSample(IAzure azure)
        {
            string rgName        = Utilities.CreateRandomName("rgCOMA");
            string availSetName1 = Utilities.CreateRandomName("av1");
            string availSetName2 = Utilities.CreateRandomName("av2");
            string vm1Name       = Utilities.CreateRandomName("vm1");
            string vm2Name       = Utilities.CreateRandomName("vm2");
            string vnetName      = Utilities.CreateRandomName("vnet");

            try
            {
                //=============================================================
                // Create an availability set

                Utilities.Log("Creating an availability set");

                var availSet1 = azure.AvailabilitySets.Define(availSetName1)
                                .WithRegion(Region.USEast)
                                .WithNewResourceGroup(rgName)
                                .WithFaultDomainCount(2)
                                .WithUpdateDomainCount(4)
                                .WithSku(AvailabilitySetSkuTypes.Aligned)
                                .WithTag("cluster", "Windowslinux")
                                .WithTag("tag1", "tag1val")
                                .Create();

                Utilities.Log("Created first availability set: " + availSet1.Id);
                Utilities.PrintAvailabilitySet(availSet1);

                //=============================================================
                // Define a virtual network for the VMs in this availability set
                var network = azure.Networks
                              .Define(vnetName)
                              .WithRegion(Region.USEast)
                              .WithExistingResourceGroup(rgName)
                              .WithAddressSpace("10.0.0.0/28");

                //=============================================================
                // Create a Windows VM in the new availability set

                Utilities.Log("Creating a Windows VM in the availability set");

                var vm1 = azure.VirtualMachines.Define(vm1Name)
                          .WithRegion(Region.USEast)
                          .WithExistingResourceGroup(rgName)
                          .WithNewPrimaryNetwork(network)
                          .WithPrimaryPrivateIPAddressDynamic()
                          .WithoutPrimaryPublicIPAddress()
                          .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
                          .WithAdminUsername(UserName)
                          .WithAdminPassword(Password)
                          .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                          .WithExistingAvailabilitySet(availSet1)
                          .Create();

                Utilities.Log("Created first VM:" + vm1.Id);
                Utilities.PrintVirtualMachine(vm1);

                //=============================================================
                // Create a Linux VM in the same availability set

                Utilities.Log("Creating a Linux VM in the availability set");

                var vm2 = azure.VirtualMachines.Define(vm2Name)
                          .WithRegion(Region.USEast)
                          .WithExistingResourceGroup(rgName)
                          .WithNewPrimaryNetwork(network)
                          .WithPrimaryPrivateIPAddressDynamic()
                          .WithoutPrimaryPublicIPAddress()
                          .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                          .WithRootUsername(UserName)
                          .WithRootPassword(Password)
                          .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                          .WithExistingAvailabilitySet(availSet1)
                          .Create();

                Utilities.Log("Created second VM: " + vm2.Id);
                Utilities.PrintVirtualMachine(vm2);

                //=============================================================
                // Update - Tag the availability set

                availSet1 = availSet1.Update()
                            .WithTag("server1", "nginx")
                            .WithTag("server2", "iis")
                            .WithoutTag("tag1")
                            .Apply();

                Utilities.Log("Tagged availability set: " + availSet1.Id);

                //=============================================================
                // Create another availability set

                Utilities.Log("Creating an availability set");

                var availSet2 = azure.AvailabilitySets.Define(availSetName2)
                                .WithRegion(Region.USEast)
                                .WithExistingResourceGroup(rgName)
                                .Create();

                Utilities.Log("Created second availability set: " + availSet2.Id);
                Utilities.PrintAvailabilitySet(availSet2);

                //=============================================================
                // List availability sets

                var resourceGroupName = availSet1.ResourceGroupName;

                Utilities.Log("Printing list of availability sets  =======");

                foreach (var availabilitySet in azure.AvailabilitySets.ListByResourceGroup(resourceGroupName))
                {
                    Utilities.PrintAvailabilitySet(availabilitySet);
                }

                //=============================================================
                // Delete an availability set

                Utilities.Log("Deleting an availability set: " + availSet2.Id);

                azure.AvailabilitySets.DeleteById(availSet2.Id);

                Utilities.Log("Deleted availability set: " + availSet2.Id);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (Exception ex)
                {
                    Utilities.Log(ex);
                }
            }
        }
コード例 #11
0
        public static async Task Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            log.Info("C# HTTP trigger function processed a request.");

            //Parse alert request
            string  requestBody = new StreamReader(req.Body).ReadToEnd();
            Webhook data        = new Webhook();

            data = JsonConvert.DeserializeObject <Webhook>(requestBody);
            Context alertResource = data.RequestBody.context;

            IConfigurationRoot config = new ConfigurationBuilder()
                                        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                                        .AddEnvironmentVariables()
                                        .Build();

            string tenantId  = config.GetConnectionString("TenantId");
            string clientId  = config.GetConnectionString("clientId");
            string clientKey = config.GetConnectionString("ClientKey");

            if (string.IsNullOrEmpty(tenantId) || string.IsNullOrEmpty(clientId) || string.IsNullOrEmpty(clientKey))
            {
                log.Error("Serivice credentials are null. Check connection string settings");
                return;
            }

            AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientKey, tenantId, AzureEnvironment.AzureGlobalCloud);
            IAzure           azure       = Azure.Configure().Authenticate(credentials).WithSubscription(alertResource.subscriptionId);

            if (azure == null)
            {
                log.Error("Error: Issues logging into Azure subscription: " + alertResource.subscriptionId + ". Exiting.");
                return;
            }

            IVirtualMachine VM = await azure.VirtualMachines.GetByIdAsync(alertResource.resourceId);

            if (VM == null)
            {
                log.Error("Error: VM: " + alertResource.resourceId + "was not found. Exiting.");
                return;
            }

            INetworkWatcher networkWatcher = await EnsureNetworkWatcherExists(azure, VM.Region, log);

            InstallNetworkWatcherExtension(VM, log);

            string storageAccountId = Environment.GetEnvironmentVariable("PacketCaptureStorageAccount");
            var    storageAccount   = await azure.StorageAccounts.GetByIdAsync(storageAccountId);

            if (storageAccount == null)
            {
                log.Error("Storage Account: " + storageAccountId + " not found. Exiting.");
                return;
            }

            string packetCaptureName = VM.Name.Substring(0, System.Math.Min(63, VM.Name.Length)) + System.DateTime.Now.ToString("s").Replace(":", "");

            IPacketCaptures packetCapturesObj = networkWatcher.PacketCaptures;
            var             packetCaptures    = packetCapturesObj.List().ToList();

            if (packetCaptures.Count >= 10)
            {
                log.Info("More than 10 Captures, finding oldest.");
                var packetCaptureTasks = new List <Task <IPacketCaptureStatus> >();
                foreach (IPacketCapture pcap in packetCaptures)
                {
                    packetCaptureTasks.Add(pcap.GetStatusAsync());
                }

                var packetCaptureStatuses = new List <Tuple <IPacketCapture, IPacketCaptureStatus> >();
                for (int i = 0; i < packetCaptureTasks.Count; ++i)
                {
                    packetCaptureStatuses.Add(new Tuple <IPacketCapture, IPacketCaptureStatus>(packetCaptures[i], await packetCaptureTasks[i]));
                }

                packetCaptureStatuses.Sort((Tuple <IPacketCapture, IPacketCaptureStatus> first, Tuple <IPacketCapture, IPacketCaptureStatus> second) =>
                {
                    return(first.Item2.CaptureStartTime.CompareTo(second.Item2.CaptureStartTime));
                });
                log.Info("Removing: " + packetCaptureStatuses.First().Item1.Name);
                await networkWatcher.PacketCaptures.DeleteByNameAsync(packetCaptureStatuses.First().Item1.Name);
            }

            log.Info("Creating Packet Capture");
            await networkWatcher.PacketCaptures
            .Define(packetCaptureName)
            .WithTarget(VM.Id)
            .WithStorageAccountId(storageAccount.Id)
            .WithTimeLimitInSeconds(15)
            .CreateAsync();

            log.Info("Packet Capture created successfully");
        }
コード例 #12
0
        /// <summary>Get the virtual machine.</summary>
        /// <param name="azure">The azure client instance</param>
        /// <param name="inputObject">The input request.</param>
        /// <param name="log">The trace writer instance</param>
        /// <returns>Returns the virtual machine.</returns>
        private static async Task <IVirtualMachineScaleSetVM> GetVirtualMachineScaleSetVm(IAzure azure, InputObject inputObject, TraceWriter log)
        {
            var vmScaleSet = await azure.VirtualMachineScaleSets.GetByIdAsync(inputObject.VirtualMachineScaleSetId);

            if (vmScaleSet == null)
            {
                log.Info("VM Scaleset Chaos: scale set is returning null for the Id: " + inputObject.VirtualMachineScaleSetId);
                return(null);
            }

            var scaleSetVms = await vmScaleSet.VirtualMachines.ListAsync();

            if (scaleSetVms != null && scaleSetVms.Any())
            {
                return(scaleSetVms.FirstOrDefault(x =>
                                                  x.Name.Equals(inputObject.ResourceId, StringComparison.OrdinalIgnoreCase)));
            }

            log.Info("VM Scaleset Chaos: scale set vm's are empty");
            return(null);
        }
コード例 #13
0
        private async Task <INetworkSecurityGroup> DeployNetworkSecurityGroup(List <IGenericResource> allResources, IPublicIPAddress gatewayIp, IAzure azure, CancellationToken cancellationToken)
        {
            string nsgName = Settings.Name + "-nsg";

            IGenericResource nsg = allResources.FirstOrDefault(r =>
                                                               r.ResourceProviderNamespace == "Microsoft.Network" &&
                                                               r.ResourceType == "networkSecurityGroups" &&
                                                               r.Name == nsgName);

            var neededRules = Settings.NeededSecurityGroupRules ?? throw new InvalidOperationException("Configuration not set correctly.");

            if (nsg == null)
            {
                Logger.LogInformation("Creating new network security group {nsgName}.", nsgName);
                var nsgDef = azure.NetworkSecurityGroups.Define(nsgName)
                             .WithRegion(Settings.Location)
                             .WithExistingResourceGroup(Settings.ResourceGroup)
                             .WithTags(DefaultTags);

                var index = 1;
                foreach ((string key, string range) in neededRules)
                {
                    nsgDef = nsgDef.DefineRule(key)
                             .AllowInbound()
                             .FromAnyAddress()
                             .FromAnyPort()
                             .ToAnyAddress()
                             .ToPortRanges(range)
                             .WithProtocol(SecurityRuleProtocol.Tcp)
                             .WithPriority(1000 + index)
                             .Attach();

                    index++;
                }

                return(await nsgDef.CreateAsync(cancellationToken));
            }

            allResources.Remove(nsg);

            Logger.LogInformation("Updating existing network security group {nsgName}.", nsg.Name);
            INetworkSecurityGroup existingGroup = await azure.NetworkSecurityGroups.GetByIdAsync(nsg.Id, cancellationToken);

            IEnumerable <string> existingRules = existingGroup.SecurityRules.Keys
                                                 .Where(ruleName => !ruleName.StartsWith("NRMS")); // Ignore rules created by the microsoft management stuff

            var updatedNsg = existingGroup.Update();

            foreach (string rule in existingRules)
            {
                updatedNsg = updatedNsg.WithoutRule(rule);
            }

            {
                var index = 1;
                foreach ((string key, string range) in neededRules)
                {
                    updatedNsg = updatedNsg.DefineRule(key)
                                 .AllowInbound()
                                 .FromAnyAddress()
                                 .FromAnyPort()
                                 .ToAnyAddress()
                                 .ToPortRanges(range)
                                 .WithProtocol(SecurityRuleProtocol.Tcp)
                                 .WithPriority(1000 + index)
                                 .Attach();

                    index++;
                }
            }

            existingGroup = await updatedNsg.ApplyAsync(cancellationToken);

            return(existingGroup);
        }
コード例 #14
0
        protected override async Task <string> DeployResourcesAsync(List <IGenericResource> unexpectedResources, IAzure azure, IResourceManager resourceManager, CancellationToken cancellationToken)
        {
            var gatewayIp = await DeployPublicIp(unexpectedResources, azure, Settings.Name + "-IP", Settings.Name, cancellationToken);

            INetworkSecurityGroup nsg = await DeployNetworkSecurityGroup(unexpectedResources, gatewayIp, azure, cancellationToken);

            INetwork vnet = await DeployVirtualNetwork(unexpectedResources, azure, nsg, cancellationToken);

            var gatewayId = await DeployApplicationGateway(unexpectedResources, resourceManager, gatewayIp, vnet, cancellationToken);

            return($"VNet='{vnet.Id}'\nGateway='{gatewayId}'\n");
        }
コード例 #15
0
        /**
         * Azure Network sample for managing network watcher -
         *  - Create Network Watcher
         *	- Manage packet capture – track traffic to and from a virtual machine
         *      Create a VM
         *      Start a packet capture
         *      Stop a packet capture
         *      Get a packet capture
         *      Delete a packet capture
         *  - Verify IP flow – verify if traffic is allowed to or from a virtual machine
         *      Get the IP address of a NIC on a virtual machine
         *      Test IP flow on the NIC
         *  - Analyze next hop – get the next hop type and IP address for a virtual machine
         *  - Retrieve network topology for a resource group
         *  - Analyze Virtual Machine Security by examining effective network security rules applied to a VM
         *      Get security group view for the VM
         *  - Configure Network Security Group Flow Logs
         *      Get flow log settings
         *      Enable NSG flow log
         *      Disable NSG flow log
         *  - Delete network watcher
         */
        public static void RunSample(IAzure azure)
        {
            string nwName = SdkContext.RandomResourceName("nw", 8);

            string          userName          = "******";
            string          vnetName          = SdkContext.RandomResourceName("vnet", 20);
            string          subnetName        = "subnet1";
            string          nsgName           = SdkContext.RandomResourceName("nsg", 20);
            string          dnsLabel          = SdkContext.RandomResourceName("pipdns", 20);
            string          rgName            = SdkContext.RandomResourceName("rg", 24);
            string          saName            = SdkContext.RandomResourceName("sa", 24);
            string          vmName            = SdkContext.RandomResourceName("vm", 24);
            string          packetCaptureName = SdkContext.RandomResourceName("pc", 8);
            INetworkWatcher nw = null;

            try
            {
                //============================================================
                // Create network watcher
                Utilities.Log("Creating network watcher...");
                nw = azure.NetworkWatchers.Define(nwName)
                     .WithRegion(region)
                     .WithNewResourceGroup()
                     .Create();

                Utilities.Log("Created network watcher");
                // Print the network watcher
                Utilities.Print(nw);

                //============================================================
                // Manage packet capture – track traffic to and from a virtual machine

                // Create network security group, virtual network and VM; add packetCapture extension to enable packet capture
                Utilities.Log("Creating network security group...");
                INetworkSecurityGroup nsg = azure.NetworkSecurityGroups.Define(nsgName)
                                            .WithRegion(region)
                                            .WithNewResourceGroup(rgName)
                                            .DefineRule("DenyInternetInComing")
                                            .DenyInbound()
                                            .FromAddress("INTERNET")
                                            .FromAnyPort()
                                            .ToAnyAddress()
                                            .ToPort(443)
                                            .WithAnyProtocol()
                                            .Attach()
                                            .Create();
                Utilities.Log("Creating virtual network...");
                ICreatable <INetwork> virtualNetwork = azure.Networks.Define(vnetName)
                                                       .WithRegion(region)
                                                       .WithExistingResourceGroup(rgName)
                                                       .WithAddressSpace("192.168.0.0/16")
                                                       .DefineSubnet(subnetName)
                                                       .WithAddressPrefix("192.168.2.0/24")
                                                       .WithExistingNetworkSecurityGroup(nsg)
                                                       .Attach();
                Utilities.Log("Creating virtual machine...");
                IVirtualMachine vm = azure.VirtualMachines.Define(vmName)
                                     .WithRegion(region)
                                     .WithExistingResourceGroup(rgName)
                                     .WithNewPrimaryNetwork(virtualNetwork)
                                     .WithPrimaryPrivateIPAddressDynamic()
                                     .WithNewPrimaryPublicIPAddress(dnsLabel)
                                     .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer14_04_Lts)
                                     .WithRootUsername(userName)
                                     .WithRootPassword("Abcdef.123456")
                                     .WithSize(VirtualMachineSizeTypes.StandardA1)
                                     .DefineNewExtension("packetCapture")
                                     .WithPublisher("Microsoft.Azure.NetworkWatcher")
                                     .WithType("NetworkWatcherAgentLinux")
                                     .WithVersion("1.4")
                                     .WithMinorVersionAutoUpgrade()
                                     .Attach()
                                     .Create();

                // Create storage account
                Utilities.Log("Creating storage account...");
                IStorageAccount storageAccount = azure.StorageAccounts.Define(saName)
                                                 .WithRegion(region)
                                                 .WithExistingResourceGroup(rgName)
                                                 .Create();

                // Start a packet capture
                Utilities.Log("Creating packet capture...");
                IPacketCapture packetCapture = nw.PacketCaptures
                                               .Define(packetCaptureName)
                                               .WithTarget(vm.Id)
                                               .WithStorageAccountId(storageAccount.Id)
                                               .WithTimeLimitInSeconds(1500)
                                               .DefinePacketCaptureFilter()
                                               .WithProtocol(PcProtocol.TCP)
                                               .Attach()
                                               .Create();
                Utilities.Log("Created packet capture");
                Utilities.Print(packetCapture);

                // Stop a packet capture
                Utilities.Log("Stopping packet capture...");
                packetCapture.Stop();
                Utilities.Print(packetCapture);

                // Get a packet capture
                Utilities.Log("Getting packet capture...");
                IPacketCapture packetCapture1 = nw.PacketCaptures.GetByName(packetCaptureName);
                Utilities.Print(packetCapture1);

                // Delete a packet capture
                Utilities.Log("Deleting packet capture");
                nw.PacketCaptures.DeleteByName(packetCapture.Name);

                //============================================================
                // Verify IP flow – verify if traffic is allowed to or from a virtual machine
                // Get the IP address of a NIC on a virtual machine
                String ipAddress = vm.GetPrimaryNetworkInterface().PrimaryPrivateIP;
                // Test IP flow on the NIC
                Utilities.Log("Verifying IP flow for vm id " + vm.Id + "...");
                IVerificationIPFlow verificationIPFlow = nw.VerifyIPFlow()
                                                         .WithTargetResourceId(vm.Id)
                                                         .WithDirection(Direction.Outbound)
                                                         .WithProtocol(Protocol.TCP)
                                                         .WithLocalIPAddress(ipAddress)
                                                         .WithRemoteIPAddress("8.8.8.8")
                                                         .WithLocalPort("443")
                                                         .WithRemotePort("443")
                                                         .Execute();
                Utilities.Print(verificationIPFlow);

                //============================================================
                // Analyze next hop – get the next hop type and IP address for a virtual machine
                Utilities.Log("Calculating next hop...");
                INextHop nextHop = nw.NextHop().WithTargetResourceId(vm.Id)
                                   .WithSourceIPAddress(ipAddress)
                                   .WithDestinationIPAddress("8.8.8.8")
                                   .Execute();
                Utilities.Print(nextHop);

                //============================================================
                // Retrieve network topology for a resource group
                Utilities.Log("Getting topology...");
                ITopology topology = nw.GetTopology(rgName);
                Utilities.Print(topology);

                //============================================================
                // Analyze Virtual Machine Security by examining effective network security rules applied to a VM
                // Get security group view for the VM
                Utilities.Log("Getting security group view for a vm");
                ISecurityGroupView sgViewResult = nw.GetSecurityGroupView(vm.Id);
                Utilities.Print(sgViewResult);

                //============================================================
                // Configure Network Security Group Flow Logs

                // Get flow log settings
                IFlowLogSettings flowLogSettings = nw.GetFlowLogSettings(nsg.Id);
                Utilities.Print(flowLogSettings);

                // Enable NSG flow log
                flowLogSettings.Update()
                .WithLogging()
                .WithStorageAccount(storageAccount.Id)
                .WithRetentionPolicyDays(5)
                .WithRetentionPolicyEnabled()
                .Apply();
                Utilities.Print(flowLogSettings);

                // Disable NSG flow log
                flowLogSettings.Update()
                .WithoutLogging()
                .Apply();
                Utilities.Print(flowLogSettings);

                //============================================================
                // Delete network watcher
                Utilities.Log("Deleting network watcher");
                azure.NetworkWatchers.DeleteById(nw.Id);
                Utilities.Log("Deleted network watcher");
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    if (nw != null)
                    {
                        Utilities.Log("Deleting network watcher resource group: " + nw.ResourceGroupName);
                        azure.ResourceGroups.BeginDeleteByName(nw.ResourceGroupName);
                    }
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception ex)
                {
                    Utilities.Log(ex);
                }
            }
        }
コード例 #16
0
        internal async Task <bool> UpdateVersionFromUrlAsync(string sourceUrl, InstanceName instance, IAzure azure, CancellationToken cancellationToken)
        {
            logger.WriteVerbose($"Refreshing local cache from {sourceUrl}.");
            await RefreshLocalPackageFromUrl(sourceUrl, cancellationToken);

            var localRuntimeVer = await GetLocalPackageVersionAsync(RuntimePackageFile);

            logger.WriteVerbose($"Locally cached Runtime package version is {localRuntimeVer}.");

            // TODO check the uploaded version before overwriting?
            SemVersion uploadedRuntimeVer = await GetDeployedRuntimeVersion(instance, azure, cancellationToken);

            if (localRuntimeVer > uploadedRuntimeVer)
            {
                logger.WriteInfo($"Using local cached runtime package {localRuntimeVer}");

                logger.WriteVerbose($"Uploading runtime package to {instance.DnsHostName}");
                bool ok = await UploadRuntimeZip(instance, azure, cancellationToken);

                if (ok)
                {
                    logger.WriteInfo($"Runtime package uploaded to {instance.PlainName}.");
                }
                else
                {
                    logger.WriteError($"Failed uploading Runtime to {instance.DnsHostName}.");
                }
                return(ok);
            }
            else
            {
                logger.WriteInfo($"Runtime package is up to date.");
                return(true);
            }
        }
コード例 #17
0
        public async Task CopyAcrImagesCommand_RuntimeDepsSharing()
        {
            const string subscriptionId = "my subscription";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            Mock <IRegistriesOperations> registriesOperationsMock = AzureHelper.CreateRegistriesOperationsMock();
            IAzure azure = AzureHelper.CreateAzureMock(registriesOperationsMock);
            Mock <IAzureManagementFactory> azureManagementFactoryMock =
                AzureHelper.CreateAzureManagementFactoryMock(subscriptionId, azure);

            Mock <IEnvironmentService> environmentServiceMock = new Mock <IEnvironmentService>();

            CopyAcrImagesCommand command = new CopyAcrImagesCommand(azureManagementFactoryMock.Object, Mock.Of <ILoggerService>());

            command.Options.Manifest         = Path.Combine(tempFolderContext.Path, "manifest.json");
            command.Options.Subscription     = subscriptionId;
            command.Options.ResourceGroup    = "my resource group";
            command.Options.SourceRepoPrefix = command.Options.RepoPrefix = "test/";
            command.Options.ImageInfoPath    = "image-info.json";

            string dockerfileRelativePath = DockerfileHelper.CreateDockerfile("3.1/runtime-deps/os", tempFolderContext);

            Manifest manifest = CreateManifest(
                CreateRepo("runtime-deps",
                           CreateImage(
                               new Platform[]
            {
                CreatePlatform(dockerfileRelativePath, new string[] { "3.1" }, osVersion: "focal")
            },
                               productVersion: "3.1"),
                           CreateImage(
                               new Platform[]
            {
                CreatePlatform(dockerfileRelativePath, new string[] { "5.0" }, osVersion: "focal")
            },
                               productVersion: "5.0"))
                );

            manifest.Registry = "mcr.microsoft.com";

            File.WriteAllText(Path.Combine(tempFolderContext.Path, command.Options.Manifest), JsonConvert.SerializeObject(manifest));

            RepoData runtimeRepo;

            ImageArtifactDetails imageArtifactDetails = new ImageArtifactDetails
            {
                Repos =
                {
                    {
                        runtimeRepo = new RepoData
                        {
                            Repo   = "runtime-deps",
                            Images =
                            {
                                new ImageData
                                {
                                    Platforms =
                                    {
                                        CreatePlatform(
                                            PathHelper.NormalizePath(dockerfileRelativePath),
                                            simpleTags: new List <string>
                                        {
                                            "3.1"
                                        },
                                            osVersion: "focal")
                                    },
                                    ProductVersion = "3.1"
                                },
                                new ImageData
                                {
                                    Platforms =
                                    {
                                        CreatePlatform(
                                            PathHelper.NormalizePath(dockerfileRelativePath),
                                            simpleTags: new List <string>
                                        {
                                            "5.0"
                                        },
                                            osVersion: "focal")
                                    },
                                    ProductVersion = "5.0"
                                }
                            }
                        }
                    }
                }
            };

            File.WriteAllText(command.Options.ImageInfoPath, JsonConvert.SerializeObject(imageArtifactDetails));

            command.LoadManifest();
            await command.ExecuteAsync();

            List <string> expectedTags = new List <string>
            {
                $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:3.1",
                $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:5.0"
            };

            foreach (string expectedTag in expectedTags)
            {
                registriesOperationsMock
                .Verify(o => o.ImportImageWithHttpMessagesAsync(
                            command.Options.ResourceGroup,
                            manifest.Registry,
                            It.Is <ImportImageParametersInner>(parameters =>
                                                               VerifyImportImageParameters(parameters, new List <string> {
                    expectedTag
                })),
                            It.IsAny <Dictionary <string, List <string> > >(),
                            It.IsAny <CancellationToken>()));
            }
        }
コード例 #18
0
        /**
         * Azure Compute sample for managing virtual machines.
         *  - Create an un-managed virtual machine from PIR image with data disks
         *  - Create managed disks from specialized un-managed OS and Data disk of virtual machine
         *  - Create a virtual machine by attaching the managed disks
         *  - Get SAS Uri to the virtual machine's managed disks
         */
        public static void RunSample(IAzure azure)
        {
            var linuxVmName1              = Utilities.CreateRandomName("VM1");
            var linuxVmName2              = Utilities.CreateRandomName("VM2");
            var managedOSDiskName         = Utilities.CreateRandomName("ds-os-");
            var managedDataDiskNamePrefix = Utilities.CreateRandomName("ds-data-");
            var rgName           = Utilities.CreateRandomName("rgCOMV");
            var publicIpDnsLabel = Utilities.CreateRandomName("pip");

            var apacheInstallScript     = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
            var apacheInstallCommand    = "bash install_apache.sh";
            var apacheInstallScriptUris = new List <string>();

            apacheInstallScriptUris.Add(apacheInstallScript);
            try
            {
                //=============================================================
                // Create a Linux VM using an image from PIR (Platform Image Repository)

                Utilities.Log("Creating a un-managed Linux VM");

                var linuxVM = azure.VirtualMachines.Define(linuxVmName1)
                              .WithRegion(region)
                              .WithNewResourceGroup(rgName)
                              .WithNewPrimaryNetwork("10.0.0.0/28")
                              .WithPrimaryPrivateIPAddressDynamic()
                              .WithNewPrimaryPublicIPAddress(publicIpDnsLabel)
                              .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                              .WithRootUsername(userName)
                              .WithRootPassword(password)
                              .WithUnmanagedDisks()
                              .DefineUnmanagedDataDisk("disk-1")
                              .WithNewVhd(50)
                              .WithLun(1)
                              .Attach()
                              .DefineUnmanagedDataDisk("disk-2")
                              .WithNewVhd(50)
                              .WithLun(2)
                              .Attach()
                              .DefineNewExtension("CustomScriptForLinux")
                              .WithPublisher("Microsoft.OSTCExtensions")
                              .WithType("CustomScriptForLinux")
                              .WithVersion("1.4")
                              .WithMinorVersionAutoUpgrade()
                              .WithPublicSetting("fileUris", apacheInstallScriptUris)
                              .WithPublicSetting("commandToExecute", apacheInstallCommand)
                              .Attach()
                              .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                              .Create();

                Utilities.Log("Created a Linux VM with un-managed OS and data disks: " + linuxVM.Id);
                Utilities.PrintVirtualMachine(linuxVM);

                // Gets the specialized OS and Data disk VHDs of the virtual machine
                //
                var specializedOSVhdUri = linuxVM.OSUnmanagedDiskVhdUri;
                var dataVhdUris         = new List <string>();
                foreach (var dataDisk  in  linuxVM.UnmanagedDataDisks.Values)
                {
                    dataVhdUris.Add(dataDisk.VhdUri);
                }

                //=============================================================
                // Delete the virtual machine
                Utilities.Log("Deleting VM: " + linuxVM.Id);

                azure.VirtualMachines.DeleteById(linuxVM.Id);

                Utilities.Log("Deleted the VM");

                //=============================================================
                // Create Managed disk from the specialized OS VHD

                Utilities.Log($"Creating managed disk from the specialized OS VHD: {specializedOSVhdUri} ");

                var osDisk = azure.Disks.Define(managedOSDiskName)
                             .WithRegion(region)
                             .WithExistingResourceGroup(rgName)
                             .WithLinuxFromVhd(specializedOSVhdUri)
                             .WithSizeInGB(100)
                             .Create();

                Utilities.Log("Created managed disk holding OS: " + osDisk.Id);
                // Utilities.Print(osDisk); TODO

                //=============================================================
                // Create Managed disks from the Data VHDs

                var dataDisks = new List <IDisk>();
                var i         = 0;
                foreach (String dataVhdUri  in  dataVhdUris)
                {
                    Utilities.Log($"Creating managed disk from the Data VHD: {dataVhdUri}");

                    var dataDisk = azure.Disks.Define(managedDataDiskNamePrefix + "-" + i)
                                   .WithRegion(region)
                                   .WithExistingResourceGroup(rgName)
                                   .WithData()
                                   .FromVhd(dataVhdUri)
                                   .WithSizeInGB(150)
                                   .WithSku(DiskSkuTypes.StandardLRS)
                                   .Create();
                    dataDisks.Add(dataDisk);

                    Utilities.Log("Created managed disk holding data: " + dataDisk.Id);
                    // Utilities.Print(dataDisk); TODO
                    i++;
                }

                //=============================================================
                // Create a Linux VM by attaching the disks

                Utilities.Log("Creating a Linux VM using specialized OS and data disks");

                var linuxVM2 = azure.VirtualMachines.Define(linuxVmName2)
                               .WithRegion(region)
                               .WithExistingResourceGroup(rgName)
                               .WithNewPrimaryNetwork("10.0.0.0/28")
                               .WithPrimaryPrivateIPAddressDynamic()
                               .WithoutPrimaryPublicIPAddress()
                               .WithSpecializedOSDisk(osDisk, OperatingSystemTypes.Linux)
                               .WithExistingDataDisk(dataDisks[0])
                               .WithExistingDataDisk(dataDisks[1], 1, CachingTypes.ReadWrite)
                               .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                               .Create();

                Utilities.PrintVirtualMachine(linuxVM2);

                var dataDiskIds = new List <string>();
                foreach (var disk  in  linuxVM2.DataDisks.Values)
                {
                    dataDiskIds.Add(disk.Id);
                }

                //=============================================================
                // Detach the data disks from the virtual machine

                Utilities.Log("Updating VM by detaching the data disks");

                linuxVM2.Update()
                .WithoutDataDisk(0)
                .WithoutDataDisk(1)
                .Apply();

                Utilities.PrintVirtualMachine(linuxVM2);

                //=============================================================
                // Get the readonly SAS URI to the data disks
                Utilities.Log("Getting data disks SAS Uris");

                foreach (String diskId  in  dataDiskIds)
                {
                    var dataDisk       = azure.Disks.GetById(diskId);
                    var dataDiskSasUri = dataDisk.GrantAccess(24 * 60);
                    Utilities.Log($"Data disk SAS Uri: {dataDiskSasUri}");
                }
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #19
0
        public async Task SyndicatedTags()
        {
            const string subscriptionId = "my subscription";

            using TempFolderContext tempFolderContext = TestHelper.UseTempFolder();
            Mock <IRegistriesOperations> registriesOperationsMock = AzureHelper.CreateRegistriesOperationsMock();
            IAzure azure = AzureHelper.CreateAzureMock(registriesOperationsMock);
            Mock <IAzureManagementFactory> azureManagementFactoryMock =
                AzureHelper.CreateAzureManagementFactoryMock(subscriptionId, azure);

            Mock <IEnvironmentService> environmentServiceMock = new Mock <IEnvironmentService>();

            CopyAcrImagesCommand command = new CopyAcrImagesCommand(azureManagementFactoryMock.Object, Mock.Of <ILoggerService>());

            command.Options.Manifest         = Path.Combine(tempFolderContext.Path, "manifest.json");
            command.Options.Subscription     = subscriptionId;
            command.Options.ResourceGroup    = "my resource group";
            command.Options.SourceRepoPrefix = command.Options.RepoPrefix = "test/";
            command.Options.ImageInfoPath    = "image-info.json";

            const string runtimeRelativeDir = "1.0/runtime/os";

            Directory.CreateDirectory(Path.Combine(tempFolderContext.Path, runtimeRelativeDir));
            string dockerfileRelativePath = Path.Combine(runtimeRelativeDir, "Dockerfile");

            File.WriteAllText(Path.Combine(tempFolderContext.Path, dockerfileRelativePath), "FROM repo:tag");

            Manifest manifest = ManifestHelper.CreateManifest(
                ManifestHelper.CreateRepo("runtime",
                                          ManifestHelper.CreateImage(
                                              ManifestHelper.CreatePlatform(dockerfileRelativePath, new string[] { "tag1", "tag2", "tag3" })))
                );

            manifest.Registry = "mcr.microsoft.com";

            const string syndicatedRepo2 = "runtime2";
            const string syndicatedRepo3 = "runtime3";

            Platform platform = manifest.Repos.First().Images.First().Platforms.First();

            platform.Tags["tag2"].Syndication = new TagSyndication
            {
                Repo = syndicatedRepo2,
            };
            platform.Tags["tag3"].Syndication = new TagSyndication
            {
                Repo            = syndicatedRepo3,
                DestinationTags = new string[]
                {
                    "tag3a",
                    "tag3b"
                }
            };

            File.WriteAllText(Path.Combine(tempFolderContext.Path, command.Options.Manifest), JsonConvert.SerializeObject(manifest));

            RepoData runtimeRepo;

            ImageArtifactDetails imageArtifactDetails = new ImageArtifactDetails
            {
                Repos =
                {
                    {
                        runtimeRepo = new RepoData
                        {
                            Repo   = "runtime",
                            Images =
                            {
                                new ImageData
                                {
                                    Platforms =
                                    {
                                        CreatePlatform(
                                            PathHelper.NormalizePath(dockerfileRelativePath),
                                            simpleTags: new List <string>
                                        {
                                            "tag1",
                                            "tag2",
                                            "tag3"
                                        })
                                    }
                                }
                            }
                        }
                    }
                }
            };

            File.WriteAllText(command.Options.ImageInfoPath, JsonConvert.SerializeObject(imageArtifactDetails));

            command.LoadManifest();
            await command.ExecuteAsync();

            List <string> expectedTags = new List <string>
            {
                $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:tag1",
                $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:tag2",
                $"{command.Options.RepoPrefix}{runtimeRepo.Repo}:tag3",
                $"{command.Options.RepoPrefix}{syndicatedRepo2}:tag2",
                $"{command.Options.RepoPrefix}{syndicatedRepo3}:tag3a",
                $"{command.Options.RepoPrefix}{syndicatedRepo3}:tag3b"
            };

            foreach (string expectedTag in expectedTags)
            {
                registriesOperationsMock
                .Verify(o => o.ImportImageWithHttpMessagesAsync(
                            command.Options.ResourceGroup,
                            manifest.Registry,
                            It.Is <ImportImageParametersInner>(parameters =>
                                                               VerifyImportImageParameters(parameters, new List <string> {
                    expectedTag
                })),
                            It.IsAny <Dictionary <string, List <string> > >(),
                            It.IsAny <CancellationToken>()));
            }
        }
コード例 #20
0
 protected override void AfterRender()
 {
     _graph.Dispose();
     _azure = null;
     _graph = null;
 }
コード例 #21
0
        /**
         * Azure Compute sample for managing virtual machine scale sets with un-managed disks -
         *  - Create a virtual machine scale set behind an Internet facing load balancer
         *  - Install Apache Web serversinvirtual machinesinthe virtual machine scale set
         *  - List the network interfaces associated with the virtual machine scale set
         *  - List scale set virtual machine instances and SSH collection string
         *  - Stop a virtual machine scale set
         *  - Start a virtual machine scale set
         *  - Update a virtual machine scale set
         *    - Double the no. of virtual machines
         *  - Restart a virtual machine scale set
         */

        public static void RunSample(IAzure azure)
        {
            var region            = Region.USWestCentral;
            var rgName            = SdkContext.RandomResourceName("rgCOVS", 15);
            var vnetName          = SdkContext.RandomResourceName("vnet", 24);
            var loadBalancerName1 = SdkContext.RandomResourceName("intlb" + "-", 18);
            var publicIpName      = "pip-" + loadBalancerName1;
            var frontendName      = loadBalancerName1 + "-FE1";
            var backendPoolName1  = loadBalancerName1 + "-BAP1";
            var backendPoolName2  = loadBalancerName1 + "-BAP2";

            var httpProbe              = "httpProbe";
            var httpsProbe             = "httpsProbe";
            var httpLoadBalancingRule  = "httpRule";
            var httpsLoadBalancingRule = "httpsRule";
            var natPool50XXto22        = "natPool50XXto22";
            var natPool60XXto23        = "natPool60XXto23";
            var vmssName = SdkContext.RandomResourceName("vmss", 24);

            var userName = "******";
            var sshKey   = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD [email protected]";

            var apacheInstallScript = "https://raw.githubusercontent.com/Azure/azure-sdk-for-java/master/azure-samples/src/main/resources/install_apache.sh";
            var installCommand      = "bash install_apache.sh";
            var fileUris            = new List <string>();

            fileUris.Add(apacheInstallScript);
            try
            {
                //=============================================================
                // Create a virtual network with a frontend subnet
                Utilities.Log("Creating virtual network with a frontend subnet ...");

                var network = azure.Networks.Define(vnetName)
                              .WithRegion(region)
                              .WithNewResourceGroup(rgName)
                              .WithAddressSpace("172.16.0.0/16")
                              .DefineSubnet("Front-end")
                              .WithAddressPrefix("172.16.1.0/24")
                              .Attach()
                              .Create();

                Utilities.Log("Created a virtual network");
                // Print the virtual network details
                Utilities.PrintVirtualNetwork(network);

                //=============================================================
                // Create a public IP address
                Utilities.Log("Creating a public IP address...");

                var publicIPAddress = azure.PublicIPAddresses.Define(publicIpName)
                                      .WithRegion(region)
                                      .WithExistingResourceGroup(rgName)
                                      .WithLeafDomainLabel(publicIpName)
                                      .Create();

                Utilities.Log("Created a public IP address");
                // Print the virtual network details
                Utilities.PrintIPAddress(publicIPAddress);

                //=============================================================
                // Create an Internet facing load balancer with
                // One frontend IP address
                // Two backend address pools which contain network interfaces for the virtual
                //  machines to receive HTTP and HTTPS network traffic from the load balancer
                // Two load balancing rules for HTTP and HTTPS to map public ports on the load
                //  balancer to portsinthe backend address pool
                // Two probes which contain HTTP and HTTPS health probes used to check availability
                //  of virtual machinesinthe backend address pool
                // Three inbound NAT rules which contain rules that map a public port on the load
                //  balancer to a port for a specific virtual machineinthe backend address pool
                //  - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23

                Utilities.Log("Creating a Internet facing load balancer with ...");
                Utilities.Log("- A frontend IP address");
                Utilities.Log("- Two backend address pools which contain network interfaces for the virtual\n"
                              + "  machines to receive HTTP and HTTPS network traffic from the load balancer");
                Utilities.Log("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n"
                              + "  balancer to portsinthe backend address pool");
                Utilities.Log("- Two probes which contain HTTP and HTTPS health probes used to check availability\n"
                              + "  of virtual machinesinthe backend address pool");
                Utilities.Log("- Two inbound NAT rules which contain rules that map a public port on the load\n"
                              + "  balancer to a port for a specific virtual machineinthe backend address pool\n"
                              + "  - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");

                var loadBalancer1 = azure.LoadBalancers.Define(loadBalancerName1)
                                    .WithRegion(region)
                                    .WithExistingResourceGroup(rgName)
                                    .DefinePublicFrontend(frontendName)
                                    .WithExistingPublicIPAddress(publicIPAddress)
                                    .Attach()
                                    // Add two backend one per rule
                                    .DefineBackend(backendPoolName1)
                                    .Attach()
                                    .DefineBackend(backendPoolName2)
                                    .Attach()
                                    // Add two probes one per rule
                                    .DefineHttpProbe(httpProbe)
                                    .WithRequestPath("/")
                                    .WithPort(80)
                                    .Attach()
                                    .DefineHttpProbe(httpsProbe)
                                    .WithRequestPath("/")
                                    .WithPort(443)
                                    .Attach()

                                    // Add two rules that uses above backend and probe
                                    .DefineLoadBalancingRule(httpLoadBalancingRule)
                                    .WithProtocol(TransportProtocol.Tcp)
                                    .WithFrontend(frontendName)
                                    .WithFrontendPort(80)
                                    .WithProbe(httpProbe)
                                    .WithBackend(backendPoolName1)
                                    .Attach()
                                    .DefineLoadBalancingRule(httpsLoadBalancingRule)
                                    .WithProtocol(TransportProtocol.Tcp)
                                    .WithFrontend(frontendName)
                                    .WithFrontendPort(443)
                                    .WithProbe(httpsProbe)
                                    .WithBackend(backendPoolName2)
                                    .Attach()

                                    // Add nat pools to enable direct VM connectivity for
                                    //  SSH to port 22 and TELNET to port 23
                                    .DefineInboundNatPool(natPool50XXto22)
                                    .WithProtocol(TransportProtocol.Tcp)
                                    .WithFrontend(frontendName)
                                    .WithFrontendPortRange(5000, 5099)
                                    .WithBackendPort(22)
                                    .Attach()
                                    .DefineInboundNatPool(natPool60XXto23)
                                    .WithProtocol(TransportProtocol.Tcp)
                                    .WithFrontend(frontendName)
                                    .WithFrontendPortRange(6000, 6099)
                                    .WithBackendPort(23)
                                    .Attach()
                                    .Create();

                // Print load balancer details
                Utilities.Log("Created a load balancer");
                Utilities.PrintLoadBalancer(loadBalancer1);

                //=============================================================
                // Create a virtual machine scale set with three virtual machines
                // And, install Apache Web servers on them

                Utilities.Log("Creating virtual machine scale set with three virtual machines"
                              + "inthe frontend subnet ...");

                var t1 = new DateTime();

                var virtualMachineScaleSet = azure.VirtualMachineScaleSets.Define(vmssName)
                                             .WithRegion(region)
                                             .WithExistingResourceGroup(rgName)
                                             .WithSku(VirtualMachineScaleSetSkuTypes.StandardD3v2)
                                             .WithExistingPrimaryNetworkSubnet(network, "Front-end")
                                             .WithExistingPrimaryInternetFacingLoadBalancer(loadBalancer1)
                                             .WithPrimaryInternetFacingLoadBalancerBackends(backendPoolName1, backendPoolName2)
                                             .WithPrimaryInternetFacingLoadBalancerInboundNatPools(natPool50XXto22, natPool60XXto23)
                                             .WithoutPrimaryInternalLoadBalancer()
                                             .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                             .WithRootUsername(userName)
                                             .WithSsh(sshKey)
                                             .WithNewDataDisk(100)
                                             .WithNewDataDisk(100, 1, CachingTypes.ReadWrite)
                                             .WithNewDataDisk(100, 2, CachingTypes.ReadWrite, StorageAccountTypes.StandardLRS)
                                             .WithCapacity(3)
                                             // Use a VM extension to install Apache Web servers
                                             .DefineNewExtension("CustomScriptForLinux")
                                             .WithPublisher("Microsoft.OSTCExtensions")
                                             .WithType("CustomScriptForLinux")
                                             .WithVersion("1.4")
                                             .WithMinorVersionAutoUpgrade()
                                             .WithPublicSetting("fileUris", fileUris)
                                             .WithPublicSetting("commandToExecute", installCommand)
                                             .Attach()
                                             .Create();

                var t2 = new DateTime();
                Utilities.Log("Created a virtual machine scale set with "
                              + "3 Linux VMs & Apache Web servers on them: (took "
                              + (t2 - t1).TotalSeconds + " seconds) ");
                Utilities.Log();

                // Print virtual machine scale set details
                // Utilities.Print(virtualMachineScaleSet);

                //=============================================================
                // List virtual machine scale set network interfaces

                Utilities.Log("Listing scale set network interfaces ...");
                var vmssNics = virtualMachineScaleSet.ListNetworkInterfaces();
                foreach (var vmssNic in vmssNics)
                {
                    Utilities.Log(vmssNic.Id);
                }

                //=============================================================
                // List virtual machine scale set instance network interfaces and SSH connection string

                Utilities.Log("Listing scale set virtual machine instance network interfaces and SSH connection string...");
                foreach (var instance in virtualMachineScaleSet.VirtualMachines.List())
                {
                    Utilities.Log("Scale set virtual machine instance #" + instance.InstanceId);
                    Utilities.Log(instance.Id);
                    var networkInterfaces = instance.ListNetworkInterfaces();
                    // Pick the first NIC
                    var networkInterface = networkInterfaces.ElementAt(0);
                    foreach (var ipConfig in networkInterface.IPConfigurations.Values)
                    {
                        if (ipConfig.IsPrimary)
                        {
                            var natRules = ipConfig.ListAssociatedLoadBalancerInboundNatRules();
                            foreach (var natRule in natRules)
                            {
                                if (natRule.BackendPort == 22)
                                {
                                    Utilities.Log("SSH connection string: " + userName + "@" + publicIPAddress.Fqdn + ":" + natRule.FrontendPort);
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }

                //=============================================================
                // Stop the virtual machine scale set

                Utilities.Log("Stopping virtual machine scale set ...");
                virtualMachineScaleSet.PowerOff();
                Utilities.Log("Stopped virtual machine scale set");

                //=============================================================
                // Deallocate the virtual machine scale set

                Utilities.Log("De-allocating virtual machine scale set ...");
                virtualMachineScaleSet.Deallocate();
                Utilities.Log("De-allocated virtual machine scale set");

                //=============================================================
                // Update the virtual machine scale set by removing and adding disk

                Utilities.Log("Updating virtual machine scale set managed data disks...");
                virtualMachineScaleSet.Update()
                .WithoutDataDisk(0)
                .WithoutDataDisk(200)
                .Apply();
                Utilities.Log("Updated virtual machine scale set");

                //=============================================================
                // Start the virtual machine scale set

                Utilities.Log("Starting virtual machine scale set ...");
                virtualMachineScaleSet.Start();
                Utilities.Log("Started virtual machine scale set");

                //=============================================================
                // Update the virtual machine scale set
                // - double the no. of virtual machines

                Utilities.Log("Updating virtual machine scale set "
                              + "- double the no. of virtual machines ...");

                virtualMachineScaleSet.Update()
                .WithCapacity(6)
                .Apply();

                Utilities.Log("Doubled the no. of virtual machinesin"
                              + "the virtual machine scale set");

                //=============================================================
                // re-start virtual machine scale set

                Utilities.Log("re-starting virtual machine scale set ...");
                virtualMachineScaleSet.Restart();
                Utilities.Log("re-started virtual machine scale set");
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.DeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resourcesinAzure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #22
0
 protected override ISupportsGettingByResourceGroup <IWebApp> GetResourceCollection(IAzure azure) => azure.AppServices.WebApps;
コード例 #23
0
        public static async Task <JObject> StartAndStopVMsCore(HttpRequest req, Logging logging)
        {
            string  requestBody     = await new StreamReader(req.Body).ReadToEndAsync();
            JObject data            = JsonConvert.DeserializeObject <JObject>(requestBody);
            string  _TaskInstanceId = data["TaskInstanceId"].ToString();
            string  _ExecutionUid   = data["ExecutionUid"].ToString();

            try
            {
                logging.LogInformation("StartAndStopVMs function processed a request.");
                string Subscription    = data["Target"]["SubscriptionUid"].ToString();
                string VmName          = data["Target"]["VMname"].ToString();
                string VmResourceGroup = data["Target"]["ResourceGroup"].ToString();
                string VmAction        = data["Target"]["Action"].ToString();

                Microsoft.Azure.Management.Fluent.Azure.IAuthenticated azureAuth = Microsoft.Azure.Management.Fluent.Azure.Configure().WithLogLevel(HttpLoggingDelegatingHandler.Level.BodyAndHeaders).Authenticate(Shared.Azure.AzureSDK.GetAzureCreds(Shared.GlobalConfigs.GetBoolConfig("UseMSI")));
                IAzure azure = azureAuth.WithSubscription(Subscription);
                logging.LogInformation("Selected subscription: " + azure.SubscriptionId);
                IVirtualMachine vm = azure.VirtualMachines.GetByResourceGroup(VmResourceGroup, VmName);
                if (vm.PowerState == Microsoft.Azure.Management.Compute.Fluent.PowerState.Deallocated && VmAction.ToLower() == "start")
                {
                    logging.LogInformation("VM State is: " + vm.PowerState.Value.ToString());
                    vm.StartAsync().Wait(5000);
                    logging.LogInformation("VM Start Initiated: " + vm.Name);
                }

                if (vm.PowerState != Microsoft.Azure.Management.Compute.Fluent.PowerState.Deallocated && VmAction.ToLower() == "stop")
                {
                    logging.LogInformation("VM State is: " + vm.PowerState.Value.ToString());
                    vm.DeallocateAsync().Wait(5000);
                    logging.LogInformation("VM Stop Initiated: " + vm.Name);
                }

                JObject Root = new JObject {
                    ["Result"] = "Complete"
                };

                TaskMetaDataDatabase TMD = new TaskMetaDataDatabase();
                if (VmName != null)
                {
                    Root["Result"] = "Complete";
                }
                else
                {
                    Root["Result"] = "Please pass a name, resourcegroup and action to request body";
                    TMD.LogTaskInstanceCompletion(System.Convert.ToInt64(_TaskInstanceId), System.Guid.Parse(_ExecutionUid), TaskMetaData.BaseTasks.TaskStatus.FailedRetry, System.Guid.Empty, "Task missing VMname, ResourceGroup or SubscriptionUid in Target element.");
                    return(Root);
                }
                //Update Task Instance

                TMD.LogTaskInstanceCompletion(System.Convert.ToInt64(_TaskInstanceId), System.Guid.Parse(_ExecutionUid), TaskMetaData.BaseTasks.TaskStatus.Complete, System.Guid.Empty, "");

                return(Root);
            }
            catch (System.Exception TaskException)
            {
                logging.LogErrors(TaskException);
                TaskMetaDataDatabase TMD = new TaskMetaDataDatabase();
                TMD.LogTaskInstanceCompletion(System.Convert.ToInt64(_TaskInstanceId), System.Guid.Parse(_ExecutionUid), TaskMetaData.BaseTasks.TaskStatus.FailedRetry, System.Guid.Empty, "Failed when trying to start or stop VM");

                JObject Root = new JObject
                {
                    ["Result"] = "Failed"
                };

                return(Root);
            }
        }
コード例 #24
0
 protected override ISupportsGettingByResourceGroup <IStorageAccount> GetResourceCollection(IAzure azure) => azure.StorageAccounts;
コード例 #25
0
 public void Init(ISubscription subscription)
 {
     _azure = _authenticated
              .WithSubscription(subscription.SubscriptionId);
 }
コード例 #26
0
        /**
         * An Azure Container Services sample for managing a container service with Docker Swarm orchestration.
         *    - Create an Azure Container Service with Docker Swarm orchestration
         *    - Create a SSH private/public key
         *    - Update the number of agent virtual machines in an Azure Container Service
         */
        public static void RunSample(IAzure azure, string clientId, string secret)
        {
            string rgName       = SdkContext.RandomResourceName("rgACS", 15);
            string acsName      = SdkContext.RandomResourceName("acssample", 30);
            Region region       = Region.USEast;
            string rootUserName = "******";
            string sshPublicKey = // replace with a real SSH public key
                                  "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCyhPdNuJUmTeLsaZL83vARuSVlN5qbKs7j"
                                  + "Cm723fqH85rIRQHgwEUXJbENEgZT0cXEgz4h36bQLMYT3/30fRnxYl8U6gRn27zFiMwaDstOjc9EofStODbiHx9A"
                                  + "Y1XYStjegdf+LNa5tmRv8dZEdj47XDxosSG3JKHpSuf0fXr4u7NjgAxdYOxyMSPAEcfXQctA+ybHkGDLdjLHT7q5C"
                                  + "4RXlQT7S9v5z532C3KuUSQW7n3QBP3xw/bC8aKcJafwZUYjYnw7owkBnv4TsZVva2le7maYkrtLH6w+XbhfHY4WwK"
                                  + "Y2Xxl1TxSGkb8tDsa6XgTmGfAKcDpnIe0DASJD8wFF [email protected]";
            string servicePrincipalClientId = clientId; // replace with a real service principal client id
            string servicePrincipalSecret   = secret;   // and corresponding secret

            try
            {
                //=============================================================
                // ...
                //=============================================================
                // If service principal client id and secret are not set via the local variables, attempt to read the service
                //     principal client id and secret from a secondary ".azureauth" file set through an environment variable.
                //
                //     If the environment variable was not set then reuse the main service principal set for running this sample.

                if (String.IsNullOrWhiteSpace(servicePrincipalClientId) || String.IsNullOrWhiteSpace(servicePrincipalSecret))
                {
                    string envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION_2");

                    if (String.IsNullOrWhiteSpace(envSecondaryServicePrincipal) || !File.Exists(envSecondaryServicePrincipal))
                    {
                        envSecondaryServicePrincipal = Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION");
                    }

                    servicePrincipalClientId = Utilities.GetSecondaryServicePrincipalClientID(envSecondaryServicePrincipal);
                    servicePrincipalSecret   = Utilities.GetSecondaryServicePrincipalSecret(envSecondaryServicePrincipal);
                }

                //=============================================================
                // Create an Azure Container Service with Kubernetes orchestration

                Utilities.Log("Creating an Azure Container Service with Kubernetes ochestration and one agent (virtual machine)");

                IContainerService azureContainerService = azure.ContainerServices.Define(acsName)
                                                          .WithRegion(region)
                                                          .WithNewResourceGroup(rgName)
                                                          .WithKubernetesOrchestration()
                                                          .WithServicePrincipal(servicePrincipalClientId, servicePrincipalSecret)
                                                          .WithLinux()
                                                          .WithRootUsername(rootUserName)
                                                          .WithSshKey(sshPublicKey)
                                                          .WithMasterNodeCount(ContainerServiceMasterProfileCount.MIN)
                                                          .WithMasterLeafDomainLabel("dns-" + acsName)
                                                          .DefineAgentPool("agentpool")
                                                          .WithVMCount(1)
                                                          .WithVMSize(ContainerServiceVMSizeTypes.StandardD1V2)
                                                          .WithLeafDomainLabel("dns-ap-" + acsName)
                                                          .Attach()
                                                          .Create();

                Utilities.Log("Created Azure Container Service: " + azureContainerService.Id);
                Utilities.Print(azureContainerService);

                //=============================================================
                // Update a Docker Swarm Azure Container Service with two agents (virtual machines)

                Utilities.Log("Updating a Kubernetes Azure Container Service with two agents (virtual machines)");

                azureContainerService.Update()
                .WithAgentVMCount(2)
                .Apply();

                Utilities.Log("Updated Azure Container Service: " + azureContainerService.Id);
                Utilities.Print(azureContainerService);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception g)
                {
                    Utilities.Log(g);
                }
            }
        }
コード例 #27
0
        /**
         * Azure Batch AI sample.
         *  - Create Storage account and Azure file share
         *  - Upload sample data to Azure file share
         *  - Create a workspace and experiment
         *  - Create Batch AI cluster that uses Azure file share to host the training data and scripts for the learning job
         *  - Create Microsoft Cognitive Toolkit job to run on the cluster
         *  - Wait for job to complete
         *  - Get output files
         *
         * Please note: in order to run this sample, please download and unzip sample package from <a href="https://batchaisamples.blob.core.windows.net/samples/BatchAIQuickStart.zip?st=2017-09-29T18%3A29%3A00Z&se=2099-12-31T08%3A00%3A00Z&sp=rl&sv=2016-05-31&sr=b&sig=hrAZfbZC%2BQ%2FKccFQZ7OC4b%2FXSzCF5Myi4Cj%2BW3sVZDo%3D">here</a>
         * Export path to the content to $SAMPLE_DATA_PATH.
         */
        public static void RunSample(IAzure azure)
        {
            string saName         = SdkContext.RandomResourceName("sa", 10);
            string rgName         = SdkContext.RandomResourceName("rg", 10);
            string workspaceName  = SdkContext.RandomResourceName("ws", 10);
            string experimentName = SdkContext.RandomResourceName("exp", 10);
            string sampleDataPath = Environment.GetEnvironmentVariable("SAMPLE_DATA_PATH");
            Region region         = Region.USWest2;
            string shareName      = SdkContext.RandomResourceName("fs", 20);
            string clusterName    = SdkContext.RandomResourceName("cluster", 15);
            string userName       = Utilities.CreateUsername();
            string sharePath      = "mnistcntksample";

            try
            {
                //=============================================================
                // Create a new storage account and an Azure file share resource
                Utilities.Log("Creating a storage account...");
                IStorageAccount storageAccount = azure.StorageAccounts.Define(saName)
                                                 .WithRegion(region)
                                                 .WithNewResourceGroup(rgName)
                                                 .Create();
                Utilities.Log("Created storage account.");
                Utilities.PrintStorageAccount(storageAccount);

                StorageAccountKey storageAccountKey = storageAccount.GetKeys().First();

                Utilities.Log("Creating Azure File share...");
                var cloudFileShare = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={saName};AccountKey={storageAccountKey.Value};EndpointSuffix=core.windows.net")
                                     .CreateCloudFileClient()
                                     .GetShareReference(shareName);
                cloudFileShare.CreateAsync().GetAwaiter().GetResult();
                Utilities.Log("Created Azure File share.");

                //=============================================================
                // Upload sample data to Azure file share

                //Get a reference to the root directory for the share.
                CloudFileDirectory rootDir = cloudFileShare.GetRootDirectoryReference();

                //Get a reference to the sampledir directory
                Utilities.Log("Creating directory and uploading data files...");
                CloudFileDirectory sampleDir = rootDir.GetDirectoryReference(sharePath);
                sampleDir.CreateAsync().GetAwaiter().GetResult();

                rootDir.GetFileReference("Train-28x28_cntk_text.txt").UploadFromFileAsync(sampleDataPath + "/Train-28x28_cntk_text.txt").GetAwaiter().GetResult();
                rootDir.GetFileReference("Test-28x28_cntk_text.txt").UploadFromFileAsync(sampleDataPath + "/Test-28x28_cntk_text.txt").GetAwaiter().GetResult();
                rootDir.GetFileReference("ConvNet_MNIST.py").UploadFromFileAsync(sampleDataPath + "/ConvNet_MNIST.py").GetAwaiter().GetResult();
                Utilities.Log("Data files uploaded.");
                //=============================================================
                // Create a workspace and experiment
                IBatchAIWorkspace workspace = azure.BatchAIWorkspaces.Define(workspaceName)
                                              .WithRegion(region)
                                              .WithNewResourceGroup(rgName)
                                              .Create();
                IBatchAIExperiment experiment = workspace.CreateExperiment(experimentName);


                //=============================================================
                // Create Batch AI cluster that uses Azure file share to host the training data and scripts for the learning job
                Utilities.Log("Creating Batch AI cluster...");
                IBatchAICluster cluster = workspace.Clusters.Define(clusterName)
                                          .WithVMSize(VirtualMachineSizeTypes.StandardNC6.Value)
                                          .WithUserName(userName)
                                          .WithPassword("MyPassword")
                                          .WithAutoScale(0, 2)
                                          .DefineAzureFileShare()
                                          .WithStorageAccountName(saName)
                                          .WithAzureFileUrl(cloudFileShare.Uri.ToString())
                                          .WithRelativeMountPath("azurefileshare")
                                          .WithAccountKey(storageAccountKey.Value)
                                          .Attach()
                                          .Create();
                Utilities.Log("Created Batch AI cluster.");
                Utilities.Print(cluster);

                // =============================================================
                // Create Microsoft Cognitive Toolkit job to run on the cluster
                Utilities.Log("Creating Batch AI job...");
                IBatchAIJob job = experiment.Jobs.Define("myJob")
                                  .WithExistingCluster(cluster)
                                  .WithNodeCount(1)
                                  .WithStdOutErrPathPrefix("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare")
                                  .DefineCognitiveToolkit()
                                  .WithPythonScriptFile("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/ConvNet_MNIST.py")
                                  .WithCommandLineArgs("$AZ_BATCHAI_MOUNT_ROOT/azurefileshare $AZ_BATCHAI_OUTPUT_MODEL")
                                  .Attach()
                                  .WithOutputDirectory("MODEL", "$AZ_BATCHAI_MOUNT_ROOT/azurefileshare/model")
                                  .WithContainerImage("microsoft/cntk:2.1-gpu-python3.5-cuda8.0-cudnn6.0")
                                  .Create();
                Utilities.Log("Created Batch AI job.");
                Utilities.Print(job);

                // =============================================================
                // Wait for job results

                // Wait for job to start running
                Utilities.Log("Waiting for Batch AI job to start running...");
                while (ExecutionState.Queued.Equals(job.ExecutionState))
                {
                    SdkContext.DelayProvider.Delay(5000);
                    job.Refresh();
                }

                // Wait for job to complete and job output to become available
                Utilities.Log("Waiting for Batch AI job to complete...");
                while (!(ExecutionState.Succeeded.Equals(job.ExecutionState) || ExecutionState.Failed.Equals(job.ExecutionState)))
                {
                    SdkContext.DelayProvider.Delay(5000);
                    job.Refresh();
                }

                // =============================================================
                // Get output files

                // Print stdout and stderr
                foreach (var outputFile in job.ListFiles("stdouterr"))
                {
                    Utilities.Log(Utilities.CheckAddress(outputFile.DownloadUrl));
                }
                // List model output files
                foreach (var outputFile in job.ListFiles("MODEL"))
                {
                    Utilities.Log(outputFile.DownloadUrl);
                }
            }
            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");
                }
            }
        }
コード例 #28
0
 public override Task ResolveResourceAsync(IAzure azure, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(base.ResolveResourceAsync(azure.NetworkSecurityGroups, cancellationToken));
 }
コード例 #29
0
        /**
         * Azure Storage sample for managing storage accounts -
         *  - Create a storage account
         *  - Get | regenerate storage account access keys
         *  - Create another storage account
         *  - List storage accounts
         *  - Delete a storage account.
         */
        public async static Task RunSampleAsync(IAzure azure)
        {
            string rgName              = SdkContext.RandomResourceName("rgSTMS", 20);
            string storageAccountName  = SdkContext.RandomResourceName("sa", 20);
            string storageAccountName2 = SdkContext.RandomResourceName("sa2", 20);

            try
            {
                // ============================================================
                // Create a storage account

                Utilities.Log("Creating a Storage Account");

                var storageAccount = await azure.StorageAccounts.Define(storageAccountName)
                                     .WithRegion(Region.USEast)
                                     .WithNewResourceGroup(rgName)
                                     .CreateAsync();

                Utilities.Log("Created a Storage Account:");
                Utilities.PrintStorageAccount(storageAccount);

                // ============================================================
                // Create another storage account

                Utilities.Log("Creating a 2nd Storage Account");

                var storageAccount2 = await azure.StorageAccounts.Define(storageAccountName2)
                                      .WithRegion(Region.USEast)
                                      .WithNewResourceGroup(rgName)
                                      .CreateAsync();

                Utilities.Log("Created a Storage Account:");
                Utilities.PrintStorageAccount(storageAccount2);

                // ============================================================
                // List storage accounts

                Utilities.Log("Listing storage accounts");

                var storageAccounts = azure.StorageAccounts;

                var accounts = await storageAccounts.ListByResourceGroupAsync(rgName);

                foreach (var account in accounts)
                {
                    Utilities.Log($"Storage Account {account.Name} created @ {account.CreationTime}");
                    // ============================================================
                    // Get | regenerate storage account access keys

                    Utilities.Log("Getting storage account access keys");
                    var storageAccountKeys = await storageAccount.GetKeysAsync();

                    Utilities.PrintStorageAccountKeys(storageAccountKeys);

                    Utilities.Log("Regenerating first storage account access key");
                    storageAccountKeys = await storageAccount.RegenerateKeyAsync(storageAccountKeys[0].KeyName);

                    Utilities.PrintStorageAccountKeys(storageAccountKeys);
                }

                // ============================================================
                // Delete a storage account

                Utilities.Log($"Deleting a storage account - {accounts.ElementAt(0).Name} created @ {accounts.ElementAt(0).CreationTime}");

                await azure.StorageAccounts.DeleteByIdAsync(accounts.ElementAt(0).Id);

                Utilities.Log("Deleted storage account");
            }
            finally
            {
                if (azure.ResourceGroups.GetByName(rgName) != null)
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    await azure.ResourceGroups.DeleteByNameAsync(rgName);

                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                else
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
            }
        }
コード例 #30
0
ファイル: Table.cs プロジェクト: christianblunden/Simple.Data
 public Table(IAzure azure, string tableName) : this(azure, tableName, IfTableDoesNotExist.ThrowAnException) { }
コード例 #31
0
        /**
         * Create a virtual network with two Subnets – frontend and backend
         * Frontend allows HTTP in and denies Internet out
         * Backend denies Internet in and Internet out
         * Create m Linux virtual machines in the frontend
         * Create m Windows virtual machines in the backend.
         */
        public static void RunSample(IAzure azure)
        {
            string rgName             = SdkContext.RandomResourceName("rgNEPP", 24);
            string frontEndNSGName    = SdkContext.RandomResourceName("fensg", 24);
            string backEndNSGName     = SdkContext.RandomResourceName("bensg", 24);
            string networkName        = SdkContext.RandomResourceName("vnetCOMV", 24);
            string storageAccountName = SdkContext.RandomResourceName("stgCOMV", 20);

            try
            {
                // Create a resource group [Where all resources gets created]
                IResourceGroup resourceGroup = azure.ResourceGroups
                                               .Define(rgName)
                                               .WithRegion(Region.USEast)
                                               .Create();

                //============================================================
                // Define a network security group for the front end of a subnet
                // front end subnet contains two rules
                // - ALLOW-SSH - allows SSH traffic into the front end subnet
                // - ALLOW-WEB- allows HTTP traffic into the front end subnet

                var frontEndNSGCreatable = azure.NetworkSecurityGroups
                                           .Define(frontEndNSGName)
                                           .WithRegion(Region.USEast)
                                           .WithExistingResourceGroup(resourceGroup)
                                           .DefineRule("ALLOW-SSH")
                                           .AllowInbound()
                                           .FromAnyAddress()
                                           .FromAnyPort()
                                           .ToAnyAddress()
                                           .ToPort(22)
                                           .WithProtocol(SecurityRuleProtocol.Tcp)
                                           .WithPriority(100)
                                           .WithDescription("Allow SSH")
                                           .Attach()
                                           .DefineRule("ALLOW-HTTP")
                                           .AllowInbound()
                                           .FromAnyAddress()
                                           .FromAnyPort()
                                           .ToAnyAddress()
                                           .ToPort(80)
                                           .WithProtocol(SecurityRuleProtocol.Tcp)
                                           .WithPriority(101)
                                           .WithDescription("Allow HTTP")
                                           .Attach();

                //============================================================
                // Define a network security group for the back end of a subnet
                // back end subnet contains two rules
                // - ALLOW-SQL - allows SQL traffic only from the front end subnet
                // - DENY-WEB - denies all outbound internet traffic from the back end subnet

                var backEndNSGCreatable = azure.NetworkSecurityGroups
                                          .Define(backEndNSGName)
                                          .WithRegion(Region.USEast)
                                          .WithExistingResourceGroup(resourceGroup)
                                          .DefineRule("ALLOW-SQL")
                                          .AllowInbound()
                                          .FromAddress("172.16.1.0/24")
                                          .FromAnyPort()
                                          .ToAnyAddress()
                                          .ToPort(1433)
                                          .WithProtocol(SecurityRuleProtocol.Tcp)
                                          .WithPriority(100)
                                          .WithDescription("Allow SQL")
                                          .Attach()
                                          .DefineRule("DENY-WEB")
                                          .DenyOutbound()
                                          .FromAnyAddress()
                                          .FromAnyPort()
                                          .ToAnyAddress()
                                          .ToAnyPort()
                                          .WithAnyProtocol()
                                          .WithDescription("Deny Web")
                                          .WithPriority(200)
                                          .Attach();

                Utilities.Log("Creating a security group for the front ends - allows SSH and HTTP");
                Utilities.Log("Creating a security group for the back ends - allows SSH and denies all outbound internet traffic");

                var networkSecurityGroups = azure.NetworkSecurityGroups
                                            .Create(frontEndNSGCreatable, backEndNSGCreatable);

                INetworkSecurityGroup frontendNSG = networkSecurityGroups.First(n => n.Name.Equals(frontEndNSGName, StringComparison.OrdinalIgnoreCase));
                INetworkSecurityGroup backendNSG  = networkSecurityGroups.First(n => n.Name.Equals(backEndNSGName, StringComparison.OrdinalIgnoreCase));

                Utilities.Log("Created a security group for the front end: " + frontendNSG.Id);
                Utilities.PrintNetworkSecurityGroup(frontendNSG);

                Utilities.Log("Created a security group for the back end: " + backendNSG.Id);
                Utilities.PrintNetworkSecurityGroup(backendNSG);

                // Create Network [Where all the virtual machines get added to]
                var network = azure.Networks
                              .Define(networkName)
                              .WithRegion(Region.USEast)
                              .WithExistingResourceGroup(resourceGroup)
                              .WithAddressSpace("172.16.0.0/16")
                              .DefineSubnet("Front-end")
                              .WithAddressPrefix("172.16.1.0/24")
                              .WithExistingNetworkSecurityGroup(frontendNSG)
                              .Attach()
                              .DefineSubnet("Back-end")
                              .WithAddressPrefix("172.16.2.0/24")
                              .WithExistingNetworkSecurityGroup(backendNSG)
                              .Attach()
                              .Create();

                // Prepare Creatable Storage account definition [For storing VMs disk]
                var creatableStorageAccount = azure.StorageAccounts
                                              .Define(storageAccountName)
                                              .WithRegion(Region.USEast)
                                              .WithExistingResourceGroup(resourceGroup);

                // Prepare a batch of Creatable Virtual Machines definitions
                List <ICreatable <IVirtualMachine> > frontendCreatableVirtualMachines = new List <ICreatable <IVirtualMachine> >();

                for (int i = 0; i < FrontendVMCount; i++)
                {
                    var creatableVirtualMachine = azure.VirtualMachines
                                                  .Define("VM-FE-" + i)
                                                  .WithRegion(Region.USEast)
                                                  .WithExistingResourceGroup(resourceGroup)
                                                  .WithExistingPrimaryNetwork(network)
                                                  .WithSubnet("Front-end")
                                                  .WithPrimaryPrivateIPAddressDynamic()
                                                  .WithoutPrimaryPublicIPAddress()
                                                  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                                  .WithRootUsername(UserName)
                                                  .WithRootPassword(Password)
                                                  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                                                  .WithNewStorageAccount(creatableStorageAccount);
                    frontendCreatableVirtualMachines.Add(creatableVirtualMachine);
                }

                List <ICreatable <IVirtualMachine> > backendCreatableVirtualMachines = new List <ICreatable <IVirtualMachine> >();

                for (int i = 0; i < BackendVMCount; i++)
                {
                    var creatableVirtualMachine = azure.VirtualMachines
                                                  .Define("VM-BE-" + i)
                                                  .WithRegion(Region.USEast)
                                                  .WithExistingResourceGroup(resourceGroup)
                                                  .WithExistingPrimaryNetwork(network)
                                                  .WithSubnet("Back-end")
                                                  .WithPrimaryPrivateIPAddressDynamic()
                                                  .WithoutPrimaryPublicIPAddress()
                                                  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                                  .WithRootUsername(UserName)
                                                  .WithRootPassword(Password)
                                                  .WithSize(VirtualMachineSizeTypes.StandardD3V2)
                                                  .WithNewStorageAccount(creatableStorageAccount);
                    backendCreatableVirtualMachines.Add(creatableVirtualMachine);
                }

                var startTime = DateTimeOffset.Now.UtcDateTime;
                Utilities.Log("Creating the virtual machines");

                List <ICreatable <IVirtualMachine> > allCreatableVirtualMachines = new List <ICreatable <IVirtualMachine> >();
                allCreatableVirtualMachines.AddRange(frontendCreatableVirtualMachines);
                allCreatableVirtualMachines.AddRange(backendCreatableVirtualMachines);

                var virtualMachines = azure.VirtualMachines.Create(allCreatableVirtualMachines.ToArray());

                var endTime = DateTimeOffset.Now.UtcDateTime;
                Utilities.Log("Created virtual machines");

                foreach (var virtualMachine in virtualMachines)
                {
                    Utilities.Log(virtualMachine.Id);
                }

                Utilities.Log($"Virtual machines create: took {(endTime - startTime).TotalSeconds } seconds");
            }
            finally
            {
                Utilities.Log($"Deleting resource group : {rgName}");
                azure.ResourceGroups.DeleteByName(rgName);
                Utilities.Log($"Deleted resource group : {rgName}");
            }
        }
コード例 #32
0
 public TableService(IAzure azure)
 {
     this._azure = azure;
 }
コード例 #33
0
 internal async Task <bool> UpdateVersionAsync(string requiredVersion, string sourceUrl, InstanceName instance, IAzure azure, CancellationToken cancellationToken)
 {
     if (string.IsNullOrWhiteSpace(sourceUrl))
     {
         return(await UpdateVersionFromGitHubAsync(requiredVersion, instance, azure, cancellationToken));
     }
     else
     {
         return(await UpdateVersionFromUrlAsync(sourceUrl, instance, azure, cancellationToken));
     }
 }