private static async Task RunCommandsAsync(
     ISshClient sshClient,
     SshRetryPolicy retryPolicy,
     string[] commands)
 {
     foreach (var command in commands)
     {
         await sshClient.ExecuteCommandAsync(
             retryPolicy,
             command);
     }
 }
Esempio n. 2
0
        private static AsyncPolicy GetPollyPolicyFromSshRetryPolicy(SshRetryPolicy retryPolicy)
        {
            AsyncPolicy policy = Policy.NoOpAsync();

            if (retryPolicy == SshRetryPolicy.AllowRetries)
            {
                policy = Policy
                         .Handle <SshCommandExecutionException>()
                         .WaitAndRetryAsync(10, i => TimeSpan.FromSeconds(i * 5));
            }

            return(policy);
        }
Esempio n. 3
0
        public async Task TransferFileAsync(
            SshRetryPolicy retryPolicy,
            string filePath,
            byte[] contents)
        {
            var policy = GetPollyPolicyFromSshRetryPolicy(retryPolicy);

            this.logger.Debug("Transfering file {FilePath}.", filePath);

            await policy.ExecuteAsync(async() =>
                                      await this.client.TransferFileAsync(
                                          filePath,
                                          contents));

            this.logger.Debug("File {FilePath} transferred.", filePath);
        }
Esempio n. 4
0
        public async Task <string> ExecuteCommandAsync(
            SshRetryPolicy retryPolicy,
            string commandText,
            Dictionary <string, string?>?arguments = null)
        {
            var policy = GetPollyPolicyFromSshRetryPolicy(retryPolicy);

            this.logger.Debug("Executing {CommandText}.", commandText);

            var commandResult = await policy.ExecuteAsync(async() =>
            {
                var result = await this.client.ExecuteCommandAsync(
                    GetSensitiveCommandText(commandText, arguments));
                if (result.ExitCode != 0)
                {
                    var sshCommandExecutionException = new SshCommandExecutionException(
                        commandText,
                        result);

                    this.logger.Debug(sshCommandExecutionException, "An error occured while executing the command {CommandText} with result {ExitCode}: {CommandResult}", commandText, result.ExitCode, StripSensitiveText(result.Text, arguments));

                    throw sshCommandExecutionException;
                }

                return(result);
            });

            if (arguments == null)
            {
                this.logger.Debug("Command {CommandText} executed with response {ResponseText}.", commandText, commandResult.Text);
            }
            else
            {
                this.logger.Debug("Command {CommandText} executed.", commandText);
            }

            return(commandResult.Text);
        }