Exemplo n.º 1
0
        public IEnumerable <ComputerMunkiEntity> GetTemplateComputers(int id)
        {
            var result = _computerMunkiServices.GetComputersForManifestTemplate(id);

            if (result == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return(result);
        }
Exemplo n.º 2
0
        public int Apply(int templateId)
        {
            var errorCount = 0;
            var basePath   = SettingServices.GetSettingValue(SettingStrings.MunkiBasePath) + Path.DirectorySeparatorChar +
                             "manifests" +
                             Path.DirectorySeparatorChar;

            var groups = _groupMunkiServices.GetGroupsForManifestTemplate(templateId);

            if (SettingServices.GetSettingValue(SettingStrings.MunkiPathType) == "Local")
            {
                foreach (var munkiGroup in groups)
                {
                    var effectiveManifest = new EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                    var computersInGroup  = _groupServices.GetGroupMembersWithImages(munkiGroup.GroupId);
                    foreach (var computer in computersInGroup)
                    {
                        if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                        {
                            errorCount++;
                        }
                    }
                }
            }
            else
            {
                using (var unc = new UncServices())
                {
                    var smbPassword =
                        new EncryptionServices().DecryptText(
                            SettingServices.GetSettingValue(SettingStrings.MunkiSMBPassword));
                    var smbDomain = string.IsNullOrEmpty(SettingServices.GetSettingValue(SettingStrings.MunkiSMBDomain))
                        ? ""
                        : SettingServices.GetSettingValue(SettingStrings.MunkiSMBDomain);
                    if (
                        unc.NetUseWithCredentials(SettingServices.GetSettingValue(SettingStrings.MunkiBasePath),
                                                  SettingServices.GetSettingValue(SettingStrings.MunkiSMBUsername), smbDomain,
                                                  smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiGroup in groups)
                        {
                            var effectiveManifest = new EffectiveMunkiTemplate().Group(munkiGroup.GroupId);
                            var computersInGroup  = _groupServices.GetGroupMembersWithImages(munkiGroup.GroupId);
                            foreach (var computer in computersInGroup)
                            {
                                if (
                                    !WritePath(basePath + computer.Name,
                                               Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                                {
                                    errorCount++;
                                }
                            }
                        }
                    }
                    else
                    {
                        log.Error("Failed to connect to " +
                                  SettingServices.GetSettingValue(SettingStrings.MunkiBasePath) + "\r\nLastError = " +
                                  unc.LastError);
                        foreach (var munkiGroup in groups)
                        {
                            var computersInGroup = _groupServices.GetGroupMembersWithImages(munkiGroup.GroupId);
                            errorCount += computersInGroup.Count();
                        }
                    }
                }
            }
            var computers = _computerMunkiServices.GetComputersForManifestTemplate(templateId);

            if (SettingServices.GetSettingValue(SettingStrings.MunkiPathType) == "Local")
            {
                foreach (var munkiComputer in computers)
                {
                    var effectiveManifest = new EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                    var computer          = _computerServices.GetComputer(munkiComputer.ComputerId);
                    if (!WritePath(basePath + computer.Name, Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                    {
                        errorCount++;
                    }
                }
            }
            else
            {
                using (var unc = new UncServices())
                {
                    var smbPassword =
                        new EncryptionServices().DecryptText(
                            SettingServices.GetSettingValue(SettingStrings.MunkiSMBPassword));
                    var smbDomain = string.IsNullOrEmpty(SettingServices.GetSettingValue(SettingStrings.MunkiSMBDomain))
                        ? ""
                        : SettingServices.GetSettingValue(SettingStrings.MunkiSMBDomain);
                    if (
                        unc.NetUseWithCredentials(SettingServices.GetSettingValue(SettingStrings.MunkiBasePath),
                                                  SettingServices.GetSettingValue(SettingStrings.MunkiSMBUsername), smbDomain,
                                                  smbPassword) || unc.LastError == 1219)
                    {
                        foreach (var munkiComputer in computers)
                        {
                            var effectiveManifest =
                                new EffectiveMunkiTemplate().Computer(munkiComputer.ComputerId);
                            var computer = _computerServices.GetComputer(munkiComputer.ComputerId);

                            if (
                                !WritePath(basePath + computer.Name,
                                           Encoding.UTF8.GetString(effectiveManifest.ToArray())))
                            {
                                errorCount++;
                            }
                        }
                    }
                    else
                    {
                        log.Error("Failed to connect to " +
                                  SettingServices.GetSettingValue(SettingStrings.MunkiBasePath) + "\r\nLastError = " +
                                  unc.LastError);
                        errorCount += computers.Count();
                    }
                }
            }

            if (errorCount > 0)
            {
                return(errorCount);
            }

            var includedTemplates = new List <MunkiManifestTemplateEntity>();

            foreach (var munkiGroup in groups)
            {
                foreach (var template in _groupServices.GetGroupMunkiTemplates(munkiGroup.GroupId))
                {
                    includedTemplates.Add(_munkiManifestTemplateServices.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var computer in computers)
            {
                foreach (var template in _computerServices.GetMunkiTemplates(computer.ComputerId))
                {
                    includedTemplates.Add(_munkiManifestTemplateServices.GetManifest(template.MunkiTemplateId));
                }
            }

            foreach (var template in includedTemplates)
            {
                template.ChangesApplied = 1;
                _munkiManifestTemplateServices.UpdateManifest(template);
            }

            return(0);
        }