/**
         * Azure App Service basic sample for managing web apps.
         *  - Create a storage account and upload a couple blobs
         *  - Create a web app that contains the connection string to the storage account
         *  - Deploy a Tomcat application that reads from the storage account
         *  - Clean up
         */
        public static void RunSample(IAzure azure)
        {
            string app1Name      = SdkContext.RandomResourceName("webapp1-", 20);
            string app1Url       = app1Name + SUFFIX;
            string storageName   = SdkContext.RandomResourceName("jsdkstore", 20);
            string containerName = SdkContext.RandomResourceName("jcontainer", 20);
            string rgName        = SdkContext.RandomResourceName("rg1NEMV_", 24);

            try {
                //============================================================
                // Create a storage account for the web app to use

                Utilities.Log("Creating storage account " + storageName + "...");

                IStorageAccount storageAccount = azure.StorageAccounts.Define(storageName)
                                                 .WithRegion(Region.USWest)
                                                 .WithNewResourceGroup(rgName)
                                                 .Create();

                string accountKey = storageAccount.GetKeys()[0].Value;

                string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}",
                                                        storageAccount.Name, accountKey);

                Utilities.Log("Created storage account " + storageAccount.Name);

                //============================================================
                // Upload a few files to the storage account blobs

                Utilities.Log("Uploading 2 blobs to container " + containerName + "...");

                Utilities.UploadFilesToContainer(
                    connectionString,
                    containerName,
                    new[]
                {
                    Path.Combine(Utilities.ProjectPath, "Asset", "helloworld.war"),
                    Path.Combine(Utilities.ProjectPath, "Asset", "install_apache.Sh")
                });

                Utilities.Log("Uploaded 2 blobs to container " + containerName);

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

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

                IWebApp app1 = azure.WebApps.Define(app1Name)
                               .WithRegion(Region.USWest)
                               .WithExistingResourceGroup(rgName)
                               .WithNewLinuxPlan(PricingTier.StandardS1)
                               .WithPublicDockerHubImage("tomcat:8-jre8")
                               .WithStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"")
                               .WithConnectionString("storage.connectionString", connectionString, ConnectionStringType.Custom)
                               .WithAppSetting("storage.containerName", containerName)
                               .WithAppSetting("PORT", "8080")
                               .Create();

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

                //============================================================
                // Deploy a web app that connects to the storage account
                // Source code: https://github.com/jianghaolu/azure-samples-blob-explorer

                Utilities.Log("Deploying azure-samples-blob-traverser.war to " + app1Name + " through FTP...");

                Utilities.UploadFileToWebApp(
                    app1.GetPublishingProfile(),
                    Path.Combine(Utilities.ProjectPath, "Asset", "azure-samples-blob-traverser.war"));

                Utilities.Log("Deployment azure-samples-blob-traverser.war to web app " + app1.Name + " completed");
                Utilities.Print(app1);

                // warm up
                Utilities.Log("Warming up " + app1Url + "/azure-samples-blob-traverser...");
                Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser");
                SdkContext.DelayProvider.Delay(5000);
                Utilities.Log("CURLing " + app1Url + "/azure-samples-blob-traverser...");
                Utilities.Log(Utilities.CheckAddress("http://" + app1Url + "/azure-samples-blob-traverser"));
            }
            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);
                }
            }
        }
예제 #2
0
        private string getPrimaryStorageKey(string name)
        {
            IStorageAccount acct = this.GetAccount(name);

            return(acct.GetKeys()[0].Value);
        }
예제 #3
0
        /**
         * Azure Network sample for managing virtual network gateway.
         *  - Create 2 virtual networks with subnets and 2 virtual network gateways corresponding to each network
         *  - Create VPN VNet-to-VNet connection
         *  - Troubleshoot the connection
         *    - Create network watcher in the same region as virtual network gateway
         *    - Create storage account to store troubleshooting information
         *    - Run troubleshooting for the connection - result will be 'UnHealthy' as need to create symmetrical connection from second gateway to the first
         *  - Create virtual network connection from second gateway to the first and run troubleshooting. Result will be 'Healthy'.
         *  - List VPN Gateway connections for the first gateway
         *  - Create 2 virtual machines, each one in its network and verify connectivity between them
         */
        public static void RunSample(IAzure azure)
        {
            string rgName          = SdkContext.RandomResourceName("rg", 24);
            string vnetName        = SdkContext.RandomResourceName("vnet", 20);
            string vnet2Name       = SdkContext.RandomResourceName("vnet", 20);
            string vpnGatewayName  = SdkContext.RandomResourceName("vngw", 20);
            string vpnGateway2Name = SdkContext.RandomResourceName("vngw2", 20);
            string connectionName  = SdkContext.RandomResourceName("con", 20);
            string connection2Name = SdkContext.RandomResourceName("con2", 20);
            string nwName          = SdkContext.RandomResourceName("nw", 20);
            string vm1Name         = SdkContext.RandomResourceName("vm1", 20);
            string vm2Name         = SdkContext.RandomResourceName("vm2", 20);
            string rootname        = Utilities.CreateUsername();
            string password        = SdkContext.RandomResourceName("pWd!", 15);
            string containerName   = "results";

            try
            {
                //============================================================
                // Create virtual network
                Utilities.Log("Creating virtual network...");
                INetwork network1 = azure.Networks.Define(vnetName)
                                    .WithRegion(region)
                                    .WithNewResourceGroup(rgName)
                                    .WithAddressSpace("10.11.0.0/16")
                                    .WithSubnet("GatewaySubnet", "10.11.255.0/27")
                                    .WithSubnet("Subnet1", "10.11.0.0/24")
                                    .Create();
                Utilities.Log("Created network");
                // Print the virtual network
                Utilities.PrintVirtualNetwork(network1);

                //============================================================
                // Create virtual network gateway
                Utilities.Log("Creating virtual network gateway...");
                IVirtualNetworkGateway vngw1 = azure.VirtualNetworkGateways.Define(vpnGatewayName)
                                               .WithRegion(region)
                                               .WithNewResourceGroup(rgName)
                                               .WithExistingNetwork(network1)
                                               .WithRouteBasedVpn()
                                               .WithSku(VirtualNetworkGatewaySkuName.VpnGw1)
                                               .Create();
                Utilities.Log("Created virtual network gateway");

                //============================================================
                // Create second virtual network
                Utilities.Log("Creating virtual network...");
                INetwork network2 = azure.Networks.Define(vnet2Name)
                                    .WithRegion(region)
                                    .WithNewResourceGroup(rgName)
                                    .WithAddressSpace("10.41.0.0/16")
                                    .WithSubnet("GatewaySubnet", "10.41.255.0/27")
                                    .WithSubnet("Subnet2", "10.41.0.0/24")
                                    .Create();
                Utilities.Log("Created virtual network");

                //============================================================
                // Create second virtual network gateway
                Utilities.Log("Creating second virtual network gateway...");
                IVirtualNetworkGateway vngw2 = azure.VirtualNetworkGateways.Define(vpnGateway2Name)
                                               .WithRegion(region)
                                               .WithNewResourceGroup(rgName)
                                               .WithExistingNetwork(network2)
                                               .WithRouteBasedVpn()
                                               .WithSku(VirtualNetworkGatewaySkuName.VpnGw1)
                                               .Create();
                Utilities.Log("Created second virtual network gateway");

                //============================================================
                // Create virtual network gateway connection
                Utilities.Log("Creating virtual network gateway connection...");
                IVirtualNetworkGatewayConnection connection = vngw1.Connections
                                                              .Define(connectionName)
                                                              .WithVNetToVNet()
                                                              .WithSecondVirtualNetworkGateway(vngw2)
                                                              .WithSharedKey("MySecretKey")
                                                              .Create();
                Utilities.Log("Created virtual network gateway connection");

                //============================================================
                // Troubleshoot the connection

                // create Network Watcher
                INetworkWatcher nw = azure.NetworkWatchers.Define(nwName)
                                     .WithRegion(region)
                                     .WithExistingResourceGroup(rgName)
                                     .Create();
                // Create storage account to store troubleshooting information
                IStorageAccount storageAccount = azure.StorageAccounts.Define("sa" + SdkContext.RandomResourceName("", 8))
                                                 .WithRegion(region)
                                                 .WithExistingResourceGroup(rgName)
                                                 .Create();
                // Create storage container to store troubleshooting results
                string accountKey       = storageAccount.GetKeys()[0].Value;
                string connectionString = string.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccount.Name, accountKey);
                Utilities.CreateContainer(connectionString, containerName);

                // Run troubleshooting for the connection - result will be 'UnHealthy' as need to create symmetrical connection from second gateway to the first
                ITroubleshooting troubleshooting = nw.Troubleshoot()
                                                   .WithTargetResourceId(connection.Id)
                                                   .WithStorageAccount(storageAccount.Id)
                                                   .WithStoragePath(storageAccount.EndPoints.Primary.Blob + containerName)
                                                   .Execute();
                Utilities.Log("Troubleshooting status is: " + troubleshooting.Code);

                //============================================================
                //  Create virtual network connection from second gateway to the first and run troubleshooting. Result will be 'Healthy'.
                vngw2.Connections
                .Define(connection2Name)
                .WithVNetToVNet()
                .WithSecondVirtualNetworkGateway(vngw1)
                .WithSharedKey("MySecretKey")
                .Create();
                // Delay before running troubleshooting to wait for connection settings to propagate
                SdkContext.DelayProvider.Delay(250000);
                troubleshooting = nw.Troubleshoot()
                                  .WithTargetResourceId(connection.Id)
                                  .WithStorageAccount(storageAccount.Id)
                                  .WithStoragePath(storageAccount.EndPoints.Primary.Blob + containerName)
                                  .Execute();
                Utilities.Log("Troubleshooting status is: " + troubleshooting.Code);

                //============================================================
                // List VPN Gateway connections for particular gateway
                var connections = vngw1.ListConnections();
                foreach (var conn in connections)
                {
                    Utilities.Print(conn);
                }

                //============================================================
                // Create 2 virtual machines, each one in its network and verify connectivity between them
                List <ICreatable <IVirtualMachine> > vmDefinitions = new List <ICreatable <IVirtualMachine> >();

                vmDefinitions.Add(azure.VirtualMachines.Define(vm1Name)
                                  .WithRegion(region)
                                  .WithExistingResourceGroup(rgName)
                                  .WithExistingPrimaryNetwork(network1)
                                  .WithSubnet("Subnet1")
                                  .WithPrimaryPrivateIPAddressDynamic()
                                  .WithoutPrimaryPublicIPAddress()
                                  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                  .WithRootUsername(rootname)
                                  .WithRootPassword(password)
                                  // Extension currently needed for network watcher support
                                  .DefineNewExtension("networkWatcher")
                                  .WithPublisher("Microsoft.Azure.NetworkWatcher")
                                  .WithType("NetworkWatcherAgentLinux")
                                  .WithVersion("1.4")
                                  .Attach());
                vmDefinitions.Add(azure.VirtualMachines.Define(vm2Name)
                                  .WithRegion(region)
                                  .WithExistingResourceGroup(rgName)
                                  .WithExistingPrimaryNetwork(network2)
                                  .WithSubnet("Subnet2")
                                  .WithPrimaryPrivateIPAddressDynamic()
                                  .WithoutPrimaryPublicIPAddress()
                                  .WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
                                  .WithRootUsername(rootname)
                                  .WithRootPassword(password)
                                  // Extension currently needed for network watcher support
                                  .DefineNewExtension("networkWatcher")
                                  .WithPublisher("Microsoft.Azure.NetworkWatcher")
                                  .WithType("NetworkWatcherAgentLinux")
                                  .WithVersion("1.4")
                                  .Attach());
                ICreatedResources <IVirtualMachine> createdVMs = azure.VirtualMachines.Create(vmDefinitions);
                IVirtualMachine vm1 = createdVMs.FirstOrDefault(vm => vm.Key == vmDefinitions[0].Key);
                IVirtualMachine vm2 = createdVMs.FirstOrDefault(vm => vm.Key == vmDefinitions[1].Key);

                IConnectivityCheck connectivity = nw.CheckConnectivity()
                                                  .ToDestinationResourceId(vm2.Id)
                                                  .ToDestinationPort(22)
                                                  .FromSourceVirtualMachine(vm1.Id)
                                                  .Execute();
                Utilities.Log("Connectivity status: " + connectivity.ConnectionStatus);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                }
                catch (NullReferenceException)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
                catch (Exception ex)
                {
                    Utilities.Log(ex);
                }
            }
        }
        /**
         * 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
         */
        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");
                }
            }
        }
예제 #5
0
        public void CanCRUDFunctionApp()
        {
            using (var context = FluentMockContext.Start(this.GetType().FullName))
            {
                string GroupName1   = TestUtilities.GenerateName("javacsmrg");
                string GroupName2   = TestUtilities.GenerateName("javacsmrg");
                string WebAppName1  = TestUtilities.GenerateName("java-webapp-");
                string WebAppName2  = TestUtilities.GenerateName("java-webapp-");
                string WebAppName3  = TestUtilities.GenerateName("java-webapp-");
                string StorageName1 = TestUtilities.GenerateName("javast");
                if (StorageName1.Length >= 23)
                {
                    StorageName1 = StorageName1.Substring(0, 20);
                }
                StorageName1 = StorageName1.Replace("-", string.Empty);

                var appServiceManager = TestHelper.CreateAppServiceManager();

                try
                {
                    // Create with consumption plan
                    var functionApp1 = appServiceManager.FunctionApps.Define(WebAppName1)
                                       .WithRegion(Region.USWest)
                                       .WithNewResourceGroup(GroupName1)
                                       .Create();
                    Assert.NotNull(functionApp1);
                    Assert.Equal(Region.USWest, functionApp1.Region);
                    var plan1 = appServiceManager.AppServicePlans.GetById(functionApp1.AppServicePlanId);
                    Assert.NotNull(plan1);
                    Assert.Equal(Region.USWest, plan1.Region);
                    Assert.Equal(new PricingTier("Dynamic", "Y1"), plan1.PricingTier);

                    IStorageAccount storageAccount1 = getStorageAccount(appServiceManager.StorageManager, functionApp1,
                                                                        out IReadOnlyDictionary <string, IAppSetting> appSettings1, out StorageSettings storageSettings1);
                    // consumption plan requires this 2 settings
                    Assert.True(appSettings1.ContainsKey(KeyContentAzureFileConnectionString));
                    Assert.True(appSettings1.ContainsKey(KeyContentShare));
                    Assert.Equal(appSettings1[KeyAzureWebJobsStorage].Value, appSettings1[KeyContentAzureFileConnectionString].Value);
                    // verify accountKey
                    Assert.Equal(storageAccount1.GetKeys()[0].Value, storageSettings1.AccountKey);

                    // List functions of App1 before deployement
                    IEnumerable <IFunctionEnvelope> envelopes = functionApp1.ListFunctions();
                    Assert.Empty(envelopes);

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

                    // List functions of App1 after deployement
                    envelopes = functionApp1.ListFunctions();
                    Assert.Single(envelopes);

                    IPagedCollection <IFunctionEnvelope> envelopesFromAsync = Extensions.Synchronize(() => functionApp1.ListFunctionsAsync(true));
                    Assert.Single(envelopesFromAsync);

                    // Verify function envelope
                    IFunctionEnvelope envelope = envelopes.First();
                    Assert.NotEmpty(envelope.Id);
                    Assert.Equal(WebAppName1 + "/square", envelope.Name);
                    Assert.Equal("Microsoft.Web/sites/functions", envelope.Type);
                    Assert.Equal(Region.USWest, envelope.Region);
                    Assert.NotEmpty(envelope.ScriptRootPathHref);
                    Assert.NotEmpty(envelope.ScriptHref);
                    Assert.NotEmpty(envelope.ConfigHref);
                    Assert.NotEmpty(envelope.SecretsFileHref);
                    Assert.NotEmpty(envelope.Href);
                    Assert.NotNull(envelope.Config);

                    // Create in a new group with existing consumption plan
                    var functionApp2 = appServiceManager.FunctionApps.Define(WebAppName2)
                                       .WithExistingAppServicePlan(plan1)
                                       .WithNewResourceGroup(GroupName2)
                                       .WithExistingStorageAccount(functionApp1.StorageAccount)
                                       .Create();
                    Assert.NotNull(functionApp2);
                    Assert.Equal(Region.USWest, functionApp2.Region);

                    // Create with app service plan
                    var functionApp3 = appServiceManager.FunctionApps.Define(WebAppName3)
                                       .WithRegion(Region.USWest)
                                       .WithExistingResourceGroup(GroupName2)
                                       .WithNewAppServicePlan(PricingTier.BasicB1)
                                       .WithExistingStorageAccount(functionApp1.StorageAccount)
                                       .Create();
                    Assert.NotNull(functionApp3);
                    Assert.Equal(Region.USWest, functionApp3.Region);

                    IStorageAccount storageAccount3 = getStorageAccount(appServiceManager.StorageManager, functionApp3,
                                                                        out IReadOnlyDictionary <string, IAppSetting> appSettings3, out StorageSettings storageSettings3);
                    // app service plan does not have this 2 settings
                    // https://github.com/Azure/azure-libraries-for-net/issues/485
                    Assert.False(appSettings3.ContainsKey(KeyContentAzureFileConnectionString));
                    Assert.False(appSettings3.ContainsKey(KeyContentShare));
                    // verify accountKey
                    Assert.Equal(storageAccount3.GetKeys()[0].Value, storageSettings3.AccountKey);

                    // Get
                    var functionApp = appServiceManager.FunctionApps.GetByResourceGroup(GroupName1, functionApp1.Name);
                    Assert.Equal(functionApp1.Id, functionApp.Id);
                    functionApp = appServiceManager.FunctionApps.GetById(functionApp2.Id);
                    Assert.Equal(functionApp2.Name, functionApp.Name);

                    // List
                    var functionApps = appServiceManager.FunctionApps.ListByResourceGroup(GroupName1);
                    Assert.Single(functionApps);
                    functionApps = appServiceManager.FunctionApps.ListByResourceGroup(GroupName2);
                    Assert.Equal(2, functionApps.Count());

                    // Update
                    functionApp2.Update()
                    .WithNewStorageAccount(StorageName1, Microsoft.Azure.Management.Storage.Fluent.Models.SkuName.StandardGRS)
                    .Apply();
                    Assert.Equal(StorageName1, functionApp2.StorageAccount.Name);
                    IStorageAccount storageAccount2 = getStorageAccount(appServiceManager.StorageManager, functionApp2,
                                                                        out IReadOnlyDictionary <string, IAppSetting> appSettings2, out StorageSettings storageSettings2);
                    Assert.True(appSettings2.ContainsKey(KeyContentAzureFileConnectionString));
                    Assert.True(appSettings2.ContainsKey(KeyContentShare));
                    Assert.Equal(appSettings2[KeyAzureWebJobsStorage].Value, appSettings2[KeyContentAzureFileConnectionString].Value);
                    Assert.Equal(StorageName1, storageAccount2.Name);
                    Assert.Equal(storageAccount2.GetKeys()[0].Value, storageSettings2.AccountKey);

                    // Update, verify modify AppSetting does not create new storage account
                    // https://github.com/Azure/azure-libraries-for-net/issues/457
                    int numStorageAccountBefore = appServiceManager.StorageManager.StorageAccounts.ListByResourceGroup(GroupName1).Count();
                    functionApp1.Update()
                    .WithAppSetting("newKey", "newValue")
                    .Apply();
                    int numStorageAccountAfter = appServiceManager.StorageManager.StorageAccounts.ListByResourceGroup(GroupName1).Count();
                    Assert.Equal(numStorageAccountBefore, numStorageAccountAfter);
                    IStorageAccount storageAccount1Updated = getStorageAccount(appServiceManager.StorageManager, functionApp1,
                                                                               out IReadOnlyDictionary <string, IAppSetting> appSettings1Updated, out _);
                    Assert.True(appSettings1Updated.ContainsKey("newKey"));
                    Assert.Equal(appSettings1[KeyAzureWebJobsStorage].Value, appSettings1Updated[KeyAzureWebJobsStorage].Value);
                    Assert.Equal(appSettings1[KeyContentAzureFileConnectionString].Value, appSettings1Updated[KeyContentAzureFileConnectionString].Value);
                    Assert.Equal(appSettings1[KeyContentShare].Value, appSettings1Updated[KeyContentShare].Value);
                    Assert.Equal(storageAccount1.Name, storageAccount1Updated.Name);

                    // Scale
                    functionApp3.Update()
                    .WithNewAppServicePlan(PricingTier.StandardS2)
                    .Apply();
                    var plan2 = appServiceManager.AppServicePlans.GetById(functionApp3.AppServicePlanId);
                    Assert.NotNull(plan2);
                    Assert.NotEqual(plan1.Id, plan2.Id);
                    Assert.Equal(Region.USWest, plan2.Region);
                    Assert.Equal(PricingTier.StandardS2, plan2.PricingTier);
                }
                finally
                {
                    try
                    {
                        TestHelper.CreateResourceManager().ResourceGroups.DeleteByName(GroupName2);
                    }
                    catch { }
                    try
                    {
                        TestHelper.CreateResourceManager().ResourceGroups.DeleteByName(GroupName1);
                    }
                    catch { }
                }
            }
        }
 public static string GetConnectionString(this IStorageAccount account)
 {
     return($"DefaultEndpointsProtocol=https;AccountName={account.Name};AccountKey={account.GetKeys()[0].Value};EndpointSuffix=core.windows.net");
 }
        /**
         * Azure Container Instance sample for managing container instances with Azure File Share mount.
         *    - Create a storage account and an Azure file share resource
         *    - Create an Azure container instance using Docker image "seanmckenna/aci-hellofiles" with a mount to the file share from above
         *    - Retrieve container log content
         *    - Delete the container group resource
         */
        public static void RunSample(IAzure azure)
        {
            string rgName             = SdkContext.RandomResourceName("rgACI", 15);
            string aciName            = SdkContext.RandomResourceName("acisample", 20);
            string saName             = SdkContext.RandomResourceName("sa", 20);
            string shareName          = SdkContext.RandomResourceName("fileshare", 20);
            string containerImageName = "seanmckenna/aci-hellofiles";
            string volumeMountName    = "aci-helloshare";

            try
            {
                //=============================================================
                // Create a new storage account and an Azure file share resource

                IStorageAccount storageAccount = azure.StorageAccounts.Define(saName)
                                                 .WithRegion(region)
                                                 .WithNewResourceGroup(rgName)
                                                 .Create();

                StorageAccountKey storageAccountKey = storageAccount.GetKeys()[0];

                CreateFileShare(saName, storageAccountKey.Value, shareName);

                //=============================================================
                // Create a container group with one container instance of default CPU core count and memory size
                //   using public Docker image "seanmckenna/aci-hellofiles" which mounts the file share created previously
                //   as read/write shared container volume.
                IContainerGroup containerGroup = azure.ContainerGroups.Define(aciName)
                                                 .WithRegion(region)
                                                 .WithNewResourceGroup(rgName)
                                                 .WithLinux()
                                                 .WithPublicImageRegistryOnly()
                                                 .DefineVolume(volumeMountName)
                                                 .WithExistingReadWriteAzureFileShare(shareName)
                                                 .WithStorageAccountName(saName)
                                                 .WithStorageAccountKey(storageAccountKey.Value)
                                                 .Attach()
                                                 .DefineContainerInstance(aciName)
                                                 .WithImage(containerImageName)
                                                 .WithExternalTcpPort(80)
                                                 .WithVolumeMountSetting(volumeMountName, "/aci/logs/")
                                                 .Attach()
                                                 .WithDnsPrefix(aciName)
                                                 .Create();

                Utilities.Print(containerGroup);

                SdkContext.DelayProvider.Delay(20000);
                Utilities.Log("Container instance IP address: " + containerGroup.IPAddress);

                //=============================================================
                // Check the container instance logs

                containerGroup = azure.ContainerGroups.GetByResourceGroup(rgName, aciName);
                string logContent = containerGroup.GetLogContent(aciName);
                Utilities.Log($"Logs for container instance: {aciName}\n{logContent}");

                //=============================================================
                // Remove the container group

                azure.ContainerGroups.DeleteById(containerGroup.Id);
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting Resource Group: " + rgName);
                    azure.ResourceGroups.BeginDeleteByName(rgName);
                    Utilities.Log("Deleted Resource Group: " + rgName);
                }
                catch (Exception)
                {
                    Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
                }
            }
        }
예제 #8
0
        public ConfigModel CreateBlob(ConfigModel model)
        {
            if (model == null)
            {
                throw new HttpRequestWithStatusException("Empty model", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (string.IsNullOrWhiteSpace(model.ClientId) ||
                string.IsNullOrWhiteSpace(model.ClientSecret) ||
                string.IsNullOrWhiteSpace(model.TenantId) ||
                string.IsNullOrWhiteSpace(model.Subscription))
            {
                throw new HttpRequestWithStatusException("ClientId/ClientSecret/TenantId/Subscription is empty", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            try
            {
                var azure = AzureClient.GetAzure(model.ClientId,
                                                 model.ClientSecret,
                                                 model.TenantId,
                                                 model.Subscription);

                var resourceGroupName = GetUniqueHash(model.TenantId + model.ClientId + CommonName);
                model.SelectedRegion = Region.USEast.Name;
                IResourceGroup resourceGroup;
                try
                {
                    resourceGroup = azure.ResourceGroups.GetByName(resourceGroupName);
                }
                catch (Exception e)
                {
                    resourceGroup =
                        ApiHelper.CreateResourceGroup(azure, resourceGroupName, model.SelectedRegion);
                }
                model.SelectedDeploymentRg = resourceGroupName;
                var storageAccountName = GetUniqueHash(model.TenantId + model.ClientId + resourceGroupName + CommonName);
                model.StorageAccountName = storageAccountName;
                var             storageAccounts    = azure.StorageAccounts.ListByResourceGroup(resourceGroupName);
                IStorageAccount storageAccountInfo = null;
                if (storageAccounts != null && storageAccounts.Count() > 0)
                {
                    storageAccountInfo = storageAccounts.FirstOrDefault(x =>
                                                                        x.Name.Equals(storageAccountName, StringComparison.OrdinalIgnoreCase));
                }

                if (storageAccountInfo == null)
                {
                    try
                    {
                        storageAccountInfo = ApiHelper.CreateStorageAccount(azure, resourceGroup.Name, model.SelectedRegion,
                                                                            storageAccountName);
                    }
                    catch (Exception e)
                    {
                        throw new HttpRequestWithStatusException("storage account not created",
                                                                 new HttpResponseException(HttpStatusCode.InternalServerError));
                    }
                }

                var    storageKeys       = storageAccountInfo.GetKeys();
                string storageConnection = string.Format(StorageConStringFormat,
                                                         model.StorageAccountName, storageKeys[0].Value);
                AddAppsettings(StorageConnectionString, storageConnection);
                var storageAccount = CloudStorageAccount.Parse(storageConnection);
                var blockBlob      = ApiHelper.CreateBlobContainer(storageAccount);
                var configString   = ApiHelper.ConvertConfigObjectToString(model);
                using (var ms = new MemoryStream())
                {
                    LoadStreamWithJson(ms, configString);
                    blockBlob.UploadFromStream(ms);
                }

                //else
                //{
                //    var azureSettings = JsonConvert.DeserializeObject<AzureSettings>(data);
                //    model = ConvertAzureSettingsConfigModel(azureSettings);
                //}

                var functionAppName =
                    GetUniqueHash(model.ClientId + model.TenantId + model.Subscription + CommonName);

                var azureFunctions =
                    azure.AppServices.FunctionApps.ListByResourceGroup(resourceGroupName);

                // try to give the read and write permissions
                if (azureFunctions != null && azureFunctions.Count() > 0)
                {
                    return(model);
                }
                if (!DeployAzureFunctions(model, functionAppName, storageConnection, resourceGroupName))
                {
                    throw new HttpRequestWithStatusException("Azure Functions are not deployed",
                                                             new HttpResponseException(HttpStatusCode.InternalServerError));
                }

                return(model);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public FaultInjectionResponseModel <ConfigModel> CreateBlob(ConfigModel model)
        {
            if (model == null)
            {
                throw new HttpRequestWithStatusException("Empty model", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (string.IsNullOrWhiteSpace(model.ClientId) ||
                string.IsNullOrWhiteSpace(model.ClientSecret) ||
                string.IsNullOrWhiteSpace(model.TenantId) ||
                string.IsNullOrWhiteSpace(model.Subscription))
            {
                throw new HttpRequestWithStatusException("ClientId/ClientSecret/TenantId/Subscription is empty", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (2 * model.RollbackFrequency > model.SchedulerFrequency)
            {
                throw new Exception("Power Cycle Schedule Frequency should be less than half the Scheduler Frequency, Please decrease the Power Cycle Schedule or increase the Scheduler Frequency value");
            }

            if (2 * model.RollbackFrequency <= model.SchedulerFrequency &&
                model.SchedulerFrequency > (model.CrawlerFrequency + model.MeanTime))
            {
                throw new HttpRequestWithStatusException("Scheduler Frequency should be less than the sum of Crawler Frequency & Mean Time between FI on VM, Please reduce the value of Scheduler Frequency", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            try
            {
                var storageAccountName = ConfigurationManager.AppSettings[StorageAccountName];
                var resourceGroupName  = ConfigurationManager.AppSettings[ResourceGroupName];

                if (string.IsNullOrWhiteSpace(storageAccountName) || string.IsNullOrWhiteSpace(resourceGroupName))
                {
                    throw new ArgumentNullException("Storage account name or resource group name is null "
                                                    + "Storage Param: " + StorageAccountName + " Resource Param: " + ResourceGroupName);
                }

                var azure = AzureClient.GetAzure(model.ClientId,
                                                 model.ClientSecret,
                                                 model.TenantId,
                                                 model.Subscription);

                var resourceGroup = azure.ResourceGroups.GetByName(resourceGroupName);
                if (resourceGroup == null)
                {
                    throw new ArgumentNullException(ResourceGroupName, "Resource group information is empty: " + ResourceGroupName);
                }

                var             storageAccounts    = azure.StorageAccounts.ListByResourceGroup(resourceGroupName);
                IStorageAccount storageAccountInfo = null;
                if (storageAccounts != null && storageAccounts.Count() > 0)
                {
                    storageAccountInfo = storageAccounts.FirstOrDefault(x =>
                                                                        x.Name.Equals(storageAccountName, StringComparison.OrdinalIgnoreCase));
                }

                if (storageAccountInfo == null)
                {
                    throw new ArgumentNullException(StorageAccountName, "Storage account information is empty: " + storageAccountName);
                }

                var    storageKeys       = storageAccountInfo.GetKeys();
                string storageConnection = string.Format(StorageConStringFormat,
                                                         storageAccountName, storageKeys[0].Value);

                InsertOrUpdateAppsettings(StorageConnectionString, storageConnection);
                var storageAccount = CloudStorageAccount.Parse(storageConnection);
                var blockBlob      = ApiHelper.CreateBlobContainer(storageAccount);
                var configString   = ApiHelper.ConvertConfigObjectToString(model);
                using (var ms = new MemoryStream())
                {
                    LoadStreamWithJson(ms, configString);
                    blockBlob.UploadFromStream(ms);
                }

                var functionAppName = CommonName +
                                      GetUniqueHash(model.ClientId + model.TenantId + model.Subscription);

                // TODO: ask ? do we deploy the azure functions, whenever user do any changes in the  config?
                if (!DeployAzureFunctions(model, functionAppName, storageConnection, resourceGroupName))
                {
                    throw new HttpRequestWithStatusException("Azure Functions are not deployed",
                                                             new HttpResponseException(HttpStatusCode.InternalServerError));
                }


                // add the tenant id and application id into the configuration file to validate the user next time.
                InsertOrUpdateAppsettings(TenantIdString, model.TenantId);
                InsertOrUpdateAppsettings(ApplicationIdString, model.ClientId);

                return(new FaultInjectionResponseModel <ConfigModel>()
                {
                    Success = true,
                    SuccessMessage = "Deployment Completed Successfully",
                    Result = model
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #10
0
        // This method will be called for each input received from the pipeline to this cmdlet; if no input is received, this method is not called
        protected override void ProcessRecord()
        {
            ProgressRecord progress = new ProgressRecord(1, "SimpleSwarm Setup", "Connecting to Azure...");

            WriteProgress(progress);
            var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));


            var azure = Microsoft.Azure.Management.Fluent.Azure
                        .Configure()
                        .Authenticate(credentials)
                        .WithDefaultSubscription();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Resource Group...");
            WriteProgress(progress);
            IResourceGroup resourceGroup = azure.ResourceGroups
                                           .Define(resourceGroupName)
                                           .WithRegion(location)
                                           .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Swarm Manager Identity...");
            WriteProgress(progress);
            IIdentity identityManager = azure.Identities
                                        .Define("AzSwarmManager")
                                        .WithRegion(location)
                                        .WithExistingResourceGroup(resourceGroup)
                                        .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Swarm Worker Identity...");
            WriteProgress(progress);
            IIdentity identityWorker = azure.Identities
                                       .Define("AzSwarmWorker")
                                       .WithRegion(location)
                                       .WithExistingResourceGroup(resourceGroup)
                                       .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Key Vault...");
            WriteProgress(progress);
            IVault keyVault = azure.Vaults
                              .Define(SdkContext.RandomResourceName("azswarmkv-", 20))
                              .WithRegion(location)
                              .WithExistingResourceGroup(resourceGroup)
                              .WithEmptyAccessPolicy().Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Updating Key Vault Swarm Manager Access Policy...");
            WriteProgress(progress);
            keyVault.Update()
            .DefineAccessPolicy()
            .ForObjectId(identityManager.PrincipalId)
            .AllowSecretAllPermissions()
            .Attach()
            .Apply();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Updating Key Vault Swarm Worker Access Policy...");
            WriteProgress(progress);
            keyVault.Update()
            .DefineAccessPolicy()
            .ForObjectId(identityWorker.PrincipalId)
            .AllowSecretPermissions(SecretPermissions.Get)
            .AllowSecretPermissions(SecretPermissions.List)
            .Attach()
            .Apply();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Virtual Network...");
            WriteProgress(progress);
            INetwork network = azure.Networks
                               .Define(SdkContext.RandomResourceName("AzSwarmNetwork", 20))
                               .WithRegion(location)
                               .WithExistingResourceGroup(resourceGroup)
                               .WithAddressSpace("10.0.0.0/16")
                               .WithSubnet("AzSwarmSubnet", "10.0.0.0/24")
                               .Create();


            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Storage Account...");
            WriteProgress(progress);
            IStorageAccount storage = azure.StorageAccounts
                                      .Define(SdkContext.RandomResourceName("azswarmst", 20))
                                      .WithRegion(location)
                                      .WithExistingResourceGroup(resourceGroup)
                                      .WithAccessFromAllNetworks()
                                      .WithAccessFromAzureServices()
                                      .WithGeneralPurposeAccountKindV2()
                                      .WithOnlyHttpsTraffic()
                                      .WithSku(StorageAccountSkuType.Standard_LRS)
                                      .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Storage Account Table...");
            WriteProgress(progress);
            var storageAccountAccessKeys = storage.GetKeys();

            string storageConnectionString = "DefaultEndpointsProtocol=https"
                                             + ";AccountName=" + storage.Name
                                             + ";AccountKey=" + storageAccountAccessKeys[0].Value
                                             + ";EndpointSuffix=core.windows.net";

            var cloudStorageAccount      = CloudStorageAccount.Parse(storageConnectionString);
            CloudTableClient tableClient = cloudStorageAccount.CreateCloudTableClient(new TableClientConfiguration());
            CloudTable       table       = tableClient.GetTableReference("SimpleSwarmSetup");

            table.CreateIfNotExists();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Manager Availability Set...");
            WriteProgress(progress);
            IAvailabilitySet availabilitySetManager = azure.AvailabilitySets.Define("azswarm-manager-avset")
                                                      .WithRegion(location)
                                                      .WithExistingResourceGroup(resourceGroup)
                                                      .WithFaultDomainCount(3)
                                                      .WithUpdateDomainCount(5)
                                                      .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Setup", "Creating Manager Availability Set...");
            WriteProgress(progress);
            IAvailabilitySet availabilitySetWorker = azure.AvailabilitySets.Define("azswarm-worker-avset")
                                                     .WithRegion(location)
                                                     .WithExistingResourceGroup(resourceGroup)
                                                     .WithFaultDomainCount(3)
                                                     .WithUpdateDomainCount(5)
                                                     .Create();

            WriteVerbose("SimpleSwarm Setup Completed");
        }