From cd2af9ba307bb0dfefb99dfb5424ad28494f536c Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 28 Aug 2018 12:54:48 -0700 Subject: [PATCH 1/3] Update argument parsing and change a namespace name --- .../FunctionMessagingClient.cs | 2 +- .../Azure.Functions.PowerShell.Worker.csproj | 3 +- .../StartupArguments.cs | 44 ----------------- .../Utility/RpcLogger.cs | 2 +- .../Worker.cs | 47 +++++++++++++++---- 5 files changed, 41 insertions(+), 57 deletions(-) delete mode 100644 src/Azure.Functions.PowerShell.Worker/StartupArguments.cs diff --git a/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs b/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs index e1257581..7f91dc41 100644 --- a/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs +++ b/src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs @@ -10,7 +10,7 @@ using Grpc.Core; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; -namespace Azure.Functions.PowerShell.Worker.Messaging +namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging { public class FunctionMessagingClient : IDisposable { diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj index bfe63885..0bdc40c5 100644 --- a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj +++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj @@ -11,8 +11,9 @@ - + + diff --git a/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs b/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs deleted file mode 100644 index ee865564..00000000 --- a/src/Azure.Functions.PowerShell.Worker/StartupArguments.cs +++ /dev/null @@ -1,44 +0,0 @@ -// -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// - -using System; - -namespace Microsoft.Azure.Functions.PowerShellWorker -{ - public class StartupArguments - { - public int GrpcMaxMessageLength { get; set; } - public string Host {get; set;} - public int Port {get; set;} - public string RequestId {get; set;} - public string WorkerId {get; set;} - - public static StartupArguments Parse(string[] args) - { - if (args.Length != 10) - { - Console.WriteLine("usage --host --port --workerId --requestId --grpcMaxMessageLength "); - throw new InvalidOperationException("Incorrect startup arguments were given."); - } - - StartupArguments arguments = new StartupArguments(); - for (int i = 1; i < 10; i+=2) - { - string currentArg = args[i]; - switch (i) - { - case 1: arguments.Host = currentArg; break; - case 3: arguments.Port = int.Parse(currentArg); break; - case 5: arguments.WorkerId = currentArg; break; - case 7: arguments.RequestId = currentArg; break; - case 9: arguments.GrpcMaxMessageLength = int.Parse(currentArg); break; - default: throw new InvalidOperationException(); - } - } - - return arguments; - } - } -} \ No newline at end of file diff --git a/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs b/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs index 5243a8cc..aeaef298 100644 --- a/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs +++ b/src/Azure.Functions.PowerShell.Worker/Utility/RpcLogger.cs @@ -5,7 +5,7 @@ using System; -using Azure.Functions.PowerShell.Worker.Messaging; +using Microsoft.Azure.Functions.PowerShellWorker.Messaging; using Microsoft.Azure.WebJobs.Script.Grpc.Messages; using Microsoft.Extensions.Logging; diff --git a/src/Azure.Functions.PowerShell.Worker/Worker.cs b/src/Azure.Functions.PowerShell.Worker/Worker.cs index 395cf27d..573295fd 100644 --- a/src/Azure.Functions.PowerShell.Worker/Worker.cs +++ b/src/Azure.Functions.PowerShell.Worker/Worker.cs @@ -5,9 +5,11 @@ using System; using System.Threading.Tasks; +using System.Management.Automation; using System.Management.Automation.Runspaces; -using Azure.Functions.PowerShell.Worker.Messaging; +using CommandLine; +using Microsoft.Azure.Functions.PowerShellWorker.Messaging; using Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host; using Microsoft.Azure.Functions.PowerShellWorker.Requests; using Microsoft.Azure.Functions.PowerShellWorker.Utility; @@ -29,12 +31,17 @@ static void InitPowerShell() s_runspace = RunspaceFactory.CreateRunspace(host); s_runspace.Open(); - s_ps = System.Management.Automation.PowerShell.Create(InitialSessionState.CreateDefault()); + s_ps = System.Management.Automation.PowerShell.Create(); s_ps.Runspace = s_runspace; - s_ps.AddScript("$PSHOME"); - //s_ps.AddCommand("Set-ExecutionPolicy").AddParameter("ExecutionPolicy", ExecutionPolicy.Unrestricted).AddParameter("Scope", ExecutionPolicyScope.Process); - s_ps.Invoke(); + if (Platform.IsWindows) + { + s_ps.AddCommand("Set-ExecutionPolicy") + .AddParameter("ExecutionPolicy", "Unrestricted") + .AddParameter("Scope", "Process") + .Invoke(); + s_ps.Commands.Clear(); + } // Add HttpResponseContext namespace so users can reference // HttpResponseContext without needing to specify the full namespace @@ -44,21 +51,23 @@ static void InitPowerShell() public async static Task Main(string[] args) { - StartupArguments startupArguments = StartupArguments.Parse(args); + WorkerArguments arguments = null; + Parser.Default.ParseArguments(args) + .WithParsed(ops => arguments = ops) + .WithNotParsed(err => Environment.Exit(1)); // Initialize Rpc client, logger, and PowerShell - s_client = new FunctionMessagingClient(startupArguments.Host, startupArguments.Port); + s_client = new FunctionMessagingClient(arguments.Host, arguments.Port); s_logger = new RpcLogger(s_client); InitPowerShell(); // Send StartStream message var streamingMessage = new StreamingMessage() { - RequestId = startupArguments.RequestId, - StartStream = new StartStream() { WorkerId = startupArguments.WorkerId } + RequestId = arguments.RequestId, + StartStream = new StartStream() { WorkerId = arguments.WorkerId } }; await s_client.WriteAsync(streamingMessage); - await ProcessEvent(); } @@ -105,4 +114,22 @@ static async Task ProcessEvent() } } } + + internal class WorkerArguments + { + [Option("host", Required = true, HelpText = "IP Address used to connect to the Host via gRPC.")] + public string Host { get; set; } + + [Option("port", Required = true, HelpText = "Port used to connect to the Host via gRPC.")] + public int Port { get; set; } + + [Option("workerId", Required = true, HelpText = "Worker ID assigned to this language worker.")] + public string WorkerId { get; set; } + + [Option("requestId", Required = true, HelpText = "Request ID used for gRPC communication with the Host.")] + public string RequestId { get; set; } + + [Option("grpcMaxMessageLength", Required = true, HelpText = "gRPC Maximum message size.")] + public int MaxMessageLength { get; set; } + } } \ No newline at end of file From b99bb44d906e32b001a7745380bf514f27ad8e6e Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 28 Aug 2018 12:58:09 -0700 Subject: [PATCH 2/3] Add a new line at the end --- src/Azure.Functions.PowerShell.Worker/Worker.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Azure.Functions.PowerShell.Worker/Worker.cs b/src/Azure.Functions.PowerShell.Worker/Worker.cs index 573295fd..ea8e4429 100644 --- a/src/Azure.Functions.PowerShell.Worker/Worker.cs +++ b/src/Azure.Functions.PowerShell.Worker/Worker.cs @@ -132,4 +132,4 @@ internal class WorkerArguments [Option("grpcMaxMessageLength", Required = true, HelpText = "gRPC Maximum message size.")] public int MaxMessageLength { get; set; } } -} \ No newline at end of file +} From e7ba1d43e9d90b552bac4030eeb63513138b8178 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Tue, 28 Aug 2018 14:16:54 -0700 Subject: [PATCH 3/3] Update the package version --- .../Azure.Functions.PowerShell.Worker.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj index 0bdc40c5..d27e5a4c 100644 --- a/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj +++ b/src/Azure.Functions.PowerShell.Worker/Azure.Functions.PowerShell.Worker.csproj @@ -13,7 +13,7 @@ - +