1
0
mirror of https://github.com/bitwarden/server.git synced 2025-02-22 02:51:33 +01:00

build docker compose and allow custom ports

This commit is contained in:
Kyle Spearrin 2017-11-06 17:28:02 -05:00
parent 0fdb9b3d2f
commit 49bd3bcf35
3 changed files with 216 additions and 16 deletions

View File

@ -0,0 +1,170 @@
using System;
using System.IO;
namespace Bit.Setup
{
public class DockerComposeBuilder
{
private const string CoreVersion = "1.13.1";
private const string WebVersion = "1.19.0";
public DockerComposeBuilder(string os)
{
MssqlDataDockerVolume = os == "mac";
}
public bool MssqlDataDockerVolume { get; private set; }
public int HttpPort { get; private set; } = 80;
public int HttpsPort { get; private set; } = 443;
public void BuildForInstaller(int httpPort, int httpsPort)
{
if(httpPort != default(int) && httpsPort != default(int))
{
HttpPort = httpPort;
HttpsPort = httpsPort;
}
Build();
}
public void BuildForUpdater()
{
if(File.Exists("/bitwarden/docker/docker-compose.yml"))
{
var fileLines = File.ReadAllLines("/bitwarden/docker/docker-compose.yml");
foreach(var line in fileLines)
{
if(!line.StartsWith("# Parameter:"))
{
continue;
}
var paramParts = line.Split("=");
if(paramParts.Length < 2)
{
continue;
}
if(paramParts[0] == "# Parameter:HttpPort" && int.TryParse(paramParts[1], out int httpPort))
{
HttpPort = httpPort;
continue;
}
if(paramParts[0] == "# Parameter:HttpsPort" && int.TryParse(paramParts[1], out int httpsPort))
{
HttpsPort = httpsPort;
continue;
}
}
}
Build();
}
private void Build()
{
Console.WriteLine("Building docker-compose.yml.");
Directory.CreateDirectory("/bitwarden/docker/");
using(var sw = File.CreateText("/bitwarden/docker/docker-compose.yml"))
{
sw.Write($@"# https://docs.docker.com/compose/compose-file/
# Parameter:MssqlDataDockerVolume={MssqlDataDockerVolume}
# Parameter:HttpPort={HttpPort}
# Parameter:HttpsPort={HttpsPort}
# Parameter:CoreVersion={CoreVersion}
# Parameter:WebVersion={WebVersion}
version: '3'
services:
mssql:
image: bitwarden/mssql:{CoreVersion}
container_name: mssql
restart: always
volumes:");
if(MssqlDataDockerVolume)
{
sw.Write(@"
- mssql_data:/var/opt/mssql/data");
}
else
{
sw.Write(@"
- ../mssql/data:/var/opt/mssql/data");
}
sw.Write($@"
- ../mssql/backups:/etc/bitwarden/mssql/backups
env_file:
- mssql.env
- ../env/mssql.override.env
web:
image: bitwarden/web:{WebVersion}
container_name: web
restart: always
volumes:
- ../web:/etc/bitwarden/web
attachments:
image: bitwarden/attachments:{CoreVersion}
container_name: attachments
restart: always
volumes:
- ../core/attachments:/etc/bitwarden/core/attachments
api:
image: bitwarden/api:{CoreVersion}
container_name: api
restart: always
volumes:
- ../core:/etc/bitwarden/core
env_file:
- global.env
- ../env/global.override.env
identity:
image: bitwarden/identity:{CoreVersion}
container_name: identity
restart: always
volumes:
- ../identity:/etc/bitwarden/identity
- ../core:/etc/bitwarden/core
env_file:
- global.env
- ../env/global.override.env
icons:
image: bitwarden/icons:{CoreVersion}
container_name: icons
restart: always
nginx:
image: bitwarden/nginx:{CoreVersion}
container_name: nginx
restart: always
ports:
- '{HttpPort}:80'
- '{HttpsPort}:443'
volumes:
- ../nginx:/etc/bitwarden/nginx
- ../letsencrypt:/etc/letsencrypt
- ../ssl:/etc/ssl");
if(MssqlDataDockerVolume)
{
sw.Write(@"
volumes:
mssql_data:");
}
// New line at end of file.
sw.Write(@"
");
}
}
}
}

View File

@ -59,23 +59,17 @@ namespace Bit.Setup
public void BuildForUpdater()
{
Build();
}
public bool UpdateContext()
{
if(!File.Exists("/bitwarden/nginx/default.conf"))
if(File.Exists("/bitwarden/nginx/default.conf"))
{
return false;
var confContent = File.ReadAllText("/bitwarden/nginx/default.conf");
Ssl = confContent.Contains("listen 443 ssl http2;");
SelfSignedSsl = confContent.Contains("/etc/ssl/self/");
LetsEncrypt = !SelfSignedSsl && confContent.Contains("/etc/letsencrypt/live/");
DiffieHellman = confContent.Contains("/dhparam.pem;");
Trusted = confContent.Contains("ssl_trusted_certificate ");
}
var confContent = File.ReadAllText("/bitwarden/nginx/default.conf");
Ssl = confContent.Contains("listen 443 ssl http2;");
SelfSignedSsl = confContent.Contains("/etc/ssl/self/");
LetsEncrypt = !SelfSignedSsl && confContent.Contains("/etc/letsencrypt/live/");
DiffieHellman = confContent.Contains("/dhparam.pem;");
Trusted = confContent.Contains("ssl_trusted_certificate ");
return true;
Build();
}
private void Build()

View File

@ -14,11 +14,17 @@ namespace Bit.Setup
private static IDictionary<string, string> _parameters = null;
private static Guid? _installationId = null;
private static string _installationKey = null;
private static string _hostOs = "win";
public static void Main(string[] args)
{
_args = args;
_parameters = ParseParameters();
if(_parameters.ContainsKey("os"))
{
_hostOs = _parameters["os"];
}
if(_parameters.ContainsKey("install"))
{
Install();
@ -73,6 +79,30 @@ namespace Bit.Setup
var nginxBuilder = new NginxConfigBuilder(domain, ssl, selfSignedSsl, letsEncrypt);
nginxBuilder.BuildForInstaller();
Console.Write("(!) Do you want to use the default HTTP (80) and HTTPS (443) ports? (y/n): ");
var defaultPorts = Console.ReadLine().ToLowerInvariant() == "y";
int httpPort = default(int), httpsPort = default(int);
if(!defaultPorts)
{
Console.Write("(!) HTTP port: ");
if(int.TryParse(Console.ReadLine().ToLowerInvariant().Trim(), out httpPort))
{
Console.Write("(!) HTTPS port: ");
if(int.TryParse(Console.ReadLine().ToLowerInvariant().Trim(), out httpsPort))
{
url += (":" + httpsPort);
}
else
{
Console.WriteLine("Invalid HTTPS port.");
}
}
else
{
Console.WriteLine("Invalid HTTP port.");
}
}
Console.Write("(!) Do you want to use push notifications? (y/n): ");
var push = Console.ReadLine().ToLowerInvariant() == "y";
@ -94,6 +124,9 @@ namespace Bit.Setup
var appIdBuilder = new AppIdBuilder(url);
appIdBuilder.Build();
var dockerComposeBuilder = new DockerComposeBuilder(_hostOs);
dockerComposeBuilder.BuildForInstaller(httpPort, httpsPort);
}
private static void Update()
@ -115,7 +148,7 @@ namespace Bit.Setup
Console.WriteLine("===================================================");
Console.WriteLine("\n- visit {0}", vaultUrl);
Console.Write("- to update, run ");
if(_parameters.ContainsKey("env") && _parameters["env"] == "win")
if(_hostOs == "win")
{
Console.Write("'.\\bitwarden.ps1 -update'");
}
@ -223,10 +256,10 @@ namespace Bit.Setup
Console.WriteLine("Unable to determine existing installation url.");
return;
}
var domain = uri.Host;
var nginxBuilder = new NginxConfigBuilder(domain);
nginxBuilder.UpdateContext();
nginxBuilder.BuildForUpdater();
var appSettingsBuilder = new AppSettingsBuilder(url, domain);
@ -234,6 +267,9 @@ namespace Bit.Setup
var appIdBuilder = new AppIdBuilder(url);
appIdBuilder.Build();
var dockerComposeBuilder = new DockerComposeBuilder(_hostOs);
dockerComposeBuilder.BuildForUpdater();
}
private static IDictionary<string, string> ParseParameters()