示例#1
0
        public void DeleteVm()
        {
            _eventLog = new EventLog();
            _cmpDbConnectionString = GetCmpContextConnectionStringFromConfig();
            CmpService service = new CmpService(_eventLog, _cmpDbConnectionString, "");

            service.VmStop(25);
        }
示例#2
0
        public void ResizeVm()
        {
            _eventLog = new EventLog();
            _cmpDbConnectionString = GetCmpContextConnectionStringFromConfig();
            CmpService service = new CmpService(_eventLog, _cmpDbConnectionString, "");

            service.VmResize(20, "D12");

            // VmOps ops = new VmOps();
        }
示例#3
0
        public override object Execute()
        {
            if (AllQueuesBlocked)
            {
                return(0);
            }

            var cdb = new CmpDb(CmpDbConnectionString);
            List <Models.VmDeploymentRequest> vmReqList = null;

            try
            {
                vmReqList = cdb.FetchVmDepRequests(
                    Constants.StatusEnum.QcVmRequest.ToString(), true);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in ProcessTransferedSubmissions() " +
                                    CmpCommon.Utilities.UnwindExceptionMessages(ex));
            }

            foreach (var vmReq in vmReqList)
            {
                try
                {
                    if (null == vmReq)
                    {
                        LogThis(new Exception("Unable to fetch body content from queue"),
                                EventLogEntryType.Error, "Exception in ProcessQcVmRequests", 1, 1);
                        continue;
                    }

                    var vmc =
                        CmpInterfaceModel.Models.VmConfig.Deserialize(vmReq.Config);

                    if (null == vmc)
                    {
                        vmReq.StatusMessage = "VmDeploymentRequest(" + vmReq.ID + ").Config could not de deserialized";
                        vmReq.StatusCode    = Constants.StatusEnum.Rejected.ToString();
                        cdb.SetVmDepRequestStatus(vmReq, null);

                        continue;
                    }

                    if (!PerformValidation)
                    {
                        if (null == vmc.ValidationConfig)
                        {
                            vmc.ValidationConfig = new ValidateSpec();
                        }

                        vmc.ValidationConfig.Validate = false;
                    }

                    if (null != vmc.ValidationConfig)
                    {
                        if (false == vmc.ValidationConfig.Validate)
                        {
                            if (vmReq.RequestType.Equals(Constants.RequestTypeEnum.NewVM.ToString()))
                            {
                                //*** for net new, update the status and move to the next queue ***
                                vmReq.StatusMessage = "Validation Bypassed";
                                vmReq.StatusCode    = Constants.StatusEnum.QcVmRequestPassed.ToString();
                            }
                            else
                            {
                                //*** for migrations, update the status but don't move to the next queue ***
                                vmReq.StatusMessage = "Validation Bypassed, ready for migration";
                                vmReq.StatusCode    = Constants.StatusEnum.ReadyForConversion.ToString();

                                //*** Update the VmMigrationRequest status ***
                                var cmpS = new CmpService(EventLog, CmpDbConnectionString, AftsDbConnectionString);
                                cmpS.UpdateVmMigrationRequest(vmReq, cdb);
                            }
                        }
                    }

                    //complete the processing of the message
                    cdb.SetVmDepRequestStatus(vmReq, null);
                }
                catch (Exception ex)
                {
                    Utilities.SetVmReqExceptionType(vmReq,
                                                    CmpInterfaceModel.Constants.RequestExceptionTypeCodeEnum.Admin);

                    LogThis(ex, EventLogEntryType.Error, "Exception in ProcessQcVmRequests", 1, 1);
                }
            }

            return(0);
        }
示例#4
0
        public override object Execute()
        {
            if (AllQueuesBlocked)
            {
                return(0);
            }

            var cdb = new CmpDb(CmpDbConnectionString);
            List <Models.VmDeploymentRequest> vmReqList = null;

            try
            {
                vmReqList = cdb.FetchVmDepRequests(
                    Constants.StatusEnum.ContactingVM.ToString(), true);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in ProcessContactVm() " +
                                    CmpCommon.Utilities.UnwindExceptionMessages(ex));
            }

            foreach (var vmReq in vmReqList)
            {
                try
                {
                    string remotePsUrl;
                    PowershellLib.VirtualMachineRemotePowerShell.RpcPortVisibility rpcPortVisibility;

                    //*** Ignore for now
                    try
                    {
                        if (DisableSmartCardAuth)
                        {
                            AzureAdminClientLib.VmOps.DisableSmartCardAuth(vmReq.TargetVmName);
                        }
                    }
                    catch (Exception)
                    {
                    }

                    //AssertIfTimedOut(vmReq, _ContactingVmMinutesTTL, Constants.StatusEnum.ContactingVM.ToString());
                    var ppidr = TestPsConnection(vmReq, out remotePsUrl, out rpcPortVisibility);

                    if (Constants.PostProvInintDisksResultEnum.Success != ppidr)
                    {
                        if (HasTimedOut(vmReq, ContactingVmMinutesTTL))
                        {
                            DeleteVm(vmReq, DeleteDwelltimeMinutes, true, false);
                            ResubmitRequest(vmReq, "Timeout " + Constants.StatusEnum.ContactingVM, cdb, false, false);
                        }

                        continue;
                    }

                    if (null != remotePsUrl)
                    {
                        try
                        {
                            var vmc = CmpInterfaceModel.Models.VmConfig.Deserialize(vmReq.Config);
                            var uri = new System.Uri(remotePsUrl);

                            if (null == vmc.InfoFromVM)
                            {
                                vmc.InfoFromVM = new CmpInterfaceModel.Models.InfoFromVmSpec();
                            }

                            if (null == vmc.PostInfoFromVM)
                            {
                                vmc.PostInfoFromVM = new CmpInterfaceModel.Models.PostInfoFromVmSpec();
                            }

                            vmc.InfoFromVM.VmAddress     = uri.Host;
                            vmc.PostInfoFromVM.VmAddress = uri.Host;

                            /*string POSH_DOMAIN = "(Get-WmiObject Win32_ComputerSystem).Domain";
                             *
                             * //get data by querying the WMI as we cann't depend on the Active directory in this context
                             * vmc.InfoFromVM.MachineDomain = GetPowerShellDataGathering(vmReq, POSH_DOMAIN);*/

                            //UnReserveStaticIpAddress(uri.Host);

                            if (rpcPortVisibility ==
                                PowershellLib.VirtualMachineRemotePowerShell.RpcPortVisibility.PublicHttps)
                            {
                                var cmpS    = new CmpService(EventLog, CmpDbConnectionString, AftsDbConnectionString);
                                var rdpPort = cmpS.FetchServicePort(vmReq.ID, "RDP");

                                if (0 < rdpPort)
                                {
                                    vmc.InfoFromVM.VmAddress     += ":" + rdpPort;
                                    vmc.PostInfoFromVM.VmAddress += ":" + rdpPort;
                                }
                            }

                            vmReq.Config = vmc.Serialize();
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Unable to acquire VM address : " +
                                                Utilities.UnwindExceptionMessages(ex));
                        }
                    }

                    vmReq.CurrentStateStartTime = DateTime.UtcNow;
                    vmReq.ExceptionMessage      = "";
                    vmReq.StatusMessage         = "Contacted VM";
                    vmReq.StatusCode            = Constants.StatusEnum.MovingPagefile.ToString();
                    cdb.SetVmDepRequestStatus(vmReq, null);
                }
                catch (Exception ex)
                {
                    if (null != vmReq)
                    {
                        vmReq.CurrentStateStartTime = DateTime.UtcNow;
                        vmReq.StatusCode            = Constants.StatusEnum.Exception.ToString();
                        vmReq.ExceptionMessage      = Utilities.UnwindExceptionMessages(ex);
                        Utilities.SetVmReqExceptionType(vmReq,
                                                        CmpInterfaceModel.Constants.RequestExceptionTypeCodeEnum.Admin);
                        cdb.SetVmDepRequestStatus(vmReq, null);
                    }
                }
            }

            return(0);
        }
        public override object Execute()
        {
            const bool aftsDeleteSourceAfterTransfer = true;
            const bool aftsOverwriteDestinationBlob  = true;

            //*** leave if Azure containers are not yet synchronized
            if (!ContainersSynchronized)
            {
                return(0);
            }

            if (AllQueuesBlocked)
            {
                return(0);
            }

            var cdb = new CmpDb(CmpDbConnectionString);

            List <Models.VmDeploymentRequest> vmReqList = null;

            try
            {
                vmReqList = cdb.FetchVmDepRequests(
                    Constants.StatusEnum.Converted.ToString(), true);
            }
            catch (Exception ex)
            {
                throw new Exception("Exception in ProcessorVm.ProcessConvertedSubmissions() : " +
                                    CmpCommon.Utilities.UnwindExceptionMessages(ex));
            }

            foreach (var vmReq in vmReqList)
            {
                try
                {
                    if (vmReq.RequestType.Equals(Constants.RequestTypeEnum.NewVM.ToString()))
                    {
                        continue;
                    }

                    //*** Temporary ***
                    vmReq.Config = vmReq.Config.Replace("<IsOS>True</IsOS>", "<IsOS>true</IsOS>");
                    vmReq.Config = vmReq.Config.Replace("<IsOS>False</IsOS>", "<IsOS>false</IsOS>");

                    if (!vmReq.Config.Contains("<IsOS>true</IsOS>"))
                    {
                        throw new Exception("Migration request does not contain a disk spec for an OS disk.");
                    }

                    //*** Get the target service provider account resource group ***
                    vmReq.ServiceProviderResourceGroup = GetTargetServiceProviderAccountResourceGroup(vmReq);

                    //*** Temporary ***
                    //vmReq.ServiceProviderResourceGroup = "DEV02";

                    //*** Get the list of service provider accounts associated with the resource group
                    var servProvAcctList = ServProvAccount.GetAzureServiceAccountList(
                        vmReq.ServiceProviderResourceGroup, CmpDbConnectionString);

                    var vmc = VmConfig.Deserialize(vmReq.Config);

                    if (null == vmc)
                    {
                        vmc = new VmConfig();
                    }

                    //*** Service Name ***

                    if (null == vmc.HostedServiceConfig)
                    {
                        vmc.HostedServiceConfig = new HostedService();
                    }

                    //*** TODO *** Handle a non CMDB case here

                    vmReq.TargetServicename             = GetSafeHostServiceName(vmReq.ParentAppID);
                    vmc.HostedServiceConfig.ServiceName = vmReq.TargetServicename;
                    vmc.HostedServiceConfig.Label       = Util.ToB64(vmc.HostedServiceConfig.ServiceName);
                    vmc.HostedServiceConfig.Description = vmc.CmdbConfig.ApplicationName;
                    //vmc.HostedServiceConfig.AffinityGroup = Constants.AUTOAFFINITYGROUP;
                    vmc.HostedServiceConfig.ExtendedProperties = null;

                    //*** Role Size ***

                    if (null == vmc.AzureVmConfig)
                    {
                        vmc.AzureVmConfig = new AzureVmDeployment();
                    }

                    if (null == vmc.AzureVmConfig.RoleList)
                    {
                        vmc.AzureVmConfig.RoleList = new List <Role>();
                    }

                    if (0 == vmc.AzureVmConfig.RoleList.Count)
                    {
                        vmc.AzureVmConfig.RoleList.Add(new CmpInterfaceModel.Models.PersistentVMRole());
                    }


                    //*** TODO *** Handle a non CMDB case here

                    ((PersistentVMRole)vmc.AzureVmConfig.RoleList[0]).RoleSize = vmc.CmdbConfig.AzureComputeSku;

                    vmReq.Config = vmc.Serialize();

                    //string storageAccountKey = "";

                    Models.ServiceProviderAccount servProdAccount;

                    var resp = GetHostService(vmReq, servProvAcctList, out servProdAccount);

                    if (resp.HadError)
                    {
                        if (resp.Retry)
                        {
                            continue;
                        }

                        throw new Exception("Unable to find/create a host service : " + resp.Body);
                    }

                    var cc = new AzureSubscription();
                    //cc.LoadStorageAccounts(servProdAccount.AccountID, servProdAccount.CertificateThumbprint);

                    //*****************

                    var vmCfg = CmpInterfaceModel.Models.VmConfig.Deserialize(vmReq.Config);

                    if (null == vmCfg.Placement)
                    {
                        throw new Exception("Placement element not found in vmReq.Config");
                    }

                    //servProdAccount.AzStorageContainerUrl = vmCfg.Placement.StorageContainerUrl;

                    //*****************

                    //var storageAcct = cc.GetStorageAccount(servProdAccount.AccountID, servProdAccount.CertificateThumbprint,
                    //    servProdAccount.AzStorageContainerUrl);

                    //********************************
                    //********************************

                    var diskCount = 1;
                    if (null != vmCfg.DiskSpecList)
                    {
                        diskCount += vmCfg.DiskSpecList.Count;
                    }

                    var connection =
                        ServProvAccount.GetAzureServiceAccountConnection(
                            servProdAccount.AccountID, servProdAccount.CertificateThumbprint,
                            servProdAccount.AzureADTenantId, servProdAccount.AzureADClientId, servProdAccount.AzureADClientKey);

                    var so = new AzureAdminClientLib.StorageOps(connection);

                    var container = so.GetLeastUsedContainer(DefaultVhdContainerName,
                                                             null, vmCfg.HostedServiceConfig.Location, BlobsPerContainerLimit,
                                                             VmsPerVnetLimit, diskCount, EnforceAppAgAffinity);

                    if (null == container)
                    {
                        throw new Exception("No suitable containers found in subscription. Suitable containers must be named '" +
                                            DefaultVhdContainerName + "' and must be in an AG with an InUse VNet");
                    }

                    var storageAcct = container.StorageAccount;

                    if (null == storageAcct)
                    {
                        throw new Exception(string.Format("No storage account associated with storage container '{0}' in subscription '{1}' in ServiceProviderResourceGroup '{2}'",
                                                          container.Url, servProdAccount.Name, vmReq.ServiceProviderResourceGroup));
                    }

                    //*** Populate placement ***

                    vmCfg.Placement.StorageContainerUrl = container.Url;
                    vmCfg.Placement.AffinityGroup       = container.StorageAccount.AffinityGroup;
                    vmCfg.Placement.Location            = container.StorageAccount.Location;
                    vmCfg.Placement.VNet      = container.StorageAccount.VirtualNetworksAvailable[0].Name;
                    vmCfg.Placement.Subnet    = servProdAccount.AzSubnet;
                    vmCfg.Placement.DiskCount = diskCount;

                    vmReq.Config = vmCfg.Serialize();


                    var cmp = new CmpService(EventLog, CmpDbConnectionString, AftsDbConnectionString);
                    cmp.InsertAftsRequest(vmReq, container.Url, storageAcct.PrimaryAccessKey,
                                          aftsDeleteSourceAfterTransfer, aftsOverwriteDestinationBlob);

                    vmReq.CurrentStateStartTime    = DateTime.UtcNow;
                    vmReq.ExceptionMessage         = "";
                    vmReq.ExceptionTypeCode        = "";
                    vmReq.StatusCode               = Constants.StatusEnum.ReadyForTransfer.ToString();
                    vmReq.StatusMessage            = "Ready for transfer to Azure storage";
                    vmReq.ServiceProviderAccountID = servProdAccount.ID;

                    so.ReserveContainerSpace(container.Url, diskCount, vmReq.ID, vmReq);

                    cdb.SetVmDepRequestStatus(vmReq, null);
                }
                catch (Exception ex)
                {
                    vmReq.StatusCode       = Constants.StatusEnum.Exception.ToString();
                    vmReq.ExceptionMessage = "Exception in ProcessorVm.ProcessConvertedSubmissions() : " +
                                             Utilities.UnwindExceptionMessages(ex);
                    vmReq.StatusMessage = vmReq.ExceptionMessage;
                    Utilities.SetVmReqExceptionType(vmReq,
                                                    Constants.RequestExceptionTypeCodeEnum.Admin);
                    cdb.SetVmDepRequestStatus(vmReq, null);
                }
            }

            return(0);
        }