Esempio n. 1
0
        public override async Task ExecuteAsync(IOperationExecutionContext context)
        {
            int exitCode = await SHUtil.ExecuteScriptAsync(context, new StringReader(this.ScriptText), null, this, this.Verbose, this.OutputLevel, this.ErrorLevel) ?? 0;

            bool exitCodeLogged = false;

            if (!string.IsNullOrWhiteSpace(this.SuccessExitCode))
            {
                var comparator = ExitCodeComparator.TryParse(this.SuccessExitCode);
                if (comparator != null)
                {
                    bool result = comparator.Evaluate(exitCode);
                    if (result)
                    {
                        this.LogInformation($"Script exited with code: {exitCode} (success)");
                    }
                    else
                    {
                        this.LogError($"Script exited with code: {exitCode} (failure)");
                    }

                    exitCodeLogged = true;
                }
            }

            if (!exitCodeLogged)
            {
                this.LogDebug("Script exited with code: " + exitCode);
            }
        }
Esempio n. 2
0
        public static void LogExit(ILogSink logger, int?exitCode, string successExitCode = null)
        {
            if (!exitCode.HasValue)
            {
                return;
            }

            var comparator = ExitCodeComparator.TryParse(successExitCode);

            if (comparator != null && !comparator.Evaluate(exitCode.Value))
            {
                logger.LogError("Script exit code: " + exitCode);
            }
            else
            {
                logger.LogDebug("Script exit code: " + exitCode);
            }
        }
        public async override Task ExecuteAsync(IOperationExecutionContext context)
        {
            if (context.Agent == null)
            {
                this.LogError("Server context is required to execute a process.");
                return;
            }

            var startInfo = GetFileNameAndArguments(this.FileName, this.Arguments, this.Target);

            if (startInfo == null)
            {
                this.LogError("Invalid configuration; Target or FileName must be specified.");
                return;
            }

            var fileOps = context.Agent.GetService <IFileOperationsExecuter>();

            startInfo.FileName         = context.ResolvePath(startInfo.FileName);
            startInfo.WorkingDirectory = context.ResolvePath(this.WorkingDirectory);

            this.LogDebug("Process: " + startInfo.FileName);
            this.LogDebug("Arguments: " + startInfo.Arguments);
            this.LogDebug("Working directory: " + startInfo.WorkingDirectory);

            this.LogDebug($"Ensuring that {startInfo.WorkingDirectory} exists...");
            await fileOps.CreateDirectoryAsync(startInfo.WorkingDirectory).ConfigureAwait(false);

            this.LogInformation($"Executing {startInfo.FileName}...");

            var remoteProcess = context.Agent.GetService <IRemoteProcessExecuter>();
            int exitCode;

            using (var process = remoteProcess.CreateProcess(startInfo))
            {
                process.OutputDataReceived += this.Process_OutputDataReceived;
                process.ErrorDataReceived  += this.Process_ErrorDataReceived;

                process.Start();
                try
                {
                    await process.WaitAsync(context.CancellationToken).ConfigureAwait(false);
                }
                catch
                {
                    try
                    {
                        process.Terminate();
                    }
                    catch
                    {
                    }

                    throw;
                }

                exitCode = process.ExitCode ?? -1;
            }

            bool exitCodeLogged = false;

            if (!string.IsNullOrEmpty(this.SuccessExitCode))
            {
                var comparator = ExitCodeComparator.TryParse(this.SuccessExitCode);
                if (comparator != null)
                {
                    bool result = comparator.Evauluate(exitCode);
                    if (result)
                    {
                        this.LogInformation($"Process exited with code: {exitCode} (success)");
                    }
                    else
                    {
                        this.LogError($"Process exited with code: {exitCode} (failure)");
                    }

                    exitCodeLogged = true;
                }
            }

            if (!exitCodeLogged)
            {
                this.LogDebug("Process exited with code: " + exitCode);
            }
        }