Skip to content

Commit

Permalink
feat: Backups
Browse files Browse the repository at this point in the history
fix: Docker Builds
fix: Better logging with ffmpeg
  • Loading branch information
SenexCrenshaw committed Feb 9, 2024
1 parent 889d584 commit 3a6174f
Show file tree
Hide file tree
Showing 37 changed files with 592 additions and 79 deletions.
3 changes: 2 additions & 1 deletion Dockerfile.base
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
FROM --platform=$TARGETPLATFORM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

RUN apt-get update -yq \
&& apt-get upgrade -yq \
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
FROM --platform=$TARGETPLATFORM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

RUN apt-get update -yq \
&& apt-get upgrade -yq
Expand Down
1 change: 1 addition & 0 deletions Dockerfile.sm.template
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ARG TARGETPLATFORM
ARG TARGETARCH
ARG BUILDPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
WORKDIR /src

COPY . .
Expand Down
6 changes: 4 additions & 2 deletions Dockerfile.template
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
ARG TARGETPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}
ARG TARGETARCH
ARG BUILDPLATFORM
ENV TARGETPLATFORM=${TARGETPLATFORM:-linux/amd64}

LABEL org.opencontainers.image.url="https://hub.docker.com/r/SenexCrenshaw/streammaster/" \
org.opencontainers.image.source="https://github.com/SenexCrenshaw/StreamMaster" \
org.opencontainers.image.vendor="SenexCrenshaw" \
Expand All @@ -18,9 +19,10 @@ COPY docker-entrypoint.sh docker-ensure-initdb.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh /usr/local/bin/docker-ensure-initdb.sh
RUN ln -sT docker-ensure-initdb.sh /usr/local/bin/docker-enforce-initdb.sh

COPY backup.sh /usr/local/bin/backup.sh
COPY entrypoint.sh /entrypoint.sh
COPY env.sh /env.sh
RUN chmod +x /entrypoint.sh /env.sh
RUN chmod +x /entrypoint.sh /env.sh /usr/local/bin/backup.sh
RUN mkdir /config

EXPOSE 5432
Expand Down
9 changes: 9 additions & 0 deletions StreamMaster.API/Controllers/MiscController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Mvc;

using StreamMaster.Domain.Common;
using StreamMaster.Domain.Services;

using System.Text;
Expand Down Expand Up @@ -43,4 +44,12 @@ public IActionResult GetTestM3U(int numberOfStreams)
FileDownloadName = $"m3u-test-{numberOfStreams}.m3u"
};
}

[HttpPut]
[Route("[action]")]
public async Task<IActionResult> Backup()
{
await FileUtil.Backup();
return Ok();
}
}
18 changes: 13 additions & 5 deletions StreamMaster.API/Controllers/VideoStreamsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using StreamMaster.Application.VideoStreams.Commands;
using StreamMaster.Application.VideoStreams.Queries;
using StreamMaster.Domain.Authentication;
using StreamMaster.Domain.Cache;
using StreamMaster.Domain.Common;
using StreamMaster.Domain.Dto;
using StreamMaster.Domain.Enums;
using StreamMaster.Domain.Pagination;
Expand Down Expand Up @@ -84,6 +86,15 @@ public async Task<ActionResult<PagedResponse<VideoStreamDto>>> GetPagedVideoStre
return Ok(res);
}

private StreamingProxyTypes GetStreamingProxyType(VideoStreamDto videoStream)
{
Setting setting = MemoryCache.GetSetting();

return videoStream.StreamingProxyType == StreamingProxyTypes.SystemDefault
? setting.StreamingProxyType
: videoStream.StreamingProxyType;
}

[Authorize(Policy = "SGLinks")]
[HttpGet]
[HttpHead]
Expand Down Expand Up @@ -123,11 +134,8 @@ public async Task<ActionResult> GetVideoStreamStream(string encodedIds, string n

HttpContext.Session.Remove("ClientId");

bool redirect = videoStream.StreamingProxyType == StreamingProxyTypes.None;
if (!redirect && videoStream.StreamingProxyType == StreamingProxyTypes.SystemDefault && Settings.StreamingProxyType == StreamingProxyTypes.None)
{
redirect = true;
}
StreamingProxyTypes proxyType = GetStreamingProxyType(videoStream);
bool redirect = proxyType == StreamingProxyTypes.None;

if (redirect)
{
Expand Down
14 changes: 13 additions & 1 deletion StreamMaster.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,24 @@
var lifetime = app.Services.GetService<IHostApplicationLifetime>();
if (lifetime != null)
{
lifetime.ApplicationStopped.Register(OnShutdown);
lifetime.ApplicationStopping.Register(OnShutdown);
}

void OnShutdown()
{
SqliteConnection.ClearAllPools();
PGSQLRepositoryContext repositoryContext = app.Services.GetRequiredService<PGSQLRepositoryContext>();
repositoryContext.Dispose();
SQLiteRepositoryContext sQLiteRepositoryContext = app.Services.GetRequiredService<SQLiteRepositoryContext>();
sQLiteRepositoryContext.Dispose();
IImageDownloadService imageDownloadService = app.Services.GetRequiredService<IImageDownloadService>();
imageDownloadService.StopAsync(CancellationToken.None).Wait();

FileUtil.Backup().Wait();
//LogDbContext logDbContext = app.Services.GetRequiredService<LogDbContext>();
//logDbContext.Dispose();
//LogDbContextInitialiser logInitialiser = app.Services.GetRequiredService<LogDbContextInitialiser>();
//logInitialiser.Dispose();
}

app.UseOpenApi();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ProcessEPGFileRequestHandler(ILogger<ProcessEPGFileRequest> logger,
}
finally
{
jobStatusService.SetEPGIsRunning(false);
jobStatusService.SetEPGStop();
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RefreshEPGFileRequestHandler(ILogger<RefreshEPGFileRequest> Logger,
{
return null;
}
jobStatusService.SetEPGIsRunning(true);
jobStatusService.SetEPGStart();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class ProcessM3UFileRequestHandler(ILogger<ProcessM3UFileRequest> logger,
}
finally
{
jobStatusService.SetM3UIsRunning(false);
jobStatusService.SetM3UStop();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class RefreshM3UFileRequestHandler(ILogger<RefreshM3UFileRequest> Logger,
{
return null;
}
jobStatusService.SetM3UIsRunning(true);
jobStatusService.SetM3UStart();
}

M3UFile? m3uFile = await Repository.M3UFile.GetM3UFileById(request.Id).ConfigureAwait(false);
Expand Down
18 changes: 18 additions & 0 deletions StreamMaster.Application/Settings/Commands/UpdateSettingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace StreamMaster.Application.Settings.Commands;

public class UpdateSettingRequest : IRequest<UpdateSettingResponse>
{
public bool? BackupEnabled { get; set; }
public int? BackupVersionsToKeep { get; set; }
public int? BackupInterval { get; set; }
public SDSettingsRequest? SDSettings { get; set; }
public bool? ShowClientHostNames { get; set; }
public string? AdminPassword { get; set; }
Expand Down Expand Up @@ -274,6 +277,21 @@ private async Task<bool> UpdateSetting(Setting currentSetting, UpdateSettingRequ
currentSetting.M3UUseChnoForId = (bool)request.M3UUseChnoForId;
}

if (request.BackupEnabled != null)
{
currentSetting.BackupEnabled = (bool)request.BackupEnabled;
}

if (request.BackupVersionsToKeep.HasValue)
{
currentSetting.BackupVersionsToKeep = request.BackupVersionsToKeep.Value;
}

if (request.BackupInterval.HasValue)
{
currentSetting.BackupInterval = request.BackupInterval.Value;
}

if (request.ShowClientHostNames != null)
{
currentSetting.ShowClientHostNames = (bool)request.ShowClientHostNames;
Expand Down
16 changes: 16 additions & 0 deletions StreamMaster.Domain/Common/BuildInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ static BuildInfo()
InitializePaths();
}

private static DateTime _startTime;

public static DateTime StartTime
{
get
{
if (_startTime == DateTime.MinValue)
{
_startTime = DateTime.Now;
}

return _startTime;
}
}

#region Database Configuration Properties

/// <summary>
Expand Down Expand Up @@ -118,6 +133,7 @@ public static DateTime BuildDateTime
public static readonly string IconDefault = Path.Combine("images", "default.png");
public static readonly string FFMPEGDefaultOptions = "-hide_banner -loglevel error -i {streamUrl} -c copy -f mpegts pipe:1";
public static readonly string LogFilePath = Path.Combine(LogFolder, "StreamMasterAPI.log");
public static readonly string BackupPath = Path.Combine(AppDataFolder, "backups");

#endregion

Expand Down
53 changes: 53 additions & 0 deletions StreamMaster.Domain/Common/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using StreamMaster.Domain.Models;
using StreamMaster.SchedulesDirect.Domain.XmltvXml;

using System.Diagnostics;
using System.IO.Compression;
using System.Net;
using System.Text;
Expand Down Expand Up @@ -247,6 +248,57 @@ public static Stream GetFileDataStream(string source)
FileStream fs = File.OpenRead(source);
return new GZipStream(fs, CompressionMode.Decompress);
}
public static async Task Backup(int? versionsToKeep = null)
{
Setting? setting = GetSetting();
if (setting.BackupEnabled == false)
{
return;
}

try
{
versionsToKeep ??= GetSetting()?.BackupVersionsToKeep ?? 5;
using Process process = new();
process.StartInfo.FileName = "/bin/bash";
process.StartInfo.Arguments = $"/usr/local/bin/backup.sh {versionsToKeep}";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;

process.Start();

string output = await process.StandardOutput.ReadToEndAsync();
string error = await process.StandardError.ReadToEndAsync();

await process.WaitForExitAsync();

if (!string.IsNullOrEmpty(output))
{
Console.WriteLine($"Backup Output: {output}");
}

if (!string.IsNullOrEmpty(error))
{
Console.WriteLine($"Backup Error: {error}");
}

if (process.ExitCode == 0)
{
Console.WriteLine("Backup executed successfully.");
}
else
{
Console.WriteLine($"Backup execution failed with exit code: {process.ExitCode}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Backup Exception occurred: {ex.Message}");
}

}

public static async Task<string> GetFileData(string source)
{
Expand Down Expand Up @@ -436,6 +488,7 @@ public static void SetupDirectories(bool alwaysRun = false)
CreateDir(BuildInfo.SDStationLogosCache);
CreateDir(BuildInfo.SDJSONFolder);
CreateDir(BuildInfo.LogFolder);
CreateDir(BuildInfo.BackupPath);

for (char c = '0'; c <= '9'; c++)
{
Expand Down
3 changes: 3 additions & 0 deletions StreamMaster.Domain/Common/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ public class TestSettings

public class BaseSettings : M3USettings
{
public bool BackupEnabled { get; set; } = true;
public int BackupVersionsToKeep { get; set; } = 18;
public int BackupInterval { get; set; } = 4;

public bool PrettyEPG { get; set; } = false;
public int MaxLogFiles { get; set; } = 10;
Expand Down
9 changes: 7 additions & 2 deletions StreamMaster.Domain/Models/JobStatus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ public void ClearForce()
Extra = false;
}

public void SetIsRunning(bool isRunning)
public void SetStart()
{
IsRunning = isRunning;
IsRunning = true;
}

public void SetStop()
{
IsRunning = false;
}
}
1 change: 1 addition & 0 deletions StreamMaster.Domain/Services/IImageDownloadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace StreamMaster.Domain.Services
{
public interface IImageDownloadService
{
Task StopAsync(CancellationToken cancellationToken);
void Start();
ImageDownloadServiceStatus GetStatus();
}
Expand Down
18 changes: 15 additions & 3 deletions StreamMaster.Domain/Services/IJobStatusService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ public interface IJobStatusService
JobStatus GetSyncJobStatus();
void SetSyncError();
void SetSyncForceNextRun(bool Extra = false);
void SetSyncIsRunning(bool v);
void SetSyncStop();
void SetSyncStart();
void SetSyncSuccessful();


// M3U
void SetM3USuccessful();
void SetM3UError();
void SetM3UForceNextRun(bool Extra = false);
void SetM3UIsRunning(bool v);
void SetM3UStop();
void SetM3UStart();
JobStatus GetM3UJobStatus();
void ClearM3UForce();

Expand All @@ -25,8 +27,18 @@ public interface IJobStatusService
void SetEPGSuccessful();
void SetEPGError();
void SetEPGForceNextRun(bool Extra = false);
void SetEPGIsRunning(bool v);
void SetEPGStop();
void SetEPGStart();
JobStatus GetEPGJobStatus();
void ClearEPGForce();

// Backup
void SetBackupSuccessful();
void SetBackupError();
void SetBackupForceNextRun(bool Extra = false);
void SetBackupStop();
void SetBackupStart();
JobStatus GetBackupJobStatus();
void ClearBackupForce();
}
}
Loading

0 comments on commit 3a6174f

Please sign in to comment.