1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-25 12:45:18 +01: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

@ -1,8 +1,8 @@
ASPNETCORE_ENVIRONMENT=Production ASPNETCORE_ENVIRONMENT=Production
globalSettings:selfHosted=true globalSettings__selfHosted=true
globalSettings:baseServiceUri:vault=http://localhost globalSettings__baseServiceUri__vault=http://localhost
globalSettings:baseServiceUri:api=http://localhost/api globalSettings__baseServiceUri__api=http://localhost/api
globalSettings:baseServiceUri:identity=http://localhost/identity globalSettings__baseServiceUri__identity=http://localhost/identity
globalSettings:baseServiceUri:internalIdentity=http://identity globalSettings__baseServiceUri__internalIdentity=http://identity
globalSettings:pushRelayBaseUri=https://push.bitwarden.com globalSettings__pushRelayBaseUri=https://push.bitwarden.com
globalSettings:installation:identityUri=https://identity.bitwarden.com globalSettings__installation__identityUri=https://identity.bitwarden.com

View File

@ -19,9 +19,19 @@ done
# Custom # Custom
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
cron \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app WORKDIR /app
EXPOSE 80 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 / COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh RUN chmod +x /entrypoint.sh

View File

@ -4,7 +4,8 @@ echo "`n# Building API"
echo "`nBuilding app" echo "`nBuilding app"
echo ".NET Core version $(dotnet --version)" 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" echo "`nBuilding docker image"
docker --version docker --version

View File

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

View File

@ -8,6 +8,12 @@
<UserSecretsId>bitwarden-Jobs</UserSecretsId> <UserSecretsId>bitwarden-Jobs</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Content Include="crontab">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" /> <PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" 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 Bit.Core.Services;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -9,28 +12,31 @@ namespace Bit.Jobs
public class Program public class Program
{ {
private static ILicensingService _licensingService; private static ILicensingService _licensingService;
private static ILogger<Program> _logger;
public static void Main(string[] args) public static void Main(string[] args)
{ {
var parameters = ParseParameters(args);
var host = new WebHostBuilder() var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory()) .UseContentRoot(parameters.ContainsKey("d") ? parameters["d"] : Directory.GetCurrentDirectory())
.UseStartup<Startup>() .UseStartup<Startup>()
.UseServer(new NoopServer()) .UseServer(new NoopServer())
.Build(); .Build();
_logger = host.Services.GetRequiredService<ILogger<Program>>();
_licensingService = host.Services.GetRequiredService<ILicensingService>(); _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; return;
} }
switch(args[0]) switch(parameters["j"])
{ {
case "validate-licenses": case "validate-licenses":
await _licensingService.ValidateOrganizationsAsync(); await _licensingService.ValidateOrganizationsAsync();
@ -38,9 +44,28 @@ namespace Bit.Jobs
case "refresh-licenses": case "refresh-licenses":
// TODO // TODO
break; break;
case "hello":
_logger.LogInformation("Hello World!");
break;
default: default:
break; 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 Bit.Core.Utilities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Serilog.Events;
namespace Bit.Jobs namespace Bit.Jobs
{ {
@ -55,7 +56,10 @@ namespace Bit.Jobs
GlobalSettings globalSettings) GlobalSettings globalSettings)
{ {
loggerFactory loggerFactory
.AddSerilog(env, appLifetime, globalSettings) .AddSerilog(env, appLifetime, globalSettings, (e) =>
{
return e.Level >= LogEventLevel.Information;
})
.AddConsole() .AddConsole()
.AddDebug(); .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.

View File

@ -261,26 +261,26 @@ server {{
using(var sw = File.CreateText("/bitwarden/docker/global.override.env")) using(var sw = File.CreateText("/bitwarden/docker/global.override.env"))
{ {
sw.Write($@"globalSettings:baseServiceUri:vault={_url} sw.Write($@"globalSettings__baseServiceUri__vault={_url}
globalSettings:baseServiceUri:api={_url}/api globalSettings__baseServiceUri__api={_url}/api
globalSettings:baseServiceUri:identity={_url}/identity globalSettings__baseServiceUri__identity={_url}/identity
globalSettings:sqlServer:connectionString={dbConnectionString} globalSettings__sqlServer__connectionString=""{dbConnectionString}""
globalSettings:identityServer:certificatePassword={_identityCertPassword} globalSettings__identityServer__certificatePassword={_identityCertPassword}
globalSettings:attachment:baseDirectory={_outputDir}/core/attachments globalSettings__attachment__baseDirectory={_outputDir}/core/attachments
globalSettings:attachment:baseUrl={_url}/attachments globalSettings__attachment__baseUrl={_url}/attachments
globalSettings:dataProtection:directory={_outputDir}/core/aspnet-dataprotection globalSettings__dataProtection__directory={_outputDir}/core/aspnet-dataprotection
globalSettings:logDirectory={_outputDir}/core/logs globalSettings__logDirectory={_outputDir}/core/logs
globalSettings:licenseDirectory={_outputDir}/core/licenses globalSettings__licenseDirectory={_outputDir}/core/licenses
globalSettings:duo:aKey={Helpers.SecureRandomString(64, alpha: true, numeric: true)} globalSettings__duo__aKey={Helpers.SecureRandomString(64, alpha: true, numeric: true)}
globalSettings:installation:id={_installationId} globalSettings__installation__id={_installationId}
globalSettings:installation:key={_installationKey} globalSettings__installation__key={_installationKey}
globalSettings:yubico:clientId=REPLACE globalSettings__yubico__clientId=REPLACE
globalSettings:yubico:key=REPLACE"); globalSettings__yubico__key=REPLACE");
if(!_push) if(!_push)
{ {
sw.Write(@" sw.Write(@"
globalSettings:pushRelayBaseUri=REPLACE"); globalSettings__pushRelayBaseUri=REPLACE");
} }
} }