Upgrade to .Net Core 2.0.0 and improve regex

This commit is contained in:
Alexander Hyett 2017-09-26 22:09:31 +01:00
parent f81547016b
commit 579794caa7
8 changed files with 50 additions and 18 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
**/bin
**/obj

7
ConsoleSettings.cs Normal file
View file

@ -0,0 +1,7 @@
namespace DotNetCron
{
public class ConsoleSettings
{
public string OutputString { get; set; }
}
}

View file

@ -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

View file

@ -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}'");
}
}
}

5
appsettings.json Normal file
View file

@ -0,0 +1,5 @@
{
"Console": {
"OutputString": "Test_String"
}
}

View file

@ -5,4 +5,4 @@ services:
build: .
container_name: recurring-console
environment:
- TEST_ENV=TestValue3
- CRON_Console:OutputString=NewTestString

View file

@ -2,7 +2,15 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0"/>
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.0.0"/>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0"/>
<Content Include="appsettings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
</Project>

View file

@ -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