1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-30 15:42:48 -05:00

run jobs crons on API image

This commit is contained in:
Kyle Spearrin
2017-08-17 16:45:27 -04:00
parent d15917d3c9
commit fd35ac45b2
9 changed files with 81 additions and 31 deletions

View File

@ -19,9 +19,19 @@ done
# Custom
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
cron \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
EXPOSE 80
COPY obj/Docker/publish .
COPY obj/Docker/publish/Api .
COPY obj/Docker/publish/Jobs /jobs
RUN mv /jobs/crontab /etc/cron.d/bitwarden-cron \
&& chmod 0644 /etc/cron.d/bitwarden-cron \
&& touch /var/log/cron.log
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh

View File

@ -4,7 +4,8 @@ echo "`n# Building API"
echo "`nBuilding app"
echo ".NET Core version $(dotnet --version)"
dotnet publish $dir\Api.csproj -f netcoreapp2.0 -c "Release" -o $dir\obj\Docker\publish
dotnet publish $dir\Api.csproj -f netcoreapp2.0 -c "Release" -o $dir\obj\Docker\publish\Api
dotnet publish $dir\..\Jobs\Jobs.csproj -f netcoreapp2.0 -c "Release" -o $dir\obj\Docker\publish\Jobs
echo "`nBuilding docker image"
docker --version

View File

@ -1,3 +1,5 @@
#!/bin/sh
env >> /etc/environment
cron
dotnet /app/Api.dll

View File

@ -8,6 +8,12 @@
<UserSecretsId>bitwarden-Jobs</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Content Include="crontab">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.2" />

View File

@ -1,6 +1,9 @@
using Bit.Core.Services;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
@ -9,28 +12,31 @@ namespace Bit.Jobs
public class Program
{
private static ILicensingService _licensingService;
private static ILogger<Program> _logger;
public static void Main(string[] args)
{
var parameters = ParseParameters(args);
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseContentRoot(parameters.ContainsKey("d") ? parameters["d"] : Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.UseServer(new NoopServer())
.Build();
_logger = host.Services.GetRequiredService<ILogger<Program>>();
_licensingService = host.Services.GetRequiredService<ILicensingService>();
MainAsync(args).Wait();
MainAsync(parameters).Wait();
}
private async static Task MainAsync(string[] args)
private async static Task MainAsync(IDictionary<string, string> parameters)
{
if(args.Length == 0)
if(!parameters.ContainsKey("j"))
{
return;
}
switch(args[0])
switch(parameters["j"])
{
case "validate-licenses":
await _licensingService.ValidateOrganizationsAsync();
@ -38,9 +44,28 @@ namespace Bit.Jobs
case "refresh-licenses":
// TODO
break;
case "hello":
_logger.LogInformation("Hello World!");
break;
default:
break;
}
}
private static IDictionary<string, string> ParseParameters(string[] args)
{
var dict = new Dictionary<string, string>();
for(var i = 0; i < args.Length; i = i + 2)
{
if(!args[i].StartsWith("-"))
{
continue;
}
dict.Add(args[i].Substring(1), args[i + 1]);
}
return dict;
}
}
}

View File

@ -6,6 +6,7 @@ using Bit.Core;
using Bit.Core.Utilities;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder;
using Serilog.Events;
namespace Bit.Jobs
{
@ -55,7 +56,10 @@ namespace Bit.Jobs
GlobalSettings globalSettings)
{
loggerFactory
.AddSerilog(env, appLifetime, globalSettings)
.AddSerilog(env, appLifetime, globalSettings, (e) =>
{
return e.Level >= LogEventLevel.Information;
})
.AddConsole()
.AddDebug();
}

2
src/Jobs/crontab Normal file
View File

@ -0,0 +1,2 @@
* * * * * root dotnet /jobs/Jobs.dll -d /jobs -j hello >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.