Skip to content

Commit

Permalink
refactor of run time configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
helto4real committed Dec 19, 2020
1 parent d8c9bb4 commit 4ac8877
Show file tree
Hide file tree
Showing 22 changed files with 157 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ ENV DEBIAN_FRONTEND=noninteractive
ENV TEST=AK

# We do not load local assemblies
ENV HASS_DISABLE_LOCAL_ASM=true
ENV NETDAEMON__APPSOURCE=/workspaces/netdaemon/exampleapps
29 changes: 0 additions & 29 deletions Docker/config.json

This file was deleted.

71 changes: 61 additions & 10 deletions Docker/rootfs/etc/services.d/NetDaemonApp/run
Original file line number Diff line number Diff line change
@@ -1,26 +1,77 @@
#!/usr/bin/with-contenv bash
echo "Starting NetDaemon Runner"

declare runtype="Service"
declare runtype="Service.dll"
declare daemondir="/daemon"
declare custom_daemondir="/custom_daemon"
declare is_custom_app_source=false
declare is_project=false

if [ ! -d "/data" ]; then
echo -e "\\033[31mMissing mapping to apps, please map '/data' to your apps folder\\033[0m" >&2
exit 1
fi

if [ -z "${NETDAEMON__PROJECTFOLDER}" ]; then
if [[ "${NETDAEMON__APPSOURCE}" == *.csproj ]] || [[ "${NETDAEMON__APPSOURCE}" == *.dll ]];
then
# make path relative /data if not hardcode
if [[ "${NETDAEMON__APPSOURCE}" != /* ]];
then
export NETDAEMON__APPSOURCE="/data/${NETDAEMON__APPSOURCE}"
fi

# The provided application source is ether a project or pre-compiled .Net application
if [ ! -f ${NETDAEMON__APPSOURCE} ] && [ ! -d ${NETDAEMON__APPSOURCE} ];
then

echo -e "\\033[31mThe executable or project ${NETDAEMON__APPSOURCE} cannot be found. Please check the settings.\\033[0m" >&2
exit 1
fi

if [[ "${NETDAEMON__APPSOURCE}" == *.csproj ]];
then
is_project=true
fi

if [ -z "${NETDAEMON__WARN_IF_CUSTOM_APP_SOURCE}" ] || [[ "${NETDAEMON__WARN_IF_CUSTOM_APP_SOURCE}" != true ]];
then
echo -e "\\033[33mWarning: you are using a custom daemon, this can potentially be unsecure. Please review your security." \
"To remove this warning, set NETDAEMON__WARN_IF_CUSTOM_APP_SOURCE=false \033[0m" >&2
fi
is_custom_app_source=true
fi

if [[ $is_custom_app_source == false ]]; then
echo -e "\\033[32mRunning pre-compiled NetDaemon...\\033[0m" >&2
# This is a hack that makes the current behavor backwards compatible
# if there is a "apps" folder use that or any kind of project
# structure will mess it up
dir "/data"
if [ -d "/data/apps" ];
then
export NETDAEMON__APPSOURCE="/data/apps"
echo -e "\\033[32mFound apps folder, using ${NETDAEMON__APPSOURCE}...\\033[0m" >&2
fi

cd "${daemondir}"
exec "./${runtype}"
else
echo -e "\\033[32mRun the custom project provided...\\033[0m" >&2
cd "${NETDAEMON__PROJECTFOLDER}" || echo -e "\\033[31mCould not change directory to run project\\033[0m" >&2
dotnet Service.dll
else
# We allow for custom projects and solutions to be used
echo -e "\\033[32mRun the custom daemon at ${NETDAEMON__APPSOURCE}..\\033[0m" >&2
cd "$(dirname "${NETDAEMON__APPSOURCE}")" || echo -e "\\033[31mCould not change directory to run project\\033[0m" >&2

if [[ "${PWD}" != "${NETDAEMON__PROJECTFOLDER}" ]]; then
if [[ "${PWD}" != "$(dirname "${NETDAEMON__APPSOURCE}")" ]]; then
echo -e "\\033[31mCould not change directory to run custom project\\033[0m" >&2
exit 1
fi

dotnet run -c Release

if [[ $is_project == true ]];
then
echo -e "\\033[32mPlease wait while restore, compile and run custom project...\\033[0m" >&2
dotnet run -v m -c Release -p "$(basename "${NETDAEMON__APPSOURCE}")"
else
echo -e "\\033[32m Running custom pre-compiled daemon...\\033[0m" >&2

dotnet "$(basename "${NETDAEMON__APPSOURCE}")"
fi
fi

26 changes: 12 additions & 14 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ RUN echo "building for $TARGETPLATFORM"
RUN export TARGETPLATFORM=$TARGETPLATFORM
# Copy the source to docker container
COPY ./src /usr/src
COPY ./Docker/build_dotnet.sh /build.sh
RUN chmod 700 /build.sh

# Run build script for all platforms since dotnet is not QEMU compatible
RUN /build.sh
RUN dotnet publish /usr/src/Service/Service.csproj -o "/daemon"

# Final stage, create the runtime container
FROM mcr.microsoft.com/dotnet/sdk:5.0.100

# Install S6 and the Admin site
RUN apt update && apt install -y \
nodejs \
yarn \
make

COPY ./Docker/rootfs/etc /etc
COPY ./Docker/s6.sh /s6.sh

Expand All @@ -42,23 +44,19 @@ RUN /s6.sh
# COPY admin
COPY --from=builder /admin /admin
COPY --from=netbuilder /daemon /daemon
# Install S6 and the Admin site
RUN apt update && apt install -y \
nodejs \
yarn \
make

# NETDAEMON__WARN_IF_CUSTOM_APP_SOURCE=

# Set default values of NetDaemon env
ENV \
DOTNET_NOLOGO=true \
DOTNET_CLI_TELEMETRY_OPTOUT=true \
HASSCLIENT_MSGLOGLEVEL=Default \
HOMEASSISTANT__HOST=localhost \
HOMEASSISTANT__PORT=8123 \
HOMEASSISTANT__TOKEN=NOT_SET \
HASSCLIENT_MSGLOGLEVEL=Default \
NETDAEMON__SOURCEFOLDER=/data \
NETDAEMON__APPSOURCE=/data \
NETDAEMON__ADMIN=true \
ASPNETCORE_URLS=http://+:5000 \
HASS_DISABLE_LOCAL_ASM=true
ASPNETCORE_URLS=http://+:5000

ENTRYPOINT ["/init"]
1 change: 0 additions & 1 deletion exampleapps/apps/secrets.yaml

This file was deleted.

4 changes: 2 additions & 2 deletions exampleapps/apps/test2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using System;
using System.Reactive.Linq;
using System.Collections.Generic;
using Netdaemon.Generated.Reactive;
using NetDaemon.Common.Reactive;
using NetDaemon.Common;

namespace NStest
{

/// <summary> cool multiple lines </summary>
public class BatteryManager : GeneratedAppBase //NetDaemonRxApp
public class BatteryManager : NetDaemonRxApp
// public class BatteryManager : NetDaemonRxApp
{
// private ISchedulerResult _schedulerResult;
Expand Down
26 changes: 0 additions & 26 deletions exampleapps/apps/test_secret.cs

This file was deleted.

5 changes: 0 additions & 5 deletions exampleapps/apps/test_secret.yaml

This file was deleted.

29 changes: 25 additions & 4 deletions src/App/NetDaemon.App/Common/Configuration/NetDaemonSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace NetDaemon.Common.Configuration
using System;
using System.IO;

namespace NetDaemon.Common.Configuration
{
/// <summary>
/// Settings related to NetDaemon instance
Expand All @@ -16,10 +19,28 @@ public class NetDaemonSettings
/// <summary>
/// Where the apps are found
/// </summary>
public string? SourceFolder { get; set; } = null;
/// <remarks>
/// Can be ether a folder where the apps is found or
/// point to a csproj file or a dll precompiled daemon.
/// In the case it is not a folder, NetDaemon expects
/// the apps to be in the file paths and tries to find
/// all apps recursivly
/// </remarks>
public string? AppSource { get; set; } = null;

/// <summary>
/// Points to non default csproj file
/// Returns the directory path of AppSource
/// </summary>
public string? ProjectFolder { get; set; } = string.Empty;
public string GetAppSourceDirectory()
{
var source = AppSource?.Trim() ?? throw new NullReferenceException("AppSource cannot be null!");

if (source.EndsWith(".csproj") || source.EndsWith(".dll"))
{
source = Path.GetDirectoryName(source);
}

return source ?? throw new NullReferenceException("Source cannot be null!");
}
}
}
2 changes: 1 addition & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/CodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IEnumerable<INetDaemonAppBase> InstanceDaemonApps()

if (!allConfigFilePaths.Any())
{
_logger.LogWarning("No yaml configuration files found, please add files to [netdaemonfolder]/apps");
_logger.LogWarning("No yaml configuration files found, please add yaml configuration to insance apps!");
return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Daemon/NetDaemon.Daemon/Daemon/Config/YamlConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class YamlConfig : IYamlConfig

public YamlConfig(IOptions<NetDaemonSettings> netDaemonSettings)
{
_configFolder = netDaemonSettings.Value.SourceFolder!;
_configFolder = netDaemonSettings.Value.GetAppSourceDirectory();
_secrets = GetAllSecretsFromPath(_configFolder);
}

Expand Down
14 changes: 8 additions & 6 deletions src/DaemonRunner/DaemonRunner/NetDaemonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ private static void RegisterNetDaemonAssembly(IServiceCollection services)
/// </summary>
private static bool BypassLocalAssemblyLoading()
{
var value = Environment.GetEnvironmentVariable("HASS_DISABLE_LOCAL_ASM");
if (bool.TryParse(value, out var result))
{
return result;
}
return false;

var appSource = Environment.GetEnvironmentVariable("NETDAEMON__APPSOURCE") ??
throw new NullReferenceException("NETDAEMON__APPSOURCE cannot be null!");

if (appSource.EndsWith(".csproj") || appSource.EndsWith(".dll"))
return false;
else
return true;
}
}
}
5 changes: 4 additions & 1 deletion src/DaemonRunner/DaemonRunner/Service/ApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ public void ConfigureServices(IServiceCollection services)
// services.Configure<NetDaemonSettings>(context.Configuration.GetSection("NetDaemon"));
services.AddHostedService<RunnerService>();
services.AddTransient<IHassClient, HassClient>();
services.AddTransient<IDataRepository>(n => new DataRepository(Path.Combine(n.GetRequiredService<IOptions<NetDaemonSettings>>().Value.SourceFolder!, ".storage")));
services.AddTransient<IDataRepository>(n => new DataRepository(
Path.Combine(
n.GetRequiredService<IOptions<NetDaemonSettings>>().Value.GetAppSourceDirectory()
, ".storage")));
services.AddTransient<IHttpHandler, NetDaemon.Daemon.HttpHandler>();
services.AddSingleton<NetDaemonHost>();
services.AddHttpClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public class DaemonAppCompiler : IDaemonAppCompiler
private readonly ILogger<DaemonAppCompiler> _logger;
private readonly IOptions<NetDaemonSettings> _netDaemonSettings;

private string? _sourceFolder = null;
public DaemonAppCompiler(ILogger<DaemonAppCompiler> logger, IOptions<NetDaemonSettings> netDaemonSettings)
{
_logger = logger;
_netDaemonSettings = netDaemonSettings;
_sourceFolder = netDaemonSettings.Value.GetAppSourceDirectory();

}

public IEnumerable<Type> GetApps()
Expand All @@ -29,7 +32,7 @@ public IEnumerable<Type> GetApps()
var apps = assembly.GetTypesWhereSubclassOf<NetDaemonAppBase>();

if (!apps.Any())
_logger.LogWarning("No .cs files found, please add files to {sourceFolder}/apps", _netDaemonSettings.Value.SourceFolder);
_logger.LogWarning("No .cs files found, please add files to {sourceFolder}", _sourceFolder);
else
_logger.LogDebug("Found total of {nr_of_apps} apps", apps.Count());

Expand All @@ -39,8 +42,7 @@ public IEnumerable<Type> GetApps()
public Assembly Load()
{
CollectibleAssemblyLoadContext alc;
var appFolder = Path.Combine(_netDaemonSettings.Value.SourceFolder!, "apps");
return DaemonCompiler.GetCompiledAppAssembly(out alc, appFolder!, _logger);
return DaemonCompiler.GetCompiledAppAssembly(out alc, _sourceFolder!, _logger);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static (IEnumerable<Type>, CollectibleAssemblyLoadContext?) GetDaemonApps
else if (string.IsNullOrEmpty(compileErrorText) == false)
logger.LogError(compileErrorText);
else if (loadedApps.Count == 0)
logger.LogWarning("No .cs files files found, please add files to [netdaemonfolder]/apps");
logger.LogWarning("No .cs files files found, please add files to netdaemonfolder {codeFolder}", codeFolder);

return (loadedApps, alc);
}
Expand Down
Loading

0 comments on commit 4ac8877

Please sign in to comment.