예제 #1
0
        private int RunAutomation()
        {
            int i  = 0;
            int rc = 0;

            try {
                GuidCollection bulletins = new GuidCollection();
                bulletins = GetSoftwareBulletins();

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

                if (config.Dry_Run)
                {
                    Console.WriteLine("\n######## THIS IS A DRY RUN ########");
                }

                foreach (Guid bulletin in bulletins)
                {
                    string bulletin_name = Item.GetItem(bulletin).Name;
                    Console.WriteLine("\n### BEGIN {0}, {1}", bulletin_name, bulletin);
                    if (wrap.IsStaged(bulletin.ToString()))
                    {
                        Console.WriteLine("PHASE 1: This bulletin is already staged.");
                    }
                    else
                    {
                        Console.WriteLine("PHASE 1: This bulletin will be staged now.");
                        if (!config.Dry_Run)
                        {
                            try {
                                EventLog.ReportInfo(String.Format("Bulletin {0} will be staged now.", bulletin_name));
                                wrap.EnsureStaged(bulletin.ToString(), true);
                            } 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 ('" + bulletin_name + "')");
                                    EventLog.ReportError(String.Format("Failed to stage bulletin {0} 3 times - the bulletin is now excluded.", bulletin_name));
                                }
                                else
                                {
                                    EventLog.ReportError(String.Format("Failed to stage bulletin {0} 3 times - skipping the bulletin now.", bulletin_name));
                                }
                                continue;
                            }
                        }
                    }

                    string policyGuids = "";
                    policyGuids = wrap.ResolveToPolicies(bulletin.ToString());

                    if (policyGuids == "" || policyGuids.Length == 0 || config.Create_Duplicates)
                    {
                        string date        = DateTime.Today.ToString("yyyy-MM-dd");
                        string policy_name = bulletin_name + ", " + config.POLICY_TEST + ", " + date;

                        Console.WriteLine("PHASE 2: Creating policy {0} now.", policy_name);
                        if (!config.Dry_Run)
                        {
                            int k = 0; //retry counter
retry_policy_creation:
                            try {
                                wrap.CreateUpdatePolicy(policy_name, bulletin.ToString(), config.Target_Guid_Test, true);
                                EventLog.ReportInfo(String.Format("SoftwareUpdateAdvertisement policy {0} (targetguid={1}) was created.", policy_name, config.Target_Guid_Test));
                            } catch {
                                if (k++ < 3)   // Policy creation  is retried 3 times - as the most likely fail case i deadlock.
                                {
                                    EventLog.ReportWarning(String.Format("Failed to create policy for bulletin {0} {1} times...", bulletin_name, k.ToString()));
                                    goto retry_policy_creation;
                                }
                                else     // Failed three times - skip or exclude based on CLI config
                                {
                                    if (config.ExcludeOnFail)
                                    {
                                        DatabaseAPI.ExecuteNonQuery("insert patchautomation_excluded (bulletin) values ('" + bulletin_name + "')");
                                        EventLog.ReportError(String.Format("Failed to create policy for bulletin {0} 3 times - the bulletin is now excluded.", bulletin_name));
                                    }
                                    else
                                    {
                                        EventLog.ReportError(String.Format("Failed to create policy for bulletin {0} 3 times - skipping the bulletin now.", bulletin_name));
                                    }
                                    continue;
                                }
                            }
                            if (config.Create_Duplicates)
                            {
                                DatabaseAPI.ExecuteNonQuery("insert patchautomation_excluded (bulletin) values ('" + bulletin_name + "')");
                            }
                            i++;
                        }
                        Console.WriteLine("\tSoftware update policy created!");
                    }
                    else
                    {
                        Console.WriteLine("PHASE 2: Policy already exists.");
                        string[] _policyGuids = policyGuids.Split(',');
                        foreach (string policy in _policyGuids)
                        {
                            Guid   policyGuid = new Guid(policy);
                            string policyName = Item.GetItem(policyGuid).Name;

                            if (policyName.Contains(config.POLICY_TEST))
                            {
                                string timestamp = policyName.Substring(policyName.Length - 10);

                                DateTime policyDate = DateTime.Parse(timestamp);
                                TimeSpan ts         = DateTime.Today - policyDate;
                                if (ts.Days >= config.Span_Test_To_Validation)
                                {
                                    Console.WriteLine("PHASE 3: Policy needs retargetting (test -> validation)");
                                    this.UpdatePolicy("TEST_TO_VALIDATION", policyGuid, timestamp);
                                }
                                else
                                {
                                    Console.WriteLine("PHASE 3: Policy '{0}' doesn't need re-targetting.", policyName);
                                }
                            }
                            else if (policyName.Contains(config.POLICY_VALIDATED))
                            {
                                string timestamp = policyName.Substring(policyName.Length - 10);

                                DateTime policyDate = DateTime.Parse(timestamp);
                                TimeSpan ts         = DateTime.Today - policyDate;
                                if (ts.Days >= config.Span_Validation_To_Production)
                                {
                                    Console.WriteLine("PHASE 4: Policy needs retargetting (validation -> production)");
                                    this.UpdatePolicy("VALIDATION_TO_PRODUCTION", policyGuid, timestamp);
                                }
                                else
                                {
                                    Console.WriteLine("PHASE 4: Policy '{0}' doesn't need re-targetting.", policyName);
                                }
                            }
                        }
                    }
                    if (i == 10 && config.Test_Run)
                    {
                        break; // Limit the staging to 10 bulletin whilst testing
                    }
                    Console.WriteLine("### END");
                    rc = 0;
                }
            } catch (Exception e) {
                LoggingAPI.ReportException(e);
                rc = -2;
            }

            Console.WriteLine("\n{0} software update policy creation tasks were started.", i.ToString());
            return(rc);
        }
예제 #2
0
        static int Main(string[] args)
        {
            int rc = 0;

            if (SecurityAPI.is_user_admin())
            {
                PatchAutomate automate = new PatchAutomate();

                automate.config = new CliConfig(config_types.PatchAutomation);

                CliInit init;
                if (args.Length == 1 && args[0].StartsWith("/config="))
                {
                    init = new CliInit(CliInit.GetConfigFromFile(ref args), ref automate.config);
                }
                else
                {
                    init = new CliInit(args, ref automate.config);
                }

                EventLog.ReportInfo(init.DumpConfig(config_types.PatchAutomation));

                if (init.ParseArgs())
                {
                    _debug = automate.config.Debug;

                    Console.WriteLine("PatchAutomate starting.");
                    EventLog.ReportInfo("Symantec BCS PatchAutomate is starting.");

                    try {
                        rc = automate.RunAutomation();
                    } catch (Exception e) {
                        LoggingAPI.ReportException(e);
                        rc = -2;
                    }
                }
                else
                {
                    if (automate.config.Print_Version)
                    {
                        Console.WriteLine("{{CWoC}} PatchAutomation version {0}", Constant.VERSION);
                        return(0);
                    }
                    Console.WriteLine(Constant.PATCH_AUTOMATE_HELP);
                    if (!automate.config.Help_Needed)
                    {
                        EventLog.ReportInfo("PatchAutomate invocation incorrect. Printed command line help and returned -1.");
                        return(-1);
                    }
                    return(0);
                }

                if (rc == 0)
                {
                    LoggingAPI.ReportInfo("PatchAutomate execution completed. See you soon...");
                }
            }
            else
            {
                LoggingAPI.ReportInfo("Access denied - Only Administrators are allowed to use this tool. This entry will be recorded in the Altiris Server logs.");
                rc = -1;
            }
            return(rc);
        }