-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dockercompose support and readme updates
- Loading branch information
Showing
8 changed files
with
162 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/ServerManager.DiscordBot/Extensions/DisposableExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public static class DisposableExtensions | ||
{ | ||
public static void DisposeAll(this IEnumerable<IDisposable> disposables) | ||
{ | ||
foreach (var disposable in disposables) | ||
{ | ||
disposable.Dispose(); | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
...rverManager.DiscordBot/ServerHostAdapters/DockerCompose/DockerComposeServerHostAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class DockerComposeServerHostAdapter( | ||
IOptions<DockerComposeServerHostAdapterOptions> options) | ||
: ServerHostAdapterBase<DockerComposeServerHostProperties> | ||
{ | ||
public DockerComposeServerHostAdapterOptions Options { get; } = options.Value; | ||
|
||
public override async Task<ServerStatus> GetServerStatusAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var process = StartDockerComposeProcess("ps --latest --format \"{{.Status}}\""); | ||
|
||
var output = await process.StandardOutput.ReadToEndAsync(); | ||
|
||
await process.WaitForExitAsync(cancellationToken); | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
throw new Exception($"Failed to get server status. Exit code: {process.ExitCode}"); | ||
} | ||
|
||
var status = output.Split(' ')[0]; | ||
|
||
return status switch | ||
{ | ||
"Up" => ServerStatus.Running, | ||
"Exited" => ServerStatus.Stopped, | ||
_ => ServerStatus.Unknown | ||
}; | ||
} | ||
|
||
public override async Task StartServerAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var process = StartDockerComposeProcess("up -d"); | ||
|
||
await process.WaitForExitAsync(cancellationToken); | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
throw new Exception($"Failed to start server. Exit code: {process.ExitCode}"); | ||
} | ||
} | ||
|
||
public override async Task StopServerAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var process = StartDockerComposeProcess("down"); | ||
|
||
await process.WaitForExitAsync(cancellationToken); | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
throw new Exception($"Failed to stop server. Exit code: {process.ExitCode}"); | ||
} | ||
} | ||
|
||
public override async Task<IDictionary<string, Stream>> GetServerLogsAsync(CancellationToken cancellationToken = default) | ||
{ | ||
using var process = StartDockerComposeProcess("logs"); | ||
|
||
var logs = await process.StandardOutput.ReadToEndAsync(cancellationToken); | ||
|
||
await process.WaitForExitAsync(cancellationToken); | ||
|
||
if (process.ExitCode != 0) | ||
{ | ||
throw new Exception($"Failed to get server logs. Exit code: {process.ExitCode}"); | ||
} | ||
|
||
return new Dictionary<string, Stream> | ||
{ | ||
{ "output", new MemoryStream(Encoding.UTF8.GetBytes(logs)) } | ||
}; | ||
} | ||
|
||
private Process StartDockerComposeProcess(string arguments) | ||
{ | ||
var process = new Process | ||
{ | ||
StartInfo = new ProcessStartInfo | ||
{ | ||
FileName = Options.DockerProcessFilePath, | ||
Arguments = $"compose -f {Context.Properties.DockerComposeFilePath} {arguments}", | ||
RedirectStandardOutput = true, | ||
RedirectStandardError = true, | ||
UseShellExecute = false, | ||
CreateNoWindow = true | ||
} | ||
}; | ||
|
||
process.Start(); | ||
|
||
return process; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ager.DiscordBot/ServerHostAdapters/DockerCompose/DockerComposeServerHostAdapterOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using Microsoft.Extensions.Options; | ||
|
||
public class DockerComposeServerHostAdapterOptions : IOptions<DockerComposeServerHostAdapterOptions> | ||
{ | ||
[Required] | ||
public string DockerProcessFilePath { get; set; } = "docker"; | ||
|
||
DockerComposeServerHostAdapterOptions IOptions<DockerComposeServerHostAdapterOptions>.Value => this; | ||
} |
7 changes: 7 additions & 0 deletions
7
...rManager.DiscordBot/ServerHostAdapters/DockerCompose/DockerComposeServerHostProperties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
public class DockerComposeServerHostProperties | ||
{ | ||
[Required] | ||
public string DockerComposeFilePath { get; set; } = default!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/ServerManager.DiscordBot/ServerHostAdapters/ServerHosterAdapterType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
public enum ServerHosterAdapterType | ||
{ | ||
Process, | ||
DockerCompose, | ||
Kubernetes | ||
} |