Ejemplo n.º 1
0
        private int GenerateProcessArguments()
        {
            var multicastArgs = new MulticastArgsDTO();

            multicastArgs.schema      = new ClientPartitionHelper(_imageProfile).GetImageSchema();
            multicastArgs.Environment = _imageProfile.Image.Environment;
            multicastArgs.ImageName   = _imageProfile.Image.Name;
            multicastArgs.Port        = _multicastSession.Port.ToString();
            if (_isOnDemand)
            {
                multicastArgs.ExtraArgs = SettingServices.GetSettingValue(SettingStrings.SenderArgs);
                if (!string.IsNullOrEmpty(_clientCount))
                {
                    multicastArgs.clientCount = _clientCount;
                }
            }
            else
            {
                multicastArgs.ExtraArgs = string.IsNullOrEmpty(_imageProfile.SenderArguments)
                    ? SettingServices.GetSettingValue(SettingStrings.SenderArgs)
                    : _imageProfile.SenderArguments;
                multicastArgs.clientCount = _computers.Count.ToString();
            }

            var pid = 0;

            if (_multicastServerId == -1)
            {
                pid = new MulticastArguments().GenerateProcessArguments(multicastArgs);
            }
            else
            {
                var secondaryServer =
                    new SecondaryServerServices().GetSecondaryServer(_multicastServerId);
                pid =
                    new APICall(new SecondaryServerServices().GetToken(secondaryServer.Name))
                    .ServiceAccountApi.GetMulticastSenderArgs(multicastArgs);
            }

            if (pid == 0)
            {
                return(pid);
            }

            var activeMulticastSessionServices = new ActiveMulticastSessionServices();

            if (_isOnDemand)
            {
                _multicastSession.Pid  = pid;
                _multicastSession.Name = _group.Name;
                activeMulticastSessionServices.AddActiveMulticastSession(_multicastSession);
            }
            else
            {
                _multicastSession.Pid = pid;
                activeMulticastSessionServices.UpdateActiveMulticastSession(_multicastSession);
            }

            return(pid);
        }
Ejemplo n.º 2
0
        public string Create()
        {
            _imageProfile = new ImageProfileServices().ReadProfile(_group.ImageProfileId);
            if (_imageProfile == null)
            {
                return("The Image Profile Does Not Exist");
            }

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

            var validation = new ImageServices().CheckApprovalAndChecksum(_imageProfile.Image, _userId);

            if (!validation.Success)
            {
                return(validation.ErrorMessage);
            }

            _multicastSession.Port = new PortServices().GetNextPort();
            if (_multicastSession.Port == 0)
            {
                return("Could Not Determine Current Port Base");
            }

            _multicastServerId = _isOnDemand
                ? new GetMulticastServer(_clusterId).Run()
                : new GetMulticastServer(_group).Run();

            if (_multicastServerId == -2)
            {
                return("Could Not Find Any Available Multicast Servers");
            }

            _multicastSession.ServerId = _multicastServerId;
            _multicastSession.UserId   = _userId;
            //End of the road for starting an on demand multicast
            if (_isOnDemand)
            {
                if (string.IsNullOrEmpty(_multicastSession.Name))
                {
                    _multicastSession.Name = _multicastSession.Port.ToString();
                }

                if (string.IsNullOrEmpty(_multicastSession.Name) || !_multicastSession.Name.All(c => char.IsLetterOrDigit(c) || c == '_' || c == '-'))
                {
                    return("Multicast Session Name Is Not Valid");
                }

                _group.Name = _multicastSession.Name;
                var onDemandprocessArguments = GenerateProcessArguments();
                if (onDemandprocessArguments == 0)
                {
                    return("Could Not Start The Multicast Application");
                }

                var ondAuditLog = new AuditLogEntity();
                ondAuditLog.AuditType = AuditEntry.Type.OndMulticast;
                ondAuditLog.ObjectId  = _imageProfile.ImageId;
                var ondUser = new UserServices().GetUser(_userId);
                if (ondUser != null)
                {
                    ondAuditLog.UserName = ondUser.Name;
                }
                ondAuditLog.ObjectName = _imageProfile.Image.Name;
                ondAuditLog.Ip         = _ipAddress;
                ondAuditLog.UserId     = _userId;
                ondAuditLog.ObjectType = "Image";
                ondAuditLog.ObjectJson = JsonConvert.SerializeObject(_multicastSession);
                new AuditLogServices().AddAuditLog(ondAuditLog);
                return("Successfully Started Multicast " + _group.Name);
            }

            //Continue On If multicast is for a group
            _multicastSession.Name = _group.Name;
            _computers             = new GroupServices().GetGroupMembers(_group.Id);
            if (_computers.Count < 1)
            {
                return("The Group Does Not Have Any Members");
            }

            var activeMulticastSessionServices = new ActiveMulticastSessionServices();

            if (!activeMulticastSessionServices.AddActiveMulticastSession(_multicastSession))
            {
                return("Could Not Create Multicast Database Task.  An Existing Task May Be Running.");
            }

            if (!CreateComputerTasks())
            {
                activeMulticastSessionServices.Delete(_multicastSession.Id);
                return("Could Not Create Computer Database Tasks.  A Computer May Have An Existing Task.");
            }

            if (!CreatePxeFiles())
            {
                activeMulticastSessionServices.Delete(_multicastSession.Id);
                return("Could Not Create Computer Boot Files");
            }

            if (!CreateTaskArguments())
            {
                activeMulticastSessionServices.Delete(_multicastSession.Id);
                return("Could Not Create Computer Task Arguments");
            }

            var processArguments = GenerateProcessArguments();

            if (processArguments == 0)
            {
                activeMulticastSessionServices.Delete(_multicastSession.Id);
                return("Could Not Start The Multicast Application");
            }

            foreach (var computer in _computers)
            {
                IpServices.WakeUp(computer.Mac);
            }

            var auditLog = new AuditLogEntity();

            auditLog.AuditType = AuditEntry.Type.Multicast;
            auditLog.ObjectId  = _group.Id;
            var user = new UserServices().GetUser(_userId);

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

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

            return("Successfully Started Multicast " + _group.Name);
        }
Ejemplo n.º 3
0
        public int Run()
        {
            //Find the best multicast server to use

            var serverId = -1;

            if (SettingServices.ServerIsNotClustered)
            {
                return(serverId);
            }
            var clusterServices = new ClusterGroupServices();

            ClusterGroupEntity clusterGroup;

            if (_group != null)
            {
                clusterGroup = new GroupServices().GetClusterGroup(_group.Id);
            }
            else
            {
                //on demand group might be null
                //use default cluster group
                if (_clusterId == -1)
                {
                    clusterGroup = clusterServices.GetDefaultClusterGroup();
                }
                else
                {
                    clusterGroup = new ClusterGroupServices().GetClusterGroup(_clusterId);
                }
            }

            var availableMulticastServers =
                new ClusterGroupServices().GetClusterMulticastServers(clusterGroup.Id);

            if (!availableMulticastServers.Any())
            {
                return(-2);
            }

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

            foreach (var mServer in availableMulticastServers)
            {
                var counter =
                    new ActiveMulticastSessionServices().GetAll()
                    .Count(x => x.ServerId == mServer.ServerId);

                taskInUseDict.Add(mServer.ServerId, counter);
            }

            if (taskInUseDict.Count == 1)
            {
                serverId = taskInUseDict.Keys.First();
            }

            else if (taskInUseDict.Count > 1)
            {
                var orderedInUse = taskInUseDict.OrderBy(x => x.Value);

                if (taskInUseDict.Values.Distinct().Count() == 1)
                {
                    //all multicast server have equal tasks - randomly choose one.

                    var index = _random.Next(0, taskInUseDict.Count);
                    serverId = taskInUseDict[index];
                }
                else
                {
                    //Just grab the first one with the smallest queue, could be a duplicate but will eventually even out on it's own
                    serverId = orderedInUse.First().Key;
                }
            }
            return(serverId);
        }
Ejemplo n.º 4
0
 public ActiveMulticastSessionController()
 {
     _activeMulticastSessionServices = new ActiveMulticastSessionServices();
     _userId = Convert.ToInt32(((ClaimsIdentity)User.Identity).Claims.Where(c => c.Type == "user_id")
                               .Select(c => c.Value).SingleOrDefault());
 }