Ejemplo n.º 1
0
        public List <string> GetImportableItems(int packageId, int itemTypeId,
                                                Type itemType, ResourceGroupInfo group)
        {
            List <string> items = new List <string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return(items);
            }

            DatabaseServer db = GetDatabaseServer(serviceId);

            if (itemType == typeof(SqlDatabase))
            {
                items.AddRange(db.GetDatabases());
            }
            else if (itemType == typeof(SqlUser))
            {
                items.AddRange(db.GetUsers());
            }

            return(items);
        }
Ejemplo n.º 2
0
        public static int BackupItem(string tempFolder, XmlWriter writer, ServiceProviderItem item)
        {
            // load item type
            ServiceProviderItemType itemType = PackageController.GetServiceItemType(item.TypeId);

            if (!itemType.Backupable)
            {
                return(-1);
            }

            // load group
            ResourceGroupInfo group = ServerController.GetResourceGroup(itemType.GroupId);

            // create controller
            IBackupController controller = null;

            try
            {
                controller = Activator.CreateInstance(Type.GetType(group.GroupController)) as IBackupController;
                if (controller != null)
                {
                    return(BackupItem(tempFolder, writer, item, group, controller));
                }
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }

            return(-2);
        }
Ejemplo n.º 3
0
        public static List <ServiceProviderItemType> GetImportableItemTypes(int packageId)
        {
            // load all service item types
            List <ServiceProviderItemType> itemTypes = PackageController.GetServiceItemTypes();

            // load package context
            PackageContext cntx = PackageController.GetPackageContext(packageId);

            // build importable items list
            List <ServiceProviderItemType> importableTypes = new List <ServiceProviderItemType>();

            foreach (ServiceProviderItemType itemType in itemTypes)
            {
                if (!itemType.Importable)
                {
                    continue;
                }

                // load group
                ResourceGroupInfo group = ServerController.GetResourceGroup(itemType.GroupId);
                if (cntx.Groups.ContainsKey(group.GroupName))
                {
                    importableTypes.Add(itemType);
                }
            }
            return(importableTypes);
        }
Ejemplo n.º 4
0
        public void ImportItem(int packageId, int itemTypeId,
                               Type itemType, ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            if (itemType == typeof(SqlDatabase))
            {
                // import database
                SqlDatabase db = new SqlDatabase();
                db.ServiceId = serviceId;
                db.PackageId = packageId;
                db.Name      = itemName;
                db.GroupName = group.GroupName;
                PackageController.AddPackageItem(db);
            }
            else if (itemType == typeof(SqlUser))
            {
                // import user
                SqlUser user = new SqlUser();
                user.ServiceId = serviceId;
                user.PackageId = packageId;
                user.Name      = itemName;
                user.GroupName = group.GroupName;
                user.Password  = "";
                PackageController.AddPackageItem(user);
            }
        }
Ejemplo n.º 5
0
        public void ImportItem(int packageId, int itemTypeId,
                               Type itemType, ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            // FTP provider
            FTPServer ftp = new FTPServer();

            ServiceProviderProxy.Init(ftp, serviceId);

            if (itemType == typeof(FtpAccount))
            {
                // load FTP account
                FtpAccount account = ftp.GetAccount(itemName);
                account.Folder = FilesController.GetFullPackagePath(packageId, "\\");     // root space folder

                // update FTP account
                ftp.UpdateAccount(account);

                // save account
                account.ServiceId = serviceId;
                account.PackageId = packageId;
                PackageController.AddPackageItem(account);
            }
        }
Ejemplo n.º 6
0
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
                               ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            StatisticsServer stats = new StatisticsServer();

            ServiceProviderProxy.Init(stats, serviceId);

            if (itemType == typeof(StatsSite))
            {
                // import statistics site
                StatsSite site = new StatsSite();
                site.ServiceId = serviceId;
                site.PackageId = packageId;
                site.Name      = itemName;
                site.GroupName = group.GroupName;

                // load site id
                site.SiteId = stats.GetSiteId(itemName);

                PackageController.AddPackageItem(site);
            }
        }
Ejemplo n.º 7
0
        private static int BackupItem(string tempFolder, XmlWriter writer,
                                      ServiceProviderItem item, ResourceGroupInfo group, IBackupController controller)
        {
            writer.WriteStartElement("Item");
            writer.WriteAttributeString("itemId", item.Id.ToString());
            writer.WriteAttributeString("itemTypeId", item.TypeId.ToString());
            writer.WriteAttributeString("itemName", item.Name);
            writer.WriteAttributeString("packageId", item.PackageId.ToString());
            writer.WriteAttributeString("serviceId", item.ServiceId.ToString());

            try
            {
                return(controller.BackupItem(tempFolder, writer, item, group));
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }
            finally
            {
                writer.WriteEndElement(); // Item
            }

            return(0);
        }
Ejemplo n.º 8
0
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
                               ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            if (itemType == typeof(SystemUser))
            {
                // import user
                SystemUser user = new SystemUser();
                user.ServiceId = serviceId;
                user.PackageId = packageId;
                user.Name      = itemName;
                user.Password  = "";
                user.GroupName = group.GroupName;
                PackageController.AddPackageItem(user);
            }
            else if (itemType == typeof(SystemGroup))
            {
                // import group
                SystemGroup spGroup = new SystemGroup();
                spGroup.ServiceId = serviceId;
                spGroup.PackageId = packageId;
                spGroup.Name      = itemName;
                spGroup.GroupName = group.GroupName;
                PackageController.AddPackageItem(spGroup);
            }
        }
Ejemplo n.º 9
0
        public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType,
                               string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType != typeof(DnsZone))
            {
                return(0);
            }

            // DNS provider
            DNSServer dns = GetDNSServer(serviceId);

            // check service item
            if (!dns.ZoneExists(itemName))
            {
                // create primary and secondary zones
                AddZone(packageId, serviceId, itemName, false, false);

                // restore records
                XmlSerializer    serializer = new XmlSerializer(typeof(DnsRecord));
                List <DnsRecord> records    = new List <DnsRecord>();
                foreach (XmlNode childNode in itemNode.ChildNodes)
                {
                    if (childNode.Name == "DnsRecord")
                    {
                        records.Add((DnsRecord)serializer.Deserialize(new XmlNodeReader(childNode)));
                    }
                }

                dns.AddZoneRecords(itemName, records.ToArray());
            }

            // check if meta-item exists
            int     zoneId = 0;
            DnsZone item   = (DnsZone)PackageController.GetPackageItemByName(packageId, itemName, typeof(DnsZone));

            if (item == null)
            {
                // restore meta-item
                item           = new DnsZone();
                item.Name      = itemName;
                item.PackageId = packageId;
                item.ServiceId = serviceId;
                zoneId         = PackageController.AddPackageItem(item);
            }
            else
            {
                zoneId = item.Id;
            }

            // restore domains
            RestoreDomainByZone(itemName, packageId, zoneId);

            return(0);
        }
Ejemplo n.º 10
0
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
                               ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            if (itemType == typeof(SystemDSN))
            {
                // save DSN info
                SystemDSN dsn = new SystemDSN();
                dsn.Name      = itemName;
                dsn.ServiceId = serviceId;
                dsn.PackageId = packageId;
                PackageController.AddPackageItem(dsn);
            }
        }
Ejemplo n.º 11
0
        public List <string> GetImportableItems(int packageId, int itemTypeId, Type itemType,
                                                ResourceGroupInfo group)
        {
            List <string> items = new List <string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return(items);
            }

            OS.OperatingSystem os = GetOS(serviceId);
            if (itemType == typeof(SystemDSN))
            {
                items.AddRange(os.GetDSNNames());
            }

            return(items);
        }
Ejemplo n.º 12
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(StatsSite))
            {
                StatisticsServer stats = GetStatisticsServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(StatsSite));
                StatsSite     site       = (StatsSite)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("StatsSite")));

                // create site if required
                if (stats.GetSite(site.SiteId) == null)
                {
                    stats.AddSite(site);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(StatsSite)) == null)
                {
                    site.PackageId = packageId;
                    site.ServiceId = serviceId;
                    PackageController.AddPackageItem(site);
                }
            }

            return(0);
        }
Ejemplo n.º 13
0
        public int BackupItem(string tempFolder, System.Xml.XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is StatsSite)
            {
                // backup stats site
                StatisticsServer stats = GetStatisticsServer(item.ServiceId);

                // read site info
                StatsSite itemSite = item as StatsSite;
                StatsSite site     = stats.GetSite(itemSite.SiteId);

                XmlSerializer serializer = new XmlSerializer(typeof(StatsSite));
                serializer.Serialize(writer, site);
            }
            return(0);
        }
Ejemplo n.º 14
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SystemUser))
            {
                SharePointServer sps = GetSharePoint(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemUser));
                SystemUser    user       = (SystemUser)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemUser")));

                // create user if required
                if (!sps.UserExists(itemName))
                {
                    user.Password = CryptoUtils.Decrypt(user.Password);
                    sps.CreateUser(user);

                    // restore password
                    user.Password = CryptoUtils.Encrypt(user.Password);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemUser)) == null)
                {
                    user.PackageId = packageId;
                    user.ServiceId = serviceId;
                    PackageController.AddPackageItem(user);
                }
            }
            else if (itemType == typeof(SystemGroup))
            {
                SharePointServer sps = GetSharePoint(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemGroup));
                SystemGroup   sysGroup   = (SystemGroup)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemGroup")));

                // create user if required
                if (!sps.GroupExists(itemName))
                {
                    sps.CreateGroup(sysGroup);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemGroup)) == null)
                {
                    sysGroup.PackageId = packageId;
                    sysGroup.ServiceId = serviceId;
                    PackageController.AddPackageItem(sysGroup);
                }
            }

            return(0);
        }
Ejemplo n.º 15
0
        public List <string> GetImportableItems(int packageId, int itemTypeId, Type itemType, ResourceGroupInfo group)
        {
            List <string> items = new List <string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return(items);
            }

            // Mail provider
            StatisticsServer stats = new StatisticsServer();

            ServiceProviderProxy.Init(stats, serviceId);

            if (itemType == typeof(StatsSite))
            {
                items.AddRange(stats.GetSites());
            }

            return(items);
        }
Ejemplo n.º 16
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(FtpAccount))
            {
                FTPServer ftp = GetFTPServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(FtpAccount));
                FtpAccount    account    = (FtpAccount)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("FtpAccount")));

                // create DSN if required
                if (!ftp.AccountExists(itemName))
                {
                    account.Password = CryptoUtils.Decrypt(account.Password);
                    ftp.CreateAccount(account);

                    // restore password
                    account.Password = CryptoUtils.Encrypt(account.Password);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(FtpAccount)) == null)
                {
                    account.PackageId = packageId;
                    account.ServiceId = serviceId;
                    PackageController.AddPackageItem(account);
                }
            }

            return(0);
        }
Ejemplo n.º 17
0
        public int BackupItem(string tempFolder, System.Xml.XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is FtpAccount)
            {
                // backup FTP account
                FTPServer ftp = GetFTPServer(item.ServiceId);

                // read FTP account info
                FtpAccount account = ftp.GetAccount(item.Name);
                account.Password = ((FtpAccount)item).Password;

                XmlSerializer serializer = new XmlSerializer(typeof(FtpAccount));
                serializer.Serialize(writer, account);
            }
            return(0);
        }
Ejemplo n.º 18
0
        public static int RestoreInternal(string taskId, int userId, int packageId, int serviceId, int serverId,
                                          int storePackageId, string storePackageBackupPath, string storeServerBackupPath)
        {
            try
            {
                // copy backup from remote or local server
                string backupFileName = (storePackageId > 0)
                                        ? Path.GetFileName(storePackageBackupPath) : Path.GetFileName(storeServerBackupPath);

                TaskManager.StartTask(taskId, "BACKUP", "RESTORE", backupFileName, SecurityContext.User.UserId);

                // create temp folder
                string tempFolder = GetTempBackupFolder();

                string backupFileNamePath = Path.Combine(tempFolder, backupFileName);
                if (storePackageId > 0)
                {
                    try
                    {
                        int osServiceId = PackageController.GetPackageServiceId(storePackageId, ResourceGroups.Os);
                        if (osServiceId > 0)
                        {
                            OS.OperatingSystem os = new OS.OperatingSystem();
                            ServiceProviderProxy.Init(os, osServiceId);

                            string remoteBackupPath = FilesController.GetFullPackagePath(storePackageId,
                                                                                         storePackageBackupPath);

                            FileStream stream = new FileStream(backupFileNamePath, FileMode.Create, FileAccess.Write);

                            byte[] buffer = new byte[FILE_BUFFER_LENGTH];
                            int    offset = 0;
                            do
                            {
                                // read remote content
                                buffer = os.GetFileBinaryChunk(remoteBackupPath, offset, FILE_BUFFER_LENGTH);

                                // write remote content
                                stream.Write(buffer, 0, buffer.Length);

                                offset += FILE_BUFFER_LENGTH;
                            }while (buffer.Length == FILE_BUFFER_LENGTH);
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Can't copy source backup set");
                        return(0);
                    }
                }
                else
                {
                    backupFileNamePath = storeServerBackupPath;
                }

                try
                {
                    // unpack archive
                    FileUtils.UnzipFiles(backupFileNamePath, tempFolder);
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Can't unzip backup set");
                    return(0);
                }

                // load backup catalog
                XmlDocument doc = new XmlDocument();

                try
                {
                    doc.Load(Path.Combine(tempFolder, BACKUP_CATALOG_FILE_NAME));
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Can't find/open backup catalog file");
                    return(0);
                }

                // validate XML document
                //if (!ValidateXmlDocument(doc))
                //{
                //    TaskManager.WriteError("Corrupted or altered backup catalog file has been read");
                //    return 0;
                //}

                // get the list of items to restore
                string condition = "";
                if (userId > 0)
                {
                    // get user spaces
                    List <PackageInfo> packages = new List <PackageInfo>();
                    packages.AddRange(PackageController.GetMyPackages(userId));
                    packages.AddRange(PackageController.GetPackages(userId));
                    List <string> parts = new List <string>();
                    foreach (PackageInfo package in packages)
                    {
                        parts.Add("@packageId = " + package.PackageId.ToString());
                    }
                    condition = "[" + String.Join(" or ", parts.ToArray()) + "]";
                }
                else if (packageId > 0)
                {
                    condition = "[@packageId = " + packageId + "]";
                }
                else if (serviceId > 0)
                {
                    condition = "[@serviceId = " + serviceId + "]";
                }
                else if (serverId > 0)
                {
                    // get server services
                    List <ServiceInfo> services = ServerController.GetServicesByServerId(serverId);
                    List <string>      parts    = new List <string>();
                    foreach (ServiceInfo service in services)
                    {
                        parts.Add("@serviceId = " + service.ServiceId.ToString());
                    }
                    condition = "[" + String.Join(" or ", parts.ToArray()) + "]";
                }

                XmlNodeList itemNodes = doc.SelectNodes("Backup/Items/Item" + condition);

                TaskManager.IndicatorMaximum = itemNodes.Count;
                TaskManager.IndicatorCurrent = 0;

                // group items by item types
                Dictionary <int, List <XmlNode> > groupedItems = new Dictionary <int, List <XmlNode> >();

                // sort by groups
                foreach (XmlNode itemNode in itemNodes)
                {
                    int itemTypeId = Utils.ParseInt(itemNode.Attributes["itemTypeId"].Value, 0);
                    // add to group
                    if (!groupedItems.ContainsKey(itemTypeId))
                    {
                        groupedItems[itemTypeId] = new List <XmlNode>();
                    }

                    groupedItems[itemTypeId].Add(itemNode);
                }

                // restore grouped items
                foreach (int itemTypeId in groupedItems.Keys)
                {
                    // load item type
                    ServiceProviderItemType itemTypeInfo = PackageController.GetServiceItemType(itemTypeId);
                    if (!itemTypeInfo.Backupable)
                    {
                        continue;
                    }

                    Type itemType = Type.GetType(itemTypeInfo.TypeName);

                    // load group
                    ResourceGroupInfo group = ServerController.GetResourceGroup(itemTypeInfo.GroupId);

                    // instantiate controller
                    IBackupController controller = null;
                    try
                    {
                        controller = Activator.CreateInstance(Type.GetType(group.GroupController)) as IBackupController;
                        if (controller != null)
                        {
                            // backup items
                            foreach (XmlNode itemNode in groupedItems[itemTypeId])
                            {
                                int    itemId        = Utils.ParseInt(itemNode.Attributes["itemId"].Value, 0);
                                string itemName      = itemNode.Attributes["itemName"].Value;
                                int    itemPackageId = Utils.ParseInt(itemNode.Attributes["packageId"].Value, 0);
                                int    itemServiceId = Utils.ParseInt(itemNode.Attributes["serviceId"].Value, 0);

                                TaskManager.Write(String.Format("Restore {0} '{1}'",
                                                                itemTypeInfo.DisplayName, itemName));

                                try
                                {
                                    int restoreResult = controller.RestoreItem(tempFolder, itemNode,
                                                                               itemId, itemType, itemName, itemPackageId, itemServiceId, group);
                                }
                                catch (Exception ex)
                                {
                                    TaskManager.WriteError(ex, "Can't restore item");
                                }

                                TaskManager.IndicatorCurrent++;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex);
                    }
                }

                // delete backup folder and all its contents
                try
                {
                    Directory.Delete(tempFolder, true);
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Can't delete temporary backup folder");
                    return(0);
                }
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }

            return(0);
        }
Ejemplo n.º 19
0
        public List <string> GetImportableItems(int packageId, int itemTypeId, Type itemType, ResourceGroupInfo group)
        {
            List <string> items = new List <string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return(items);
            }

            // FTP provider
            FTPServer ftp = new FTPServer();

            ServiceProviderProxy.Init(ftp, serviceId);

            FtpAccount[] accounts = ftp.GetAccounts();

            foreach (FtpAccount account in accounts)
            {
                items.Add(account.Name);
            }

            return(items);
        }
Ejemplo n.º 20
0
        public static List <string> GetImportableItems(int packageId, int itemTypeId)
        {
            List <string> items = new List <string>();

            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.IsAdmin | DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(items);
            }

            // load item type
            if (itemTypeId > 0)
            {
                ServiceProviderItemType itemType = PackageController.GetServiceItemType(itemTypeId);

                // load group
                ResourceGroupInfo group = ServerController.GetResourceGroup(itemType.GroupId);

                // Is it DNS Zones? Then create a IDN Mapping object
                var isDnsZones = group.GroupName == "DNS";
                var idn        = new IdnMapping();

                // get service id
                int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);
                if (serviceId == 0)
                {
                    return(items);
                }

                // Read existing packages and serviceitems
                DataTable dtServiceItems = PackageController.GetServiceItemsDataSet(serviceId).Tables[0];
                DataTable dtPackageItems = PackageController.GetPackageItemsDataSet(packageId).Tables[0];

                // Add already existing packages and serviceitems to lowercase ignorelist
                List <string> ignorelist = new List <string>();
                foreach (DataRow dr in dtServiceItems.Rows)
                {
                    string serviceItemName   = (string)dr["ItemName"];
                    int    serviceItemTypeId = (int)dr["ItemTypeId"];

                    if (serviceItemTypeId == itemTypeId)
                    {
                        if (!ignorelist.Contains(serviceItemName))
                        {
                            ignorelist.Add(serviceItemName.ToLower());
                        }
                    }
                }
                foreach (DataRow dr in dtPackageItems.Rows)
                {
                    string packageItemName   = (string)dr["ItemName"];
                    int    packageItemTypeId = (int)dr["ItemTypeId"];

                    if (packageItemTypeId == itemTypeId)
                    {
                        if (!ignorelist.Contains(packageItemName))
                        {
                            ignorelist.Add(packageItemName.ToLower());
                        }
                    }
                }

                // instantiate controller
                IImportController ctrl = null;
                try
                {
                    List <string> importableItems = null;
                    ctrl = Activator.CreateInstance(Type.GetType(group.GroupController)) as IImportController;
                    if (ctrl != null)
                    {
                        importableItems = ctrl.GetImportableItems(packageId, itemTypeId, Type.GetType(itemType.TypeName), group);
                    }

                    foreach (string importableItem in importableItems)
                    {
                        if (!ignorelist.Contains(importableItem.ToLower()))
                        {
                            var itemToImport = importableItem;

                            // For DNS zones the compare has been made using ascii, convert to unicode if necessary to make the list of items easier to read
                            if (isDnsZones && itemToImport.StartsWith("xn--"))
                            {
                                itemToImport = idn.GetUnicode(importableItem);
                            }

                            items.Add(itemToImport);
                        }
                    }
                }
                catch { /* do nothing */ }
            }
            else
            {
                return(GetImportableCustomItems(packageId, itemTypeId));
            }

            return(items);
        }
Ejemplo n.º 21
0
        public int BackupItem(string tempFolder, XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (!(item is DnsZone))
            {
                return(0);
            }

            // DNS provider
            DNSServer dns = GetDNSServer(item.ServiceId);

            // zone records serialized
            XmlSerializer serializer = new XmlSerializer(typeof(DnsRecord));

            try
            {
                // get zone records
                DnsRecord[] records = dns.GetZoneRecords(item.Name);

                // serialize zone records
                foreach (DnsRecord record in records)
                {
                    serializer.Serialize(writer, record);
                }
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex, "Could not read zone records");
            }

            return(0);
        }
Ejemplo n.º 22
0
        public int BackupItem(string tempFolder, System.Xml.XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is SystemUser)
            {
                // backup system user
                SharePointServer sps = GetSharePoint(item.ServiceId);

                // read user info
                SystemUser user = sps.GetUser(item.Name);
                user.Password = ((SystemUser)item).Password;

                XmlSerializer serializer = new XmlSerializer(typeof(SystemUser));
                serializer.Serialize(writer, user);
            }
            else if (item is SystemGroup)
            {
                // backup system group
                SharePointServer sps = GetSharePoint(item.ServiceId);

                // read site info
                SystemGroup sysGroup = sps.GetGroup(item.Name);

                XmlSerializer serializer = new XmlSerializer(typeof(SystemGroup));
                serializer.Serialize(writer, group);
            }
            return(0);
        }
Ejemplo n.º 23
0
        public int BackupItem(string tempFolder, System.Xml.XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is HomeFolder)
            {
                // backup home folder files
                string backupName = String.Format("SpaceFiles_{0}_{1}.zip",
                                                  item.Id, DateTime.Now.Ticks);

                // get the list of remote files
                List <SystemFile> files = FilesController.GetFiles(item.PackageId, "\\", true);

                string[] zipFiles = new string[files.Count];
                for (int i = 0; i < zipFiles.Length; i++)
                {
                    zipFiles[i] = files[i].Name;
                }

                // zip remote files
                FilesController.ZipFiles(item.PackageId, zipFiles, backupName);

                // download zipped file
                string localBackupPath = Path.Combine(tempFolder, backupName);

                byte[]     buffer = null;
                FileStream stream = new FileStream(localBackupPath, FileMode.Create, FileAccess.Write);

                int  offset = 0;
                long length = 0;
                do
                {
                    // read remote content
                    buffer = FilesController.GetFileBinaryChunk(item.PackageId, backupName, offset, FILE_BUFFER_LENGTH);

                    // write remote content
                    stream.Write(buffer, 0, buffer.Length);

                    length += buffer.Length;
                    offset += FILE_BUFFER_LENGTH;
                }while (buffer.Length == FILE_BUFFER_LENGTH);
                stream.Close();

                // delete zipped file
                if (FilesController.FileExists(item.PackageId, backupName))
                {
                    FilesController.DeleteFiles(item.PackageId, new string[] { backupName });
                }

                // add file pointer
                BackupController.WriteFileElement(writer, "SpaceFiles", backupName, length);

                // store meta item
                XmlSerializer serializer = new XmlSerializer(typeof(HomeFolder));
                serializer.Serialize(writer, item);
            }
            else if (item is SystemDSN)
            {
                // backup ODBC DSN
                OS.OperatingSystem os = GetOS(item.ServiceId);

                // read DSN info
                SystemDSN itemDsn = item as SystemDSN;
                SystemDSN dsn     = os.GetDSN(item.Name);
                dsn.DatabasePassword = itemDsn.DatabasePassword;

                XmlSerializer serializer = new XmlSerializer(typeof(SystemDSN));
                serializer.Serialize(writer, dsn);
            }

            return(0);
        }
Ejemplo n.º 24
0
        public int BackupItem(string tempFolder, XmlWriter writer, ServiceProviderItem item, ResourceGroupInfo group)
        {
            if (item is SqlDatabase)
            {
                // backup database
                DatabaseServer sql = GetDatabaseServer(item.ServiceId);

                string backupName       = String.Format("DatabaseBackup_{0}.zip", item.Id);
                string remoteBackupFile = sql.BackupDatabase(item.Name, backupName, true);

                // download remote backup
                string localBackupPath = Path.Combine(tempFolder, backupName);

                byte[]     buffer = null;
                FileStream stream = new FileStream(localBackupPath, FileMode.Create, FileAccess.Write);

                int  offset = 0;
                long length = 0;
                do
                {
                    // read remote content
                    buffer = sql.GetTempFileBinaryChunk(remoteBackupFile, offset, FILE_BUFFER_LENGTH);

                    // write remote content
                    stream.Write(buffer, 0, buffer.Length);

                    length += buffer.Length;
                    offset += FILE_BUFFER_LENGTH;
                }while (buffer.Length == FILE_BUFFER_LENGTH);
                stream.Close();

                // add file pointer

                BackupController.WriteFileElement(writer, "DatabaseBackup", backupName, length);

                // store meta item
                SqlDatabase   database   = sql.GetDatabase(item.Name);
                XmlSerializer serializer = new XmlSerializer(typeof(SqlDatabase));
                serializer.Serialize(writer, database);
            }
            else if (item is SqlUser)
            {
                // backup user
                DatabaseServer sql = GetDatabaseServer(item.ServiceId);

                SqlUser userItem = item as SqlUser;

                // store user info
                SqlUser user = sql.GetUser(item.Name, GetSqlDatabasesArray(item.PackageId, item.GroupName));
                user.Password = userItem.Password;

                XmlSerializer serializer = new XmlSerializer(typeof(SqlUser));
                serializer.Serialize(writer, user);
            }

            return(0);
        }
Ejemplo n.º 25
0
        public static int BackupInternal(string taskId, int userId, int packageId, int serviceId, int serverId,
                                         string backupFileName, int storePackageId, string storePackageFolder, string storeServerFolder,
                                         bool deleteTempBackup)
        {
            try
            {
                TaskManager.StartTask(taskId, "BACKUP", "BACKUP", backupFileName, SecurityContext.User.UserId);

                // Set Ending .scpak
                if (!backupFileName.EndsWith(".scpak"))
                {
                    backupFileName += ".scpak";
                }

                // get the list of items to backup
                TaskManager.Write("Calculate items to backup");
                List <ServiceProviderItem> items = GetBackupItems(userId, packageId, serviceId, serverId);

                if (items.Count == 0)
                {
                    return(0);
                }

                // group items by item types
                Dictionary <int, List <ServiceProviderItem> > groupedItems = new Dictionary <int, List <ServiceProviderItem> >();

                // sort by groups
                foreach (ServiceProviderItem item in items)
                {
                    // add to group
                    if (!groupedItems.ContainsKey(item.TypeId))
                    {
                        groupedItems[item.TypeId] = new List <ServiceProviderItem>();
                    }

                    groupedItems[item.TypeId].Add(item);
                }

                // temp backup folder
                string tempFolder = GetTempBackupFolder();

                // create backup catalog file
                StringWriter  sw     = new StringWriter();
                XmlTextWriter writer = new XmlTextWriter(sw);

                // write backup file header
                writer.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"");
                writer.WriteStartElement("Backup");
                writer.WriteStartElement("Info");
                writer.WriteElementString("Name", backupFileName);
                writer.WriteElementString("Created", DateTime.Now.ToString("r"));
                writer.WriteElementString("User", GetLoggedUsername());
                writer.WriteEndElement();                 // Info

                // determine the number of items to backup
                int totalItems = 0;
                foreach (int itemTypeId in groupedItems.Keys)
                {
                    // load item type
                    ServiceProviderItemType itemType = PackageController.GetServiceItemType(itemTypeId);
                    if (!itemType.Backupable)
                    {
                        continue;
                    }

                    totalItems += groupedItems[itemTypeId].Count;
                }

                TaskManager.IndicatorMaximum = totalItems + 2;
                TaskManager.IndicatorCurrent = 0;

                // backup grouped items
                writer.WriteStartElement("Items");
                foreach (int itemTypeId in groupedItems.Keys)
                {
                    // load item type
                    ServiceProviderItemType itemType = PackageController.GetServiceItemType(itemTypeId);
                    if (!itemType.Backupable)
                    {
                        continue;
                    }

                    // load group
                    ResourceGroupInfo group = ServerController.GetResourceGroup(itemType.GroupId);

                    // instantiate controller
                    IBackupController controller = null;
                    try
                    {
                        if (group.GroupController != null)
                        {
                            controller = Activator.CreateInstance(Type.GetType(group.GroupController)) as IBackupController;
                        }
                        if (controller != null)
                        {
                            // backup items
                            foreach (ServiceProviderItem item in groupedItems[itemTypeId])
                            {
                                TaskManager.Write(String.Format("Backup {0} of {1} - {2} '{3}'",
                                                                TaskManager.IndicatorCurrent + 1,
                                                                totalItems,
                                                                itemType.DisplayName,
                                                                item.Name));

                                try
                                {
                                    int backupResult = BackupItem(tempFolder, writer, item, group, controller);
                                }
                                catch (Exception ex)
                                {
                                    TaskManager.WriteError(ex, "Can't backup item");
                                }

                                // increment progress
                                TaskManager.IndicatorCurrent += 1;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex);
                    }
                }
                writer.WriteEndElement();                 // Items

                // close catalog writer
                writer.WriteEndElement();                 // Backup
                writer.Close();

                // convert to Xml document
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(sw.ToString());

                // sign XML document
                //SignXmlDocument(doc);

                // save signed doc to file
                try
                {
                    doc.Save(Path.Combine(tempFolder, BACKUP_CATALOG_FILE_NAME));
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Can't save backup catalog file: "
                                           + Path.Combine(tempFolder, BACKUP_CATALOG_FILE_NAME));
                    return(0);
                }

                TaskManager.Write("Packaging backup...");

                // compress backup files
                string[] zipFiles     = Directory.GetFiles(tempFolder);
                string[] zipFileNames = new string[zipFiles.Length];
                for (int i = 0; i < zipFiles.Length; i++)
                {
                    zipFileNames[i] = Path.GetFileName(zipFiles[i]);
                }


                string backupFileNamePath = Path.Combine(tempFolder, backupFileName);

                try
                {
                    FileUtils.ZipFiles(backupFileNamePath, tempFolder, zipFileNames);

                    // delete packed files
                    foreach (string zipFile in zipFiles)
                    {
                        File.Delete(zipFile);
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Can't zip backed up files");
                    return(0);
                }

                TaskManager.IndicatorCurrent += 1;

                TaskManager.Write("Copying backup...");
                // move/copy backup file
                if (!String.IsNullOrEmpty(storeServerFolder))
                {
                    // copy to local folder or UNC
                    try
                    {
                        string destFile = Path.Combine(storeServerFolder, backupFileName);
                        File.Copy(backupFileNamePath, destFile, true);
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Can't copy backup to destination location");
                        return(0);
                    }
                }
                else if (storePackageId > 0)
                {
                    try
                    {
                        // copy to space folder
                        int osServiceId = PackageController.GetPackageServiceId(storePackageId, ResourceGroups.Os);
                        if (osServiceId > 0)
                        {
                            OS.OperatingSystem os = new OS.OperatingSystem();
                            ServiceProviderProxy.Init(os, osServiceId);

                            string remoteBackupPath = FilesController.GetFullPackagePath(storePackageId,
                                                                                         Path.Combine(storePackageFolder, backupFileName));

                            FileStream stream = new FileStream(backupFileNamePath, FileMode.Open, FileAccess.Read);
                            byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                            int readBytes = 0;
                            do
                            {
                                // read package file
                                readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);

                                if (readBytes < FILE_BUFFER_LENGTH)
                                {
                                    // resize buffer
                                    Array.Resize <byte>(ref buffer, readBytes);
                                }

                                // write remote backup file
                                os.AppendFileBinaryContent(remoteBackupPath, buffer);
                            }while (readBytes == FILE_BUFFER_LENGTH);
                            stream.Close();
                        }
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Can't copy backup to destination hosting space");
                        return(0);
                    }
                }

                TaskManager.IndicatorCurrent += 1;

                // delete backup file if required
                if (deleteTempBackup)
                {
                    try
                    {
                        // delete backup folder and all its contents
                        Directory.Delete(tempFolder, true);
                    }
                    catch (Exception ex)
                    {
                        TaskManager.WriteError(ex, "Can't delete temporary backup folder");
                        return(0);
                    }
                }

                BackgroundTask topTask = TaskManager.TopTask;

                topTask.IndicatorCurrent = topTask.IndicatorMaximum;

                TaskController.UpdateTask(topTask);
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex);
            }
            finally
            {
                TaskManager.CompleteTask();
            }

            return(0);
        }
Ejemplo n.º 26
0
        public static int ImportItemsInternal(string taskId, int packageId, string[] items)
        {
            PackageInfo package = PackageController.GetPackage(packageId);

            TaskManager.StartTask(taskId, "IMPORT", "IMPORT", package.PackageName, packageId);

            TaskManager.IndicatorMaximum = items.Length;
            TaskManager.IndicatorCurrent = 0;

            Dictionary <int, List <string> > groupedItems = new Dictionary <int, List <string> >();
            List <string> customItems = new List <string>();

            // sort by groups
            foreach (string item in items)
            {
                string[] itemParts = item.Split('|');
                if (!item.StartsWith("+"))
                {
                    int    itemTypeId = Utils.ParseInt(itemParts[0], 0);
                    string itemName   = itemParts[1];

                    // add to group
                    if (!groupedItems.ContainsKey(itemTypeId))
                    {
                        groupedItems[itemTypeId] = new List <string>();
                    }

                    groupedItems[itemTypeId].Add(itemName);
                }
                else
                {
                    switch (itemParts[0])
                    {
                    case ("+100"):
                        if (itemParts.Length > 2)
                        {
                            customItems.Add(item);
                        }
                        break;
                    }
                }
            }

            // import each group
            foreach (int itemTypeId in groupedItems.Keys)
            {
                // load item type
                ServiceProviderItemType itemType = PackageController.GetServiceItemType(itemTypeId);

                // load group
                ResourceGroupInfo group = ServerController.GetResourceGroup(itemType.GroupId);

                // instantiate controller
                IImportController ctrl = null;
                try
                {
                    ctrl = Activator.CreateInstance(Type.GetType(group.GroupController)) as IImportController;
                    if (ctrl != null)
                    {
                        foreach (string itemName in groupedItems[itemTypeId])
                        {
                            TaskManager.Write(String.Format("Import {0} '{1}'",
                                                            itemType.DisplayName, itemName));

                            try
                            {
                                // perform import
                                ctrl.ImportItem(packageId, itemTypeId,
                                                Type.GetType(itemType.TypeName), group, itemName);
                            }
                            catch (Exception ex)
                            {
                                TaskManager.WriteError(ex, "Can't import item");
                            }

                            TaskManager.IndicatorCurrent++;
                        }
                    }
                }
                catch { /* do nothing */ }
            }

            foreach (string s in customItems)
            {
                try
                {
                    string[] sParts = s.Split('|');
                    switch (sParts[0])
                    {
                    case "+100":
                        TaskManager.Write(String.Format("Import {0}", sParts[4]));

                        int result = WebServerController.ImporHostHeader(int.Parse(sParts[2], 0), int.Parse(sParts[3], 0), int.Parse(sParts[5], 0));

                        if (result < 0)
                        {
                            TaskManager.WriteError(String.Format("Failed to Import {0} ,error: {1}: ", sParts[4], result.ToString()));
                        }

                        break;
                    }
                }
                catch { /* do nothing */ }

                TaskManager.IndicatorCurrent++;
            }

            TaskManager.IndicatorCurrent = items.Length;

            TaskManager.CompleteTask();

            return(0);
        }
Ejemplo n.º 27
0
        public List <string> GetImportableItems(int packageId, int itemTypeId, Type itemType, ResourceGroupInfo group)
        {
            List <string> items = new List <string>();

            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return(items);
            }

            // Mail provider
            DNSServer dns = new DNSServer();

            ServiceProviderProxy.Init(dns, serviceId);

            // IDN: The list of importable names is populated with unicode names, to make it easier for the user
            var idn = new IdnMapping();

            if (itemType == typeof(DnsZone))
            {
                items.AddRange(dns.GetZones().Select(z =>
                {
                    try
                    {
                        return(idn.GetUnicode(z));
                    }
                    catch
                    {
                        return(null);
                    }
                }));
            }


            return(items);
        }
Ejemplo n.º 28
0
        public int RestoreItem(string tempFolder, XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(SqlDatabase))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlDatabase));
                SqlDatabase   db         = (SqlDatabase)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlDatabase")));

                // create database if required
                if (!sql.DatabaseExists(itemName))
                {
                    sql.CreateDatabase(db);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='DatabaseBackup']");
                string  backupFileName      = fileNode.Attributes["path"].Value;
                long    backupFileLength    = Int64.Parse(fileNode.Attributes["size"].Value);
                string  localBackupFilePath = Path.Combine(tempFolder, backupFileName);

                if (new FileInfo(localBackupFilePath).Length != backupFileLength)
                {
                    return(-3);
                }

                FileStream stream = new FileStream(localBackupFilePath, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                int    readBytes        = 0;
                long   length           = 0;
                string remoteBackupPath = null;
                do
                {
                    // read package file
                    readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
                    length   += readBytes;

                    if (readBytes < FILE_BUFFER_LENGTH)
                    {
                        // resize buffer
                        Array.Resize <byte>(ref buffer, readBytes);
                    }

                    // write remote backup file
                    string tempPath = sql.AppendTempFileBinaryChunk(backupFileName, remoteBackupPath, buffer);
                    if (remoteBackupPath == null)
                    {
                        remoteBackupPath = tempPath;
                    }
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // restore database
                sql.RestoreDatabase(itemName, new string[] { remoteBackupPath });

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlDatabase)) == null)
                {
                    db.PackageId = packageId;
                    db.ServiceId = serviceId;
                    db.GroupName = group.GroupName;
                    PackageController.AddPackageItem(db);
                }
            }
            else if (itemType == typeof(SqlUser))
            {
                DatabaseServer sql = GetDatabaseServer(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SqlUser));
                SqlUser       user       = (SqlUser)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SqlUser")));

                // create user if required
                if (!sql.UserExists(itemName))
                {
                    sql.CreateUser(user, CryptoUtils.Decrypt(user.Password));
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, group.GroupName,
                                                           itemName, typeof(SqlUser)) == null)
                {
                    user.PackageId = packageId;
                    user.ServiceId = serviceId;
                    user.GroupName = group.GroupName;
                    PackageController.AddPackageItem(user);
                }
            }

            return(0);
        }
Ejemplo n.º 29
0
        public int RestoreItem(string tempFolder, System.Xml.XmlNode itemNode, int itemId, Type itemType, string itemName, int packageId, int serviceId, ResourceGroupInfo group)
        {
            if (itemType == typeof(HomeFolder))
            {
                OS.OperatingSystem os = GetOS(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(HomeFolder));
                HomeFolder    homeFolder = (HomeFolder)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("HomeFolder")));

                // create home folder if required
                if (!os.DirectoryExists(homeFolder.Name))
                {
                    os.CreatePackageFolder(homeFolder.Name);
                }

                // copy database backup to remote server
                XmlNode fileNode            = itemNode.SelectSingleNode("File[@name='SpaceFiles']");
                string  backupFileName      = fileNode.Attributes["path"].Value;
                long    backupFileLength    = Int64.Parse(fileNode.Attributes["size"].Value);
                string  localBackupFilePath = Path.Combine(tempFolder, backupFileName);

                if (new FileInfo(localBackupFilePath).Length != backupFileLength)
                {
                    return(-3);
                }

                FileStream stream = new FileStream(localBackupFilePath, FileMode.Open, FileAccess.Read);
                byte[]     buffer = new byte[FILE_BUFFER_LENGTH];

                int    readBytes        = 0;
                long   length           = 0;
                string remoteBackupPath = Path.Combine(homeFolder.Name, backupFileName);
                do
                {
                    // read package file
                    readBytes = stream.Read(buffer, 0, FILE_BUFFER_LENGTH);
                    length   += readBytes;

                    if (readBytes < FILE_BUFFER_LENGTH)
                    {
                        // resize buffer
                        Array.Resize <byte>(ref buffer, readBytes);
                    }

                    // write remote backup file
                    os.AppendFileBinaryContent(remoteBackupPath, buffer);
                }while (readBytes == FILE_BUFFER_LENGTH);
                stream.Close();

                // unzip files
                os.UnzipFiles(remoteBackupPath, homeFolder.Name);

                // delete archive
                if (os.FileExists(remoteBackupPath))
                {
                    os.DeleteFile(remoteBackupPath);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(HomeFolder)) == null)
                {
                    homeFolder.PackageId = packageId;
                    homeFolder.ServiceId = serviceId;
                    PackageController.AddPackageItem(homeFolder);
                }
            }
            else if (itemType == typeof(SystemDSN))
            {
                OS.OperatingSystem os = GetOS(serviceId);

                // extract meta item
                XmlSerializer serializer = new XmlSerializer(typeof(SystemDSN));
                SystemDSN     dsn        = (SystemDSN)serializer.Deserialize(
                    new XmlNodeReader(itemNode.SelectSingleNode("SystemDSN")));

                // create DSN if required
                if (os.GetDSN(itemName) == null)
                {
                    dsn.DatabasePassword = CryptoUtils.Decrypt(dsn.DatabasePassword);
                    os.CreateDSN(dsn);

                    // restore password
                    dsn.DatabasePassword = CryptoUtils.Encrypt(dsn.DatabasePassword);
                }

                // add meta-item if required
                if (PackageController.GetPackageItemByName(packageId, itemName, typeof(SystemDSN)) == null)
                {
                    dsn.PackageId = packageId;
                    dsn.ServiceId = serviceId;
                    PackageController.AddPackageItem(dsn);
                }
            }

            return(0);
        }
Ejemplo n.º 30
0
        public void ImportItem(int packageId, int itemTypeId, Type itemType,
                               ResourceGroupInfo group, string itemName)
        {
            // get service id
            int serviceId = PackageController.GetPackageServiceId(packageId, group.GroupName);

            if (serviceId == 0)
            {
                return;
            }

            if (itemType == typeof(DnsZone))
            {
                // Get ascii form in punycode
                var zoneName = GetAsciiZoneName(itemName);

                // add DNS zone
                DnsZone zone = new DnsZone();
                zone.Name      = zoneName;
                zone.ServiceId = serviceId;
                zone.PackageId = packageId;
                int zoneId = PackageController.AddPackageItem(zone);

                // Add secondary zone(s)
                try
                {
                    // get secondary DNS services
                    var primSettings         = ServerController.GetServiceSettings(serviceId);
                    var secondaryServiceIds  = new List <int>();
                    var strSecondaryServices = primSettings["SecondaryDNSServices"];
                    if (!String.IsNullOrEmpty(strSecondaryServices))
                    {
                        var secondaryServices = strSecondaryServices.Split(',');
                        secondaryServiceIds.AddRange(secondaryServices.Select(strSecondaryId => Utils.ParseInt(strSecondaryId, 0)).Where(secondaryId => secondaryId != 0));
                    }

                    // add secondary zones
                    var secondaryZoneFound = false;

                    foreach (var secondaryId in secondaryServiceIds)
                    {
                        var secDns = GetDNSServer(secondaryId);
                        if (secDns.ZoneExists(zoneName))
                        {
                            secondaryZoneFound = true;

                            var secondaryZone = new SecondaryDnsZone
                            {
                                Name      = zoneName,
                                ServiceId = secondaryId,
                                PackageId = packageId
                            };

                            PackageController.AddPackageItem(secondaryZone);
                        }
                    }

                    if (!secondaryZoneFound)
                    {
                        TaskManager.WriteWarning("No secondary zone(s) found when importing zone " + itemName);
                    }
                }
                catch (Exception ex)
                {
                    TaskManager.WriteError(ex, "Error importing secondary zone(s)");
                }


                // add/update domains/pointers
                RestoreDomainByZone(itemName, packageId, zoneId);
            }
        }