Esempio n. 1
0
        public int EnsureStaged(string bulletinGuids, bool sync)
        {
            int num;
            ITaskExecutionInstance inst  = null;
            GuidCollection         guids = SoftwareUpdateAdvertismentSetPolicy.EnsureUpdatesStaged((IList <Guid>)ParseGuidList(ResolveToUpdates(bulletinGuids), true), sync, out inst);

            return(num = (guids != null) ? guids.Count : -1);
        }
Esempio n. 2
0
        public bool IsStaged(string bulletinGuids)
        {
            GuidCollection nonstagedUpdates = SoftwareUpdateAdvertismentSetPolicy.GetNonstagedUpdates(ParseGuidList(ResolveToUpdates(bulletinGuids), true), true);

            if (nonstagedUpdates != null)
            {
                return(nonstagedUpdates.Count == 0);
            }
            return(true);
        }
        private int UpdatePolicy(string stage, Guid policyGuid, string timestamp)
        {
            int  rc = 0;
            Guid targetGuid;

            SoftwareUpdateAdvertismentSetPolicy policyItem = Item.GetItem <SoftwareUpdateAdvertismentSetPolicy>(policyGuid, ItemLoadFlags.Writeable);

            if (stage == "TEST_TO_VALIDATION")
            {
                policyItem.Name = policyItem.Name.Replace(config.POLICY_TEST, config.POLICY_VALIDATED);
                targetGuid      = new Guid(config.Target_Guid_Validation);
            }
            else if (stage == "VALIDATION_TO_PRODUCTION")
            {
                policyItem.Name = policyItem.Name.Replace(config.POLICY_VALIDATED, config.POLICY_PRODUCTION);
                targetGuid      = new Guid(config.Target_Guid_Production);
            }
            else
            {
                return(-1);
            }

            policyItem.Name = policyItem.Name.Replace(timestamp, DateTime.Today.ToString("yyyy-MM-dd"));

            // FIXME: Catch exceptions here so we can link to non-existing targets. -> should validate target?
            try {
                if (policyItem.ResourceTargets.ContainsInstance(targetGuid))
                {
                    Console.WriteLine("\tTarget already exist in the policy.");
                }
                else
                {
                    policyItem.ResourceTargets.Add(targetGuid);
                }
            } catch (Exception e) {
                EventLog.ReportWarning("Caught error " + e.Message + "\n.Inner exception = " + e.InnerException + "\nStack trace = " + e.StackTrace);
            }

            Console.WriteLine("\tChanged policy name to {0} with target {1} added.", policyItem.Name, targetGuid);
            if (!config.Dry_Run)
            {
                policyItem.Save();
                EventLog.ReportInfo("Changed policy name to " + policyItem.Name + " with target " + targetGuid + " added.");
            }
            Console.WriteLine("\tPolicy saved!");
            return(rc);
        }
Esempio n. 4
0
        private int RunAutomation(GuidCollection bulletins)
        {
            Console.Write("\n\n");
            string now = DateTime.Now.ToString("yyyyMMddHHmmss");

            operation_log = "journal_" + now.ToString() + ".log";

            int i = 0;

            try {
                SecurityContextManager.SetContextData();
                PatchAPI wrap = new PatchAPI();

                string name = "";

                if (config.Dry_Run)
                {
                    Console.WriteLine("\n######## THIS IS A DRY RUN ########");
                }
                foreach (Guid bulletin in bulletins)
                {
                    name = Item.GetItem(bulletin).Name;
                    Console.WriteLine("");
                    Console.WriteLine("Processing bulletin {0} ({1}) now.", name, bulletin);

                    if (wrap.IsStaged(bulletin.ToString()))
                    {
                        Console.WriteLine("\tThis bulletin is already staged.");
                    }
                    else
                    {
                        if (config.RecreateMissingPolicies || config.Retarget)
                        {
                            // Skip this bulletin as it is not yet downloaded
                            continue;
                        }
                        Console.WriteLine("\t... bulletin will be staged now.");
                        if (!config.Dry_Run)
                        {
                            try {
                                wrap.EnsureStaged(bulletin.ToString(), true);
                                LogOp(String.Format("{0}: Staged bulletin {1} (guid={2}).", DateTime.Now.ToString(), name, bulletin.ToString()));
                            } catch {
                                // Do not retry staging error. Any download error is retried at the task level. Other errors won't be solved by retry...
                                if (config.ExcludeOnFail)
                                {
                                    DatabaseAPI.ExecuteNonQuery("insert patchautomation_excluded (bulletin) values ('" + name + "')");
                                    Console.WriteLine("Failed to stage bulletin {0} - the bulletin is now excluded.", name);
                                }
                                else
                                {
                                    Console.WriteLine("Failed to stage bulletin {0} - skipping the bulletin now.", name);
                                }
                                continue; // Go to the next bulletin
                            }
                        }
                        Console.WriteLine("\tBulletin is now staged.");
                    }
                    Console.WriteLine("\tChecking if we need to create a new policy now.");

                    string   policies_str = wrap.ResolveToPolicies(bulletin.ToString());
                    string[] policies_arr = policies_str.Split(',');

                    if (!config.Retarget && (policies_str == "" || policies_str.Length == 0 || config.Create_Duplicates))
                    {
                        Console.WriteLine("\t... create a policy for the bulletin now.");
                        if (!config.Dry_Run)
                        {
                            int j = 0; // Used for retry count
retry_create_policy:
                            try {
                                if (config.Target_Guids.Count == 0)
                                {
                                    wrap.CreateUpdatePolicy(name, bulletin.ToString(), true);
                                    LogOp(String.Format("{0}: Created policy for bulletin {1} (guid={2})", DateTime.Now.ToString(), name, bulletin.ToString()));
                                }
                                else
                                {
                                    wrap.CreateUpdatePolicy(name, bulletin.ToString(), config.Target_Guids, true);
                                    LogOp(String.Format("{0}: Created policy for bulletin {1} (guid={2}, target={3})", DateTime.Now.ToString(), name, bulletin.ToString(), config.Get_TargetGuids()));
                                }
                                // Added the bulletin to the exclusion list here
                                if (config.Create_Duplicates)
                                {
                                    DatabaseAPI.ExecuteNonQuery("insert patchautomation_excluded (bulletin) values ('" + name + "')");
                                }
                                i++;
                            } catch (Exception e) {
                                if (j++ < 3)
                                {
                                    Console.WriteLine(e.Message);
                                    Console.WriteLine(e.StackTrace);
                                    Console.WriteLine("\tFailed to create policy for bulletin {0} {1} time(s)...", name, j.ToString());
                                    goto retry_create_policy; // Retry ceiling not reach - let's do it again.
                                }
                                else                          // Retried 3 times - we quit and document the problem
                                {
                                    if (config.ExcludeOnFail)
                                    {
                                        DatabaseAPI.ExecuteNonQuery("insert patchautomation_excluded (bulletin) values ('" + name + "')");
                                        Console.WriteLine("\tFailed to create policy for bulletin {0} 3 times - the bulletin is now excluded.", name);
                                    }
                                    else
                                    {
                                        Console.WriteLine("\tFailed to create policy for bulletin {0} 3 times - skipping the bulletin now.", name);
                                    }
                                    continue; // Go to the next bulletin
                                }
                            }
                        }
                        Console.WriteLine("\tSoftware update policy created!");
                    }
                    else if (config.Retarget)
                    {
                        if (policies_arr.Length > 0)
                        {
                            /* ENHANCEMENT: 2018-01-24; There is no need to update each policy - one of them will update the "parent" policy which is enough :D.
                             *                          This avoid doing the same task 153 times when an Office policy as 153 updates!
                             *
                             * */
                            //                            foreach (string p in policies_arr) {
                            string p = policies_arr[0];
                            if (p.Length != 36)
                            {
                                continue;
                            }

                            Console.WriteLine("\tA policy already exists for this bulletin...");

                            Guid policyGuid = new Guid(p);
                            SoftwareUpdateAdvertismentSetPolicy policyItem = Item.GetItem <SoftwareUpdateAdvertismentSetPolicy>(policyGuid, ItemLoadFlags.Writeable);

                            Console.WriteLine("\tPolicy {0} will be retargetted now.", policyItem.Name);

                            policyItem.ResourceTargets.Clear();
                            foreach (string target in config.Target_Guids)
                            {
                                policyItem.ResourceTargets.Add(new Guid(target));
                            }
                            if (!config.Dry_Run)
                            {
                                int retry = 0;
save_item:
                                try {
                                    policyItem.Save();
                                    LogOp(String.Format("{0}: Retargetted policy for bulletin {1} (guid={2}, new target={3})", DateTime.Now.ToString(), name, bulletin.ToString(), config.Get_TargetGuids()));
                                    i++;
                                } catch {
                                    Console.WriteLine("\tCaught an exception. Retry " + retry.ToString() + "will start now.");
                                    if (retry < 10)
                                    {
                                        goto save_item;
                                    }
                                    Console.WriteLine("\tSaving the policy failed 10 times. Moving on to the next item.");
                                }
                            }
//                            } // Commented out for each node removed to fix
                        }
                    }
                    else
                    {
                        Console.WriteLine("\tA policy already exists for this bulletin.");
                    }
                    if (i > 9 && config.Test_Run)
                    {
                        break;
                    }
                }
            } catch (Exception e) {
                Console.WriteLine("Error message={0}\nInner Exception={1}\nStacktrace={2}", e.Message, e.InnerException, e.StackTrace);
                return(-1);
            }
            return(i);
        }