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

mssql image setup script, db up migrations with upgrade from setup

This commit is contained in:
Kyle Spearrin 2017-08-18 18:22:25 -04:00
parent 1fd7f5dd03
commit d4809686db
9 changed files with 141 additions and 10 deletions

View File

@ -36,17 +36,44 @@ namespace Bit.Core
public class SqlServerSettings
{
public string ConnectionString { get; set; }
private string _connectionString;
public string ConnectionString
{
get => _connectionString;
set
{
_connectionString = value.Trim('"');
}
}
}
public class StorageSettings
{
public string ConnectionString { get; set; }
private string _connectionString;
public string ConnectionString
{
get => _connectionString;
set
{
_connectionString = value.Trim('"');
}
}
}
public class AttachmentSettings
{
public string ConnectionString { get; set; }
private string _connectionString;
public string ConnectionString
{
get => _connectionString;
set
{
_connectionString = value.Trim('"');
}
}
public string BaseDirectory { get; set; }
public string BaseUrl { get; set; }
}
@ -87,7 +114,16 @@ namespace Bit.Core
public class NotificationHubSettings
{
public string ConnectionString { get; set; }
private string _connectionString;
public string ConnectionString
{
get => _connectionString;
set
{
_connectionString = value.Trim('"');
}
}
public string HubName { get; set; }
}

View File

@ -1,3 +1,4 @@
*
!entrypoint.sh
!setup.sql
!setup.sh

View File

@ -1,6 +1,8 @@
FROM microsoft/mssql-server-linux
COPY setup.sql /
COPY setup.sh /
RUN chmod +x /setup.sh
COPY entrypoint.sh /
RUN chmod +x /entrypoint.sh

View File

@ -1,4 +1,4 @@
#!/bin/sh
/setup.sh &
/opt/mssql/bin/sqlservr
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${SA_PASSWORD} -i /setup.sql

4
util/MsSql/setup.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
sleep 60s
/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${SA_PASSWORD} -i /setup.sql

View File

@ -1,4 +1,4 @@
FROM microsoft/dotnet:2.0.0-preview2-runtime
FROM microsoft/dotnet:2.0.0-runtime
RUN apt-get update \
&& apt-get install -y --no-install-recommends \

View File

@ -1,6 +1,8 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Setup
{
@ -87,5 +89,31 @@ namespace Setup
return characters;
}
public static string MakeSqlConnectionString(string server, string database, string username, string password)
{
return $"Server=tcp:{server},1433;Initial Catalog={database};Persist Security Info=False;User ID={username};" +
$"Password={password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;" +
"Connection Timeout=30;";
}
public static string GetDatabasePasswordFronEnvFile()
{
if(!File.Exists("/bitwarden/docker/mssql.override.env"))
{
return null;
}
var lines = File.ReadAllLines("/bitwarden/docker/mssql.override.env");
foreach(var line in lines)
{
if(line.StartsWith("SA_PASSWORD="))
{
return line.Split(new char[] { '=' }, 2)[1];
}
}
return null;
}
}
}

View File

@ -1,7 +1,9 @@
using System;
using DbUp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
namespace Setup
@ -25,7 +27,22 @@ namespace Setup
{
_args = args;
_parameters = ParseParameters();
if(_parameters.ContainsKey("install"))
{
Install();
}
else if(_parameters.ContainsKey("update"))
{
Update();
}
else
{
Console.WriteLine("No top-level command detected. Exiting...");
}
}
private static void Install()
{
_installationId = _parameters.ContainsKey("install_id") ?
_parameters["install_id"].ToLowerInvariant() : null;
_installationKey = _parameters.ContainsKey("install_key") ?
@ -65,6 +82,41 @@ namespace Setup
BuildAppSettingsFiles();
}
private static void Update()
{
if(_parameters.ContainsKey("db"))
{
MigrateDatabase();
}
}
private static void MigrateDatabase()
{
Console.WriteLine("Migrating database.");
var dbPass = Helpers.GetDatabasePasswordFronEnvFile();
var connectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass ?? string.Empty);
var upgrader = DeployChanges.To
.SqlDatabase(connectionString)
.JournalToSqlTable("dbo", "Migration")
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(),
s => s.Contains($".DbScripts.") && !s.Contains(".Archive."))
.WithTransaction()
.WithExecutionTimeout(new TimeSpan(0, 5, 0))
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
if(result.Successful)
{
Console.WriteLine("Migration successful.");
}
else
{
Console.WriteLine("Migration failed.");
}
}
private static void MakeCerts()
{
if(!_ssl)
@ -255,9 +307,7 @@ server {{
Console.WriteLine("Building docker environment override files.");
Directory.CreateDirectory("/bitwarden/docker/");
var dbPass = _parameters.ContainsKey("db_pass") ? _parameters["db_pass"].ToLowerInvariant() : "REPLACE";
var dbConnectionString = "Server=tcp:mssql,1433;Initial Catalog=vault;Persist Security Info=False;User ID=sa;" +
$"Password={dbPass};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=True;" +
"Connection Timeout=30;";
var dbConnectionString = Helpers.MakeSqlConnectionString("mssql", "vault", "sa", dbPass);
using(var sw = File.CreateText("/bitwarden/docker/global.override.env"))
{

View File

@ -3,6 +3,16 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<NoWarn>1701;1702;1705;NU1701</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="dbup" Version="3.3.5" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="DbScripts\" />
</ItemGroup>
</Project>