From 4063685a173f020fb33c444131155c507a05a23d Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 30 Nov 2022 23:21:27 +0200 Subject: [PATCH] [1.0.7] Code cleanup and refactoring + Exposing two new methods "/ping" and "/version" in a new controller "/system". --- Directory.Build.props | 2 +- NetworkResurrector.sln | 58 +++++++++---------- ReleaseNotes.xml | 9 +++ buildx.sh | 22 ------- dependencies.props | 2 +- {notes => solution items/notes}/generic.txt | 0 {notes => solution items/notes}/nugets.txt | 0 .../notes}/proxmox-shutdown-notes.txt | 0 .../scripts/api/build-amd64.sh | 0 .../scripts/api/buildx-arm32.sh | 0 solution items/scripts/api/buildx.sh | 26 +++++++++ .../scripts/api/release.sh | 4 +- .../Queries/GetSystemVersion.cs | 47 +++++++++++++++ .../Controllers/SystemController.cs | 36 ++++++++++++ .../Docker/amd64/Dockerfile | 9 ++- .../appsettings.Development.json | 9 --- 16 files changed, 157 insertions(+), 67 deletions(-) delete mode 100644 buildx.sh rename {notes => solution items/notes}/generic.txt (100%) rename {notes => solution items/notes}/nugets.txt (100%) rename {notes => solution items/notes}/proxmox-shutdown-notes.txt (100%) rename build-amd64.sh => solution items/scripts/api/build-amd64.sh (100%) rename buildx-arm32.sh => solution items/scripts/api/buildx-arm32.sh (100%) create mode 100644 solution items/scripts/api/buildx.sh rename release.sh => solution items/scripts/api/release.sh (93%) create mode 100644 src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs create mode 100644 src/api/NetworkResurrector.Api/Controllers/SystemController.cs delete mode 100644 src/api/NetworkResurrector.Api/appsettings.Development.json diff --git a/Directory.Build.props b/Directory.Build.props index 76d10a1..14578a5 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ - 1.0.3 + 1.0.7 Tudor Stanciu STA NetworkResurrector diff --git a/NetworkResurrector.sln b/NetworkResurrector.sln index 9a2ec7d..0d251b8 100644 --- a/NetworkResurrector.sln +++ b/NetworkResurrector.sln @@ -5,17 +5,6 @@ VisualStudioVersion = 17.0.32014.148 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Server", "src\server\NetworkResurrector.Server\NetworkResurrector.Server.csproj", "{F6600491-5D79-4548-8745-59D9D337D3DB}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{FDDE879C-E0A3-4BF1-945F-7FEA324C8EBA}" - ProjectSection(SolutionItems) = preProject - .dockerignore = .dockerignore - .gitattributes = .gitattributes - .gitignore = .gitignore - dependencies.props = dependencies.props - Directory.Build.props = Directory.Build.props - NuGet.config = NuGet.config - ReleaseNotes.xml = ReleaseNotes.xml - EndProjectSection -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Server.Application", "src\server\NetworkResurrector.Server.Application\NetworkResurrector.Server.Application.csproj", "{15D65D65-CC96-45DE-8590-AF9132889D98}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Server.Domain", "src\server\NetworkResurrector.Server.Domain\NetworkResurrector.Server.Domain.csproj", "{EC78E88E-22DC-4FFD-881E-DEECF0D2494E}" @@ -40,16 +29,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Api.Doma EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Api.Domain.Data", "src\api\NetworkResurrector.Api.Domain.Data\NetworkResurrector.Api.Domain.Data.csproj", "{509767A7-D11C-4143-8D45-01E62DFC2C74}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{DF5CBFF4-B348-4D4C-A6D4-B65BA1D64CEF}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{C5096F61-7319-453B-AA28-7A1A854A6220}" - ProjectSection(SolutionItems) = preProject - build-amd64.sh = build-amd64.sh - buildx-arm32.sh = buildx-arm32.sh - buildx.sh = buildx.sh - release.sh = release.sh - EndProjectSection -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "agent", "agent", "{C04663A1-E0CD-41D6-A8D7-FADDF9DA8302}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent", "src\agent\NetworkResurrector.Agent\NetworkResurrector.Agent.csproj", "{C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E}" @@ -66,14 +45,35 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Server.P EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Api.PublishedLanguage", "src\api\NetworkResurrector.Api.PublishedLanguage\NetworkResurrector.Api.PublishedLanguage.csproj", "{1EA9EAD7-8896-4756-A2C2-7969E9073FA6}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "notes", "notes", "{6973CB33-B940-41B0-B3BB-22ED2ECB8E1D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Wrapper", "src\agent\NetworkResurrector.Agent.Wrapper\NetworkResurrector.Agent.Wrapper.csproj", "{3624DFF4-C692-42F8-BDC2-1C2107E6EDC8}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution items", "solution items", "{C683E36F-A75F-4620-9D4F-2D73EE933AE1}" ProjectSection(SolutionItems) = preProject - notes\generic.txt = notes\generic.txt - notes\nugets.txt = notes\nugets.txt - notes\proxmox-shutdown-notes.txt = notes\proxmox-shutdown-notes.txt + .dockerignore = .dockerignore + .gitattributes = .gitattributes + .gitignore = .gitignore + dependencies.props = dependencies.props + Directory.Build.props = Directory.Build.props + NuGet.config = NuGet.config + ReleaseNotes.xml = ReleaseNotes.xml EndProjectSection EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetworkResurrector.Agent.Wrapper", "src\agent\NetworkResurrector.Agent.Wrapper\NetworkResurrector.Agent.Wrapper.csproj", "{3624DFF4-C692-42F8-BDC2-1C2107E6EDC8}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{F92904AA-DC7A-4FD7-9807-3C5583C26D98}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "api", "api", "{1AE7B305-E24D-48A3-A732-91DAAACA39D6}" + ProjectSection(SolutionItems) = preProject + solution items\scripts\api\build-amd64.sh = solution items\scripts\api\build-amd64.sh + solution items\scripts\api\buildx-arm32.sh = solution items\scripts\api\buildx-arm32.sh + solution items\scripts\api\buildx.sh = solution items\scripts\api\buildx.sh + solution items\scripts\api\release.sh = solution items\scripts\api\release.sh + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "notes", "notes", "{A28CE618-9D99-491A-8DCF-23D4A19E7750}" + ProjectSection(SolutionItems) = preProject + solution items\notes\generic.txt = solution items\notes\generic.txt + solution items\notes\nugets.txt = solution items\notes\nugets.txt + solution items\notes\proxmox-shutdown-notes.txt = solution items\notes\proxmox-shutdown-notes.txt + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -170,8 +170,6 @@ Global {EE31B126-12EC-46B3-8FB4-AD5BCF14C029} = {43C78941-52E6-4AB8-9170-CC7C006E4784} {885D5625-028A-4B35-8C89-7EF718BC6E34} = {43C78941-52E6-4AB8-9170-CC7C006E4784} {509767A7-D11C-4143-8D45-01E62DFC2C74} = {43C78941-52E6-4AB8-9170-CC7C006E4784} - {DF5CBFF4-B348-4D4C-A6D4-B65BA1D64CEF} = {FDDE879C-E0A3-4BF1-945F-7FEA324C8EBA} - {C5096F61-7319-453B-AA28-7A1A854A6220} = {DF5CBFF4-B348-4D4C-A6D4-B65BA1D64CEF} {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} = {B0C5F0C1-0BF8-4651-AAFC-BE01F516D7B8} {C8C4CA6F-39E2-46FE-89E2-0A81D2F4161E} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} {3795AB02-7F2A-424B-BFD2-1B915E155829} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} @@ -180,8 +178,10 @@ Global {696DAC79-9C18-4DC3-9925-755AD6C1FEC9} = {6889D39C-D8DA-4B99-AFC1-F0B6355E73C0} {388D632D-8104-45E2-8017-D04848941F29} = {6889D39C-D8DA-4B99-AFC1-F0B6355E73C0} {1EA9EAD7-8896-4756-A2C2-7969E9073FA6} = {43C78941-52E6-4AB8-9170-CC7C006E4784} - {6973CB33-B940-41B0-B3BB-22ED2ECB8E1D} = {FDDE879C-E0A3-4BF1-945F-7FEA324C8EBA} {3624DFF4-C692-42F8-BDC2-1C2107E6EDC8} = {C04663A1-E0CD-41D6-A8D7-FADDF9DA8302} + {F92904AA-DC7A-4FD7-9807-3C5583C26D98} = {C683E36F-A75F-4620-9D4F-2D73EE933AE1} + {1AE7B305-E24D-48A3-A732-91DAAACA39D6} = {F92904AA-DC7A-4FD7-9807-3C5583C26D98} + {A28CE618-9D99-491A-8DCF-23D4A19E7750} = {C683E36F-A75F-4620-9D4F-2D73EE933AE1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {351D76E9-FE02-4C30-A464-7B29AFC64BC7} diff --git a/ReleaseNotes.xml b/ReleaseNotes.xml index bc5f1e5..476a62c 100644 --- a/ReleaseNotes.xml +++ b/ReleaseNotes.xml @@ -29,4 +29,13 @@ The system will be able to execute these operations in multiple ways. This is just the first one and all of them are handled by the NetworkResurrector.Api. For example, if the user wants to shutdown a clean Windows machine, he can use the agent and configure the API to call it, but in case of a proxmox machine, the API can be configured to execute a http request directly to the OS with the shutdown action (without the need for the agent). + + 1.0.7 + + Code cleanup and refactoring + • Preparing to make the project open source on my Gitea instance. + • Removed secrets from source code and from git history + • Exposing two new methods "/ping" and "/version" in a new controller "/system". + + \ No newline at end of file diff --git a/buildx.sh b/buildx.sh deleted file mode 100644 index a4f3ea6..0000000 --- a/buildx.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash -echo "Welcome!" - -#version="1.0.6" -#platform="linux/amd64,linux/arm64" -version="1.0.6-arm64" -platform="linux/arm64" -localRegistryPass="*********" - -echo "Login to alpine-nexus registry." -docker login --username=admin --password=$localRegistryPass alpine-nexus:8500 - -echo "Create docker image with version $version for platform $platform" -docker buildx build \ - --build-arg APP_VERSION=$version \ - --platform $platform \ - --output=type=image,push=true,registry.insecure=true \ - --push \ - --tag alpine-nexus:8500/network-resurrector/network-resurrector-api:$version \ - -f "src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile" . - -echo "Done!" \ No newline at end of file diff --git a/dependencies.props b/dependencies.props index dfa073c..2493739 100644 --- a/dependencies.props +++ b/dependencies.props @@ -10,7 +10,7 @@ 8.1.1 6.0.0 5.0.12 - 1.0.2 + 1.0.4 1.0.0 1.0.7 1.1.3 diff --git a/notes/generic.txt b/solution items/notes/generic.txt similarity index 100% rename from notes/generic.txt rename to solution items/notes/generic.txt diff --git a/notes/nugets.txt b/solution items/notes/nugets.txt similarity index 100% rename from notes/nugets.txt rename to solution items/notes/nugets.txt diff --git a/notes/proxmox-shutdown-notes.txt b/solution items/notes/proxmox-shutdown-notes.txt similarity index 100% rename from notes/proxmox-shutdown-notes.txt rename to solution items/notes/proxmox-shutdown-notes.txt diff --git a/build-amd64.sh b/solution items/scripts/api/build-amd64.sh similarity index 100% rename from build-amd64.sh rename to solution items/scripts/api/build-amd64.sh diff --git a/buildx-arm32.sh b/solution items/scripts/api/buildx-arm32.sh similarity index 100% rename from buildx-arm32.sh rename to solution items/scripts/api/buildx-arm32.sh diff --git a/solution items/scripts/api/buildx.sh b/solution items/scripts/api/buildx.sh new file mode 100644 index 0000000..75fca70 --- /dev/null +++ b/solution items/scripts/api/buildx.sh @@ -0,0 +1,26 @@ +#!/bin/bash +echo "Welcome!" + +version="1.0.7" +platform="linux/amd64,linux/arm64" +localRegistryPass="***********" +databaseConnection="Server=#########;Database=#########;uid=#########;Password=#########;MultipleActiveResultSets=true" +identityServerBaseAddress="http://:/" +networkResurrectorServerBaseAddress="http://:/" + +echo "Login to alpine-nexus registry." +docker login --username=admin --password=$localRegistryPass alpine-nexus:8500 + +echo "Create docker image with version $version for platform $platform" +docker buildx build \ + --build-arg APP_VERSION=$version \ + --build-arg DATABASE_CONNECTION=$databaseConnection \ + --build-arg IDENTITY_SERVER_BASE_ADDRESS=$identityServerBaseAddress \ + --build-arg NETWORK_RESURRECTOR_SERVER_BASE_ADDRESS=$networkResurrectorServerBaseAddress \ + --platform $platform \ + --output=type=image,push=true,registry.insecure=true \ + --push \ + --tag alpine-nexus:8500/network-resurrector/network-resurrector-api:$version \ + -f "src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile" . + +echo "Done!" \ No newline at end of file diff --git a/release.sh b/solution items/scripts/api/release.sh similarity index 93% rename from release.sh rename to solution items/scripts/api/release.sh index f1d2d5a..6422d41 100644 --- a/release.sh +++ b/solution items/scripts/api/release.sh @@ -1,8 +1,8 @@ #!/bin/sh echo "Welcome!" -version="1.0.3" -oldver="1.0.2" +version="1.0.7" +oldver="1.0.6" echo "Pull docker image network-resurrector-api:$version from registry." docker pull alpine-nexus:8500/network-resurrector/network-resurrector-api:$version diff --git a/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs b/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs new file mode 100644 index 0000000..82d5d87 --- /dev/null +++ b/src/api/NetworkResurrector.Api.Application/Queries/GetSystemVersion.cs @@ -0,0 +1,47 @@ +using MediatR; +using NDB.Application.DataContracts; +using System; +using System.IO; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; + +namespace NetworkResurrector.Api.Application.Queries +{ + public class GetSystemVersion + { + public class Query : Query + { + public Query() + { + } + } + + public record Model(string Version, DateTime LastUpdateDate); + + public class QueryHandler : IRequestHandler + { + public QueryHandler() + { + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var version = Environment.GetEnvironmentVariable("APP_VERSION"); + var appDate = Environment.GetEnvironmentVariable("APP_DATE"); + + if (string.IsNullOrEmpty(version)) + version = Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion; + + if (!DateTime.TryParse(appDate, out var lastUpdateDate)) + { + var location = Assembly.GetExecutingAssembly().Location; + lastUpdateDate = File.GetLastWriteTime(location); + } + + var result = new Model(version, lastUpdateDate); + return await Task.FromResult(result); + } + } + } +} diff --git a/src/api/NetworkResurrector.Api/Controllers/SystemController.cs b/src/api/NetworkResurrector.Api/Controllers/SystemController.cs new file mode 100644 index 0000000..6cc023f --- /dev/null +++ b/src/api/NetworkResurrector.Api/Controllers/SystemController.cs @@ -0,0 +1,36 @@ +using MediatR; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using NetworkResurrector.Api.Application.Queries; +using System; +using System.Threading.Tasks; + +namespace IdentityServer.Api.Controllers +{ + [Authorize] + [ApiController] + [Route("system")] + public class SystemController : ControllerBase + { + private readonly IMediator _mediator; + + public SystemController(IMediator mediator) + { + _mediator = mediator; + } + + [AllowAnonymous] + [HttpGet("ping")] + public IActionResult Ping() + { + return Ok($"Ping success. System datetime: {DateTime.Now}"); + } + + [HttpGet("version")] + public async Task GetSystemVersion([FromRoute] GetSystemVersion.Query query) + { + var result = await _mediator.Send(query); + return Ok(result); + } + } +} diff --git a/src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile b/src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile index 8ecc9cc..6407d83 100644 --- a/src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile +++ b/src/api/NetworkResurrector.Api/Docker/amd64/Dockerfile @@ -27,14 +27,17 @@ WORKDIR /app COPY --from=publish /app/publish . ENV urls="http://*:80" -ENV ConnectionStrings__DatabaseConnection="*********" -ENV IdentityServer__BaseAddress="*********" -ENV NetworkResurrectorServer__BaseAddress="*********" ENV TZ=Europe/Bucharest RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone ARG APP_VERSION=0.0.0.0 ENV APP_VERSION=${APP_VERSION} +ARG DATABASE_CONNECTION="" +ENV ConnectionStrings__DatabaseConnection=${DATABASE_CONNECTION} +ARG IDENTITY_SERVER_BASE_ADDRESS="" +ENV IdentityServer__BaseAddress=${IDENTITY_SERVER_BASE_ADDRESS} +ARG NETWORK_RESURRECTOR_SERVER_BASE_ADDRESS="" +ENV NetworkResurrectorServer__BaseAddress=${NETWORK_RESURRECTOR_SERVER_BASE_ADDRESS} #Workaround to lower the TLS level in container for old sql server version RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf diff --git a/src/api/NetworkResurrector.Api/appsettings.Development.json b/src/api/NetworkResurrector.Api/appsettings.Development.json deleted file mode 100644 index 8983e0f..0000000 --- a/src/api/NetworkResurrector.Api/appsettings.Development.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" - } - } -}