Exemplo n.º 1
0
		public static ResultObject GrantWebDeployPublishingAccess(int siteItemId, string accountName, string accountPassword)
		{
			ResultObject result = new ResultObject { IsSuccess = true };

			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "GrantWeDeployPublishingAccess");
				TaskManager.WriteParameter("SiteItemId", siteItemId);

				// load site item
				var item = (WebSite)PackageController.GetPackageItem(siteItemId);

				//
				if (item == null)
				{
					TaskManager.WriteError("Web site not found");
					//
					result.AddError("WEBSITE_NOT_FOUND", null);
					result.IsSuccess = false;
					return result;
				}

				//
				int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
				if (accountCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either demo or inactive");
					//
					result.AddError("DEMO_USER", null);
					result.IsSuccess = false;
					return result;
				}

				// check package
				int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
				if (packageCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either not allowed to access the package or the package inactive");
					//
					result.AddError("NOT_ALLOWED", null);
					result.IsSuccess = false;
					return result;
				}

				//
				WebServer server = GetWebServer(item.ServiceId);

				// Most part of the functionality used to enable Web Deploy publishing correspond to those created for Web Management purposes,
				// so we can re-use the existing functionality to deliver seamless development experience.
				if (server.CheckWebManagementAccountExists(accountName))
				{
					TaskManager.WriteWarning("Account name specified already exists");
					//
					result.AddError("ACCOUNTNAME_PROHIBITED", null);
					result.IsSuccess = false;
					return result;
				}

				// Most part of the functionality used to enable Web Deploy publishing correspond to those created for Web Management purposes,
				// so we can re-use the existing functionality to deliver seamless development experience.
				ResultObject passwResult = server.CheckWebManagementPasswordComplexity(accountPassword);
				if (!passwResult.IsSuccess)
				{
					TaskManager.WriteWarning("Account password does not meet complexity requirements");
					//
					result.ErrorCodes.AddRange(passwResult.ErrorCodes);
					result.IsSuccess = false;
					//
					return result;
				}

				// Execute a call to remote server to enable Web Deploy publishing access for the specified user account
				server.GrantWebDeployPublishingAccess(item.SiteId, accountName, accountPassword);
				// Enable Web Deploy flag for the web site
				item.WebDeploySitePublishingEnabled = true;
				// Remember Web Deploy publishing account
				item.WebDeployPublishingAccount = accountName;
				// Remember Web Deploy publishing password
				item.WebDeployPublishingPassword = CryptoUtils.Encrypt(accountPassword);
				// Put changes in effect
				PackageController.UpdatePackageItem(item);
			}
			catch (Exception ex)
			{
				TaskManager.WriteError(ex);
				//
				result.IsSuccess = false;
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			//
			return result;
		}
Exemplo n.º 2
0
		public static ResultObject ChangeWebDeployPublishingPassword(int siteItemId, string newAccountPassword)
		{
			ResultObject result = new ResultObject { IsSuccess = true };
			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "ChangeWebDeployPublishingPassword");
				TaskManager.WriteParameter("SiteItemId", siteItemId);

				//
				var item = (WebSite)PackageController.GetPackageItem(siteItemId);

				//
				if (item == null)
				{
					TaskManager.WriteWarning("Web site not found");

					//
					result.AddError("WEBSITE_NOT_FOUND", null);
					result.IsSuccess = false;

					return result;
				}

				//
				int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
				if (accountCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either demo or inactive");

					result.AddError("DEMO_USER", null);
					result.IsSuccess = false;

					return result;
				}

				// check package
				int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
				if (packageCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either not allowed to access the package or the package inactive");

					//
					result.AddError("NOT_ALLOWED", null);
					result.IsSuccess = false;

					return result;
				}

				// Revoke access first
				RevokeWebDeployPublishingAccess(siteItemId);
				//
				result = GrantWebDeployPublishingAccess(siteItemId, item.WebDeployPublishingAccount, newAccountPassword);
			}
			catch (Exception ex)
			{
				TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			//
			return result;
		}
Exemplo n.º 3
0
		public new ResultObject CheckWebManagementPasswordComplexity(string accountPassword)
		{
			// Preserve setting to restore it back
			bool adEnabled = ServerSettings.ADEnabled;
			// !!! Bypass AD for WMSVC as it requires full-qualified username to authenticate user
			// against the web server
			ServerSettings.ADEnabled = false;
			
			//
			ResultObject result = new ResultObject { IsSuccess = true };

			//
			if (IdentityCredentialsMode == "IISMNGR")
			{
				InvalidPasswordReason reason = ManagementAuthentication.IsPasswordStrongEnough(accountPassword);
				//
				if (reason != InvalidPasswordReason.NoError)
				{
					result.IsSuccess = false;
					result.AddError(reason.ToString(), new Exception("Password complexity check failed"));
				}
			}
			// Restore setting back
			ServerSettings.ADEnabled = adEnabled;

			//
			return result;
		}
Exemplo n.º 4
0
		public static ResultObject SaveWebDeployPublishingProfile(int siteItemId, int[] serviceItemIds)
		{
			ResultObject result = new ResultObject { IsSuccess = true };

			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "SaveWebDeployPublishingProfile");
				TaskManager.WriteParameter("SiteItemId", siteItemId);

				// load site item
				var item = (WebSite)PackageController.GetPackageItem(siteItemId);

				//
				if (item == null)
				{
					TaskManager.WriteError("Web site not found");
					//
					result.AddError("WEBSITE_NOT_FOUND", null);
					result.IsSuccess = false;
					return result;
				}

				//
				int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
				if (accountCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either demo or inactive");
					//
					result.AddError("DEMO_USER", null);
					result.IsSuccess = false;
					return result;
				}

				// check package
				int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
				if (packageCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either not allowed to access the package or the package inactive");
					//
					result.AddError("NOT_ALLOWED", null);
					result.IsSuccess = false;
					return result;
				}

				// Ensure all service items specified are within the same hosting space
				// This is a logcally correct and secure statement than the other one
				// TO-DO: Uncomment this line before demo!
				//var profileIntegritySucceeded = false;

				var profileIntegritySucceeded = true;
				//
				foreach (int itemId in serviceItemIds)
				{
					try
					{
						//
						PackageController.GetPackageItem(itemId);
						//
						profileIntegritySucceeded = true;
					}
					catch (Exception ex)
					{
						TaskManager.WriteError(ex);
						//
						profileIntegritySucceeded = false;
						// Exit from the loop
						break;
					}
				}

				//
				if (profileIntegritySucceeded == true)
				{
					// Build service items list
					item.WebDeploySitePublishingProfile = String.Join(",", Array.ConvertAll<int, string>(serviceItemIds, (int x) => { return x.ToString(); }));
					// Put changes in effect
					PackageController.UpdatePackageItem(item);
				}
			}
			catch (Exception ex)
			{
				TaskManager.WriteError(ex);
				//
				result.IsSuccess = false;
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			//
			return result;
		}
 public static ResultObject UnsetReplicaServer(int serviceId, string remoteServer)
 {
     ResultObject result = new ResultObject();
     try
     {
         VirtualizationServer2012 vs = GetVirtualizationProxy(serviceId);
         vs.UnsetReplicaServer(remoteServer);
         result.IsSuccess = true;
     }
     catch (Exception ex)
     {
         result.AddError(VirtualizationErrorCodes.UNSET_REPLICA_SERVER_ERROR, ex);
     }
     return result;
 }
        public static ResultObject ResumeReplication(int itemId)
        {
            ResultObject result = new ResultObject();
            try
            {
                VirtualMachine vm = GetVirtualMachineByItemId(itemId);

                // Check Quotas
                ReplicationHelper.CheckReplicationQuota(vm.PackageId, ref result);
                if (result.ErrorCodes.Count > 0) return result;

                VirtualizationServer2012 vs = GetVirtualizationProxy(vm.ServiceId);
                vs.ResumeReplication(vm.VirtualMachineId);

                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                result.AddError(VirtualizationErrorCodes.RESUME_REPLICATION_ERROR, ex);
            }
            return result; 
        }
		public static ResultObject DeleteCertificateRequest(int siteId, int csrID)
		{
			ResultObject result = new ResultObject { IsSuccess = true };

			try
			{
				WebSite item = GetWebSite(siteId);
				PackageInfo service = PackageController.GetPackage(item.PackageId);

				DataProvider.DeleteCertificate(SecurityContext.User.UserId, service.PackageId, csrID);
			}
			catch (Exception ex)
			{

				result.IsSuccess = false;
				result.AddError("610", ex);
			}
			return result;
		}
        public static void CreateVMFromVMAsunc(int packageId, VMInfo vmTemplate, string vmName)
        {
            ResultObject res = new ResultObject();

            Guid taksId = Guid.NewGuid();

            res = TaskManager.StartResultTask<ResultObject>("VPSForPC", "CREATE", taksId);
            TaskManager.ItemName = vmName;
            TaskManager.PackageId = packageId;

            string templateName = vmTemplate.Name;

            vmTemplate.Id = 0;
            vmTemplate.Name = vmName;
            vmTemplate.CurrentTaskId = taksId.ToString("N"); // generate creation task id
            vmTemplate.ProvisioningStatus = VirtualMachineProvisioningStatus.InProgress;

            try
            {
                vmTemplate.Id = PackageController.AddPackageItem(vmTemplate);
            }
            catch (Exception ex)
            {
                res.AddError(VirtualizationErrorCodes.CREATE_META_ITEM_ERROR, ex);
                TaskManager.CompleteResultTask(res);
                throw;
            }

            TaskManager.ItemId = vmTemplate.Id;

            try
            {
                var vs = GetVirtualizationProxy(vmTemplate.ServiceId);
                vmTemplate = vs.CreateVMFromVM(templateName, vmTemplate, taksId);
                PackageController.UpdatePackageItem(vmTemplate);

//                    CreateAsyncVMfromVM.Run(templateName, vmTemplate);
            }
            catch (Exception ex)
            {
                // delete meta item
                PackageController.DeletePackageItem(vmTemplate.Id);

                // return from method
                res.AddError(VirtualizationErrorCodes.CREATE_TASK_START_ERROR, ex);
                TaskManager.CompleteResultTask(res);
                throw;
            }

            if (!String.IsNullOrEmpty(vmTemplate.exMessage))
            {
                TaskManager.CompleteResultTask(res
                            , VirtualizationErrorCodes.CREATE_ERROR
                            , new Exception(vmTemplate.exMessage)
                            , vmTemplate.logMessage);
            }
            else
            {
                TaskManager.CompleteResultTask(res, null, null, vmTemplate.logMessage);
            }
        }
		public static ResultObject InstallPfx(byte[] pfx, int siteItemId, string password)
		{
			ResultObject result = new ResultObject { IsSuccess = true };
			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "installPFX");
				TaskManager.WriteParameter("SiteItemId", siteItemId);

				WebSite item = GetWebSite(siteItemId) as WebSite;
				PackageInfo service = PackageController.GetPackage(item.PackageId);

				TaskManager.WriteParameter("WebSite.Name", item.Name);
				WebServer server = GetWebServer(item.ServiceId);
				TaskManager.WriteParameter("item.ServiceId", item.ServiceId);

                // remove all web site pointers
                List<DomainInfo> pointers = GetWebSitePointers(siteItemId);
                foreach (DomainInfo pointer in pointers)
                    DeleteWebSitePointer(siteItemId, pointer.DomainId, true, true, true);

				SSLCertificate certificate = server.installPFX(pfx, password, item);
				if (certificate.SerialNumber == null)
				{
					result.AddError("Error_Installing_certificate", null);
					result.IsSuccess = false;
				}
				DataProvider.AddPFX(SecurityContext.User.UserId, item.PackageId, item.Id, service.UserId, certificate.Hostname,
				   certificate.FriendlyName, certificate.DistinguishedName, certificate.CSRLength, certificate.SerialNumber,
				   certificate.ValidFrom, certificate.ExpiryDate);


			}
			catch (Exception ex)
			{
				result.IsSuccess = false;
				result.AddError("Error_Installing_certificate", ex);
				TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			return result;
		}
Exemplo n.º 10
0
		public static ResultObject CheckCertificate(int siteId)
		{
			ResultObject result = new ResultObject { IsSuccess = false };
			bool serverResult = false;

			try
			{
				WebSite item = GetWebSite(siteId) as WebSite;
				WebServer server = GetWebServer(item.ServiceId);
				serverResult = server.CheckCertificate(item);
				if (serverResult && !DataProvider.CheckSSLExistsForWebsite(siteId))
				{
					result.IsSuccess = true;
				}
			}
			catch (Exception ex)
			{

				result.IsSuccess = false;
				result.AddError("610", ex);
			}
			return result;
		}
Exemplo n.º 11
0
		public static ResultObject InstallCertificate(SSLCertificate certificate, int siteItemId)
		{
			ResultObject result = new ResultObject { IsSuccess = true };
			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "installCertificate");
				TaskManager.WriteParameter("SiteItemId", siteItemId);
				TaskManager.WriteParameter("Hostname", certificate.Hostname);


				WebSite item = GetWebSite(siteItemId) as WebSite;
				TaskManager.WriteParameter("WebSite.Name", item.Name);
				WebServer server = GetWebServer(item.ServiceId);
				TaskManager.WriteParameter("item.ServiceId", item.ServiceId);

                IPAddressInfo ip = ServerController.GetIPAddress(item.SiteIPAddressId);

                if (ip != null)
                    //item.SiteIPAddress = !String.IsNullOrEmpty(ip.InternalIP) ? ip.InternalIP : ip.ExternalIP;
                    item.SiteIPAddress = ip.ExternalIP;

				certificate = server.installCertificate(certificate, item);
				if (certificate.SerialNumber == null)
				{
					result.AddError("Error_Installing_certificate", null);
					result.IsSuccess = false;
				}


				DataProvider.CompleteSSLRequest(SecurityContext.User.UserId, item.PackageId,
												certificate.id, certificate.Certificate,
												certificate.DistinguishedName, certificate.SerialNumber,
												certificate.Hash, certificate.ValidFrom, certificate.ExpiryDate);
				if (certificate.IsRenewal)
				{
					DataProvider.DeleteCertificate(SecurityContext.User.UserId, item.PackageId, certificate.PreviousId);
				}

			}
			catch (Exception ex)
			{
				result.AddError("0", ex);
				TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			return result;
		}
Exemplo n.º 12
0
        public new ResultObject DeleteCertificate(SSLCertificate certificate, WebSite website)
        {
            // This method removes all https bindings and all certificates associated with them.
            // Old implementation (IIS70) removed a single binding (there could not be more than one) and the first certificate that matched via serial number
            var result = new ResultObject { IsSuccess = true };

            if (certificate == null)
            {
                return result;
            }

            try
            {
                var certificatesAndStoreNames = new List<Tuple<string, byte[]>>();

                // User servermanager to get aLL SSL-bindings on this website and try to remove the certificates used
                using (var srvman = GetServerManager())
                {

                    var site = srvman.Sites[website.Name];
                    var bindings = site.Bindings.Where(b => b.Protocol == "https");

                    foreach (Binding binding in bindings.ToList())
                    {
                        if (binding.SslFlags.HasFlag(SslFlags.CentralCertStore))
                        {
                            if (!string.IsNullOrWhiteSpace(CCSUncPath) && Directory.Exists(CCSUncPath))
                            {
                                // This is where it will be if CCS is used
                                var path = GetCCSPath(certificate.Hostname);
                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }

                                // If binding with hostname, also try to delete with the hostname in the binding
                                // This is because if SNI is used, several bindings are created for every valid name in the cerificate, but only one name exists in the SSLCertificate
                                if (!string.IsNullOrEmpty(binding.Host))
                                {
                                    path = GetCCSPath(binding.Host);
                                    if (File.Exists(path))
                                    {
                                        File.Delete(path);
                                    }
                                }
                            }
                        }
                        else
                        {
                            var certificateAndStoreName = new Tuple<string, byte[]>(binding.CertificateStoreName, binding.CertificateHash);

                            if (!string.IsNullOrEmpty(binding.CertificateStoreName) && !certificatesAndStoreNames.Contains(certificateAndStoreName))
                            {
                                certificatesAndStoreNames.Add(certificateAndStoreName);
                            }
                        }

                        // Remove binding from site
                        site.Bindings.Remove(binding);
                    }

                    srvman.CommitChanges();

                    foreach (var certificateAndStoreName in certificatesAndStoreNames)
                    {
                        // Delete all certs with the same serialnumber in Store
                        var store = new X509Store(certificateAndStoreName.Item1, StoreLocation.LocalMachine);
                        store.Open(OpenFlags.MaxAllowed);

                        var certs = store.Certificates.Find(X509FindType.FindByThumbprint, BitConverter.ToString(certificateAndStoreName.Item2).Replace("-", ""), false);
                        foreach (var cert in certs)
                        {
                            store.Remove(cert);
                        }

                        store.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteError(String.Format("Unable to delete certificate for website {0}", website.Name), ex);
                result.IsSuccess = false;
                result.AddError("", ex);
            }

            return result;
        }
Exemplo n.º 13
0
		public ResultObject DeleteCertificate(SSLCertificate certificate, WebSite website)
		{
			ResultObject result = new ResultObject();

			try
			{
				X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);

				store.Open(OpenFlags.MaxAllowed);

				X509Certificate2 cert =
					store.Certificates.Find(X509FindType.FindBySerialNumber, certificate.SerialNumber, false)[0];
				store.Remove(cert);

				store.Close();
				RemoveBinding(certificate, website);

				result.IsSuccess = true;
			}
			catch (Exception ex)
			{
				result.IsSuccess = false;
				result.AddError("", ex);
			}
			return result;
		}
Exemplo n.º 14
0
		public static ResultObject GrantWebManagementAccess(int siteItemId, string accountName, string accountPassword)
		{
			ResultObject result = new ResultObject { IsSuccess = true };

			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "GrantWebManagementAccess");
				TaskManager.WriteParameter("SiteItemId", siteItemId);
				
				//
				WebSite item = GetWebSite(siteItemId);
				
				//
				if (item == null)
				{
					TaskManager.WriteError("Web site not found");
					//
					result.AddError("WEBSITE_NOT_FOUND", null);
					result.IsSuccess = false;
					return result;
				}

				//
				int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
				if (accountCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either demo or inactive");
					//
					result.AddError("DEMO_USER", null);
					result.IsSuccess = false;
					return result;
				}

				// check package
				int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
				if (packageCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either not allowed to access the package or the package inactive");
					//
					result.AddError("NOT_ALLOWED", null);
					result.IsSuccess = false;
					return result;
				}
				
				//
				WebServer server = GetWebServer(item.ServiceId);

				//
				if (server.CheckWebManagementAccountExists(accountName))
				{
					TaskManager.WriteWarning("Account name specified already exists");
					//
					result.AddError("ACCOUNTNAME_PROHIBITED", null);
					result.IsSuccess = false;
					return result;
				}

				//
				ResultObject passwResult = server.CheckWebManagementPasswordComplexity(accountPassword);
				if (!passwResult.IsSuccess)
				{
					TaskManager.WriteWarning("Account password does not meet complexity requirements");
					//
					result.ErrorCodes.AddRange(passwResult.ErrorCodes);
					result.IsSuccess = false;
					//
					return result;
				}
				
				//
				server.GrantWebManagementAccess(item.SiteId, accountName, accountPassword);
			}
			catch (Exception ex)
			{
				TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			//
			return result;
		}
 public static ResultObject SetReplicaServer(int serviceId, string remoteServer, string thumbprint, string storagePath)
 {
     ResultObject result = new ResultObject();
     try
     {
         if (string.IsNullOrEmpty(storagePath))
             throw new Exception("Please enter replication path"); 
         
         VirtualizationServer2012 vs = GetVirtualizationProxy(serviceId);
         vs.SetReplicaServer(remoteServer, thumbprint, storagePath);
         result.IsSuccess = true;
     }
     catch (Exception ex)
     {
         result.AddError(VirtualizationErrorCodes.SET_REPLICA_SERVER_ERROR, ex);
     }
     return result;
 }
Exemplo n.º 16
0
		public static ResultObject ChangeWebManagementAccessPassword(int siteItemId, string accountPassword)
		{
			ResultObject result = new ResultObject { IsSuccess = true };
			try
			{
				TaskManager.StartTask(LOG_SOURCE_WEB, "ChangeWebManagementAccessPassword");
				TaskManager.WriteParameter("SiteItemId", siteItemId);

				//
				WebSite item = GetWebSite(siteItemId) as WebSite;

				//
				if (item == null)
				{
					TaskManager.WriteWarning("Web site not found");

					//
					result.AddError("WEBSITE_NOT_FOUND", null);
					result.IsSuccess = false;
					
					return result;
				}

				//
				int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo | DemandAccount.IsActive);
				if (accountCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either demo or inactive");

					result.AddError("DEMO_USER", null);
					result.IsSuccess = false;

					return result;
				}

				// check package
				int packageCheck = SecurityContext.CheckPackage(item.PackageId, DemandPackage.IsActive);
				if (packageCheck < 0)
				{
					TaskManager.WriteWarning("Current user is either not allowed to access the package or the package inactive");

					//
					result.AddError("NOT_ALLOWED", null);
					result.IsSuccess = false;

					return result;
				}

				//
				string accountName = item.GetValue<string>(WebSite.WmSvcAccountName);
				//
				if (String.IsNullOrEmpty(accountName))
				{
					TaskManager.WriteWarning("WebManagement Access account name is either not set or empty");

					result.AddError("EMPTY_WMSVC_ACCOUNT", null);
					result.IsSuccess = false;

					return result;
				}

				//
				WebServer server = GetWebServer(item.ServiceId);
				
				//
				ResultObject passwResult = server.CheckWebManagementPasswordComplexity(accountPassword);
				if (!passwResult.IsSuccess)
				{
					TaskManager.WriteWarning("Account password does not meet complexity requirements");
					//
					result.ErrorCodes.AddRange(passwResult.ErrorCodes);
					result.IsSuccess = false;
					//
					return result;
				}
				//
				server.ChangeWebManagementAccessPassword(accountName, accountPassword);
			}
			catch (Exception ex)
			{
				TaskManager.WriteError(ex);
			}
			finally
			{
				TaskManager.CompleteTask();
			}
			//
			return result;
		}
        //#region VPS Create
        public static ResultObject CreateVMFromVM(int packageId, VMInfo vmTemplate, string vmName)
        {
            ResultObject res = new ResultObject();

            // load service item
            if (!SecurityContext.CheckAccount(res, DemandAccount.NotDemo | DemandAccount.IsActive))
                return res;

            // check package
            if (!SecurityContext.CheckPackage(res, packageId, DemandPackage.IsActive))
                return res;

            #region Check if host name is already used

            try
            {
                ServiceProviderItem item = PackageController.GetPackageItemByName(packageId, vmName,
                                                                                  typeof(VMInfo));

                if (item != null)
                {
                    res.ErrorCodes.Add(VirtualizationErrorCodes.HOST_NAMER_IS_ALREADY_USED);
                    return res;
                }
            }
            catch (Exception ex)
            {
                res.AddError(VirtualizationErrorCodes.CANNOT_CHECK_HOST_EXISTS, ex);
                return res;
            }

            #endregion

            try
            {
                CreateVMFromVMAsyncWorker worker = new CreateVMFromVMAsyncWorker();

                worker.ThreadUserId = SecurityContext.User.UserId;
                worker.vmTemplate = vmTemplate;
                worker.vmName = vmName;
                worker.packageId = packageId;

                worker.CreateAsync();
            }
            catch (Exception ex)
            {
                res.AddError(VirtualizationErrorCodes.CREATE_ERROR, ex);
                return res;
            }

            res.IsSuccess = true;
            return res;
        }