Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#139 Fix systemd not running #141

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
ac7d963
remove keyring & double quote in execStart
forsthug Dec 19, 2023
e48df30
add sudo
forsthug Dec 21, 2023
6ac66e6
increase debug
forsthug Dec 21, 2023
b0b218c
further increase logging
forsthug Dec 21, 2023
3eaf359
logging
forsthug Dec 21, 2023
81a6f15
dont use Protector on fallback
forsthug Dec 22, 2023
24e5036
increase logging
forsthug Dec 28, 2023
b7665c5
..
forsthug Dec 28, 2023
0a36b28
..
forsthug Dec 28, 2023
876b31c
..
forsthug Dec 28, 2023
da3383f
..
forsthug Dec 28, 2023
467355b
..
forsthug Dec 28, 2023
d380cc5
..
forsthug Dec 28, 2023
87ffafb
check if run command is called
forsthug Dec 29, 2023
3ae0c76
..
forsthug Dec 29, 2023
70fb194
logging run command
forsthug Dec 29, 2023
cd8b832
..
forsthug Dec 29, 2023
4aa12ab
exclude lifetime
forsthug Dec 29, 2023
bbd9e30
..
forsthug Dec 29, 2023
298e9e1
use systd extension
forsthug Jan 2, 2024
33f68ce
remove ilifetime for test
forsthug Jan 2, 2024
176fff6
call startAsync
forsthug Jan 2, 2024
42824c7
customized systemd lifetime
forsthug Jan 2, 2024
dc28cc2
..
forsthug Jan 3, 2024
efbd5c9
..
forsthug Jan 3, 2024
02ab382
..
forsthug Jan 3, 2024
cd220fc
use startApp instead o f run
forsthug Jan 5, 2024
b6ceb9a
Merge remote-tracking branch 'origin/main' into #100-Keyring-dataprot…
forsthug Jan 9, 2024
24ca2b6
logging
forsthug Jan 9, 2024
ce713fd
further increase logging
forsthug Jan 10, 2024
96eba6b
set start timeout
forsthug Jan 10, 2024
75c85b8
..
forsthug Jan 10, 2024
b5dd0b9
add working dir
forsthug Jan 10, 2024
5a80057
further logging
forsthug Jan 10, 2024
d242ce8
set type forking
forsthug Jan 10, 2024
634aaa2
use systemd in host
forsthug Jan 10, 2024
91f64e5
..
forsthug Jan 10, 2024
8268885
set start timeout infinit
forsthug Jan 10, 2024
23328cd
--
forsthug Jan 11, 2024
4237dc3
remove additional logging and tries
forsthug Jan 11, 2024
bd3ddfd
set timoutstart and timeoutstartu
forsthug Jan 11, 2024
475fcfd
set timout
forsthug Jan 11, 2024
340f8ba
merge
forsthug Jan 11, 2024
40c3454
use SystemdHelper and Customized Timeline
forsthug Jan 11, 2024
f04e29c
::
forsthug Jan 11, 2024
217a87a
..
forsthug Jan 11, 2024
53dce29
..
forsthug Jan 11, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/fiskaltrust.Launcher/Commands/HostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public static async Task<int> HandleAsync(HostOptions hostOptions, HostServices
System.Text.Encoding.RegisterProvider(new LauncherEncodingProvider());

var builder = Host.CreateDefaultBuilder()
.UseSystemd()
.UseSerilog()
.ConfigureServices(services =>
{
Expand Down
12 changes: 2 additions & 10 deletions src/fiskaltrust.Launcher/Commands/RunCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.CommandLine;
using System.CommandLine.Invocation;
using fiskaltrust.Launcher.ProcessHost;
using fiskaltrust.Launcher.Services;
using Serilog;
Expand All @@ -8,12 +7,6 @@
using fiskaltrust.Launcher.Extensions;
using fiskaltrust.Launcher.Helpers;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using fiskaltrust.Launcher.Common.Configuration;
using fiskaltrust.storage.serialization.V0;
using System.Security.Cryptography;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Hosting.Server.Features;


namespace fiskaltrust.Launcher.Commands
Expand Down Expand Up @@ -67,8 +60,8 @@ public static class RunHandler
public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonProperties commonProperties, RunOptions _, RunServices runServices)
{
var builder = WebApplication.CreateBuilder();

builder.Host
.UseSystemd()
.UseSerilog()
.ConfigureServices((_, services) =>
{
Expand All @@ -88,7 +81,7 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro
builder.Services.AddCodeFirstGrpc();

var app = builder.Build();

Log.Verbose($"RunHandler builder.Build");
app.UseRouting();
#pragma warning disable ASP0014
app.UseEndpoints(endpoints => endpoints.MapGrpcService<ProcessHostService>());
Expand All @@ -111,7 +104,6 @@ public static async Task<int> HandleAsync(CommonOptions commonOptions, CommonPro
{
Log.CloseAndFlush();
}

return 0;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/fiskaltrust.Launcher/Download/PackageDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ public async Task DownloadAsync(string name, string version, string platform, st

return request;
});

response.EnsureSuccessStatusCode();

await using var fileStream = new FileStream(sourcePath, FileMode.Create, FileAccess.Write, FileShare.None);
Expand All @@ -163,7 +162,6 @@ public async Task DownloadAsync(string name, string version, string platform, st

return request;
});

response.EnsureSuccessStatusCode();
await File.WriteAllTextAsync($"{sourcePath}.hash", await response.Content.ReadAsStringAsync());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,7 @@ public static IDataProtectionBuilder ProtectKeysCustom(this IDataProtectionBuild
}
else if (OperatingSystem.IsLinux())
{
try
{
Marshal.PrelinkAll(typeof(KeyUtils));
builder.Services.Configure<KeyManagementOptions>(options => options.XmlEncryptor = new KeyringXmlEncryptor());
return builder;
}
catch (Exception e)
{
Log.Warning(e, "Fallback config encryption mechanism used.");
}
Log.Warning("Fallback config encryption mechanism is used on linux.");
}
else if (OperatingSystem.IsMacOS())
{
Expand Down
50 changes: 50 additions & 0 deletions src/fiskaltrust.Launcher/Extensions/LifetimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.Versioning;
using System.Text;
using Microsoft.Extensions.Hosting.Systemd;
using Microsoft.Extensions.Hosting.WindowsServices;
using Microsoft.Extensions.Options;

Expand Down Expand Up @@ -28,6 +29,21 @@ public static IHostBuilder UseCustomHostLifetime(this IHostBuilder builder)
#pragma warning restore CA1416
});
}
else if (SystemdHelpers.IsSystemdService())
{
builder.UseSystemd();
builder.ConfigureServices(services => services.AddSingleton<ILifetime, Lifetime>());
return builder;
/*
return builder.ConfigureServices(services =>
{
var lifetime = services.FirstOrDefault(s => s.ImplementationType == typeof(SystemdLifetime));
@@ -51,7 +44,7 @@ public static IHostBuilder UseCustomHostLifetime(this IHostBuilder builder, stri
services.AddSingleton<ILifetime, CustomSystemdServiceLifetime>();
services.AddSingleton<IHostLifetime>(sp => sp.GetRequiredService<ILifetime>());
#pragma warning restore CA1416
});*/
}
else
{
Console.OutputEncoding = Encoding.UTF8;
Expand Down Expand Up @@ -133,4 +149,38 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}
}

[SupportedOSPlatform("linux")]
public class CustomSystemdServiceLifetime : SystemdLifetime, ILifetime
{
private readonly CancellationTokenSource _starting = new();
private readonly ManualResetEventSlim _started = new();

public IHostApplicationLifetime ApplicationLifetime { get; init; }

public CustomSystemdServiceLifetime(
IHostEnvironment environment,
IHostApplicationLifetime applicationLifetime,
ILoggerFactory loggerFactory)
: base(environment, applicationLifetime, new SystemdNotifier(), loggerFactory)
{
ApplicationLifetime = applicationLifetime;
}

public void ServiceStartupCompleted()
{
ApplicationLifetime.ApplicationStarted.Register(() => _started.Set());
}

public new async Task WaitForStartAsync(CancellationToken cancellationToken)
{
try
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(_starting.Token, cancellationToken);
await base.WaitForStartAsync(cts.Token);
}
catch (OperationCanceledException) when (_starting.IsCancellationRequested) { }
}

}
}
23 changes: 8 additions & 15 deletions src/fiskaltrust.Launcher/ServiceInstallation/LinuxSystemD.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using fiskaltrust.Launcher.Helpers;
using Microsoft.Extensions.Hosting.Systemd;
using Serilog;

namespace fiskaltrust.Launcher.ServiceInstallation
Expand All @@ -15,7 +16,7 @@ public LinuxSystemD(string? serviceName, LauncherExecutablePath launcherExecutab

public override async Task<int> InstallService(string commandArgs, string? displayName, bool delayedStart = false)
{
if (!await IsSystemd())
if (!SystemdHelpers.IsSystemdService())
{
return -1;
}
Expand All @@ -32,7 +33,7 @@ public override async Task<int> InstallService(string commandArgs, string? displ

public override async Task<int> UninstallService()
{
if (!await IsSystemd())
if (!SystemdHelpers.IsSystemdService())
{
return -1;
}
Expand All @@ -49,30 +50,22 @@ public override async Task<int> UninstallService()
return (await ProcessHelper.RunProcess("systemctl", new[] { "reset-failed" })).exitCode;
}

private static async Task<bool> IsSystemd()
{
var (exitCode, output) = await ProcessHelper.RunProcess("ps", new[] { "--no-headers", "-o", "comm", "1" });
if (exitCode != 0 && output.Contains("systemd"))
{
Log.Error("Service installation works only for systemd setup.");
return false;
}
return true;
}

private string[] GetServiceFileContent(string serviceDescription, string commandArgs)
{
var processPath = _launcherExecutablePath.Path;

var command = $"{processPath} {commandArgs}";
var command = $"sudo {processPath} {commandArgs}";
return new[]
{
"[Unit]",
$"Description=\"{serviceDescription}\"",
"",
"[Service]",
"Type=simple",
$"ExecStart=\"{command}\"",
"Type=notify",
$"ExecStart={command}",
"TimeoutSec=0",
$"WorkingDirectory={Path.GetDirectoryName(_launcherExecutablePath.Path)}",
"",
"[Install]",
"WantedBy = multi-user.target"
Expand Down
3 changes: 2 additions & 1 deletion src/fiskaltrust.Launcher/fiskaltrust.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<PackageReference Include="fiskaltrust.storage.serialization" Version="1.3.47" />
<PackageReference Include="fiskaltrust.Middleware.Interface.Client.Grpc" Version="1.3.50-rc2" />
<PackageReference Include="fiskaltrust.Middleware.Interface.Client.Http" Version="1.3.50-rc2" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="8.0.0" />
<PackageReference Include="Polly" Version="7.2.4" />
<PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
Expand All @@ -31,7 +32,7 @@
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="8.0.0" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="SemanticVersioning" Version="2.0.2" />
<PackageReference Include="DiffPlex" Version="1.7.1" />
Expand Down
Loading