From 387a967f9c968d7c33a46f5f3828be763707e925 Mon Sep 17 00:00:00 2001 From: Pavel Iakovenko Date: Fri, 12 Apr 2024 18:30:38 -0700 Subject: [PATCH] Run as a service on macOS (#84) * Run as a service on macOS * macOS Launchd documentation updates * Pin mysql to 8.0.35 to get around libssl and libicu dependency errors * MySql pinning no longer needed * AzIdentity 1.11.0 --- OVERVIEW.md | 12 ++++++++++ build/repo.props | 3 +++ examples/rdp/README.md | 12 ++++++++++ examples/sqlserver/README.md | 17 ++++++++++++- .../Microsoft.Azure.Relay.Bridge.csproj | 2 +- src/azbridge/Program.cs | 2 +- src/azbridge/RelayBridgeService.cs | 2 +- src/azbridge/ServiceLauncher.cs | 22 ++++++++++++++++- src/azbridge/azbridge.csproj | 3 +++ src/azbridge/azbridge.plist | 24 +++++++++++++++++++ test/mysql/test.sh | 2 +- 11 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 src/azbridge/azbridge.plist diff --git a/OVERVIEW.md b/OVERVIEW.md index 126c1b0..1cc237f 100644 --- a/OVERVIEW.md +++ b/OVERVIEW.md @@ -146,6 +146,18 @@ which is described in [CONFIG.md](CONFIG.md#configuration-file). The file requires administrative permissions to change. +### MacOS Launchd daemon + +On MacOS, the service is registered with Launchd as "com.azure.relay.bridge" and can +be managed with `launchctl`. + +To run either the client or the server side in that daemon, merge the +configuration file snippets above into the +`/etc/azbridge/azbridge_config.svc.yml` file, which is described in +[CONFIG.md](CONFIG.md#configuration-file). + +The file requires administrative permissions to change. + ## Downloads Unsigned (!) binaries are available for direct download from the [Github diff --git a/build/repo.props b/build/repo.props index b5cf7d7..f7137d5 100644 --- a/build/repo.props +++ b/build/repo.props @@ -14,6 +14,9 @@ _SYSTEMD + + _LAUNCHD + diff --git a/examples/rdp/README.md b/examples/rdp/README.md index 3c96d45..5589f26 100644 --- a/examples/rdp/README.md +++ b/examples/rdp/README.md @@ -174,4 +174,16 @@ configuration file snippets above into the `/etc/azbridge/azbridge_config.svc.yml` file, which is described in [CONFIG.md](CONFIG.md#configuration-file). +The file requires administrative permissions to change. + +### MacOS Launchd daemon + +On MacOS, the service is registered with Launchd as "com.azure.relay.bridge" and can +be managed with `launchctl`. + +To run either the client or the server side in that daemon, merge the +configuration file snippets above into the +`/etc/azbridge/azbridge_config.svc.yml` file, which is described in +[CONFIG.md](CONFIG.md#configuration-file). + The file requires administrative permissions to change. \ No newline at end of file diff --git a/examples/sqlserver/README.md b/examples/sqlserver/README.md index 39be3b0..5c8efb2 100644 --- a/examples/sqlserver/README.md +++ b/examples/sqlserver/README.md @@ -192,4 +192,19 @@ configuration file snippets above into the The file requires administrative permissions to change. As with Windows above, you can also override connection strings at the forwarder -level on Linux. \ No newline at end of file +level on Linux. + +### MacOS Launchd daemon + +On MacOS, the service is registered with Launchd as "com.azure.relay.bridge" and can +be managed with `launchctl`. + +To run either the client or the server side in that daemon, merge the +configuration file snippets above into the +`/etc/azbridge/azbridge_config.svc.yml` file, which is described in +[CONFIG.md](CONFIG.md#configuration-file). + +The file requires administrative permissions to change. + +As with Windows and Linux above, you can also override connection strings at the forwarder +level on macOS. diff --git a/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.csproj b/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.csproj index 4514223..a7ba3d5 100644 --- a/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.csproj +++ b/src/Microsoft.Azure.Relay.Bridge/Microsoft.Azure.Relay.Bridge.csproj @@ -17,7 +17,7 @@ - + diff --git a/src/azbridge/Program.cs b/src/azbridge/Program.cs index c0708f4..c9b9127 100644 --- a/src/azbridge/Program.cs +++ b/src/azbridge/Program.cs @@ -54,7 +54,7 @@ static int Run(CommandLineSettings settings, string[] args) return 0; } #endif -#if _WINDOWS || _SYSTEMD +#if _WINDOWS || _SYSTEMD || _LAUNCHD if (settings.ServiceRun.HasValue && settings.ServiceRun.Value) { ServiceLauncher.RunAsync(settings).GetAwaiter().GetResult(); diff --git a/src/azbridge/RelayBridgeService.cs b/src/azbridge/RelayBridgeService.cs index 6a46b2c..75b309b 100644 --- a/src/azbridge/RelayBridgeService.cs +++ b/src/azbridge/RelayBridgeService.cs @@ -1,4 +1,4 @@ -#if _WINDOWS || _SYSTEMD +#if _WINDOWS || _SYSTEMD || _LAUNCHD using Microsoft.Azure.Relay.Bridge.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; diff --git a/src/azbridge/ServiceLauncher.cs b/src/azbridge/ServiceLauncher.cs index a585e29..bc49c97 100644 --- a/src/azbridge/ServiceLauncher.cs +++ b/src/azbridge/ServiceLauncher.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -#if _WINDOWS || _SYSTEMD +#if _WINDOWS || _SYSTEMD || _LAUNCHD namespace azbridge { using Microsoft.Azure.Relay.Bridge.Configuration; @@ -9,6 +9,7 @@ namespace azbridge using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Configuration; + using Microsoft.Extensions.Logging.Console; using Microsoft.Extensions.Logging.EventLog; using System; using System.Collections; @@ -95,6 +96,8 @@ internal static async Task RunAsync(CommandLineSettings settings) }) #elif _SYSTEMD .UseSystemd() +#elif _LAUNCHD + .UseLaunchd() #endif .ConfigureServices(services => @@ -183,6 +186,23 @@ internal static bool IsInstalled() return true; #endif } + + private static IHostBuilder UseLaunchd(this IHostBuilder hostBuilder) + { + hostBuilder.ConfigureServices(services => + { + services.Configure(options => + { + options.FormatterName = ConsoleFormatterNames.Simple; + }); + services.Configure(options => + { + options.SingleLine = true; + }); + }); + + return hostBuilder; + } } } #endif \ No newline at end of file diff --git a/src/azbridge/azbridge.csproj b/src/azbridge/azbridge.csproj index aa9dddf..24c17b8 100644 --- a/src/azbridge/azbridge.csproj +++ b/src/azbridge/azbridge.csproj @@ -66,6 +66,9 @@ /etc/systemd/system/azbridge.service + + /Library/LaunchDaemons/com.azure.relay.bridge.plist + /etc/profile.d/azbridge.sh 0555 diff --git a/src/azbridge/azbridge.plist b/src/azbridge/azbridge.plist new file mode 100644 index 0000000..40758e6 --- /dev/null +++ b/src/azbridge/azbridge.plist @@ -0,0 +1,24 @@ + + + + + Label + com.azure.relay.bridge + ProgramArguments + + /usr/local/share/azbridge/azbridge + --svc + + RunAtLoad + + KeepAlive + + Crashed + + + StandardOutPath + /var/log/azbridge.log + StandardErrorPath + /var/log/azbridge.log + + diff --git a/test/mysql/test.sh b/test/mysql/test.sh index 7568eaf..4f7d3f9 100755 --- a/test/mysql/test.sh +++ b/test/mysql/test.sh @@ -48,7 +48,7 @@ else echo AZBRIDGE_TEST_CXNSTRING environment variable must be set to valid relay connection string exit 2 fi - + # start the web server server_name=$(docker run -v $(pwd):/tests -d -v $(pwd)/my.cnf:/etc/mysqld/conf.d/my.cnf --rm -d -e AZBRIDGE_TEST_CXNSTRING="$_CXNSTRING" -e MYSQL_ROOT_PASSWORD=PaSsWoRd112233 -e MYSQL_PASSWORD=PaSsWoRd112233 -e MYSQL_USER=mysql azbridge-mysql-server:latest) # wait for server to start