|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
| 15 | +using System; |
15 | 16 | using System.Collections;
|
16 | 17 | using System.Collections.Generic;
|
| 18 | +using System.Collections.ObjectModel; |
| 19 | +using System.Globalization; |
| 20 | +using System.Linq; |
17 | 21 | using System.Management.Automation;
|
| 22 | +using System.Management.Automation.Runspaces; |
18 | 23 | using System.Security.Permissions;
|
| 24 | +using System.Threading; |
19 | 25 | using Microsoft.Azure.Commands.Automation.Common;
|
| 26 | +using Microsoft.Azure.Commands.Automation.Model; |
20 | 27 | using Job = Microsoft.Azure.Commands.Automation.Model.Job;
|
| 28 | +using JobStream = Microsoft.Azure.Commands.Automation.Model.JobStream; |
| 29 | +using Microsoft.Azure.Commands.Automation.Properties; |
21 | 30 |
|
22 | 31 |
|
23 | 32 | namespace Microsoft.Azure.Commands.Automation.Cmdlet
|
24 | 33 | {
|
25 | 34 | /// <summary>
|
26 | 35 | /// Starts an Azure automation runbook.
|
27 | 36 | /// </summary>
|
28 |
| - [Cmdlet(VerbsLifecycle.Start, "AzureRmAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByRunbookName)] |
29 |
| - [OutputType(typeof(Job))] |
| 37 | + [Cmdlet(VerbsLifecycle.Start, "AzureRmAutomationRunbook", DefaultParameterSetName = AutomationCmdletParameterSets.ByAsynchronousReturnJob)] |
| 38 | + [OutputType(typeof(Job), ParameterSetName = new []{ AutomationCmdletParameterSets.ByAsynchronousReturnJob })] |
| 39 | + [OutputType(typeof(PSObject), ParameterSetName = new []{ AutomationCmdletParameterSets.BySynchronousReturnJobOutput })] |
30 | 40 | public class StartAzureAutomationRunbook : AzureAutomationBaseCmdlet
|
31 | 41 | {
|
| 42 | + /// <summary> |
| 43 | + /// True to wait for job output |
| 44 | + /// </summary> |
| 45 | + private bool wait; |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Timeout value |
| 49 | + /// </summary> |
| 50 | + private int timeout = Constants.MaxWaitSeconds; |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Polling interval |
| 54 | + /// </summary> |
| 55 | + private const int pollingIntervalInSeconds = 5; |
| 56 | + |
32 | 57 | /// <summary>
|
33 | 58 | /// Gets or sets the runbook name
|
34 | 59 | /// </summary>
|
35 |
| - [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByRunbookName, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] |
| 60 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAsynchronousReturnJob, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] |
| 61 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.BySynchronousReturnJobOutput, Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook name.")] |
36 | 62 | [ValidateNotNullOrEmpty]
|
37 | 63 | [Alias("RunbookName")]
|
38 | 64 | public string Name { get; set; }
|
39 | 65 |
|
40 | 66 | /// <summary>
|
41 | 67 | /// Gets or sets the runbook parameters.
|
42 | 68 | /// </summary>
|
43 |
| - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The runbook parameters.")] |
| 69 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAsynchronousReturnJob, Mandatory = false, HelpMessage = "The runbook parameters.")] |
| 70 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.BySynchronousReturnJobOutput, Mandatory = false, HelpMessage = "The runbook parameters.")] |
44 | 71 | public IDictionary Parameters { get; set; }
|
45 | 72 |
|
46 | 73 | /// <summary>
|
47 | 74 | /// Gets or sets the optional hybrid agent friendly name upon which the runbook should be executed.
|
48 | 75 | /// </summary>
|
49 |
| - [Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Optional name of the hybrid agent which should execute the runbook")] |
| 76 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.ByAsynchronousReturnJob, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Optional name of the hybrid agent which should execute the runbook")] |
| 77 | + [Parameter(ParameterSetName = AutomationCmdletParameterSets.BySynchronousReturnJobOutput, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Optional name of the hybrid agent which should execute the runbook")] |
50 | 78 | [Alias("HybridWorker")]
|
51 | 79 | public string RunOn { get; set; }
|
52 | 80 |
|
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Gets or sets the switch parameter to wait for job output |
| 84 | + /// </summary> |
| 85 | + [Parameter(Mandatory = false, ParameterSetName = AutomationCmdletParameterSets.BySynchronousReturnJobOutput, HelpMessage = "Wait for job to complete, suspend, or fail.")] |
| 86 | + public SwitchParameter Wait |
| 87 | + { |
| 88 | + get { return this.wait; } |
| 89 | + set { this.wait = value; } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Gets or sets the switch parameter to wait for job output |
| 94 | + /// </summary> |
| 95 | + [Parameter(Mandatory = false, ParameterSetName = AutomationCmdletParameterSets.BySynchronousReturnJobOutput, HelpMessage = "Maximum time in seconds to wait for job completion. Default max wait time is 10800 seconds.")] |
| 96 | + public int MaxWaitSeconds |
| 97 | + { |
| 98 | + get { return this.timeout; } |
| 99 | + set { this.timeout = value; } |
| 100 | + } |
| 101 | + |
53 | 102 | /// <summary>
|
54 | 103 | /// Execute this cmdlet.
|
55 | 104 | /// </summary>
|
56 | 105 | [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
|
57 | 106 | protected override void AutomationProcessRecord()
|
58 | 107 | {
|
59 |
| - Job job = null; |
| 108 | + var job = this.AutomationClient.StartRunbook(this.ResourceGroupName, this.AutomationAccountName, this.Name, this.Parameters, this.RunOn); |
| 109 | + |
| 110 | + // if no wait, return job object |
| 111 | + if (!this.Wait.IsPresent) |
| 112 | + { |
| 113 | + this.WriteObject(job); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + // wait for job completion |
| 118 | + if (!PollJobCompletion(job.JobId)) |
| 119 | + { |
| 120 | + throw new ResourceCommonException(typeof(Job), string.Format(CultureInfo.CurrentCulture, Resources.JobCompletionMaxWaitReached)); |
| 121 | + } |
| 122 | + |
| 123 | + // retrieve job streams |
| 124 | + var nextLink = string.Empty; |
| 125 | + do |
| 126 | + { |
| 127 | + var jobOutputList = this.AutomationClient.GetJobStream(this.ResourceGroupName, this.AutomationAccountName, job.JobId, null, StreamType.Output.ToString(), ref nextLink); |
| 128 | + foreach (var jobOutputRecord in jobOutputList) |
| 129 | + { |
| 130 | + var response = this.AutomationClient.GetJobStreamRecordAsPsObject(this.ResourceGroupName, this.AutomationAccountName, job.JobId, jobOutputRecord.StreamRecordId); |
| 131 | + this.GenerateCmdletOutput(response); |
| 132 | + } |
| 133 | + } while (!string.IsNullOrEmpty(nextLink)); |
| 134 | + } |
| 135 | + |
| 136 | + private bool PollJobCompletion(Guid jobId) |
| 137 | + { |
| 138 | + var timeoutIncrement = 0; |
| 139 | + do |
| 140 | + { |
| 141 | + Thread.Sleep(TimeSpan.FromSeconds(pollingIntervalInSeconds)); |
| 142 | + timeoutIncrement += pollingIntervalInSeconds; |
| 143 | + |
| 144 | + var job = this.AutomationClient.GetJob(this.ResourceGroupName, this.AutomationAccountName, jobId); |
| 145 | + if (!IsJobTerminalState(job)) |
| 146 | + { |
| 147 | + if (IsVerbose()) WriteVerbose(string.Format(Resources.JobProgressState, job.JobId, job.Status, DateTime.Now)); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + if (IsVerbose()) WriteVerbose(string.Format(Resources.JobTerminalState, job.JobId, job.Status, DateTime.Now)); |
| 152 | + return true; |
| 153 | + } |
| 154 | + } while (this.MaxWaitSeconds < 0 || timeoutIncrement <= this.MaxWaitSeconds); |
60 | 155 |
|
61 |
| - job = this.AutomationClient.StartRunbook(this.ResourceGroupName, this.AutomationAccountName, this.Name, this.Parameters, this.RunOn); |
| 156 | + return false; |
| 157 | + } |
62 | 158 |
|
63 |
| - this.WriteObject(job); |
| 159 | + private static bool IsJobTerminalState(Job job) |
| 160 | + { |
| 161 | + return job.Status.Equals(JobState.Completed.ToString(), StringComparison.OrdinalIgnoreCase) || |
| 162 | + job.Status.Equals(JobState.Failed.ToString(), StringComparison.OrdinalIgnoreCase) || |
| 163 | + job.Status.Equals(JobState.Stopped.ToString(), StringComparison.OrdinalIgnoreCase) || |
| 164 | + job.Status.Equals(JobState.Suspended.ToString(), StringComparison.OrdinalIgnoreCase); |
64 | 165 | }
|
65 | 166 | }
|
66 | 167 | }
|
0 commit comments