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 ARG BUILDCONFIG=RELEASE
@ -11,21 +11,21 @@ COPY . .
RUN dotnet publish -c $BUILDCONFIG -o out RUN dotnet publish -c $BUILDCONFIG -o out
# build runtime image # build runtime image
FROM microsoft/dotnet:1.1.1-runtime FROM microsoft/dotnet:2.0.0-runtime
WORKDIR /app WORKDIR /app
COPY --from=build /build/out ./ 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 # Install Cron
RUN apt-get update -qq && apt-get -y install cron -qq --force-yes RUN apt-get update -qq && apt-get -y install cron -qq --force-yes
# Set up schedule # Add export environment variable script and schedule
ADD schedule /etc/cron.d/schedule COPY *.sh ./
RUN chmod 0644 /etc/cron.d/schedule 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 # Create log file
RUN touch /var/log/cron.log RUN touch /var/log/cron.log

View file

@ -1,13 +1,23 @@
using System; using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace test_dotnet namespace DotNetCron
{ {
class Program class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine($"This program should be running on a schedule: {DateTime.Now}"); var configuration = new ConfigurationBuilder()
Console.WriteLine($"TEST_ENV={Environment.GetEnvironmentVariable("TEST_ENV")}"); .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: . build: .
container_name: recurring-console container_name: recurring-console
environment: environment:
- TEST_ENV=TestValue3 - CRON_Console:OutputString=NewTestString

View file

@ -2,7 +2,15 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
</Project> <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 #!/bin/bash
echo '#!/bin/bash' > /app/set_env.sh 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 chmod +x /app/set_env.sh