public void TryKillProcessAndChildrenRecursively(Process process) { var messages = new List <string>(); var result = ShellExecutor.ExecuteCommand( "/bin/bash", $"-c \"kill -TERM {process.Id}\"", Environment.CurrentDirectory, m => { }, m => { }, m => messages.Add(m) ); if (result != 0) { throw new ShellExecutionException(result, messages); } //process.Kill() doesnt seem to work in netcore 2.2 there have been some improvments in netcore 3.0 as well as also allowing to kill child processes //https://github.com/dotnet/corefx/pull/34147 //In netcore 2.2 if the process is terminated we still get stuck on process.WaitForExit(); we need to manually check to see if the process has exited and then close it. if (process.HasExited) { process.Close(); } }
private static EsDriveInfo GetEsDriveInfoUnix(string directory, ILogger log) { // http://unix.stackexchange.com/questions/11311/how-do-i-find-on-which-physical-device-a-folder-is-located // example // Filesystem 1K-blocks Used Available Use% Mounted on // /dev/sda1 153599996 118777100 34822896 78% /media/CC88FD3288FD1C20 try { if (!Directory.Exists(directory)) { return(null); } var driveInfo = ShellExecutor.GetOutput("df", string.Format("-P {0}", directory)); var driveInfoLines = driveInfo.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (driveInfoLines.Length == 0) { return(null); } var ourline = driveInfoLines[1]; var trimmedLine = SystemStatsHelper.SpacesRegex.Replace(ourline, " "); var info = trimmedLine.Split(' '); var totalBytes = long.Parse(info[1].Trim()) * 1024; // the '1024-blocks' column var availableBytes = long.Parse(info[3].Trim()) * 1024; // the 'Available' column var mountPoint = info[5]; // the 'Mounted on' column return(new EsDriveInfo(mountPoint, totalBytes, availableBytes)); } catch (Exception ex) { log.Debug(ex, "Could not get drive name for directory '{directory}' on Unix.", directory); return(null); } }
private static string GetDirectoryRootInUnix(string directory, ILogger log) { // http://unix.stackexchange.com/questions/11311/how-do-i-find-on-which-physical-device-a-folder-is-located // example // Filesystem 1K-blocks Used Available Use% Mounted on // /dev/sda1 153599996 118777100 34822896 78% /media/CC88FD3288FD1C20 try { if (!Directory.Exists(directory)) { return(null); } var driveInfo = ShellExecutor.GetOutput("df", string.Format("-P {0}", directory)); var driveInfoLines = driveInfo.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); if (driveInfoLines.Length == 0) { return(null); } var ourline = driveInfoLines[1]; var trimmedLine = SystemStatsHelper.SpacesRegex.Replace(ourline, " "); var driveName = trimmedLine.Split(' ')[5]; //we choose the 'mounted on' column return(driveName); } catch (Exception ex) { log.DebugException(ex, "Couldn't get drive name for directory '{0}' on Unix.", directory); return(null); } }
public static Func <HttpRequest, HttpResponse, RouteData, Task> Create(ShellExecutor executor, GitValidationFlow gitValidationFlow, ILogger logger, IMetrics metrics) => async(req, res, routedata) => { var oldCommit = req.Query["oldrev"].ToString().Trim(); var newCommit = req.Query["newrev"].ToString().Trim(); var quarantinePath = req.Query["quarantinepath"].ToString(); var objectsDir = quarantinePath.Substring(quarantinePath.IndexOf("./objects")); var gitExecutor = executor.CreateCommandExecutor("git", pStart => { pStart.Environment["GIT_ALTERNATE_OBJECT_DIRECTORIES"] = "./objects"; pStart.Environment["GIT_OBJECT_DIRECTORY"] = objectsDir; }); try { await gitValidationFlow.Validate(oldCommit, newCommit, gitExecutor); metrics.Measure.Counter.Increment(Validation, Success); } catch (Exception ex) { metrics.Measure.Counter.Increment(Validation, Failure); res.StatusCode = 400; logger.LogWarning("Validation failed, {error}", ex.Message); await res.WriteAsync(ex.Message); } };
public static ShellExecutor ForwardEnvVariable(this ShellExecutor shellExecutor, string variableName) => (cmd, args, init) => shellExecutor(cmd, args, (p) => { p.EnvironmentVariables[variableName] = Environment.GetEnvironmentVariable(variableName); init?.Invoke(p); });
public static ShellExecutor WithWorkingDirectory(this ShellExecutor shellExecutor, string directory) => (cmd, args, init) => shellExecutor(cmd, args, (p) => { p.WorkingDirectory = directory; init?.Invoke(p); });
public static ShellExecutor WithUser(this ShellExecutor shellExecutor, string userName) => (cmd, args, init) => shellExecutor(cmd, args, (p) => { p.UserName = userName; init?.Invoke(p); });
private static string GetDirectoryRootInLinux(string directory, ILogger log) { // http://unix.stackexchange.com/questions/11311/how-do-i-find-on-which-physical-device-a-folder-is-located // example // Filesystem 1K-blocks Used Available Use% Mounted on // /dev/sda1 153599996 118777100 34822896 78% /media/CC88FD3288FD1C20 try { var driveInfo = ShellExecutor.GetOutput("df", directory); var driveInfoLines = driveInfo.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var ourline = driveInfoLines[1]; var spaces = new Regex(@"[\s\t]+", RegexOptions.Compiled); var trimmedLine = spaces.Replace(ourline, " "); var driveName = trimmedLine.Split(' ')[5]; //we choose the 'mounted on' column return(driveName); } catch (Exception ex) { log.DebugException(ex, "couldn't get drive name for directory {0} on linux", directory); return(null); } }
private long GetFreeMemOnOSX() { int freePages = 0; try { var vmstat = ShellExecutor.GetOutput("vm_stat"); var sysctlStats = vmstat.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); foreach (var line in sysctlStats) { var l = line.Substring(0, line.Length - 1); var pieces = l.Split(':'); if (pieces.Length == 2) { if (pieces[0].Trim().ToLower() == "pages free") { freePages = int.Parse(pieces[1]); break; } } } return(4096 * freePages); } catch (Exception ex) { _log.DebugException(ex, "Could not get free memory on OSX."); return(-1); } }
private string GetFOP2WebInterfaceURL() { var settings = Properties.Settings.Default; return(ShellExecutor.ReplacePlaceholder(settings.FOP2Url, new[] { new KeyValuePair <string, string>("%CONTEXT%", settings.PBXContext), new KeyValuePair <string, string>("%USER%", settings.Username), new KeyValuePair <string, string>("%PASS%", settings.Password) })); }
private void event_BalloonClicked(object sender, DDBalloonClickedEventArgs e) { var s = Properties.Settings.Default; if (!string.IsNullOrEmpty(s.DialCmd_File)) { var phonenumber = Filters.DigitsOnly(e.BalloonInfo.CallerIdNumber); var command = new ShellCommand(s.DialCmd_File, s.DialCmd_Args); command.WorkingDirectory = s.DialCmd_WorkDir; ShellExecutor.ExecuteCommand(command, new[] { new KeyValuePair <string, string>("%PHONENUMBER%", phonenumber) }); } }
// http://www.cyberciti.biz/files/scripts/freebsd-memory.pl.txt private long GetFreeMemOnBSD() { try { var sysctl = ShellExecutor.GetOutput("sysctl", "-n hw.physmem hw.pagesize vm.stats.vm.v_free_count vm.stats.vm.v_cache_count vm.stats.vm.v_inactive_count"); var sysctlStats = sysctl.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); long pageSize = long.Parse(sysctlStats[1]); long freePages = long.Parse(sysctlStats[2]); long cachePages = long.Parse(sysctlStats[3]); long inactivePages = long.Parse(sysctlStats[4]); return(pageSize * (freePages + cachePages + inactivePages)); } catch (Exception ex) { _log.DebugException(ex, "Could not get free memory on BSD."); return(-1); } }
private long GetFreeMemOnLinux() { string meminfo = null; try { meminfo = ShellExecutor.GetOutput("free", "-b"); var meminfolines = meminfo.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var ourline = meminfolines[1]; var trimmedLine = SpacesRegex.Replace(ourline, " "); var freeRamStr = trimmedLine.Split(' ')[3]; return(long.Parse(freeRamStr)); } catch (Exception ex) { _log.DebugException(ex, "Could not get free mem on linux, received memory info raw string: [{meminfo}]", meminfo); return(-1); } }
private long GetFreeMemOnLinux() { try { var meminfo = ShellExecutor.GetOutput("free", "-b"); var meminfolines = meminfo.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); var ourline = meminfolines[1]; var Spaces = new Regex(@"[\s\t]+", RegexOptions.Compiled); var trimmedLine = Spaces.Replace(ourline, " "); var freeRamStr = trimmedLine.Split(' ')[3]; return(long.Parse(freeRamStr)); } catch (Exception ex) { _log.DebugException(ex, "couldn't get free mem on linux"); return(-1); } }
static int Execute( string command, string arguments, string workingDirectory, out StringBuilder debugMessages, out StringBuilder infoMessages, out StringBuilder errorMessages, NetworkCredential?networkCredential, IDictionary <string, string>?customEnvironmentVariables, CancellationToken cancel ) { var debug = new StringBuilder(); var info = new StringBuilder(); var error = new StringBuilder(); var exitCode = ShellExecutor.ExecuteCommand( command, arguments, workingDirectory, x => { Console.WriteLine($"{DateTime.UtcNow} DBG: {x}"); debug.Append(x); }, x => { Console.WriteLine($"{DateTime.UtcNow} INF: {x}"); info.Append(x); }, x => { Console.WriteLine($"{DateTime.UtcNow} ERR: {x}"); error.Append(x); }, networkCredential, customEnvironmentVariables, cancel); debugMessages = debug; infoMessages = info; errorMessages = error; return(exitCode); }
private void event_WebInterface(object sender, EventArgs e) { string url; if (sender == _fop2webinterface) { url = Properties.Settings.Default.FOP2Url; } else { url = Properties.Settings.Default.FOP2UserPortal; } var command = new ShellCommand(url); ShellExecutor.ExecuteCommand(command, new[] { new KeyValuePair <string, string>("%CONTEXT%", _client.Context), new KeyValuePair <string, string>("%USER%", _client.Username), new KeyValuePair <string, string>("%PASS%", _client.Password), }); }
public static Func <HttpRequest, HttpResponse, RouteData, Task> Create(ShellExecutor executor, GitValidationFlow gitValidationFlow) => async(req, res, routedata) => { var oldCommit = req.Query["oldrev"].ToString().Trim(); var newCommit = req.Query["newrev"].ToString().Trim(); var quarantinePath = req.Query["quarantinepath"].ToString(); var objectsDir = quarantinePath.Substring(quarantinePath.IndexOf("./objects")); var gitExecutor = executor.CreateCommandExecutor("git", pStart => { pStart.Environment["GIT_ALTERNATE_OBJECT_DIRECTORIES"] = "./objects"; pStart.Environment["GIT_OBJECT_DIRECTORY"] = objectsDir; }); try { await gitValidationFlow.Validate(oldCommit, newCommit, gitExecutor); } catch (Exception ex) { res.StatusCode = 400; await res.WriteAsync(ex.Message); } };
public static void OctoVersionDiscoverLocalGitBranch(out string branch, Action <LoggerConfiguration> additionLogs) { var(_, configuration) = ConfigurationBootstrapper.BootstrapWithoutValidation <AppSettings>(); LogBootstrapper.Bootstrap(configuration, additionLogs); var stdout = new List <string>(); var stderr = new List <string>(); var exitCode = ShellExecutor.ExecuteCommand( "git", "rev-parse --abbrev-ref HEAD", Environment.CurrentDirectory, log => { }, log => { stdout.Add(log); }, log => { stdout.Add(log); } ); if (exitCode != 0) { throw new Exception("Calling git binary to determine local branch failed.") .WithData(nameof(stdout), stdout) .WithData(nameof(stderr), stderr); } var bareBranch = stdout.FirstOrDefault() ?? throw new Exception("Failed to determine local branch.") .WithData(nameof(stdout), stdout) .WithData(nameof(stderr), stderr); branch = $"refs/heads/{bareBranch}"; var environmentVariableName = $"{ConfigurationBootstrapper.EnvironmentVariablePrefix}{nameof(AppSettings.CurrentBranch)}"; Environment.SetEnvironmentVariable(environmentVariableName, branch); Log.Warning("The current Git branch has been automatically determined to be {branch}.", branch); Log.Warning($"It is STRONGLY RECOMMENDED to NOT rely on automatic branch detection on your build agents. It will fail in unexpected ways for tags, pull requests, commit hashes etc. Please set the {environmentVariableName} variable deterministically instead"); }
private void fop2WebInterfaceToolStripMenuItem_Click(object sender, EventArgs e) { ShellExecutor.ExecuteCommand(new ShellCommand(this.GetFOP2WebInterfaceURL())); }
public static IObservable <(byte[] data, OutputType outputType)> ExecObservable(this ShellExecutor shellExecutor, string command, string args, Action <ProcessStartInfo> paramsInit = null) { return(Observable.Defer(() => { var(process, exited) = shellExecutor(command, args, paramsInit); var sbErr = new StringBuilder(); return process.StandardOutput.BaseStream.ToObservable().Select(data => (data, OutputType.StdOut)) .Merge(process.StandardError.BaseStream.ToObservable() .Do(data => sbErr.Append(Encoding.Default.GetString(data))) .Select(data => (data, OutputType.StdErr)) ) .With(exited.ToObservable().Do(_ => { if (process.ExitCode != 0) { throw new Exception("proccess failed", new Exception(sbErr.ToString())) { Data = { ["Command"] = command, ["Args"] = args, ["ExitCode"] = process.ExitCode, ["StdErr"] = sbErr.ToString() } } } ; })); })); }
public static void BuildProtocol() { ShellExecutor.ExecuteShell(commandProtocol, ""); AssetDatabase.Refresh(); }
public static void BuildStorage() { ShellExecutor.ExecuteShell(commandStorage, ""); AssetDatabase.Refresh(); }
public static void BuildConfigs() { ShellExecutor.ExecuteShell(commandConfigs, ""); AssetDatabase.Refresh(); }
public static Func <string, Task <string> > CreateCommandExecutor(this ShellExecutor shellExecutor, string command, Action <ProcessStartInfo> paramsInit = null) => args => shellExecutor.ExecTask(command, args, paramsInit);
public static async Task <string> ExecTask(this ShellExecutor shellExecutor, string command, string args, Action <ProcessStartInfo> paramsInit = null) { return(await shellExecutor.ExecObservable(command, args, paramsInit) .Where(x => x.outputType == OutputType.StdOut) .Aggregate("", (acc, x) => acc + Encoding.Default.GetString(x.data))); }