diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..31db540 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +**/bin +**/obj \ No newline at end of file diff --git a/ConsoleSettings.cs b/ConsoleSettings.cs new file mode 100644 index 0000000..b1d5f2a --- /dev/null +++ b/ConsoleSettings.cs @@ -0,0 +1,7 @@ +namespace DotNetCron +{ + public class ConsoleSettings + { + public string OutputString { get; set; } + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 0604350..2d5e704 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM microsoft/dotnet:1.1.1-sdk AS build +FROM microsoft/dotnet:2.0.0-sdk AS build ARG BUILDCONFIG=RELEASE @@ -11,21 +11,21 @@ COPY . . RUN dotnet publish -c $BUILDCONFIG -o out # build runtime image -FROM microsoft/dotnet:1.1.1-runtime +FROM microsoft/dotnet:2.0.0-runtime WORKDIR /app COPY --from=build /build/out ./ -# Add export environment variable script -COPY export_env.sh . -COPY run_app.sh . -RUN chmod +x export_env.sh run_app.sh - # Install Cron RUN apt-get update -qq && apt-get -y install cron -qq --force-yes -# Set up schedule -ADD schedule /etc/cron.d/schedule -RUN chmod 0644 /etc/cron.d/schedule +# Add export environment variable script and schedule +COPY *.sh ./ +COPY schedule /etc/cron.d/schedule +RUN sed -i 's/\r//' export_env.sh \ + && sed -i 's/\r//' run_app.sh \ + && sed -i 's/\r//' /etc/cron.d/schedule \ + && chmod +x export_env.sh run_app.sh \ + && chmod 0644 /etc/cron.d/schedule # Create log file RUN touch /var/log/cron.log diff --git a/Program.cs b/Program.cs index bbfdc71..6b06c13 100644 --- a/Program.cs +++ b/Program.cs @@ -1,13 +1,23 @@ using System; +using System.IO; +using Microsoft.Extensions.Configuration; -namespace test_dotnet +namespace DotNetCron { class Program { static void Main(string[] args) { - Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}"); - Console.WriteLine($"TEST_ENV={Environment.GetEnvironmentVariable("TEST_ENV")}"); + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional:true, reloadOnChange:true) + //.AddEnvironmentVariables(prefix: "CRON_") + .Build(); + + var consoleSettings = new ConsoleSettings(); + configuration.GetSection("Console").Bind(consoleSettings); + + Console.WriteLine($"{DateTime.UtcNow}: Output String: '{consoleSettings.OutputString}'"); } } } diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..b263189 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,5 @@ +{ + "Console": { + "OutputString": "Test_String" + } +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 854507a..a83d9ca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,4 +5,4 @@ services: build: . container_name: recurring-console environment: - - TEST_ENV=TestValue3 \ No newline at end of file + - CRON_Console:OutputString=NewTestString \ No newline at end of file diff --git a/dotnet-cron.csproj b/dotnet-cron.csproj index abb9969..7b29e7f 100644 --- a/dotnet-cron.csproj +++ b/dotnet-cron.csproj @@ -2,7 +2,15 @@ Exe - netcoreapp1.1 + netcoreapp2.0 - - + + + + + + + PreserveNewest + + + \ No newline at end of file diff --git a/export_env.sh b/export_env.sh index 0371539..e508822 100644 --- a/export_env.sh +++ b/export_env.sh @@ -1,5 +1,5 @@ #!/bin/bash echo '#!/bin/bash' > /app/set_env.sh -printenv | sed '/^affinity:container/ d' | sed -r 's/^([a-zA-Z_]+[a-zA-Z0-9_:-]*)=(.*)$/export \1="\2"/g' >> /app/set_env.sh +printenv | sed '/^affinity:container/ d' | sed -r ':b; s/^([^=]*):/\1__/g; tb;' | sed -r 's/^([a-zA-Z_]+[a-zA-Z0-9_-]*)=(.*)$/export \1="\2"/g' >> /app/set_env.sh chmod +x /app/set_env.sh \ No newline at end of file