Add system extensions

This commit is contained in:
code liturgy 2022-11-16 23:46:58 +00:00
parent 61f4a0abac
commit 694e4ea311
5 changed files with 59 additions and 31 deletions

View File

@ -42,12 +42,4 @@
<ProjectReference Include="..\CodeLiturgy.Domain\CodeLiturgy.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="appsettings.json">
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>

View File

@ -1,22 +0,0 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStringDocker": {
"MySQL": "server=db;user=blueuser;password=dXjw127124dJ;database=bluedb;"
},
"ConnectionStringNoDocker": {
"MySQL": "server=localhost;user=blueuser;password=dXjw127124dJ;database=bluedb;"
},
"AuthSettings": {
"SecretKey": "iJWHDmHLpUA283sqsfhqGbMRdRj1PVkH"
},
"JwtIssuerOptions": {
"Issuer": "SomeIssuer",
"Audience": "http://localhost:5000"
}
}

View File

@ -5,6 +5,7 @@
<Nullable>disable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<ItemGroup>
@ -14,6 +15,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CodeLiturgy.Data.Auth\CodeLiturgy.Data.Auth.csproj" />

View File

@ -32,7 +32,7 @@ namespace BlueWest.WebApi
private static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSystemd()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();

View File

@ -0,0 +1,56 @@
using System.Diagnostics;
namespace CodeLiturgy.Views
{
public static class SystemExtensions
{
public static void ExecuteCommand(string command)
{
Process proc = new();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c \" " + command + " \"";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start ();
while (!proc.StandardOutput.EndOfStream) {
Console.WriteLine (proc.StandardOutput.ReadLine ());
}
}
public static void AddUser(
string username,
bool createHome,
bool useSudo)
{
var sudoStr = useSudo ? "sudo" : "";
var createHomeStr = createHome ? "-m" : "";
var addUser = $"{sudoStr} useradd {createHomeStr} {username}";
var deletePass = $"{sudoStr} passwd -d {username}";
var lockPass = $"{sudoStr} passwd -l {username} ";
ExecuteCommand(addUser);
ExecuteCommand(deletePass);
ExecuteCommand(lockPass);
}
public static void DeleteUser(
string username,
bool deleteHome,
bool useSudo)
{
var sudo = useSudo ? "sudo" : "";
var rmUser = $"{sudo} userdel {username}";
ExecuteCommand(rmUser);
if (deleteHome)
{
var delHome = $"{sudo} rm -rf /home/{username}";
ExecuteCommand(delHome);
}
}
}
}