From 597dfb0745535d667e5d13d58e5aad3f95f30181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Thu, 20 Jan 2022 00:47:13 +0100 Subject: [PATCH 1/4] Added HA add-on support the proper way --- .../etc/services.d/NetDaemonAddOnApp/run | 43 +++++++++------ Dockerfile.AddOn | 18 +----- NetDaemon.sln | 15 ----- .../Internal/Config/PluginConfig.cs | 30 ---------- .../Extensions/HostBuilderExtension.cs | 55 ------------------- .../Extensions/ServiceCollectionExtensions.cs | 34 ------------ .../Internal/Helpers/LogLevelParser.cs | 17 ------ .../NetDaemon.Host.AddOn.csproj | 38 ------------- src/Host/NetDaemon.Host.AddOn/Program.cs | 19 ------- .../NetDaemon.Host.AddOn/appsettings.json | 9 --- 10 files changed, 26 insertions(+), 252 deletions(-) delete mode 100644 src/Host/NetDaemon.Host.AddOn/Internal/Config/PluginConfig.cs delete mode 100644 src/Host/NetDaemon.Host.AddOn/Internal/Extensions/HostBuilderExtension.cs delete mode 100644 src/Host/NetDaemon.Host.AddOn/Internal/Extensions/ServiceCollectionExtensions.cs delete mode 100644 src/Host/NetDaemon.Host.AddOn/Internal/Helpers/LogLevelParser.cs delete mode 100644 src/Host/NetDaemon.Host.AddOn/NetDaemon.Host.AddOn.csproj delete mode 100644 src/Host/NetDaemon.Host.AddOn/Program.cs delete mode 100644 src/Host/NetDaemon.Host.AddOn/appsettings.json diff --git a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run index 67df18b0b..dc880b0fc 100644 --- a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run +++ b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run @@ -1,5 +1,15 @@ -#!/usr/bin/with-contenv bash -echo "Starting NetDaemon Runner" +#!/usr/bin/with-contenv /usr/bin/bashio +echo "Starting NetDaemon Runner for add-on" + +# Set configuration values to environment variables +export Netdaemon__ApplicationAssembly=$(bashio::config 'app_assembly') +export Logging__Loglevel__Default=$(bashio::config 'log_level') +export NetDaemon__ApplicationConfigurationFolder=$(bashio::config 'app_config_folder') +export Homeassistant__TOKEN=$SUPERVISOR_TOKEN +export Homeassistant__HOST=supervisor +export Homeassistant__Ssl=false +export Homeassistant__WebsocketPath="core/websocket" +export Homeassistant__Port=80 declare daemondir="/daemon" declare is_custom_app_source=false @@ -14,43 +24,40 @@ if [ ! -f "/data/options.json" ]; then exit 1 fi -# Get the settings for application source -export NETDAEMON__APPLICATION_ASSEMBLY=$( jq -r .app_assembly /data/options.json ) - -if [[ "${NETDAEMON__APPLICATION_ASSEMBLY}" == *.csproj ]]; +if [[ "${Netdaemon__ApplicationAssembly}" == *.csproj ]]; then echo -e "\\033[31mcsproj deployments are not supported in v3, use compiled option instead!\\033[0m" >&2 exit 1 fi -if [[ "${NETDAEMON__APPLICATION_ASSEMBLY}" == *.csproj ]]; +if [[ "${Netdaemon__ApplicationAssembly}" == *.csproj ]]; then echo -e "\\033[31mcsproj deployments are not supported in v3, use compiled option instead!\\033[0m" >&2 exit 1 fi -if [[ ! -z "${NETDAEMON__APPLICATION_ASSEMBLY}" ]] && [[ "${NETDAEMON__APPLICATION_ASSEMBLY}" != *".dll" ]]; +if [[ ! -z "${Netdaemon__ApplicationAssembly}" ]] && [[ "${Netdaemon__ApplicationAssembly}" != *".dll" ]]; then echo -e "\\033[31mAssembly needs to point to a .dll file!\\033[0m" >&2 exit 1 fi -if [[ "${NETDAEMON__APPLICATION_ASSEMBLY}" == *.dll ]]; +if [[ "${Netdaemon__ApplicationAssembly}" == *.dll ]]; then is_custom_app_source=true # make path relative to data folder (/config/netdaemon if addon) # if the path is a relative path - if [[ "${NETDAEMON__APPLICATION_ASSEMBLY}" != /* ]]; + if [[ "${Netdaemon__ApplicationAssembly}" != /* ]]; then - export NETDAEMON__APPLICATION_ASSEMBLY="/config/netdaemon3/${NETDAEMON__APPLICATION_ASSEMBLY}" + export Netdaemon__ApplicationAssembly="/config/netdaemon3/${Netdaemon__ApplicationAssembly}" fi # The provided application source is ether a project or pre-compiled .Net application - if [ ! -f ${NETDAEMON__APPLICATION_ASSEMBLY} ]; + if [ ! -f ${Netdaemon__ApplicationAssembly} ]; then - echo -e "\\033[31mThe assembly ${NETDAEMON__APPLICATION_ASSEMBLY} cannot be found. Please check the settings.\\033[0m" >&2 + echo -e "\\033[31mThe assembly ${Netdaemon__ApplicationAssembly} cannot be found. Please check the settings.\\033[0m" >&2 exit 1 fi fi @@ -58,17 +65,17 @@ fi if [[ $is_custom_app_source == false ]]; then echo -e "\\033[32mRunning NetDaemon at ${daemondir}...\\033[0m" >&2 cd "${daemondir}" - exec dotnet NetDaemon.Host.AddOn.dll + exec dotnet NetDaemon.Host.Default.dll else # This is a pre-built deamon - echo -e "\\033[32mRunning the pre-built NetDaemon at ${NETDAEMON__APPLICATION_ASSEMBLY}...\\033[0m" >&2 - cd "$(dirname "${NETDAEMON__APPLICATION_ASSEMBLY}")" || echo -e "\\033[31mCould not change directory to run project\\033[0m" >&2 + echo -e "\\033[32mRunning the pre-built NetDaemon at ${Netdaemon__ApplicationAssembly}...\\033[0m" >&2 + cd "$(dirname "${Netdaemon__ApplicationAssembly}")" || echo -e "\\033[31mCould not change directory to run project\\033[0m" >&2 - if [[ "${PWD}" != "$(dirname "${NETDAEMON__APPLICATION_ASSEMBLY}")" ]]; then + if [[ "${PWD}" != "$(dirname "${Netdaemon__ApplicationAssembly}")" ]]; then echo -e "\\033[31mCould not change directory to run custom project\\033[0m" >&2 exit 1 fi - runme="$(basename "${NETDAEMON__APPLICATION_ASSEMBLY}")" + runme="$(basename "${Netdaemon__ApplicationAssembly}")" exec dotnet $runme fi diff --git a/Dockerfile.AddOn b/Dockerfile.AddOn index 02b8533ab..5033bb208 100644 --- a/Dockerfile.AddOn +++ b/Dockerfile.AddOn @@ -1,19 +1,4 @@ # No admin support yet, we need to build the websocket API - -# # Build the NetDaemon Admin with build container -# FROM ludeeus/container:frontend as builder - -# RUN \ -# apk add make \ -# \ -# && git clone https://github.com/net-daemon/admin.git /admin \ -# && cd /admin \ -# && git checkout tags/1.3.5 \ -# && make deploy \ -# \ -# && rm -fr /var/lib/apt/lists/* \ -# && rm -fr /tmp/* /var/{cache,log}/* - # Pre-build .NET NetDaemon core project FROM mcr.microsoft.com/dotnet/sdk:6.0.100-bullseye-slim-amd64 as netbuilder ARG TARGETPLATFORM @@ -25,13 +10,12 @@ RUN echo "building for $TARGETPLATFORM" RUN export TARGETPLATFORM=$TARGETPLATFORM # Copy the source to docker container COPY ./src /usr/src -RUN dotnet publish /usr/src/Host/NetDaemon.Host.AddOn/NetDaemon.Host.AddOn.csproj -o "/daemon" +RUN dotnet publish /usr/src/Host/NetDaemon.Host.Default/NetDaemon.Host.Default.csproj -o "/daemon" # Final stage, create the runtime container FROM ghcr.io/net-daemon/netdaemon_addonbase # # Install S6 and the Admin site -# COPY ./Docker/rootfs/etc/services.d/NetDaemonAdmin /etc/services.d/NetDaemonAdmin COPY ./Docker/rootfs/etc/services.d/NetDaemonAddOnApp /etc/services.d/NetDaemonAddOnApp # COPY admin diff --git a/NetDaemon.sln b/NetDaemon.sln index 5d9b9fc64..e6d6d3504 100644 --- a/NetDaemon.sln +++ b/NetDaemon.sln @@ -51,8 +51,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Debug", "Debug", "{E15D4280 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DebugHost", "dev\DebugHost\DebugHost.csproj", "{898966EA-F814-4B7B-9A3D-5E78C38174B2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.Host.AddOn", "src\Host\NetDaemon.Host.AddOn\NetDaemon.Host.AddOn.csproj", "{55F0B65B-BA59-4E90-963E-7D661374511D}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.Extensions.Logging", "src\Extensions\NetDaemon.Extensions.Logging\NetDaemon.Extensions.Logging.csproj", "{00333EBA-DB52-4D56-ADF7-940FB533E530}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NetDaemon.Extensions.Tts", "src\Extensions\NetDaemon.Extensions.Tts\NetDaemon.Extensions.Tts.csproj", "{F4B29B77-9B92-4037-A884-288CA5EF0B78}" @@ -247,18 +245,6 @@ Global {898966EA-F814-4B7B-9A3D-5E78C38174B2}.Release|x64.Build.0 = Release|Any CPU {898966EA-F814-4B7B-9A3D-5E78C38174B2}.Release|x86.ActiveCfg = Release|Any CPU {898966EA-F814-4B7B-9A3D-5E78C38174B2}.Release|x86.Build.0 = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|x64.ActiveCfg = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|x64.Build.0 = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|x86.ActiveCfg = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Debug|x86.Build.0 = Debug|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|Any CPU.Build.0 = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|x64.ActiveCfg = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|x64.Build.0 = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|x86.ActiveCfg = Release|Any CPU - {55F0B65B-BA59-4E90-963E-7D661374511D}.Release|x86.Build.0 = Release|Any CPU {00333EBA-DB52-4D56-ADF7-940FB533E530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {00333EBA-DB52-4D56-ADF7-940FB533E530}.Debug|Any CPU.Build.0 = Debug|Any CPU {00333EBA-DB52-4D56-ADF7-940FB533E530}.Debug|x64.ActiveCfg = Debug|Any CPU @@ -304,7 +290,6 @@ Global {DA86C13F-E31D-412E-94E6-B59F4AC0465D} = {A6D03AB9-6C8E-42BE-ACF6-7FA6A1C539C2} {966C5143-7667-4E85-B7E3-336F7C28549F} = {8F8846B8-BD99-4855-96EA-392E012C4421} {898966EA-F814-4B7B-9A3D-5E78C38174B2} = {E15D4280-7FFC-4F8B-9B8C-CF9AF2BF838C} - {55F0B65B-BA59-4E90-963E-7D661374511D} = {A6D03AB9-6C8E-42BE-ACF6-7FA6A1C539C2} {00333EBA-DB52-4D56-ADF7-940FB533E530} = {DFF3E7AA-7A50-4A1E-B3F8-EC01531FB83D} {F4B29B77-9B92-4037-A884-288CA5EF0B78} = {DFF3E7AA-7A50-4A1E-B3F8-EC01531FB83D} EndGlobalSection diff --git a/src/Host/NetDaemon.Host.AddOn/Internal/Config/PluginConfig.cs b/src/Host/NetDaemon.Host.AddOn/Internal/Config/PluginConfig.cs deleted file mode 100644 index 289316bfe..000000000 --- a/src/Host/NetDaemon.Host.AddOn/Internal/Config/PluginConfig.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; -using System.Diagnostics; -using System.IO; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace NetDaemon.Host.AddOn.Internal.Config; - -internal record AddOnConfig -{ - [JsonPropertyName("log_level")] public string LogLevel { get; set; } = "information"; - - [JsonPropertyName("app_config_path")] public string ApplicationConfigFolderPath { get; set; } = string.Empty; -} - -internal static class ConfigManager -{ - private const string AddOnConfigPath = "/data/options.json"; - - public static AddOnConfig Get() - { - if (!Debugger.IsAttached) - return JsonSerializer.Deserialize(File.ReadAllBytes(AddOnConfigPath)) - ?? throw new InvalidOperationException("Failed to read addon config"); - - // If we are in a debugging session we are obviously not in an add-on - // in this case we fake the data so we will be able to debug addon host - return new AddOnConfig {LogLevel = "trace", ApplicationConfigFolderPath = "./"}; - } -} \ No newline at end of file diff --git a/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/HostBuilderExtension.cs b/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/HostBuilderExtension.cs deleted file mode 100644 index c0910cd8d..000000000 --- a/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/HostBuilderExtension.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System; -using System.IO; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using NetDaemon.AppModel; -using NetDaemon.Extensions.Logging; -using NetDaemon.Extensions.Scheduler; -using NetDaemon.Extensions.Tts; -using NetDaemon.Host.AddOn.Internal.Config; -using NetDaemon.Host.AddOn.Internal.Helpers; - -namespace NetDaemon.Runtime.Internal.Extensions; - -public static class HostBuilderExtensions -{ - public static IHostBuilder UseNetDaemonAddon(this IHostBuilder hostBuilder) - { - ArgumentNullException.ThrowIfNull(hostBuilder); - - return hostBuilder - .UseNetDaemonAddOnSettings() - .UseNetDaemonDefaultLogging() - .UseNetDaemonRuntime() - .UseNetDaemonTextToSpeech() - .ConfigureServices((_, services) => - { - services - .AddAppsFromSource() - .AddNetDaemonScheduler() - .AddNetDaemonStateManager(); - }); - } - - private static IHostBuilder UseNetDaemonAddOnSettings(this IHostBuilder hostBuilder) - { - var logLevel = LogLevel.Information; - - return hostBuilder - .ConfigureServices((_, services) => { services.AddNetDaemonAddOnConfiguration(); }) - .ConfigureAppConfiguration((_, config) => - { - config.SetBasePath(Directory.GetCurrentDirectory()); - config.AddJsonFile("appsettings.json"); - - var addOnConfig = ConfigManager.Get(); - - logLevel = LogLevelParser.ParseLogLevelFromSetting(addOnConfig.LogLevel); - - config.AddYamlAppConfig( - addOnConfig.ApplicationConfigFolderPath); - }) - .ConfigureLogging((_, builder) => builder.SetMinimumLevel(logLevel)); - } -} \ No newline at end of file diff --git a/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/ServiceCollectionExtensions.cs b/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/ServiceCollectionExtensions.cs deleted file mode 100644 index e005aca72..000000000 --- a/src/Host/NetDaemon.Host.AddOn/Internal/Extensions/ServiceCollectionExtensions.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.IO; -using Microsoft.Extensions.DependencyInjection; -using NetDaemon.AppModel; -using NetDaemon.Client.Common.Settings; -using NetDaemon.Host.AddOn.Internal.Config; - -namespace NetDaemon.Runtime.Internal.Extensions; - -public static class ServiceCollectionExtensions -{ - public static IServiceCollection AddNetDaemonAddOnConfiguration(this IServiceCollection services) - { - var config = ConfigManager.Get(); - var token = Environment.GetEnvironmentVariable("HASSIO_TOKEN") ?? - throw new InvalidOperationException( - "Expected HASSIO_TOKEN to be present in environment. Is this running as addon?"); - - var appFolderPath = Path.Combine("/config/netdaemon3", config.ApplicationConfigFolderPath); - services.Configure(n => - { - n.Host = "supervisor"; - - n.Port = 80; - n.Token = token; - n.Ssl = false; - n.WebsocketPath = "core/websocket"; - }); - - services.Configure(s => s.ApplicationConfigurationFolder = appFolderPath); - - return services; - } -} \ No newline at end of file diff --git a/src/Host/NetDaemon.Host.AddOn/Internal/Helpers/LogLevelParser.cs b/src/Host/NetDaemon.Host.AddOn/Internal/Helpers/LogLevelParser.cs deleted file mode 100644 index ec3e0d5f4..000000000 --- a/src/Host/NetDaemon.Host.AddOn/Internal/Helpers/LogLevelParser.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using Microsoft.Extensions.Logging; -using Serilog; - -namespace NetDaemon.Host.AddOn.Internal.Helpers; - -internal static class LogLevelParser -{ - public static LogLevel ParseLogLevelFromSetting(string? logLevelSetting) - { - logLevelSetting ??= "information"; - // try parsing the loglevel ignoring case since - // home assistant add-ons usually is configured with - // lower case only - return Enum.TryParse(logLevelSetting, true, out LogLevel logLevel) ? logLevel : LogLevel.Information; - } -} \ No newline at end of file diff --git a/src/Host/NetDaemon.Host.AddOn/NetDaemon.Host.AddOn.csproj b/src/Host/NetDaemon.Host.AddOn/NetDaemon.Host.AddOn.csproj deleted file mode 100644 index 8cd9513c4..000000000 --- a/src/Host/NetDaemon.Host.AddOn/NetDaemon.Host.AddOn.csproj +++ /dev/null @@ -1,38 +0,0 @@ - - - - Exe - net6.0 - 10.0 - enable - false - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - - - - - - Always - - - - ..\..\..\.linting\roslynator.ruleset - true - AllEnabledByDefault - - diff --git a/src/Host/NetDaemon.Host.AddOn/Program.cs b/src/Host/NetDaemon.Host.AddOn/Program.cs deleted file mode 100644 index 0d087db64..000000000 --- a/src/Host/NetDaemon.Host.AddOn/Program.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System; -using Microsoft.Extensions.Hosting; -using NetDaemon.Runtime.Internal.Extensions; - -#pragma warning disable CA1812 - -try -{ - await Host.CreateDefaultBuilder(args) - .UseNetDaemonAddon() - .Build() - .RunAsync() - .ConfigureAwait(false); -} -catch (Exception e) -{ - Console.WriteLine($"Failed to start host... {e}"); - throw; -} \ No newline at end of file diff --git a/src/Host/NetDaemon.Host.AddOn/appsettings.json b/src/Host/NetDaemon.Host.AddOn/appsettings.json deleted file mode 100644 index 3e8d8159b..000000000 --- a/src/Host/NetDaemon.Host.AddOn/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft": "Warning" - }, - "ConsoleThemeType": "Ansi" - } -} \ No newline at end of file From 29c4e598f22587daabcc588623c6eb0ef7c6b259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Thu, 20 Jan 2022 01:06:36 +0100 Subject: [PATCH 2/4] fix casing --- Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run index dc880b0fc..794554969 100644 --- a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run +++ b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run @@ -2,15 +2,16 @@ echo "Starting NetDaemon Runner for add-on" # Set configuration values to environment variables -export Netdaemon__ApplicationAssembly=$(bashio::config 'app_assembly') +export NetDaemon__ApplicationAssembly=$(bashio::config 'app_assembly') export Logging__Loglevel__Default=$(bashio::config 'log_level') export NetDaemon__ApplicationConfigurationFolder=$(bashio::config 'app_config_folder') -export Homeassistant__TOKEN=$SUPERVISOR_TOKEN -export Homeassistant__HOST=supervisor +export Homeassistant__Token=$SUPERVISOR_TOKEN +export Homeassistant__Host="supervisor" export Homeassistant__Ssl=false export Homeassistant__WebsocketPath="core/websocket" export Homeassistant__Port=80 + declare daemondir="/daemon" declare is_custom_app_source=false From 6f1b4652f8a3118bc94b106a112c55a3791584b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Thu, 20 Jan 2022 14:05:29 +0100 Subject: [PATCH 3/4] Set needed environment using new env style --- Dockerfile | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 59ab2b0b0..63382127a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,5 +36,9 @@ COPY ./Docker/rootfs/etc/services.d/NetDaemonApp /etc/services.d/NetDaemonApp # COPY --from=builder /admin /admin COPY --from=netbuilder /daemon /daemon -ENV NETDAEMON__APPLICATION_CONFIGURATION_FOLDER=/data +ENV \ + HomeAssistant__Host=localhost \ + HomeAssistant__Port=8123 \ + HomeAssistant__Token=NOT_SET \ + NetDaemon__ApplicationConfigurationFolder=/data From cea0d8a0732d223822368debd6b2dc9a37540cc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomas=20Hellstr=C3=B6m?= Date: Thu, 20 Jan 2022 14:05:42 +0100 Subject: [PATCH 4/4] Fixed run script after review --- .../etc/services.d/NetDaemonAddOnApp/run | 75 +++++++------------ 1 file changed, 27 insertions(+), 48 deletions(-) diff --git a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run index 794554969..2f7f11593 100644 --- a/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run +++ b/Docker/rootfs/etc/services.d/NetDaemonAddOnApp/run @@ -1,51 +1,41 @@ #!/usr/bin/with-contenv /usr/bin/bashio -echo "Starting NetDaemon Runner for add-on" # Set configuration values to environment variables +export HomeAssistant__Host="supervisor" +export HomeAssistant__WebsocketPath="core/websocket" +export HomeAssistant__Port=80 +export HomeAssistant__Token=$SUPERVISOR_TOKEN + export NetDaemon__ApplicationAssembly=$(bashio::config 'app_assembly') export Logging__Loglevel__Default=$(bashio::config 'log_level') export NetDaemon__ApplicationConfigurationFolder=$(bashio::config 'app_config_folder') -export Homeassistant__Token=$SUPERVISOR_TOKEN -export Homeassistant__Host="supervisor" -export Homeassistant__Ssl=false -export Homeassistant__WebsocketPath="core/websocket" -export Homeassistant__Port=80 - declare daemondir="/daemon" -declare is_custom_app_source=false if [ ! -d "/data" ]; then - echo -e "\\033[31mMissing mapping to apps, please map '/data' to your apps folder\\033[0m" >&2 - exit 1 + bashio::exit.nok "Missing mapping to apps, please map '/data' to your apps folder" fi if [ ! -f "/data/options.json" ]; then - echo -e "\\033[31mThis container only supports running as Home Assistant add-on!\\033[0m" >&2 - exit 1 -fi - -if [[ "${Netdaemon__ApplicationAssembly}" == *.csproj ]]; -then - echo -e "\\033[31mcsproj deployments are not supported in v3, use compiled option instead!\\033[0m" >&2 - exit 1 + bashio::exit.nok "This container only supports running as Home Assistant add-on!" fi -if [[ "${Netdaemon__ApplicationAssembly}" == *.csproj ]]; +if ! bashio::config.has_value "app_assembly" then - echo -e "\\033[31mcsproj deployments are not supported in v3, use compiled option instead!\\033[0m" >&2 - exit 1 -fi - -if [[ ! -z "${Netdaemon__ApplicationAssembly}" ]] && [[ "${Netdaemon__ApplicationAssembly}" != *".dll" ]]; -then - echo -e "\\033[31mAssembly needs to point to a .dll file!\\033[0m" >&2 - exit 1 -fi + bashio::log.info "Starting NetDaemon V3 Runtime ..." + cd "${daemondir}" + exec dotnet NetDaemon.Host.Default.dll +else + # We have provided an application assembly setting + if [[ "${Netdaemon__ApplicationAssembly}" == *.csproj ]]; + then + bashio::exit.nok "csproj deployments are not supported in v3, use compiled option instead!" + fi -if [[ "${Netdaemon__ApplicationAssembly}" == *.dll ]]; -then - is_custom_app_source=true + if [[ "${Netdaemon__ApplicationAssembly}" != *".dll" ]]; + then + bashio::exit.nok "Assembly needs to point to a .dll file!" + fi # make path relative to data folder (/config/netdaemon if addon) # if the path is a relative path @@ -55,28 +45,17 @@ then fi # The provided application source is ether a project or pre-compiled .Net application - if [ ! -f ${Netdaemon__ApplicationAssembly} ]; + if ! bashio::fs.file_exists "${Netdaemon__ApplicationAssembly}"; then - - echo -e "\\033[31mThe assembly ${Netdaemon__ApplicationAssembly} cannot be found. Please check the settings.\\033[0m" >&2 - exit 1 + bashio::exit.nok "The assembly ${Netdaemon__ApplicationAssembly} cannot be found. Please check the settings." fi -fi - -if [[ $is_custom_app_source == false ]]; then - echo -e "\\033[32mRunning NetDaemon at ${daemondir}...\\033[0m" >&2 - cd "${daemondir}" - exec dotnet NetDaemon.Host.Default.dll -else - # This is a pre-built deamon - echo -e "\\033[32mRunning the pre-built NetDaemon at ${Netdaemon__ApplicationAssembly}...\\033[0m" >&2 - cd "$(dirname "${Netdaemon__ApplicationAssembly}")" || echo -e "\\033[31mCould not change directory to run project\\033[0m" >&2 + + bashio::log.info "Starting NetDaemon V3 pre-built Runtime using assembly ${Netdaemon__ApplicationAssembly}..." + cd "$(dirname "${Netdaemon__ApplicationAssembly}")" || bashio::exit.nok "Could not change directory to run project" if [[ "${PWD}" != "$(dirname "${Netdaemon__ApplicationAssembly}")" ]]; then - echo -e "\\033[31mCould not change directory to run custom project\\033[0m" >&2 - exit 1 + bashio::exit.nok "Could not change directory to run custom project" fi runme="$(basename "${Netdaemon__ApplicationAssembly}")" exec dotnet $runme fi -