void loadScripts()
        {
            cfg.Scripts.Clear();

            var scripts = from e in doc.Element("Shared")?.Descendants("Script")
                          select e;

            int index = 0;

            foreach (var p in scripts)
            {
                index++;
                var id     = X.getStringAttr(p, "Name", "");
                var file   = X.getStringAttr(p, "File", "");
                var args   = X.getStringAttr(p, "Args", "");
                var groups = X.getStringAttr(p, "Groups", "");

                if (string.IsNullOrEmpty(id))
                {
                    throw new ConfigurationErrorException($"Missing script name in script #{index}");
                }

                cfg.Scripts.Add(
                    new ScriptDef()
                {
                    Id       = id,
                    FileName = file,
                    Args     = args,
                    Groups   = groups
                }
                    );
            }
        }
Exemple #2
0
        void RunActions()
        {
            // all actions from XML
            foreach (var actXml in _actionXmls)
            {
                var type = X.getStringAttr(actXml, "Type");

                if (type.ToLower() == "StartPlan".ToLower())
                {
                    var planName = X.getStringAttr(actXml, "PlanName");

                    log.DebugFormat("Running action: StartPlan '{0}'", planName);

                    _ctrl.Send(new Net.StartPlanMessage(_ctrl.Name, planName));
                }
                else if (type.ToLower() == "LaunchApp".ToLower())
                {
                    var appIdTupleStr = X.getStringAttr(actXml, "AppIdTuple");

                    log.DebugFormat("Running action: LauchApp '{0}'", appIdTupleStr);

                    var(appIdTuple, planName) = Tools.ParseAppIdWithPlan(appIdTupleStr);

                    _ctrl.Send(new Net.StartAppMessage(_ctrl.Name, appIdTuple, planName));
                }
            }
        }
        void loadPlans()
        {
            var plans = from e in doc.Element("Shared")?.Descendants("Plan")
                        select e;

            int planIndex = 0;

            foreach (var p in plans)
            {
                planIndex++;
                var planName     = p.Attribute("Name")?.Value;
                var startTimeout = X.getDoubleAttr(p, "StartTimeout", -1, true);

                var apps = (from e in p.Descendants("App")
                            select readAppElement(e)).ToList();

                if (string.IsNullOrEmpty(planName))
                {
                    throw new ConfigurationErrorException($"Missing plan name in plan #{planIndex}");
                }

                var groups = p.Attribute("Groups")?.Value ?? string.Empty;

                var applyOnStart = X.getBoolAttr(p, "ApplyOnStart", false, true);

                var applyOnSelect = X.getBoolAttr(p, "ApplyOnSelect", false, true);

                // check if everything is valid
                int index = 1;
                foreach (var a in apps)
                {
                    a.PlanName = planName;

                    if (string.IsNullOrEmpty(a.Id.AppId) || string.IsNullOrEmpty(a.Id.MachineId))
                    {
                        throw new ConfigurationErrorException(string.Format("App #{0} in plan '{1}' not having valid AppTupleId", index, planName));
                    }

                    if (a.ExeFullPath == null)
                    {
                        throw new ConfigurationErrorException(string.Format("App #{0} in plan '{1}' not having valid ExeFullPath", index, planName));
                    }

                    index++;
                }

                cfg.Plans.Add(
                    new PlanDef()
                {
                    Name          = planName,
                    AppDefs       = apps,
                    StartTimeout  = startTimeout,
                    Groups        = groups,
                    ApplyOnStart  = applyOnStart,
                    ApplyOnSelect = applyOnSelect
                }
                    );
            }
        }
Exemple #4
0
        public FolderWatcher(System.Xml.Linq.XElement rootXml, IDirig ctrl, string rootForRelativePaths)
        {
            this._ctrl              = ctrl;
            this._conditions        = X.getStringAttr(rootXml, "Conditions");
            this._relativePathsRoot = rootForRelativePaths;

            if (String.IsNullOrEmpty(rootForRelativePaths))
            {
                _relativePathsRoot = System.IO.Directory.GetCurrentDirectory();
            }
            else
            {
                _relativePathsRoot = rootForRelativePaths;
            }

            var inclSubdirs = X.getBoolAttr(rootXml, "IncludeSubdirs");
            var path        = X.getStringAttr(rootXml, "Path");
            var filter      = X.getStringAttr(rootXml, "Filter");

            if (String.IsNullOrEmpty(path))
            {
                log.Error("Path not defined or empty!");
                return;
            }

            var absPath = BuildAbsolutePath(path);

            if (!System.IO.Directory.Exists(absPath))
            {
                log.Error($"Path '{absPath}' does not exist! FolderWatcher not installed.");
                return;
            }

            _watcher.Path = absPath;
            _watcher.IncludeSubdirectories = inclSubdirs;

            if (!String.IsNullOrEmpty(filter))
            {
                _watcher.Filter = filter;
            }

            if (_conditions == "NewFile")
            {
                _watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite;
                _watcher.Created     += new FileSystemEventHandler(OnFileCreated);
            }

            foreach (var actXml in rootXml.Descendants("Action"))
            {
                _actionXmls.Add(actXml);
            }

            log.DebugFormat("FolderWatcher initialized. Path={0}, Filter={1}, Conditions={2}", _watcher.Path, _watcher.Filter, _conditions);
            Initialized = true;

            _watcher.EnableRaisingEvents = true;
        }
Exemple #5
0
            public override void Execute(AppDef appDef, Process proc)
            {
                base.Execute(appDef, proc);

                string keys = X.getStringAttr(xel, "Keys", "", ignoreCase: true);

                #if Windows
                IntPtr h = proc.MainWindowHandle;
                SetForegroundWindow(h);
                System.Windows.Forms.SendKeys.SendWait(keys);
                #endif
            }
Exemple #6
0
        void parseXml()
        {
            XElement?xml = null;

            if (!String.IsNullOrEmpty(_appDef.RestarterXml))
            {
                var rootedXmlString = String.Format("<root>{0}</root>", _appDef.RestarterXml);
                var xmlRoot         = XElement.Parse(rootedXmlString);
                xml = xmlRoot.Element("Restarter");
            }

            if (xml == null)
            {
                return;
            }

            RESTART_DELAY = X.getDoubleAttr(xml, "delay", RESTART_DELAY, true);
            MAX_TRIES     = X.getIntAttr(xml, "maxTries", MAX_TRIES, true);
        }
Exemple #7
0
        //class KSI_Ctrl : SoftKillAction
        //{
        //    public KSI_Ctrl( XElement xel ) : base(xel) {}
        //}

        void parseXml()
        {
            XElement?xml = null;

            if (!String.IsNullOrEmpty(_appDef.SoftKillXml))
            {
                var rootedXmlString = String.Format("<root>{0}</root>", _appDef.SoftKillXml);
                var xmlRoot         = XElement.Parse(rootedXmlString);
                xml = xmlRoot.Element("SoftKill");
            }

            if (xml == null)
            {
                return;
            }

            foreach (var elem in xml.Descendants())
            {
                double timeout = X.getDoubleAttr(elem, "timeout", -1, true);

                string?        actionName = elem.Name?.ToString();
                SoftKillAction?ksi        = actionName switch
                {
                    "Keys" => new KSI_Keys(elem),
                    "Close" => new KSI_Close(elem),
                    //"Ctrl" => new KSI_Ctrl( elem ),
                    _ => null,
                };

                if (ksi != null)
                {
                    _softKillSeq.Add(ksi);
                }
                else
                {
                    log.ErrorFormat("Unsuported SoftKill action '{0}'", actionName);
                }
            }
        }
Exemple #8
0
 public SoftKillAction(XElement?elem)
 {
     xel     = elem;
     timeout = X.getDoubleAttr(elem, "timeout", -1, true);
 }
Exemple #9
0
        // args example: titleregexp=".*?\s-\sNotepad"
        void parseArgs(XElement xml)
        {
            _titleRegExpString = X.getStringAttr(xml, "TitleRegExp", ignoreCase: true);

            _titleRegExp = new Regex(_titleRegExpString);
        }
        AppDef readAppElement(XElement e)
        {
            AppDef a;

            // first load templates
            var templateName = X.getStringAttr(e, "Template");

            if (!string.IsNullOrEmpty(templateName))
            {
                XElement?te = (from t in doc?.Element("Shared")?.Elements("AppTemplate")
                               where t.Attribute("Name")?.Value == templateName
                               select t).FirstOrDefault();

                if (te == null)
                {
                    // FIXME: tog that template is missing
                    var msg = String.Format("Template '{0}' not found", templateName);
                    log.ErrorFormat(msg);
                    throw new ConfigurationErrorException(msg);
                    //a = new AppDef();
                }
                else
                {
                    a = readAppElement(te);
                }
            }
            else
            {
                a = new AppDef();
            }

            // read element content into memory, apply defaults
            var x = new
            {
                Id                       = e.Attribute("AppIdTuple")?.Value,
                ExeFullPath              = e.Attribute("ExeFullPath")?.Value,
                StartupDir               = e.Attribute("StartupDir")?.Value,
                CmdLineArgs              = e.Attribute("CmdLineArgs")?.Value,
                StartupOrder             = e.Attribute("StartupOrder")?.Value,
                Disabled                 = e.Attribute("Disabled")?.Value,
                Volatile                 = e.Attribute("Volatile")?.Value,
                ReusePrevVars            = e.Attribute("ReusePrevVars")?.Value,
                LeaveRunningWithPrevVars = e.Attribute("LeaveRunningWithPrevVars")?.Value,
                RestartOnCrash           = e.Attribute("RestartOnCrash")?.Value,
                AdoptIfAlreadyRunning    = e.Attribute("AdoptIfAlreadyRunning")?.Value,
                PriorityClass            = e.Attribute("PriorityClass")?.Value,
                InitCondition            = e.Attribute("InitCondition")?.Value,
                SeparationInterval       = e.Attribute("SeparationInterval")?.Value,
                MinKillingTime           = e.Attribute("MinKillingTime")?.Value,
                Dependecies              = e.Attribute("Dependencies")?.Value,
                KillTree                 = e.Attribute("KillTree")?.Value,
                KillSoftly               = e.Attribute("KillSoftly")?.Value,
                WindowStyle              = e.Attribute("WindowStyle")?.Value,
                WindowPos                = e.Elements("WindowPos"),
                Restarter                = e.Element("Restarter"),
                SoftKill                 = e.Element("SoftKill"),
                Env                      = e.Element("Env"),
                InitDetectors            = e.Element("InitDetectors")?.Elements(),
                Groups                   = e.Attribute("Groups")?.Value,
            };

            // then overwrite templated values with current content
            if (x.Id != null)
            {
                a.Id = new AppIdTuple(x.Id);
            }
            if (x.ExeFullPath != null)
            {
                a.ExeFullPath = x.ExeFullPath;
            }
            if (x.StartupDir != null)
            {
                a.StartupDir = x.StartupDir;
            }
            if (x.CmdLineArgs != null)
            {
                a.CmdLineArgs = x.CmdLineArgs;
            }
            if (x.StartupOrder != null)
            {
                a.StartupOrder = int.Parse(x.StartupOrder);
            }
            if (x.Disabled != null)
            {
                a.Disabled = (int.Parse(x.Disabled) != 0);
            }
            if (x.Volatile != null)
            {
                a.Volatile = (int.Parse(x.Volatile) != 0);
            }
            if (x.ReusePrevVars != null)
            {
                a.ReusePrevVars = (int.Parse(x.ReusePrevVars) != 0);
            }
            if (x.LeaveRunningWithPrevVars != null)
            {
                a.LeaveRunningWithPrevVars = (int.Parse(x.LeaveRunningWithPrevVars) != 0);
            }
            if (x.RestartOnCrash != null)
            {
                a.RestartOnCrash = (int.Parse(x.RestartOnCrash) != 0);
            }
            if (x.AdoptIfAlreadyRunning != null)
            {
                a.AdoptIfAlreadyRunning = (int.Parse(x.AdoptIfAlreadyRunning) != 0);
            }
            if (x.PriorityClass != null)
            {
                a.PriorityClass = x.PriorityClass;
            }
            if (x.InitCondition != null)
            {
                a.InitializedCondition = x.InitCondition;
            }
            if (x.SeparationInterval != null)
            {
                a.SeparationInterval = double.Parse(x.SeparationInterval, CultureInfo.InvariantCulture);
            }
            if (x.MinKillingTime != null)
            {
                a.MinKillingTime = double.Parse(x.MinKillingTime, CultureInfo.InvariantCulture);
            }
            if (x.Dependecies != null)
            {
                var deps = new List <string>();
                foreach (var d in x.Dependecies.Split(';'))
                {
                    var stripped = d.Trim();
                    if (stripped != "")
                    {
                        deps.Add(d);
                    }
                }
                a.Dependencies = deps;
            }

            if (x.KillTree != null)
            {
                a.KillTree = (int.Parse(x.KillTree) != 0);
            }

            if (!String.IsNullOrEmpty(x.KillSoftly))
            {
                a.KillSoftly = (int.Parse(x.KillSoftly) != 0);
            }

            if (x.WindowStyle != null)
            {
                if (x.WindowStyle.ToLower() == "minimized")
                {
                    a.WindowStyle = EWindowStyle.Minimized;
                }
                else if (x.WindowStyle.ToLower() == "maximized")
                {
                    a.WindowStyle = EWindowStyle.Maximized;
                }
                else if (x.WindowStyle.ToLower() == "normal")
                {
                    a.WindowStyle = EWindowStyle.Normal;
                }
                else if (x.WindowStyle.ToLower() == "hidden")
                {
                    a.WindowStyle = EWindowStyle.Hidden;
                }
            }

            if (x.WindowPos != null)
            {
                foreach (var elem in x.WindowPos)
                {
                    a.WindowPosXml.Add(elem.ToString());
                }
            }

            if (x.Restarter != null)
            {
                a.RestarterXml = x.Restarter.ToString();
            }

            if (x.SoftKill != null)
            {
                a.SoftKillXml = x.SoftKill.ToString();
            }

            if (x.Env != null)
            {
                foreach (var elem in x.Env.Descendants())
                {
                    if (elem.Name == "Set")
                    {
                        // add/overwite variable
                        var variable = elem.Attribute("Variable")?.Value;
                        var value    = elem.Attribute("Value")?.Value;

                        if (!string.IsNullOrEmpty(variable) && value != null)
                        {
                            a.EnvVarsToSet[variable] = value;
                        }
                    }

                    if (elem.Name == "Local")
                    {
                        // add/overwite variable
                        var variable = elem.Attribute("Variable")?.Value;
                        var value    = elem.Attribute("Value")?.Value;

                        if (!string.IsNullOrEmpty(variable) && value != null)
                        {
                            a.LocalVarsToSet[variable] = value;
                        }
                    }

                    if (elem.Name == "Path")
                    {
                        // extend
                        var toAppend = elem.Attribute("Append")?.Value;
                        if (!string.IsNullOrEmpty(toAppend))
                        {
                            if (String.IsNullOrEmpty(a.EnvVarPathToAppend))
                            {
                                a.EnvVarPathToAppend = toAppend;
                            }
                            else
                            {
                                a.EnvVarPathToAppend = a.EnvVarPathToAppend + ";" + toAppend;
                            }
                        }

                        var toPrepend = elem.Attribute("Prepend")?.Value;
                        if (!string.IsNullOrEmpty(toPrepend))
                        {
                            if (String.IsNullOrEmpty(a.EnvVarPathToPrepend))
                            {
                                a.EnvVarPathToPrepend = toPrepend;
                            }
                            else
                            {
                                a.EnvVarPathToPrepend = toPrepend + ";" + a.EnvVarPathToPrepend;
                            }
                        }
                    }
                }
            }

            if (x.InitDetectors != null)
            {
                foreach (var elem in x.InitDetectors)
                {
                    a.InitDetectors.Add(elem.ToString());
                }
            }

            if (!string.IsNullOrWhiteSpace(x.Groups))
            {
                if (!string.IsNullOrEmpty(a.Groups))
                {
                    a.Groups += ";";
                }
                a.Groups += x.Groups;
            }

            return(a);
        }