示例#1
0
        public void NewAzureVMProcess()
        {
            SubscriptionData    currentSubscription = this.GetCurrentSubscription();
            CloudStorageAccount currentStorage      = null;

            try
            {
                currentStorage = currentSubscription.GetCurrentStorageAccount();
            }
            catch (ServiceManagementClientException) // couldn't access
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
            }
            if (currentStorage == null) // not set
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
            }

            bool serviceExists = DoesCloudServiceExist(this.ServiceName);

            if (!string.IsNullOrEmpty(this.Location))
            {
                if (serviceExists)
                {
                    throw new ApplicationException(Resources.ServiceExistsLocationCanNotBeSpecified);
                }
            }

            if (!string.IsNullOrEmpty(this.AffinityGroup))
            {
                if (serviceExists)
                {
                    throw new ApplicationException(Resources.ServiceExistsAffinityGroupCanNotBeSpecified);
                }
            }

            if (!serviceExists)
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        //Implicitly created hosted service2012-05-07 23:12

                        // Create the Cloud Service when
                        // Location or Affinity Group is Specified
                        // or VNET is specified and the service doesn't exist
                        var chsi = new CreateHostedServiceInput
                        {
                            AffinityGroup = this.AffinityGroup,
                            Location      = this.Location,
                            ServiceName   = this.ServiceName,
                            Description   = String.Format("Implicitly created hosted service{0}", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")),
                            Label         = this.ServiceName
                        };

                        ExecuteClientAction(chsi, CommandRuntime + Resources.QuickVMCreateCloudService, s => this.Channel.CreateHostedService(s, chsi));
                    }

                    catch (ServiceManagementClientException ex)
                    {
                        this.WriteErrorDetails(ex);
                        return;
                    }
                }
            }

            if (ParameterSetName.Equals("Windows", StringComparison.OrdinalIgnoreCase))
            {
                if (WinRMCertificate != null)
                {
                    if (!CertUtils.HasExportablePrivateKey(WinRMCertificate))
                    {
                        throw new ArgumentException(Resources.WinRMCertificateDoesNotHaveExportablePrivateKey);
                    }
                    var operationDescription = string.Format(Resources.QuickVMUploadingWinRMCertificate, CommandRuntime, WinRMCertificate.Thumbprint);
                    var certificateFile      = CertUtils.Create(WinRMCertificate);
                    ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, certificateFile));
                }

                if (X509Certificates != null)
                {
                    var certificateFilesWithThumbprint = from c in X509Certificates
                                                         select new
                    {
                        c.Thumbprint,
                        CertificateFile = CertUtils.Create(c, this.NoExportPrivateKey.IsPresent)
                    };
                    foreach (var current in certificateFilesWithThumbprint.ToList())
                    {
                        var operationDescription = string.Format(Resources.QuickVMUploadingCertificate, CommandRuntime, current.Thumbprint);
                        ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, current.CertificateFile));
                    }
                }
            }

            var vm = CreatePersistenVMRole(currentStorage);

            // If the current deployment doesn't exist set it create it
            if (CurrentDeployment == null)
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        var deployment = new Deployment
                        {
                            DeploymentSlot = DeploymentSlotType.Production,
                            Name           = this.ServiceName,
                            Label          = this.ServiceName,
                            RoleList       = new RoleList {
                                vm
                            },
                            VirtualNetworkName = this.VNetName
                        };

                        if (this.DnsSettings != null)
                        {
                            deployment.Dns = new DnsSettings {
                                DnsServers = new DnsServerList()
                            };
                            foreach (DnsServer dns in this.DnsSettings)
                            {
                                deployment.Dns.DnsServers.Add(dns);
                            }
                        }

                        var operationDescription = string.Format(Resources.QuickVMCreateDeploymentWithVM, CommandRuntime, vm.RoleName);
                        ExecuteClientAction(deployment, operationDescription, s => this.Channel.CreateDeployment(s, this.ServiceName, deployment));

                        if (WaitForBoot.IsPresent)
                        {
                            WaitForRoleToBoot(vm.RoleName);
                        }
                    }

                    catch (ServiceManagementClientException ex)
                    {
                        if (ex.HttpStatus == HttpStatusCode.NotFound)
                        {
                            throw new Exception(Resources.ServiceDoesNotExistSpecifyLocationOrAffinityGroup);
                        }
                        else
                        {
                            this.WriteErrorDetails(ex);
                        }
                        return;
                    }

                    _createdDeployment = true;
                }
            }
            else
            {
                if (VNetName != null || DnsSettings != null)
                {
                    WriteWarning(Resources.VNetNameOrDnsSettingsCanOnlyBeSpecifiedOnNewDeployments);
                }
            }


            // Only create the VM when a new VM was added and it was not created during the deployment phase.
            if ((_createdDeployment == false))
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        var operationDescription = string.Format(Resources.QuickVMCreateVM, CommandRuntime, vm.RoleName);
                        ExecuteClientAction(vm, operationDescription, s => this.Channel.AddRole(s, this.ServiceName, this.ServiceName, vm));
                        if (WaitForBoot.IsPresent)
                        {
                            WaitForRoleToBoot(vm.RoleName);
                        }
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        this.WriteErrorDetails(ex);
                        return;
                    }
                }
            }
        }
示例#2
0
        public void NewAzureVMProcess()
        {
            SubscriptionData currentSubscription = this.GetCurrentSubscription();

            CloudStorageAccount currentStorage = null;

            try
            {
                currentStorage = currentSubscription.GetCurrentStorageAccount();
            }
            catch (ServiceManagementClientException) // couldn't access
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotAccessible);
            }
            if (currentStorage == null) // not set
            {
                throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
            }


            Operation lastOperation = null;

            using (new OperationContextScope(Channel.ToContextChannel()))
            {
                try
                {
                    if (this.ParameterSetName.Equals("CreateService", StringComparison.OrdinalIgnoreCase) == true)
                    {
                        var chsi = new CreateHostedServiceInput
                        {
                            AffinityGroup = this.AffinityGroup,
                            Location      = this.Location,
                            ServiceName   = this.ServiceName,
                            Description   = this.ServiceDescription ??
                                            String.Format("Implicitly created hosted service{0}", DateTime.Now.ToUniversalTime().ToString("yyyy-MM-dd HH:mm")),
                            Label = this.ServiceLabel ?? this.ServiceName
                        };

                        ExecuteClientAction(chsi, CommandRuntime + " - Create Cloud Service", s => this.Channel.CreateHostedService(s, chsi));
                    }
                }
                catch (ServiceManagementClientException ex)
                {
                    this.WriteErrorDetails(ex);
                    return;
                }
            }

            if (lastOperation != null && string.Compare(lastOperation.Status, OperationState.Failed, StringComparison.OrdinalIgnoreCase) == 0)
            {
                return;
            }

            foreach (var vm in VMs)
            {
                var configuration = vm.ConfigurationSets.OfType <WindowsProvisioningConfigurationSet>().FirstOrDefault();
                if (configuration != null)
                {
                    if (vm.WinRMCertificate != null)
                    {
                        if (!CertUtils.HasExportablePrivateKey(vm.WinRMCertificate))
                        {
                            throw new ArgumentException(Resources.WinRMCertificateDoesNotHaveExportablePrivateKey);
                        }
                        var operationDescription = string.Format(Resources.AzureVMUploadingWinRMCertificate, CommandRuntime, vm.WinRMCertificate.Thumbprint);
                        var certificateFile      = CertUtils.Create(vm.WinRMCertificate);
                        ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, certificateFile));
                    }
                    var certificateFilesWithThumbprint = from c in vm.X509Certificates
                                                         select new
                    {
                        c.Thumbprint,
                        CertificateFile = CertUtils.Create(c, vm.NoExportPrivateKey)
                    };
                    foreach (var current in certificateFilesWithThumbprint.ToList())
                    {
                        var operationDescription = string.Format(Resources.AzureVMCommandUploadingCertificate, CommandRuntime, current.Thumbprint);
                        ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, current.CertificateFile));
                    }
                }
            }

            var persistentVMs = this.VMs.Select(vm => CreatePersistenVMRole(vm, currentStorage)).ToList();

            // If the current deployment doesn't exist set it create it
            if (CurrentDeployment == null)
            {
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        var deployment = new Deployment
                        {
                            DeploymentSlot = DeploymentSlotType.Production,
                            Name           = this.DeploymentName ?? this.ServiceName,
                            Label          = this.DeploymentLabel ?? this.ServiceName,
                            RoleList       = new RoleList(new List <Role> {
                                persistentVMs[0]
                            }),
                            VirtualNetworkName = this.VNetName
                        };

                        if (this.DnsSettings != null)
                        {
                            deployment.Dns = new DnsSettings {
                                DnsServers = new DnsServerList()
                            };
                            foreach (var dns in this.DnsSettings)
                            {
                                deployment.Dns.DnsServers.Add(dns);
                            }
                        }

                        var operationDescription = string.Format(Resources.AzureVMCommandCreateDeploymentWithVM, CommandRuntime, persistentVMs[0].RoleName);
                        ExecuteClientAction(deployment, operationDescription, s => this.Channel.CreateDeployment(s, this.ServiceName, deployment));

                        if (this.WaitForBoot.IsPresent)
                        {
                            WaitForRoleToBoot(persistentVMs[0].RoleName);
                        }
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        if (ex.HttpStatus == HttpStatusCode.NotFound)
                        {
                            throw new Exception(Resources.ServiceDoesNotExistSpecifyLocationOrAffinityGroup);
                        }
                        else
                        {
                            this.WriteErrorDetails(ex);
                        }
                        return;
                    }

                    this.createdDeployment = true;
                }
            }
            else
            {
                if (this.VNetName != null || this.DnsSettings != null || !string.IsNullOrEmpty(this.DeploymentLabel) || !string.IsNullOrEmpty(this.DeploymentName))
                {
                    WriteWarning(Resources.VNetNameDnsSettingsDeploymentLabelDeploymentNameCanBeSpecifiedOnNewDeployments);
                }
            }

            if (this.createdDeployment == false && CurrentDeployment != null)
            {
                this.DeploymentName = CurrentDeployment.Name;
            }

            int startingVM = (this.createdDeployment == true) ? 1 : 0;

            for (int i = startingVM; i < persistentVMs.Count; i++)
            {
                var operationDescription = string.Format(Resources.AzureVMCommandCreateVM, CommandRuntime, persistentVMs[i].RoleName);
                ExecuteClientActionInOCS(persistentVMs[i], operationDescription, s => this.Channel.AddRole(s, this.ServiceName, this.DeploymentName ?? this.ServiceName, persistentVMs[i]));
            }

            if (this.WaitForBoot.IsPresent)
            {
                for (int i = startingVM; i < persistentVMs.Count; i++)
                {
                    WaitForRoleToBoot(persistentVMs[i].RoleName);
                }
            }
        }
 protected void ValidateThumbprint(bool uploadCert)
 {
     if (X509Certificate != null)
     {
         var operationDescription = string.Format(Resources.ServiceExtensionUploadingCertificate, CommandRuntime, X509Certificate.Thumbprint);
         if (uploadCert)
         {
             ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, CertUtils.Create(X509Certificate)));
         }
         CertificateThumbprint = X509Certificate.Thumbprint;
     }
     ThumbprintAlgorithm   = ThumbprintAlgorithm == null ? "" : ThumbprintAlgorithm;
     CertificateThumbprint = CertificateThumbprint == null ? "" : CertificateThumbprint;
 }
示例#4
0
        public void NewPaaSDeploymentProcess()
        {
            bool removePackage = false;

            AssertNoPersistenVmRoleExistsInDeployment(DeploymentSlotType.Production);
            AssertNoPersistenVmRoleExistsInDeployment(DeploymentSlotType.Staging);

            var storageName = CurrentSubscription.CurrentStorageAccount;

            Uri packageUrl;

            if (this.Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||
                this.Package.StartsWith(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
            {
                packageUrl = new Uri(this.Package);
            }
            else
            {
                var progress = new ProgressRecord(0, Resources.WaitForUploadingPackage, Resources.UploadingPackage);
                WriteProgress(progress);
                removePackage = true;
                packageUrl    = this.RetryCall(s =>
                                               AzureBlob.UploadPackageToBlob(
                                                   this.Channel,
                                                   storageName,
                                                   s,
                                                   this.Package,
                                                   null));
            }

            ExtensionConfiguration extConfig = null;

            if (ExtensionConfiguration != null)
            {
                var roleList = (from c in ExtensionConfiguration
                                where c != null
                                from r in c.Roles
                                select r).GroupBy(r => r.ToString()).Select(g => g.First());

                foreach (var role in roleList)
                {
                    var result = from c in ExtensionConfiguration
                                 where c != null && c.Roles.Any(r => r.ToString() == role.ToString())
                                 select string.Format("{0}.{1}", c.ProviderNameSpace, c.Type);

                    foreach (var s in result)
                    {
                        if (result.Count(t => t == s) > 1)
                        {
                            throw new Exception(string.Format(Resources.ServiceExtensionCannotApplyExtensionsInSameType, s));
                        }
                    }
                }

                ExtensionManager       extensionMgr              = new ExtensionManager(this, ServiceName);
                Deployment             currentDeployment         = null;
                ExtensionConfiguration deploymentExtensionConfig = null;
                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        currentDeployment         = this.RetryCall(s => this.Channel.GetDeploymentBySlot(s, this.ServiceName, Slot));
                        deploymentExtensionConfig = currentDeployment == null ? null : extensionMgr.GetBuilder(currentDeployment.ExtensionConfiguration).ToConfiguration();
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        if (ex.HttpStatus != HttpStatusCode.NotFound && IsVerbose() == false)
                        {
                            this.WriteErrorDetails(ex);
                        }
                    }
                }
                ExtensionConfigurationBuilder configBuilder = extensionMgr.GetBuilder();
                foreach (ExtensionConfigurationInput context in ExtensionConfiguration)
                {
                    if (context != null)
                    {
                        if (context.X509Certificate != null)
                        {
                            var operationDescription = string.Format(Resources.ServiceExtensionUploadingCertificate, CommandRuntime, context.X509Certificate.Thumbprint);
                            ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, CertUtils.Create(context.X509Certificate)));
                        }

                        ExtensionConfiguration currentConfig = extensionMgr.InstallExtension(context, Slot, deploymentExtensionConfig);
                        foreach (var r in currentConfig.AllRoles)
                        {
                            if (currentDeployment == null || !extensionMgr.GetBuilder(currentDeployment.ExtensionConfiguration).ExistAny(r.Id))
                            {
                                configBuilder.AddDefault(r.Id);
                            }
                        }
                        foreach (var r in currentConfig.NamedRoles)
                        {
                            foreach (var e in r.Extensions)
                            {
                                if (currentDeployment == null || !extensionMgr.GetBuilder(currentDeployment.ExtensionConfiguration).ExistAny(e.Id))
                                {
                                    configBuilder.Add(r.RoleName, e.Id);
                                }
                            }
                        }
                    }
                }
                extConfig = configBuilder.ToConfiguration();
            }

            var deploymentInput = new CreateDeploymentInput
            {
                PackageUrl             = packageUrl,
                Configuration          = General.GetConfiguration(this.Configuration),
                ExtensionConfiguration = extConfig,
                Label                = this.Label,
                Name                 = this.Name,
                StartDeployment      = !this.DoNotStart.IsPresent,
                TreatWarningsAsError = this.TreatWarningsAsError.IsPresent
            };

            using (new OperationContextScope(Channel.ToContextChannel()))
            {
                try
                {
                    var progress = new ProgressRecord(0, Resources.WaitForUploadingPackage, Resources.CreatingNewDeployment);
                    WriteProgress(progress);

                    ExecuteClientAction(deploymentInput, CommandRuntime.ToString(), s => this.Channel.CreateOrUpdateDeployment(s, this.ServiceName, this.Slot, deploymentInput));

                    if (removePackage == true)
                    {
                        this.RetryCall(s =>
                                       AzureBlob.DeletePackageFromBlob(
                                           this.Channel,
                                           storageName,
                                           s,
                                           packageUrl));
                    }
                }
                catch (ServiceManagementClientException ex)
                {
                    this.WriteErrorDetails(ex);
                }
            }
        }
示例#5
0
        public void ExecuteCommand()
        {
            string configString = string.Empty;

            if (!string.IsNullOrEmpty(Configuration))
            {
                configString = General.GetConfiguration(Configuration);
            }

            ExtensionConfiguration extConfig = null;

            if (ExtensionConfiguration != null)
            {
                var roleList = (from c in ExtensionConfiguration
                                where c != null
                                from r in c.Roles
                                select r).GroupBy(r => r.ToString()).Select(g => g.First());

                foreach (var role in roleList)
                {
                    var result = from c in ExtensionConfiguration
                                 where c != null && c.Roles.Any(r => r.ToString() == role.ToString())
                                 select string.Format("{0}.{1}", c.ProviderNameSpace, c.Type);

                    foreach (var s in result)
                    {
                        if (result.Count(t => t == s) > 1)
                        {
                            throw new Exception(string.Format(Resources.ServiceExtensionCannotApplyExtensionsInSameType, s));
                        }
                    }
                }

                Deployment       deployment   = Channel.GetDeploymentBySlot(CurrentSubscription.SubscriptionId, ServiceName, Slot);
                ExtensionManager extensionMgr = new ExtensionManager(Channel, CurrentSubscription.SubscriptionId, ServiceName);
                ExtensionConfigurationBuilder configBuilder = extensionMgr.GetBuilder();
                foreach (ExtensionConfigurationInput context in ExtensionConfiguration)
                {
                    if (context != null)
                    {
                        if (context.X509Certificate != null)
                        {
                            var operationDescription = string.Format(Resources.ServiceExtensionUploadingCertificate, CommandRuntime, context.X509Certificate.Thumbprint);
                            ExecuteClientActionInOCS(null, operationDescription, s => this.Channel.AddCertificates(s, this.ServiceName, CertUtils.Create(context.X509Certificate)));
                        }

                        ExtensionConfiguration currentConfig = extensionMgr.InstallExtension(context, Slot, deployment.ExtensionConfiguration);
                        foreach (var r in currentConfig.AllRoles)
                        {
                            if (!extensionMgr.GetBuilder(deployment.ExtensionConfiguration).ExistAny(r.Id))
                            {
                                configBuilder.AddDefault(r.Id);
                            }
                        }
                        foreach (var r in currentConfig.NamedRoles)
                        {
                            foreach (var e in r.Extensions)
                            {
                                if (!extensionMgr.GetBuilder(deployment.ExtensionConfiguration).ExistAny(e.Id))
                                {
                                    configBuilder.Add(r.RoleName, e.Id);
                                }
                            }
                        }
                    }
                }
                extConfig = configBuilder.ToConfiguration();
            }

            // Upgrade Parameter Set
            if (string.Compare(ParameterSetName, "Upgrade", StringComparison.OrdinalIgnoreCase) == 0)
            {
                bool removePackage = false;
                CurrentSubscription = this.GetCurrentSubscription();
                var storageName = CurrentSubscription.CurrentStorageAccount;

                Uri packageUrl = null;
                if (Package.StartsWith(Uri.UriSchemeHttp, StringComparison.OrdinalIgnoreCase) ||
                    Package.StartsWith(Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase))
                {
                    packageUrl = new Uri(Package);
                }
                else
                {
                    if (string.IsNullOrEmpty(storageName))
                    {
                        throw new ArgumentException(Resources.CurrentStorageAccountIsNotSet);
                    }

                    var progress = new ProgressRecord(0, Resources.WaitForUploadingPackage, Resources.UploadingPackage);
                    WriteProgress(progress);
                    removePackage = true;
                    InvokeInOperationContext(() => packageUrl = RetryCall(s => AzureBlob.UploadPackageToBlob(this.Channel, storageName, s, Package, null)));
                }

                var upgradeDeploymentInput = new UpgradeDeploymentInput
                {
                    Mode                   = Mode ?? UpgradeType.Auto,
                    Configuration          = configString,
                    ExtensionConfiguration = extConfig,
                    PackageUrl             = packageUrl,
                    Label                  = Label ?? ServiceName,
                    Force                  = Force.IsPresent
                };

                if (!string.IsNullOrEmpty(RoleName))
                {
                    upgradeDeploymentInput.RoleToUpgrade = RoleName;
                }

                using (new OperationContextScope(Channel.ToContextChannel()))
                {
                    try
                    {
                        ExecuteClientAction(upgradeDeploymentInput, CommandRuntime.ToString(), s => this.Channel.UpgradeDeploymentBySlot(s, this.ServiceName, this.Slot, upgradeDeploymentInput));
                        if (removePackage == true)
                        {
                            this.RetryCall(s =>
                                           AzureBlob.DeletePackageFromBlob(
                                               this.Channel,
                                               storageName,
                                               s,
                                               packageUrl));
                        }
                    }
                    catch (ServiceManagementClientException ex)
                    {
                        this.WriteErrorDetails(ex);
                    }
                }
            }
            else if (string.Compare(this.ParameterSetName, "Config", StringComparison.OrdinalIgnoreCase) == 0)
            {
                // Config parameter set
                var changeConfiguration = new ChangeConfigurationInput
                {
                    Configuration          = configString,
                    ExtensionConfiguration = extConfig
                };

                ExecuteClientActionInOCS(changeConfiguration, CommandRuntime.ToString(), s => this.Channel.ChangeConfigurationBySlot(s, this.ServiceName, this.Slot, changeConfiguration));
            }
            else
            {
                // Status parameter set
                var updateDeploymentStatus = new UpdateDeploymentStatusInput
                {
                    Status = this.NewStatus
                };

                ExecuteClientActionInOCS(null, CommandRuntime.ToString(), s => this.Channel.UpdateDeploymentStatusBySlot(s, this.ServiceName, this.Slot, updateDeploymentStatus));
            }
        }