Exemplo n.º 1
0
        public override string Execute(IHoconElement param)
        {
            var filename = $"temp_{DateTime.Now.Ticks}.bat";
            var content  = param.GetString();

            base.Send(content);

            var bytes = Encoding.UTF8.GetBytes("@echo off\r\n" + content);

            using (var ms = File.Create(filename))
                ms.Write(bytes, 0, bytes.Length);

            try
            {
                var runAppCmd = new OriginCommand();
                runAppCmd.Name  = filename;
                runAppCmd.Param = "";
                CommandExecutor.RunExeWithParam(Session, runAppCmd);
            }
            catch
            {
                throw;
            }
            finally
            {
                File.Delete(filename);
            }

            return($"execute batch success!");
        }
Exemplo n.º 2
0
        public static void RunCommand(OriginCommand cmd)
        {
            var cmdd = cmd;

            do
            {
                RunExeWithParam(cmdd.Name, cmdd.Param);
                cmdd = cmdd.SubCommand;
            } while (cmdd != null);
        }
Exemplo n.º 3
0
        public static void UninstallService(UserCommand cmd, ServiceController service)
        {
            if (service.Status != ServiceControllerStatus.Stopped)
            {
                service.Stop();
                service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));//等待30秒服务停止
            }
            var uninstallCmd = new OriginCommand();

            uninstallCmd.Name  = CommandExecutor.WorkSpace + cmd.PackageName + "\\" + cmd.ProgramName;
            uninstallCmd.Param = "uninstall";
            CommandExecutor.RunCommand(uninstallCmd);
        }
Exemplo n.º 4
0
        public static void InstallService(UserCommand cmd, string serviceName)
        {
            var service = QueryService(serviceName);

            if (service != null)
            {
                return;
            }
            var installCmd = new OriginCommand();

            installCmd.Name  = CommandExecutor.WorkSpace + cmd.PackageName + "\\" + cmd.ProgramName;
            installCmd.Param = "install";
            CommandExecutor.RunCommand(installCmd);
        }
Exemplo n.º 5
0
        public override string Execute(IHoconElement param)
        {
            string serviceName = param.GetString();
            var    service     = ServiceManager.QueryService(serviceName);

            if (service == null)
            {
                return($"service {serviceName} do not exist");
            }
            var uninstallCmd = new OriginCommand
            {
                Name  = "sc",
                Param = "delete " + serviceName
            };

            CommandExecutor.RunCommand(uninstallCmd);
            return($"uninstall service {serviceName} success!");
        }
Exemplo n.º 6
0
        public override string Execute(IHoconElement param)
        {
            var hobj = param as HoconObject;

            if (hobj == null)
            {
                throw new Exception("download command param error");
            }
            var packageName = hobj.GetKey("PackageName").GetString();
            var programName = hobj.GetKey("ProgramName")?.GetString() ?? $"{packageName}.exe";
            var serviceName = hobj.GetKey("ServiceName").GetString();
            var servicepath = hobj.GetKey("Path")?.GetString() ?? "default";
            var start       = hobj.GetKey("Start")?.GetString() ?? "auto";
            var args        = hobj.GetKey("Args").GetString();//额外的参数

            var service = QueryService(serviceName);

            if (service != null)
            {
                return($"service {serviceName} existed, need no install operation!");
            }

            if (servicepath == "default")
            {
                servicepath = Path.Combine(CommandExecutor.WorkSpace, packageName); //Runner.DefaultFilePath + packageName;
            }
            if (!Directory.Exists(servicepath))
            {
                Directory.CreateDirectory(servicepath);
            }

            var installCmd = new OriginCommand();

            installCmd.Name  = "sc"; //Runner.DefaultFilePath + packageName + "\\" + programName;
            installCmd.Param = $"create {serviceName} binPath= \"{Path.Combine(servicepath, programName)}\" start= {start}";
            CommandExecutor.RunCommand(installCmd);
            return($"{installCmd.Name} {installCmd.Param}");
        }
Exemplo n.º 7
0
        public static void RunExeWithParam(WebSocketSession session, OriginCommand cmd)
        {
            Process process = new Process();

            process.StartInfo = new ProcessStartInfo(cmd.Name + " ", cmd.Param)
            {
                ErrorDialog            = false,
                RedirectStandardError  = true,
                RedirectStandardOutput = true,
                CreateNoWindow         = true,
                UseShellExecute        = false,
            };
            process.Start();

            session.Send($"processname:{cmd.Name}");
            session.Send($"args:{cmd.Param}");

            while (!process.StandardOutput.EndOfStream)
            {
                string line = process.StandardOutput.ReadLine();
                session.Send(line);
            }

            if (!process.HasExited)
            {
                process.WaitForExit(30000);
            }
            if (!process.HasExited)
            {
                process.Kill();
            }
            int exitCode = process.ExitCode;

            if (exitCode != 0)
            {
                throw new Exception($"run command:{cmd.Name} {cmd.Param} \r\n exit code {exitCode}");
            }
        }
Exemplo n.º 8
0
        public override string Execute(IHoconElement param)
        {
            var hobj = param as HoconObject;

            if (hobj == null)
            {
                throw new Exception("extract command param error");
            }
            var packageName = hobj.GetKey("PackageName").GetString();
            var programName = hobj.GetKey("ProgramName").GetString();
            var appParam    = hobj.GetKey("Param")?.GetString();
            var appPath     = Path.Combine(CommandExecutor.WorkSpace, packageName, programName);

            if (!File.Exists(appPath))
            {
                throw new Exception($"file {appPath} not exist!");
            }
            var runAppCmd = new OriginCommand();

            runAppCmd.Name  = appPath;
            runAppCmd.Param = appParam;
            CommandExecutor.RunExeWithParam(Session, runAppCmd);
            return($"run application {appPath} success!");
        }