Exemple #1
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 Manager Setup", "Connecting to Azure...");
            var            credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));

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

            var storageAccount = new List <IStorageAccount>(azure.StorageAccounts.ListByResourceGroup(resourceGroupName))[0];

            string storageConnectionString = "DefaultEndpointsProtocol=https"
                                             + ";AccountName=" + storageAccount.Name
                                             + ";AccountKey=" + storageAccount.GetKeys()[0].Value
                                             + ";EndpointSuffix=core.windows.net";

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

            TableQuery <SimpleSwarmVM> query = new TableQuery <SimpleSwarmVM>()
                                               .Where(
                TableQuery.GenerateFilterCondition(
                    "PartitionKey",
                    QueryComparisons.GreaterThanOrEqual,
                    "Manager"
                    )
                );
            SimpleSwarmVM deleteManager = null;

            foreach (SimpleSwarmVM worker in table.ExecuteQuery(query))
            {
                if (ManagerName != null && ManagerName.Equals(worker.RowKey))
                {
                    deleteManager = worker;
                    break;
                }
                else
                {
                    deleteManager = worker;
                }
            }

            azure.VirtualMachines.DeleteById(deleteManager.vmId);
            azure.NetworkInterfaces.DeleteById(deleteManager.nicId);
            azure.Disks.DeleteById(deleteManager.diskId);

            if (deleteManager.ipId != null)
            {
                azure.PublicIPAddresses.DeleteById(deleteManager.ipId);
            }

            TableOperation deleteOperation = TableOperation.Delete(deleteManager);
            TableResult    result          = table.Execute(deleteOperation);
        }
Exemple #2
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 Manager Setup", "Connecting to Azure...");
            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 Manager Setup", "Searching SimpleSwarm Information...");
            WriteProgress(progress);
            IAvailabilitySet availabilitySet = azure.AvailabilitySets.GetByResourceGroup(resourceGroupName, "azswarm-manager-avset");
            var    vmIds          = availabilitySet.VirtualMachineIds;
            String swarmManagerIp = "";

            foreach (var id in vmIds)
            {
                var vm = azure.VirtualMachines.GetById(id);
                swarmManagerIp = vm.GetPrimaryNetworkInterface().PrimaryPrivateIP;
            }

            var network  = new List <INetwork>(azure.Networks.ListByResourceGroup(resourceGroupName))[0];
            var keyVault = new List <IVault>(azure.Vaults.ListByResourceGroup(resourceGroupName))[0];
            var identity = azure.Identities.GetByResourceGroup(resourceGroupName, "AzSwarmWorker");
            IAvailabilitySet availabilitySetWorker = azure.AvailabilitySets.GetByResourceGroup(resourceGroupName, "azswarm-worker-avset");

            progress = new ProgressRecord(1, "SimpleSwarm Manager Setup", "Creating Virtual Machine...");
            WriteProgress(progress);
            //Will refactor this later. Github Origin https://github.com/AzSwarm/Cloud-Init/blob/main/distribution/ubuntu/18.04/cloud-init-manager.yml
            String cloudInitBase64 = "I2Nsb3VkLWNvbmZpZwpwYWNrYWdlX3VwZGF0ZTogdHJ1ZQoKcmVzb2x2X2NvbmY6CiAgICBuYW1lc2VydmVyczoKICAgICAgLSAnOC44LjguOCcKCnBhY2thZ2VzOgogIC0gYXB0LXRyYW5zcG9ydC1odHRwcwogIC0gY2EtY2VydGlmaWNhdGVzCiAgLSBjdXJsCiAgLSBnbnVwZy1hZ2VudAogIC0gc29mdHdhcmUtcHJvcGVydGllcy1jb21tb24KICAtIGxzYi1yZWxlYXNlCiAgLSB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cmlwdGlvbnMvPHN1YnNjcmlwdGlvbklkPi9yZXNvdXJjZWdyb3Vwcy88cmVzb3VyY2VHcm91cE5hbWU+L3Byb3ZpZGVycy9NaWNyb3NvZnQuTWFuYWdlZElkZW50aXR5L3VzZXJBc3NpZ25lZElkZW50aXRpZXMvPHVzZXJBc3NpZ25lZElkZW50aXR5TmFtZT4gLS1hbGxvdy1uby1zdWJzY3JpcHRpb25zCiAgIyBJbml0aWFsaXplIGEgU3dhcm0gV29ya2VyCiAgLSBkb2NrZXIgc3dhcm0gam9pbiAtLXRva2VuICIkKGF6IGtleXZhdWx0IHNlY3JldCBzaG93IC0tbmFtZSAid29ya2VyLWtleSIgLS12YXVsdC1uYW1lIDxrZXlWYXVsdE5hbWU+IC0tcXVlcnkgdmFsdWUgLS1vdXRwdXQgdHN2KSIgPHN3YXJtTWFuYWdlcklwPjoyMzc3";
            String cloudInit       = Encoding.UTF8.GetString(Convert.FromBase64String(cloudInitBase64));

            cloudInit       = cloudInit.Replace("<subscriptionId>", credentials.DefaultSubscriptionId);
            cloudInit       = cloudInit.Replace("<resourceGroupName>", resourceGroupName);
            cloudInit       = cloudInit.Replace("<userAssignedIdentityName>", identity.Name);
            cloudInit       = cloudInit.Replace("<keyVaultName>", keyVault.Name);
            cloudInit       = cloudInit.Replace("<swarmManagerIp>", swarmManagerIp);
            cloudInitBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(cloudInit));

            var             vmName  = SdkContext.RandomResourceName("azswarworker", 20);
            IVirtualMachine linuxVM = azure.VirtualMachines.Define(vmName)
                                      .WithRegion(network.Region)
                                      .WithExistingResourceGroup(resourceGroupName)
                                      .WithExistingPrimaryNetwork(network)
                                      .WithSubnet("AzSwarmSubnet")
                                      .WithPrimaryPrivateIPAddressDynamic()
                                      .WithoutPrimaryPublicIPAddress()
                                      .WithLatestLinuxImage("Canonical", "UbuntuServer", "18.04-LTS")
                                      .WithRootUsername(DefaultUsers.azuser_worker.ToString())
                                      .WithRootPassword(KeyGenerator.GetUniqueKey(20))
                                      .WithCustomData(cloudInitBase64)
                                      .WithComputerName(vmName)
                                      .WithExistingUserAssignedManagedServiceIdentity(identity)
                                      .WithSize(Microsoft.Azure.Management.Compute.Fluent.Models.VirtualMachineSizeTypes.StandardB1s)
                                      .WithExistingAvailabilitySet(availabilitySetWorker)
                                      .Create();

            progress = new ProgressRecord(1, "SimpleSwarm Manager Setup", "Updating SimpleSwarm status...");
            WriteProgress(progress);
            var storageAccount = new List <IStorageAccount>(azure.StorageAccounts.ListByResourceGroup(resourceGroupName))[0];

            string storageConnectionString = "DefaultEndpointsProtocol=https"
                                             + ";AccountName=" + storageAccount.Name
                                             + ";AccountKey=" + storageAccount.GetKeys()[0].Value
                                             + ";EndpointSuffix=core.windows.net";

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


            // Create the InsertOrReplace table operation
            SimpleSwarmVM  simpleSwarmVM          = new SimpleSwarmVM(linuxVM.Name, "Worker", linuxVM.PrimaryNetworkInterfaceId, linuxVM.OSDiskId, null, linuxVM.Id);
            TableOperation insertOrMergeOperation = TableOperation.InsertOrMerge(simpleSwarmVM);

            // Execute the operation.
            TableResult   result = table.Execute(insertOrMergeOperation);
            SimpleSwarmVM insertedSimpleSwarmVM = result.Result as SimpleSwarmVM;
        }
        // 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 Manager Setup", "Connecting to Azure...");
            var            credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));

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

            var storageAccount = new List <IStorageAccount>(azure.StorageAccounts.ListByResourceGroup(resourceGroupName))[0];

            string storageConnectionString = "DefaultEndpointsProtocol=https"
                                             + ";AccountName=" + storageAccount.Name
                                             + ";AccountKey=" + storageAccount.GetKeys()[0].Value
                                             + ";EndpointSuffix=core.windows.net";

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

            TableQuery <SimpleSwarmVM> query = new TableQuery <SimpleSwarmVM>()
                                               .Where(
                TableQuery.GenerateFilterCondition(
                    "PartitionKey",
                    QueryComparisons.GreaterThanOrEqual,
                    "Worker"
                    )
                );
            SimpleSwarmVM deleteWorker = null;

            foreach (SimpleSwarmVM worker in table.ExecuteQuery(query))
            {
                if (WorkerName != null && WorkerName.Equals(worker.RowKey))
                {
                    deleteWorker = worker;
                    break;
                }
                else
                {
                    deleteWorker = worker;
                }
            }

            //SEARCH MANAGER TO EXECUTE COMMAND
            progress = new ProgressRecord(1, "SimpleSwarm Manager Information", "Searching SimpleSwarm Information...");
            WriteProgress(progress);
            IAvailabilitySet availabilitySet = azure.AvailabilitySets.GetByResourceGroup(resourceGroupName, "azswarm-manager-avset");
            var    vmIds = availabilitySet.VirtualMachineIds;
            String vmId  = "";

            foreach (var id in vmIds)
            {
                vmId = id;
            }
            var vm = azure.VirtualMachines.GetById(vmId);

            //DRAIN AND REMOVE NODE FROM CLUSTER
            vm.RunShellScript(
                new List <string>()
            {
                "sudo docker node update --availability drain $workerDrain; sudo docker node remove $workerRemove"
            },
                new List <RunCommandInputParameter>()
            {
                new RunCommandInputParameter("workerDrain", deleteWorker.RowKey),
                new RunCommandInputParameter("workerRemove", deleteWorker.RowKey)
            });

            azure.VirtualMachines.DeleteById(deleteWorker.vmId);
            azure.NetworkInterfaces.DeleteById(deleteWorker.nicId);
            azure.Disks.DeleteById(deleteWorker.diskId);

            TableOperation deleteOperation = TableOperation.Delete(deleteWorker);
            TableResult    result          = table.Execute(deleteOperation);
        }