Esempio n. 1
0
        public string Start()
        {
            if (string.IsNullOrEmpty(SettingServices.GetSettingValue(SettingStrings.ServerIdentifier)))
            {
                return("The Server Identifier Must Be Set Before Tasks Can Be Started");
            }

            if (_computer == null)
            {
                return("The Computer Does Not Exist");
            }

            _imageProfile = new ImageProfileServices().ReadProfile(_computer.ImageProfileId);
            if (_imageProfile == null)
            {
                return("The Image Profile Does Not Exist");
            }

            if (_imageProfile.Image == null)
            {
                return("The Image Does Not Exist");
            }

            if (_direction == "deploy" || _direction == "permanentdeploy")
            {
                var validation = new ImageServices().CheckApprovalAndChecksum(_imageProfile.Image, _userId);
                if (!validation.Success)
                {
                    return(validation.ErrorMessage);
                }
            }

            var dp = new DistributionPointServices().GetPrimaryDistributionPoint();

            if (dp == null)
            {
                return("Could Not Find A Primary Distribution Point");
            }

            if (SettingServices.ServerIsClusterPrimary)
            {
                var clusterGroup = new ComputerServices().GetClusterGroup(_computer.Id);
                if (clusterGroup == null)
                {
                    return("Could Not Find A Cluster Group For This Computer");
                }
            }

            if (new ComputerServices().IsComputerActive(_computer.Id))
            {
                return("This Computer Is Already Part Of An Active Task");
            }

            _activeTask = new ActiveImagingTaskEntity
            {
                ComputerId = _computer.Id,
                Direction  = _direction,
                UserId     = _userId
            };

            _activeTask.Type = _direction;

            var activeImagingTaskServices = new ActiveImagingTaskServices();

            if (!activeImagingTaskServices.AddActiveImagingTask(_activeTask))
            {
                return("Could Not Create The Database Entry For This Task");
            }

            if (!new TaskBootMenu(_computer, _imageProfile).CreatePxeBootFiles())
            {
                activeImagingTaskServices.DeleteActiveImagingTask(_activeTask.Id);
                return("Could Not Create PXE Boot File");
            }

            _activeTask.Arguments = new CreateTaskArguments(_computer, _imageProfile, _direction).Execute();
            if (!activeImagingTaskServices.UpdateActiveImagingTask(_activeTask))
            {
                activeImagingTaskServices.DeleteActiveImagingTask(_activeTask.Id);
                return("Could Not Create Task Arguments");
            }

            IpServices.WakeUp(_computer.Mac);

            var auditLog = new AuditLogEntity();

            switch (_direction)
            {
            case "deploy":
                auditLog.AuditType = AuditEntry.Type.Deploy;
                break;

            case "permanentdeploy":
                auditLog.AuditType = AuditEntry.Type.PermanentPush;
                break;

            default:
                auditLog.AuditType = AuditEntry.Type.Upload;
                break;
            }

            auditLog.ObjectId = _computer.Id;
            var user = new UserServices().GetUser(_userId);

            if (user != null)
            {
                auditLog.UserName = user.Name;
            }
            auditLog.ObjectName = _computer.Name;
            auditLog.Ip         = _ipAddress;
            auditLog.UserId     = _userId;
            auditLog.ObjectType = "Computer";
            auditLog.ObjectJson = JsonConvert.SerializeObject(_activeTask);
            new AuditLogServices().AddAuditLog(auditLog);

            auditLog.ObjectId   = _imageProfile.ImageId;
            auditLog.ObjectName = _imageProfile.Image.Name;
            auditLog.ObjectType = "Image";
            new AuditLogServices().AddAuditLog(auditLog);

            return("Successfully Started Task For " + _computer.Name);
        }
Esempio n. 2
0
        public int Run()
        {
            //Find best distribution point to use

            int dpId;
            var distributionPointServices = new DistributionPointServices();

            if (SettingServices.ServerIsNotClustered)
            {
                var dp = distributionPointServices.GetPrimaryDistributionPoint();
                if (dp != null)
                {
                    return(dp.Id);
                }
                return(-1);
            }

            var clusterServices = new ClusterGroupServices();

            ClusterGroupEntity clusterGroup;

            if (_computer != null)
            {
                clusterGroup = new ComputerServices().GetClusterGroup(_computer.Id);
            }
            else
            {
                //on demand computer might be null
                //use default cluster group
                clusterGroup = clusterServices.GetDefaultClusterGroup();
            }

            //Something went wrong
            if (clusterGroup == null)
            {
                return(-1);
            }

            var queueSizesDict = new Dictionary <int, int>();
            var toRemove       = new List <DistributionPointEntity>();
            var clusterDps     = clusterServices.GetClusterDps(clusterGroup.Id);
            var aDps           = new List <DistributionPointEntity>();

            foreach (var clusterDp in clusterDps)
            {
                aDps.Add(distributionPointServices.GetDistributionPoint(clusterDp.DistributionPointId));
            }
            var availableDistributionPoints =
                clusterServices.GetClusterDps(clusterGroup.Id)
                .Select(
                    clusterDp => distributionPointServices.GetDistributionPoint(clusterDp.DistributionPointId))
                .ToList();

            //Check if any Distribution point in the cluster group is the primary dp
            foreach (var dp in availableDistributionPoints.Where(dp => dp.IsPrimary == 1))
            {
                //Cluster Group has a primary, always return the primary for an upload, not necessary but saves syncing to the primary later
                if (_task.Contains("upload"))
                {
                    return(dp.Id);
                }
            }

            foreach (var dp in availableDistributionPoints)
            {
                if (dp.QueueSize == 0)
                {
                    toRemove.Add(dp);
                }
                else
                {
                    queueSizesDict.Add(dp.Id, dp.QueueSize);
                }
            }
            availableDistributionPoints = availableDistributionPoints.Except(toRemove).ToList();

            var taskInUseDict = new Dictionary <int, int>();

            foreach (var dp in availableDistributionPoints)
            {
                var counter = 0;
                foreach (var activeTask in new ActiveImagingTaskServices().GetAll().Where(x => x.Status != "0"))
                {
                    if (activeTask.DpId == dp.Id)
                    {
                        counter++;
                    }
                }

                taskInUseDict.Add(dp.Id, counter);
            }

            var freeDps = new List <DistributionPointEntity>();

            foreach (var dp in availableDistributionPoints)
            {
                if (taskInUseDict[dp.Id] < queueSizesDict[dp.Id])
                {
                    freeDps.Add(dp);
                }
            }

            if (freeDps.Count == 1)
            {
                dpId = freeDps.First().Id;
            }

            else if (freeDps.Count > 1)
            {
                var freeDictionary = new Dictionary <int, int>();
                var slotsInUseList = new List <int>();
                foreach (var dp in freeDps)
                {
                    freeDictionary.Add(dp.Id, taskInUseDict[dp.Id]);
                    slotsInUseList.Add(taskInUseDict[dp.Id]);
                }

                if (slotsInUseList.All(x => x == slotsInUseList[0]))
                {
                    //all image servers have equal free slots - randomly choose one.
                    var index = _random.Next(0, freeDps.Count);
                    dpId = freeDps[index].Id;
                }
                else
                {
                    //Just grab the first one with the smallest queue, could be a duplicate but will eventually even out on it's own
                    var orderedInUse = freeDictionary.OrderBy(x => x.Value);
                    dpId = orderedInUse.First().Key;
                }
            }
            else
            {
                //Free image servers count is 0, pick the one with the lowest number of tasks to be added to the queue
                var orderedInUse = taskInUseDict.OrderBy(x => x.Value);
                dpId = orderedInUse.First().Key;
            }

            return(dpId);
        }
        public int GenerateProcessArguments(MulticastArgsDTO mArgs)
        {
            var    schemaCounter      = -1;
            var    multicastHdCounter = 0;
            string processArguments   = null;

            foreach (var hd in mArgs.schema.HardDrives)
            {
                schemaCounter++;
                if (!hd.Active)
                {
                    continue;
                }
                multicastHdCounter++;

                var x = 0;
                foreach (var part in mArgs.schema.HardDrives[schemaCounter].Partitions)
                {
                    if (!part.Active)
                    {
                        continue;
                    }
                    string imageFile = null;
                    foreach (var ext in new[] { "ntfs", "fat", "extfs", "hfsp", "imager", "winpe", "xfs" })
                    {
                        imageFile = new FilesystemServices().GetFileNameWithFullPath(mArgs.ImageName,
                                                                                     schemaCounter.ToString(), part.Number, ext);

                        if (!string.IsNullOrEmpty(imageFile))
                        {
                            break;
                        }

                        //Look for lvm
                        if (part.VolumeGroup == null)
                        {
                            continue;
                        }
                        if (part.VolumeGroup.LogicalVolumes == null)
                        {
                            continue;
                        }
                        foreach (var lv in part.VolumeGroup.LogicalVolumes.Where(lv => lv.Active))
                        {
                            imageFile = new FilesystemServices().GetLVMFileNameWithFullPath(mArgs.ImageName,
                                                                                            schemaCounter.ToString(), lv.VolumeGroup, lv.Name, ext);
                        }
                    }

                    if (string.IsNullOrEmpty(imageFile))
                    {
                        continue;
                    }
                    if (mArgs.Environment == "winpe" &&
                        mArgs.schema.HardDrives[schemaCounter].Table.ToLower() == "gpt")
                    {
                        if (part.Type.ToLower() == "system" || part.Type.ToLower() == "recovery" ||
                            part.Type.ToLower() == "reserved")
                        {
                            continue;
                        }
                    }
                    if (mArgs.Environment == "winpe" &&
                        mArgs.schema.HardDrives[schemaCounter].Table.ToLower() == "mbr")
                    {
                        if (part.Number == mArgs.schema.HardDrives[schemaCounter].Boot &&
                            mArgs.schema.HardDrives[schemaCounter].Partitions.Length > 1)
                        {
                            continue;
                        }
                    }
                    x++;

                    var minReceivers = "";

                    if (!string.IsNullOrEmpty(mArgs.clientCount))
                    {
                        minReceivers = " --min-receivers " + mArgs.clientCount;
                    }

                    var isUnix = Environment.OSVersion.ToString().Contains("Unix");

                    string compAlg;
                    var    stdout = "";
                    switch (Path.GetExtension(imageFile))
                    {
                    case ".lz4":
                        compAlg = isUnix ? "lz4 -d " : "lz4.exe\" -d ";
                        stdout  = " - ";
                        break;

                    case ".gz":
                        compAlg = isUnix ? "gzip -c -d " : "gzip.exe\" -c -d ";
                        stdout  = "";
                        break;

                    case ".uncp":
                        compAlg = "none";
                        break;

                    case ".wim":
                        compAlg = "none";
                        break;

                    default:
                        return(0);
                    }

                    if (isUnix)
                    {
                        string prefix = null;
                        if (multicastHdCounter == 1)
                        {
                            prefix = x == 1 ? " -c \"" : " ; ";
                        }
                        else
                        {
                            prefix = " ; ";
                        }

                        if (compAlg == "none" ||
                            SettingServices.GetSettingValue(SettingStrings.MulticastDecompression) == "client")
                        {
                            processArguments += prefix + "cat " + "\"" + imageFile + "\"" + " | udp-sender" +
                                                " --portbase " + mArgs.Port + minReceivers + " " +
                                                " --ttl 32 " +
                                                mArgs.ExtraArgs;
                        }

                        else
                        {
                            processArguments += prefix + compAlg + "\"" + imageFile + "\"" + stdout + " | udp-sender" +
                                                " --portbase " + mArgs.Port + minReceivers + " " +
                                                " --ttl 32 " +
                                                mArgs.ExtraArgs;
                        }
                    }
                    else
                    {
                        var appPath = HttpContext.Current.Server.MapPath("~") + Path.DirectorySeparatorChar + "private" +
                                      Path.DirectorySeparatorChar + "apps" + Path.DirectorySeparatorChar;

                        string prefix = null;
                        if (multicastHdCounter == 1)
                        {
                            //Relative to the multicast server being called
                            var primaryDp = new DistributionPointServices().GetPrimaryDistributionPoint();
                            if (primaryDp == null)
                            {
                                return(0);
                            }
                            if (primaryDp.Location == "Local")
                            {
                                prefix = x == 1 ? " /c \"" : " & ";
                            }
                            else //Remote
                            {
                                if (x == 1)
                                {
                                    prefix = " /c \"net use \\\\" + primaryDp.Server + "\\" + primaryDp.ShareName +
                                             " /user:"******" " +
                                             new EncryptionServices().DecryptText(primaryDp.RoPassword) + " & ";
                                }
                                else
                                {
                                    prefix = " & ";
                                }
                            }
                        }
                        else
                        {
                            prefix = " & ";
                        }

                        if (compAlg == "none" ||
                            SettingServices.GetSettingValue(SettingStrings.MulticastDecompression) == "client")
                        {
                            processArguments += prefix + "\"" + appPath +
                                                "udp-sender.exe" + "\"" + " --file " + "\"" + imageFile + "\"" +
                                                " --portbase " + mArgs.Port + minReceivers + " " +
                                                " --ttl 32 " +
                                                mArgs.ExtraArgs;
                        }
                        else
                        {
                            processArguments += prefix + "\"" + appPath + compAlg + "\"" + imageFile + "\"" + stdout +
                                                " | " + "\"" + appPath +
                                                "udp-sender.exe" + "\"" +
                                                " --portbase " + mArgs.Port + minReceivers + " " +
                                                " --ttl 32 " +
                                                mArgs.ExtraArgs;
                        }
                    }
                }
            }

            var pDp = new DistributionPointServices().GetPrimaryDistributionPoint();

            if (pDp == null)
            {
                return(0);
            }
            if (pDp.Location == "Remote")
            {
                processArguments += " & net use /delete \\\\" + pDp.Server + "\\" + pDp.ShareName;
            }
            processArguments += "\"";

            return(StartMulticastSender(processArguments, mArgs.groupName));
        }
 public DistributionPointController()
 {
     _distributionPointServices = new DistributionPointServices();
 }