/
SSHRunShellCommand.cs
155 lines (125 loc) · 6.31 KB
/
SSHRunShellCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Activities;
using System.Threading;
using System.Threading.Tasks;
using Renci.SshNet;
using UiPathTeam.SSHConnector.Activities.Properties;
using UiPath.Shared.Activities;
using UiPath.Shared.Activities.Localization;
using System.Text.RegularExpressions;
namespace UiPathTeam.SSHConnector.Activities
{
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_Description))]
public class SSHRunShellCommand : ContinuableAsyncCodeActivity
{
#region Properties
/// <summary>
/// If set, continue executing the remaining activities even if the current activity has failed.
/// </summary>
[LocalizedCategory(nameof(Resources.Common_Category))]
[LocalizedDisplayName(nameof(Resources.ContinueOnError_DisplayName))]
[LocalizedDescription(nameof(Resources.ContinueOnError_Description))]
public override InArgument<bool> ContinueOnError { get; set; }
[LocalizedCategory(nameof(Resources.Common_Category))]
[LocalizedDisplayName(nameof(Resources.Timeout_DisplayName))]
[LocalizedDescription(nameof(Resources.Timeout_Description))]
public InArgument<int> TimeoutMS { get; set; } = 60000;
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_Command_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_Command_Description))]
[LocalizedCategory(nameof(Resources.Input_Category))]
public InArgument<string> Command { get; set; }
[LocalizedDisplayName(nameof(Resources.SSHTimeout_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHTimeout_Description))]
[LocalizedCategory(nameof(Resources.Input_Category))]
public InArgument<int> SSHTimeoutMS { get; set; } = 30000;
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_CheckExitCode_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_CheckExitCode_Description))]
[LocalizedCategory(nameof(Resources.Options_Category))]
public InArgument<bool> CheckExitCode { get; set; } = true;
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_ShellStream_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_ShellStream_Description))]
[LocalizedCategory(nameof(Resources.Options_Category))]
public InArgument<ShellStream> ShellStream { get; set; }
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_ShellExpectedPrompt_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_ShellExpectedPrompt_Description))]
[LocalizedCategory(nameof(Resources.Options_Category))]
public InArgument<string> ShellExpectedPrompt { get; set; }
[LocalizedDisplayName(nameof(Resources.SSHRunShellCommand_Result_DisplayName))]
[LocalizedDescription(nameof(Resources.SSHRunShellCommand_Result_Description))]
[LocalizedCategory(nameof(Resources.Output_Category))]
public OutArgument<string> Result { get; set; }
#endregion
#region Constructors
public SSHRunShellCommand()
{
}
#endregion
#region Protected Methods
protected override void CacheMetadata(CodeActivityMetadata metadata)
{
if (Command == null) metadata.AddValidationError(string.Format(Resources.ValidationValue_Error, nameof(Command)));
base.CacheMetadata(metadata);
}
protected override async Task<Action<AsyncCodeActivityContext>> ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
{
// Inputs
var command = Command.Get(context);
var checkExitCode = CheckExitCode.Get(context);
var shellStream = ShellStream.Get(context);
var shellExpectedPrompt = ShellExpectedPrompt.Get(context);
// Set a timeout on the execution
var task = ExecuteWithTimeout(context, cancellationToken);
if (await Task.WhenAny(task, Task.Delay(TimeoutMS.Get(context), cancellationToken)) == task)
{
await task;
}
else
{
throw new TimeoutException(Resources.Timeout_Error);
}
// Outputs
return (ctx) => {
Result.Set(ctx, task.Result);
};
}
private async Task<string> ExecuteWithTimeout(AsyncCodeActivityContext context, CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
ShellStream shellStream;
Regex expectedShellPromptRegex;
IObjectContainer scopeObjectContainer = context.DataContext.GetProperties()[SSHConnectScope.ParentContainerPropertyTag]
?.GetValue(context.DataContext) as IObjectContainer;
if (ShellStream.Expression == null)
{
if (scopeObjectContainer == null)
{
throw new ArgumentException("ShellStream was not provided and cannot be retrieved from the container.");
}
shellStream = scopeObjectContainer.Get<ShellStream>();
}
else
{
shellStream = ShellStream.Get(context);
}
if (ShellExpectedPrompt.Expression == null)
{
if (scopeObjectContainer == null)
{
throw new ArgumentException("Shell Expected Prompt was not provided and cannot be retrieved from the container.");
}
expectedShellPromptRegex = scopeObjectContainer.Get<Regex>();
}
else
{
expectedShellPromptRegex = new Regex(ShellExpectedPrompt.Get(context), RegexOptions.Compiled);
}
return SSHHelpers.Execute( shellStream,
expectedShellPromptRegex,
Command.Get(context),
TimeSpan.FromMilliseconds(SSHTimeoutMS.Get(context)),
CheckExitCode.Get(context));
}
#endregion
}
}