Skip to content

Commit

Permalink
Run as a service on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
paveliak committed Mar 22, 2024
1 parent 89b5018 commit b1de038
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build/repo.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith('linux'))">
<DefineConstants>_SYSTEMD</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith('osx'))">
<DefineConstants>_LAUNCHD</DefineConstants>
</PropertyGroup>
<ItemGroup>
<DotNetCoreRuntime Include="$(MicrosoftNETCoreAppPackageVersion)" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/azbridge/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/azbridge/RelayBridgeService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
22 changes: 21 additions & 1 deletion src/azbridge/ServiceLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// 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;
using Microsoft.Extensions.DependencyInjection;
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;
Expand Down Expand Up @@ -95,6 +96,8 @@ internal static async Task RunAsync(CommandLineSettings settings)
})
#elif _SYSTEMD
.UseSystemd()
#elif _LAUNCHD
.UseLaunchd()
#endif

.ConfigureServices(services =>
Expand Down Expand Up @@ -183,6 +186,23 @@ internal static bool IsInstalled()
return true;
#endif
}

private static IHostBuilder UseLaunchd(this IHostBuilder hostBuilder)
{
hostBuilder.ConfigureServices(services =>
{
services.Configure<ConsoleLoggerOptions>(options =>
{
options.FormatterName = ConsoleFormatterNames.Simple;
});
services.Configure<SimpleConsoleFormatterOptions>(options =>
{
options.SingleLine = true;
});
});

return hostBuilder;
}
}
}
#endif
3 changes: 3 additions & 0 deletions src/azbridge/azbridge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
<Content Include="azbridge.service" CopyToPublishDirectory="PreserveNewest" Condition="$(DefineConstants.Contains('_SYSTEMD'))">
<LinuxPath>/etc/systemd/system/azbridge.service</LinuxPath>
</Content>
<Content Include="azbridge.plist" CopyToPublishDirectory="PreserveNewest" Condition="$(DefineConstants.Contains('_LAUNCHD'))">
<LinuxPath>/Library/LaunchDaemons/com.azure.relay.bridge.plist</LinuxPath>
</Content>
<Content Include="azbridge.sh" CopyToPublishDirectory="PreserveNewest" Condition="! $(RuntimeIdentifier.StartsWith('win'))" RemoveOnUninstall="true">
<LinuxPath>/etc/profile.d/azbridge.sh</LinuxPath>
<LinuxFileMode Condition="$(RuntimeIdentifier.StartsWith('linux'))">0555</LinuxFileMode>
Expand Down
24 changes: 24 additions & 0 deletions src/azbridge/azbridge.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.azure.relay.bridge</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/share/azbridge/azbridge</string>
<string>--svc</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<dict>
<key>Crashed</key>
<true/>
</dict>
<key>StandardOutPath</key>
<string>/var/log/azbridge.log</string>
<key>StandardErrorPath</key>
<string>/var/log/azbridge.log</string>
</dict>
</plist>

0 comments on commit b1de038

Please sign in to comment.