From b6f54324a56731a33ce9fc0e4528beda124890e3 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Tue, 12 Mar 2019 10:26:14 -0400 Subject: [PATCH] quiet output for setup scripts --- util/Setup/AppIdBuilder.cs | 2 +- util/Setup/CertBuilder.cs | 12 +++++----- util/Setup/Context.cs | 10 ++++++-- util/Setup/DockerComposeBuilder.cs | 4 ++-- util/Setup/EnvironmentFileBuilder.cs | 4 ++-- util/Setup/Helpers.cs | 35 +++++++++++++++++++++++++++- util/Setup/NginxConfigBuilder.cs | 4 ++-- util/Setup/Program.cs | 21 ++++++++++++----- 8 files changed, 70 insertions(+), 22 deletions(-) diff --git a/util/Setup/AppIdBuilder.cs b/util/Setup/AppIdBuilder.cs index f31a84e8c7..0b92666a12 100644 --- a/util/Setup/AppIdBuilder.cs +++ b/util/Setup/AppIdBuilder.cs @@ -19,7 +19,7 @@ namespace Bit.Setup Url = _context.Config.Url }; - Console.WriteLine("Building FIDO U2F app id."); + Helpers.WriteLine(_context, "Building FIDO U2F app id."); Directory.CreateDirectory("/bitwarden/web/"); var template = Helpers.ReadTemplate("AppId"); using(var sw = File.CreateText("/bitwarden/web/app-id.json")) diff --git a/util/Setup/CertBuilder.cs b/util/Setup/CertBuilder.cs index fd8e485be5..b6582b6d2a 100644 --- a/util/Setup/CertBuilder.cs +++ b/util/Setup/CertBuilder.cs @@ -24,12 +24,12 @@ namespace Bit.Setup Directory.CreateDirectory($"/bitwarden/ssl/{_context.Install.Domain}/"); var message = "Make sure 'certificate.crt' and 'private.key' are provided in the \n" + "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?")) { 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.Install.Trusted = false; _context.Install.SelfSignedCert = true; @@ -55,7 +55,7 @@ namespace Bit.Setup "(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); Directory.CreateDirectory("/bitwarden/identity/"); 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 " + $"-in identity.crt -certfile identity.crt -passout pass:{_context.Install.IdentityCertPassword}"); - Console.WriteLine(); + Helpers.WriteLine(_context); if(!_context.Config.Ssl) { 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" + "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) { @@ -78,7 +78,7 @@ namespace Bit.Setup "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" + "to connect to your installation."; - Helpers.ShowBanner("WARNING", message, ConsoleColor.Yellow); + Helpers.ShowBanner(_context, "WARNING", message, ConsoleColor.Yellow); } } } diff --git a/util/Setup/Context.cs b/util/Setup/Context.cs index 7bc7fd7f36..8c7fc22a6d 100644 --- a/util/Setup/Context.cs +++ b/util/Setup/Context.cs @@ -11,6 +11,7 @@ namespace Bit.Setup private const string ConfigPath = "/bitwarden/config.yml"; public string[] Args { get; set; } + public bool Quiet { get; set; } public IDictionary Parameters { get; set; } public string OutputDir { get; set; } = "/etc/bitwarden"; public string HostOS { get; set; } = "win"; @@ -19,17 +20,22 @@ namespace Bit.Setup public Installation Install { get; set; } = new Installation(); public Configuration Config { get; set; } = new Configuration(); + public bool PrintToScreen() + { + return !Quiet || Parameters.ContainsKey("install"); + } + public void LoadConfiguration() { 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. var url = Helpers.GetValueFromEnvFile("global", "globalSettings__baseServiceUri__vault"); 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; } Config.Url = url; diff --git a/util/Setup/DockerComposeBuilder.cs b/util/Setup/DockerComposeBuilder.cs index 5126594abc..bfb98bbee4 100644 --- a/util/Setup/DockerComposeBuilder.cs +++ b/util/Setup/DockerComposeBuilder.cs @@ -26,10 +26,10 @@ namespace Bit.Setup private void Build() { Directory.CreateDirectory("/bitwarden/docker/"); - Console.WriteLine("Building docker-compose.yml."); + Helpers.WriteLine(_context, "Building docker-compose.yml."); if(!_context.Config.GenerateComposeConfig) { - Console.WriteLine("...skipped"); + Helpers.WriteLine(_context, "...skipped"); return; } diff --git a/util/Setup/EnvironmentFileBuilder.cs b/util/Setup/EnvironmentFileBuilder.cs index 370faf734d..48e3483476 100644 --- a/util/Setup/EnvironmentFileBuilder.cs +++ b/util/Setup/EnvironmentFileBuilder.cs @@ -167,7 +167,7 @@ namespace Bit.Setup { var template = Helpers.ReadTemplate("EnvironmentFile"); - Console.WriteLine("Building docker environment files."); + Helpers.WriteLine(_context, "Building docker environment files."); Directory.CreateDirectory("/bitwarden/docker/"); using(var sw = File.CreateText("/bitwarden/docker/global.env")) { @@ -181,7 +181,7 @@ namespace Bit.Setup } 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/"); using(var sw = File.CreateText("/bitwarden/env/global.override.env")) { diff --git a/util/Setup/Helpers.cs b/util/Setup/Helpers.cs index 39f853cebe..a7e038cb80 100644 --- a/util/Setup/Helpers.cs +++ b/util/Setup/Helpers.cs @@ -165,8 +165,12 @@ namespace Bit.Setup 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) { Console.ForegroundColor = color.Value; @@ -192,5 +196,34 @@ namespace Bit.Setup 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(); + } + } } } diff --git a/util/Setup/NginxConfigBuilder.cs b/util/Setup/NginxConfigBuilder.cs index 88edc27d0d..7db218d498 100644 --- a/util/Setup/NginxConfigBuilder.cs +++ b/util/Setup/NginxConfigBuilder.cs @@ -55,10 +55,10 @@ namespace Bit.Setup private void Build(TemplateModel model) { Directory.CreateDirectory("/bitwarden/nginx/"); - Console.WriteLine("Building nginx config."); + Helpers.WriteLine(_context, "Building nginx config."); if(!_context.Config.GenerateNginxConfig) { - Console.WriteLine("...skipped"); + Helpers.WriteLine(_context, "...skipped"); return; } diff --git a/util/Setup/Program.cs b/util/Setup/Program.cs index 170b143d7f..881fd6267a 100644 --- a/util/Setup/Program.cs +++ b/util/Setup/Program.cs @@ -14,13 +14,16 @@ namespace Bit.Setup public static void Main(string[] args) { - Console.WriteLine(); _context = new Context { Args = args }; ParseParameters(); + if(_context.Parameters.ContainsKey("q")) + { + _context.Quiet = _context.Parameters["q"] == "true" || _context.Parameters["q"] == "1"; + } if(_context.Parameters.ContainsKey("os")) { _context.HostOS = _context.Parameters["os"]; @@ -34,6 +37,8 @@ namespace Bit.Setup _context.WebVersion = _context.Parameters["webv"]; } + Helpers.WriteLine(_context); + if(_context.Parameters.ContainsKey("install")) { Install(); @@ -48,7 +53,7 @@ namespace Bit.Setup } 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() { _context.LoadConfiguration(); + if(!_context.PrintToScreen()) + { + return; + } Console.WriteLine("\nBitwarden is up and running!"); Console.WriteLine("==================================================="); Console.WriteLine("\nvisit {0}", _context.Config.Url); @@ -144,7 +153,7 @@ namespace Bit.Setup { try { - Console.WriteLine("Migrating database."); + Helpers.WriteLine(_context, "Migrating database."); var vaultConnectionString = Helpers.GetValueFromEnvFile("global", "globalSettings__sqlServer__connectionString"); @@ -179,11 +188,11 @@ namespace Bit.Setup var result = upgrader.PerformUpgrade(); if(result.Successful) { - Console.WriteLine("Migration successful."); + Helpers.WriteLine(_context, "Migration successful."); } else { - Console.WriteLine("Migration failed."); + Helpers.WriteLine(_context, "Migration failed."); } } catch(SqlException e) @@ -191,7 +200,7 @@ namespace Bit.Setup if(e.Message.Contains("Server is in script upgrade mode") && attempt < 10) { 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); System.Threading.Thread.Sleep(20000); MigrateDatabase(nextAttempt);