1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-07 03:30:32 -05:00

quiet output for setup scripts

This commit is contained in:
Kyle Spearrin 2019-03-12 10:26:14 -04:00
parent 14fd7e2801
commit b6f54324a5
8 changed files with 70 additions and 22 deletions

View File

@ -19,7 +19,7 @@ namespace Bit.Setup
Url = _context.Config.Url Url = _context.Config.Url
}; };
Console.WriteLine("Building FIDO U2F app id."); Helpers.WriteLine(_context, "Building FIDO U2F app id.");
Directory.CreateDirectory("/bitwarden/web/"); Directory.CreateDirectory("/bitwarden/web/");
var template = Helpers.ReadTemplate("AppId"); var template = Helpers.ReadTemplate("AppId");
using(var sw = File.CreateText("/bitwarden/web/app-id.json")) using(var sw = File.CreateText("/bitwarden/web/app-id.json"))

View File

@ -24,12 +24,12 @@ namespace Bit.Setup
Directory.CreateDirectory($"/bitwarden/ssl/{_context.Install.Domain}/"); Directory.CreateDirectory($"/bitwarden/ssl/{_context.Install.Domain}/");
var message = "Make sure 'certificate.crt' and 'private.key' are provided in the \n" + var message = "Make sure 'certificate.crt' and 'private.key' are provided in the \n" +
"appropriate directory before running 'start' (see docs for info)."; "appropriate directory before running 'start' (see docs for info).";
Helpers.ShowBanner("NOTE", message); Helpers.ShowBanner(_context, "NOTE", message);
} }
else if(Helpers.ReadQuestion("Do you want to generate a self-signed SSL certificate?")) else if(Helpers.ReadQuestion("Do you want to generate a self-signed SSL certificate?"))
{ {
Directory.CreateDirectory($"/bitwarden/ssl/self/{_context.Install.Domain}/"); Directory.CreateDirectory($"/bitwarden/ssl/self/{_context.Install.Domain}/");
Console.WriteLine("Generating self signed SSL certificate."); Helpers.WriteLine(_context, "Generating self signed SSL certificate.");
_context.Config.Ssl = true; _context.Config.Ssl = true;
_context.Install.Trusted = false; _context.Install.Trusted = false;
_context.Install.SelfSignedCert = true; _context.Install.SelfSignedCert = true;
@ -55,7 +55,7 @@ namespace Bit.Setup
"(requires ca.crt, see docs)?"); "(requires ca.crt, see docs)?");
} }
Console.WriteLine("Generating key for IdentityServer."); Helpers.WriteLine(_context, "Generating key for IdentityServer.");
_context.Install.IdentityCertPassword = Helpers.SecureRandomString(32, alpha: true, numeric: true); _context.Install.IdentityCertPassword = Helpers.SecureRandomString(32, alpha: true, numeric: true);
Directory.CreateDirectory("/bitwarden/identity/"); Directory.CreateDirectory("/bitwarden/identity/");
Helpers.Exec("openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout identity.key " + Helpers.Exec("openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout identity.key " +
@ -63,14 +63,14 @@ namespace Bit.Setup
Helpers.Exec("openssl pkcs12 -export -out /bitwarden/identity/identity.pfx -inkey identity.key " + Helpers.Exec("openssl pkcs12 -export -out /bitwarden/identity/identity.pfx -inkey identity.key " +
$"-in identity.crt -certfile identity.crt -passout pass:{_context.Install.IdentityCertPassword}"); $"-in identity.crt -certfile identity.crt -passout pass:{_context.Install.IdentityCertPassword}");
Console.WriteLine(); Helpers.WriteLine(_context);
if(!_context.Config.Ssl) if(!_context.Config.Ssl)
{ {
var message = "You are not using a SSL certificate. Bitwarden requires HTTPS to operate. \n" + var message = "You are not using a SSL certificate. Bitwarden requires HTTPS to operate. \n" +
"You must front your installation with a HTTPS proxy or the web vault (and \n" + "You must front your installation with a HTTPS proxy or the web vault (and \n" +
"other Bitwarden apps) will not work properly."; "other Bitwarden apps) will not work properly.";
Helpers.ShowBanner("WARNING", message, ConsoleColor.Yellow); Helpers.ShowBanner(_context, "WARNING", message, ConsoleColor.Yellow);
} }
else if(_context.Config.Ssl && !_context.Install.Trusted) else if(_context.Config.Ssl && !_context.Install.Trusted)
{ {
@ -78,7 +78,7 @@ namespace Bit.Setup
"trusted by Bitwarden client applications. You must add this certificate to \n" + "trusted by Bitwarden client applications. You must add this certificate to \n" +
"the trusted store on each device or else you will receive errors when trying \n" + "the trusted store on each device or else you will receive errors when trying \n" +
"to connect to your installation."; "to connect to your installation.";
Helpers.ShowBanner("WARNING", message, ConsoleColor.Yellow); Helpers.ShowBanner(_context, "WARNING", message, ConsoleColor.Yellow);
} }
} }
} }

View File

@ -11,6 +11,7 @@ namespace Bit.Setup
private const string ConfigPath = "/bitwarden/config.yml"; private const string ConfigPath = "/bitwarden/config.yml";
public string[] Args { get; set; } public string[] Args { get; set; }
public bool Quiet { get; set; }
public IDictionary<string, string> Parameters { get; set; } public IDictionary<string, string> Parameters { get; set; }
public string OutputDir { get; set; } = "/etc/bitwarden"; public string OutputDir { get; set; } = "/etc/bitwarden";
public string HostOS { get; set; } = "win"; public string HostOS { get; set; } = "win";
@ -19,17 +20,22 @@ namespace Bit.Setup
public Installation Install { get; set; } = new Installation(); public Installation Install { get; set; } = new Installation();
public Configuration Config { get; set; } = new Configuration(); public Configuration Config { get; set; } = new Configuration();
public bool PrintToScreen()
{
return !Quiet || Parameters.ContainsKey("install");
}
public void LoadConfiguration() public void LoadConfiguration()
{ {
if(!File.Exists(ConfigPath)) if(!File.Exists(ConfigPath))
{ {
Console.WriteLine("No existing `config.yml` detected. Let's generate one."); Helpers.WriteLine(this, "No existing `config.yml` detected. Let's generate one.");
// Looks like updating from older version. Try to create config file. // Looks like updating from older version. Try to create config file.
var url = Helpers.GetValueFromEnvFile("global", "globalSettings__baseServiceUri__vault"); var url = Helpers.GetValueFromEnvFile("global", "globalSettings__baseServiceUri__vault");
if(!Uri.TryCreate(url, UriKind.Absolute, out var uri)) if(!Uri.TryCreate(url, UriKind.Absolute, out var uri))
{ {
Console.WriteLine("Unable to determine existing installation url."); Helpers.WriteLine(this, "Unable to determine existing installation url.");
return; return;
} }
Config.Url = url; Config.Url = url;

View File

@ -26,10 +26,10 @@ namespace Bit.Setup
private void Build() private void Build()
{ {
Directory.CreateDirectory("/bitwarden/docker/"); Directory.CreateDirectory("/bitwarden/docker/");
Console.WriteLine("Building docker-compose.yml."); Helpers.WriteLine(_context, "Building docker-compose.yml.");
if(!_context.Config.GenerateComposeConfig) if(!_context.Config.GenerateComposeConfig)
{ {
Console.WriteLine("...skipped"); Helpers.WriteLine(_context, "...skipped");
return; return;
} }

View File

@ -167,7 +167,7 @@ namespace Bit.Setup
{ {
var template = Helpers.ReadTemplate("EnvironmentFile"); var template = Helpers.ReadTemplate("EnvironmentFile");
Console.WriteLine("Building docker environment files."); Helpers.WriteLine(_context, "Building docker environment files.");
Directory.CreateDirectory("/bitwarden/docker/"); Directory.CreateDirectory("/bitwarden/docker/");
using(var sw = File.CreateText("/bitwarden/docker/global.env")) using(var sw = File.CreateText("/bitwarden/docker/global.env"))
{ {
@ -181,7 +181,7 @@ namespace Bit.Setup
} }
Helpers.Exec("chmod 600 /bitwarden/docker/mssql.env"); Helpers.Exec("chmod 600 /bitwarden/docker/mssql.env");
Console.WriteLine("Building docker environment override files."); Helpers.WriteLine(_context, "Building docker environment override files.");
Directory.CreateDirectory("/bitwarden/env/"); Directory.CreateDirectory("/bitwarden/env/");
using(var sw = File.CreateText("/bitwarden/env/global.override.env")) using(var sw = File.CreateText("/bitwarden/env/global.override.env"))
{ {

View File

@ -165,8 +165,12 @@ namespace Bit.Setup
return input == "y" || input == "yes"; return input == "y" || input == "yes";
} }
public static void ShowBanner(string title, string message, ConsoleColor? color = null) public static void ShowBanner(Context context, string title, string message, ConsoleColor? color = null)
{ {
if(!context.PrintToScreen())
{
return;
}
if(color != null) if(color != null)
{ {
Console.ForegroundColor = color.Value; Console.ForegroundColor = color.Value;
@ -192,5 +196,34 @@ namespace Bit.Setup
return HandlebarsDotNet.Handlebars.Compile(templateText); return HandlebarsDotNet.Handlebars.Compile(templateText);
} }
} }
public static void WriteLine(Context context, string format = null, object arg0 = null, object arg1 = null,
object arg2 = null)
{
if(!context.PrintToScreen())
{
return;
}
if(format != null && arg0 != null && arg1 != null && arg2 != null)
{
Console.WriteLine(format, arg0, arg1, arg2);
}
else if(format != null && arg0 != null && arg1 != null)
{
Console.WriteLine(format, arg0, arg1);
}
else if(format != null && arg0 != null)
{
Console.WriteLine(format, arg0);
}
else if(format != null)
{
Console.WriteLine(format);
}
else
{
Console.WriteLine();
}
}
} }
} }

View File

@ -55,10 +55,10 @@ namespace Bit.Setup
private void Build(TemplateModel model) private void Build(TemplateModel model)
{ {
Directory.CreateDirectory("/bitwarden/nginx/"); Directory.CreateDirectory("/bitwarden/nginx/");
Console.WriteLine("Building nginx config."); Helpers.WriteLine(_context, "Building nginx config.");
if(!_context.Config.GenerateNginxConfig) if(!_context.Config.GenerateNginxConfig)
{ {
Console.WriteLine("...skipped"); Helpers.WriteLine(_context, "...skipped");
return; return;
} }

View File

@ -14,13 +14,16 @@ namespace Bit.Setup
public static void Main(string[] args) public static void Main(string[] args)
{ {
Console.WriteLine();
_context = new Context _context = new Context
{ {
Args = args Args = args
}; };
ParseParameters(); ParseParameters();
if(_context.Parameters.ContainsKey("q"))
{
_context.Quiet = _context.Parameters["q"] == "true" || _context.Parameters["q"] == "1";
}
if(_context.Parameters.ContainsKey("os")) if(_context.Parameters.ContainsKey("os"))
{ {
_context.HostOS = _context.Parameters["os"]; _context.HostOS = _context.Parameters["os"];
@ -34,6 +37,8 @@ namespace Bit.Setup
_context.WebVersion = _context.Parameters["webv"]; _context.WebVersion = _context.Parameters["webv"];
} }
Helpers.WriteLine(_context);
if(_context.Parameters.ContainsKey("install")) if(_context.Parameters.ContainsKey("install"))
{ {
Install(); Install();
@ -48,7 +53,7 @@ namespace Bit.Setup
} }
else else
{ {
Console.WriteLine("No top-level command detected. Exiting..."); Helpers.WriteLine(_context, "No top-level command detected. Exiting...");
} }
} }
@ -125,6 +130,10 @@ namespace Bit.Setup
private static void PrintEnvironment() private static void PrintEnvironment()
{ {
_context.LoadConfiguration(); _context.LoadConfiguration();
if(!_context.PrintToScreen())
{
return;
}
Console.WriteLine("\nBitwarden is up and running!"); Console.WriteLine("\nBitwarden is up and running!");
Console.WriteLine("==================================================="); Console.WriteLine("===================================================");
Console.WriteLine("\nvisit {0}", _context.Config.Url); Console.WriteLine("\nvisit {0}", _context.Config.Url);
@ -144,7 +153,7 @@ namespace Bit.Setup
{ {
try try
{ {
Console.WriteLine("Migrating database."); Helpers.WriteLine(_context, "Migrating database.");
var vaultConnectionString = Helpers.GetValueFromEnvFile("global", var vaultConnectionString = Helpers.GetValueFromEnvFile("global",
"globalSettings__sqlServer__connectionString"); "globalSettings__sqlServer__connectionString");
@ -179,11 +188,11 @@ namespace Bit.Setup
var result = upgrader.PerformUpgrade(); var result = upgrader.PerformUpgrade();
if(result.Successful) if(result.Successful)
{ {
Console.WriteLine("Migration successful."); Helpers.WriteLine(_context, "Migration successful.");
} }
else else
{ {
Console.WriteLine("Migration failed."); Helpers.WriteLine(_context, "Migration failed.");
} }
} }
catch(SqlException e) catch(SqlException e)
@ -191,7 +200,7 @@ namespace Bit.Setup
if(e.Message.Contains("Server is in script upgrade mode") && attempt < 10) if(e.Message.Contains("Server is in script upgrade mode") && attempt < 10)
{ {
var nextAttempt = attempt + 1; var nextAttempt = attempt + 1;
Console.WriteLine("Database is in script upgrade mode. " + Helpers.WriteLine(_context, "Database is in script upgrade mode. " +
"Trying again (attempt #{0})...", nextAttempt); "Trying again (attempt #{0})...", nextAttempt);
System.Threading.Thread.Sleep(20000); System.Threading.Thread.Sleep(20000);
MigrateDatabase(nextAttempt); MigrateDatabase(nextAttempt);