Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB Initialization script in local & prod environment #321

Merged
merged 2 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions MSSQL/configure-db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash

# Wait 60 seconds for SQL Server to start up by ensuring that
# calling SQLCMD does not return an error code, which will ensure that sqlcmd is accessible
# and that system and user databases return "0" which means all databases are in an "online" state
# https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-databases-transact-sql?view=sql-server-2017

DBSTATUS=1
ERRCODE=1
i=0

while [[ $DBSTATUS -ne 0 ]] && [[ $i -lt 60 ]] && [[ $ERRCODE -ne 0 ]]; do
i=$i+10
DBSTATUS=$(/opt/mssql-tools/bin/sqlcmd -h -1 -t 1 -U sa -P $SA_PASSWORD -Q "SET NOCOUNT ON; Select SUM(state) from sys.databases")
ERRCODE=$?
sleep 10
done

if [ $DBSTATUS -ne 0 ] OR [ $ERRCODE -ne 0 ]; then
echo "SQL Server took more than 60 seconds to start up or one or more databases are not in an ONLINE state"
exit 1
fi

# Run the setup script to create the DB and the schema in the DB
/opt/mssql-tools/bin/sqlcmd \
-v OOS_API_PASS=${OOS_API_PASS} \
-v OOS_AUTH_PASS=${OOS_AUTH_PASS} \
-S localhost \
-U sa \
-P $SA_PASSWORD \
-d master \
-i /usr/config/configure-db.sql
29 changes: 29 additions & 0 deletions MSSQL/configure-db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
USE [master]
GO

IF DB_ID('OutOfSchool') IS NOT NULL
set noexec on -- prevent creation when already exists

CREATE DATABASE [OutOfSchool];
GO

USE [OutOfSchool]
GO

ALTER DATABASE [OutOfSchool] COLLATE Ukrainian_100_CI_AS;

CREATE LOGIN [oos_api] WITH PASSWORD = '$(OOS_API_PASS)';

CREATE LOGIN [oos_auth] WITH PASSWORD = '$(OOS_AUTH_PASS)';

CREATE USER [oos_api] FOR LOGIN [oos_api] WITH DEFAULT_SCHEMA=[dbo];

CREATE USER [oos_auth] FOR LOGIN [oos_auth] WITH DEFAULT_SCHEMA=[dbo];
GO

-- TODO: Think about better permissions
EXEC sp_AddRoleMember 'db_datareader', 'oos_api';
EXEC sp_AddRoleMember 'db_datawriter', 'oos_api';
EXEC sp_AddRoleMember 'db_datareader', 'oos_auth';
EXEC sp_AddRoleMember 'db_datawriter', 'oos_auth';
EXEC sp_AddRoleMember 'db_ddladmin', 'oos_auth';
7 changes: 7 additions & 0 deletions MSSQL/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

# Start the script to create the DB and user
/bin/bash /usr/config/configure-db.sh &

# Start SQL Server
/opt/mssql/bin/sqlservr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=sql-server; Database=Master;User Id=SA;Password=Oos-password1"
"DefaultConnection": "Server=sql-server; Database=OutOfSchool;User Id=oos_auth;Password=Oos-password1"
},
"IdentityAccessConfig": {
"AdditionalIdentityClients": [
Expand Down
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/appsettings.Compose.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=sql-server; Database=Master;User Id=SA;Password=Oos-password1"
"DefaultConnection": "Server=sql-server; Database=OutOfSchool;User Id=oos_api;Password=Oos-password1"
}
}
9 changes: 7 additions & 2 deletions create-local-ssl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ if ($null -eq (Get-Command "openssl.exe" -ErrorAction SilentlyContinue))
$Https = ".\https"

if (-Not (Test-Path -Path $Https)) {
mkdir $Https

New-Item -Type Directory -Path "$Https\ca-certificates"
New-Item -Type File -Path "$Https\ca-certificates\type" -Value "ca-certificates"

$Env:Passphrase=-join ((0x30..0x39) + ( 0x41..0x5A) + ( 0x61..0x7A) | Get-Random -Count 128 | ForEach-Object {[char]$_})
$subj="/C=UA/ST=Kyiv/L=Kyiv/O=SS/OU=ITA/CN=$Domain/emailAddress=admin@$Domain/"
openssl req `
Expand All @@ -32,7 +35,9 @@ if (-Not (Test-Path -Path $Https)) {
-in $Https\${Domain}.key `
-out $Https\${Domain}.key `
-passin env:Passphrase


Copy-Item "$Https\${Domain}.crt" -Destination "$Https\ca-certificates"

$LocalCert = Get-ChildItem -Path Cert:\LocalMachine\Root | Where-Object { $_.Subject -match "CN=$Domain" }
if ($LocalCert) {
certutil -delstore -f "ROOT" ${LocalCert}.Thumbprint
Expand Down
5 changes: 4 additions & 1 deletion create-local-ssl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ if [ ! -d "./https" ]; then
exit 1
fi

mkdir ./https
mkdir -p ./https/ca-certificates
echo "ca-certificates" > ./https/ca-certificates/type
# Generate a passphrase
export PASSPHRASE=$(head -c 500 /dev/urandom | LC_CTYPE=C tr -dc 'a-z0-9A-Z' | head -c 128; echo)
fail_if_error $?
Expand Down Expand Up @@ -64,6 +65,8 @@ if [ ! -d "./https" ]; then
-passin env:PASSPHRASE
fail_if_error $?

cp ./https/${DOMAIN}.crt ./https/ca-certificates/

if [[ $OSTYPE == 'darwin'* ]]; then
sudo security find-certificate -a /Library/Keychains/System.keychain | awk -F'"' '/alis/{print $4}' | grep ${DOMAIN} -q
if [ $? -eq 0 ];then
Expand Down
8 changes: 8 additions & 0 deletions docker-compose.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ services:
sql-server:
environment:
SA_PASSWORD: Oos-password1
OOS_API_PASS: Oos-password1
OOS_AUTH_PASS: Oos-password1
elasticsearch:
# Expose ports directly for local debug
ports:
Expand Down Expand Up @@ -31,6 +33,9 @@ services:
environment:
ASPNETCORE_ENVIRONMENT: Compose
Email__Enabled: "false"
SERVICE_BINDING_ROOT: /platform/bindings
volumes:
- ./https/ca-certificates:/platform/bindings/my-certificates
webapi-server:
# Expose ports directly for local debug
ports:
Expand All @@ -40,6 +45,9 @@ services:
Elasticsearch__Url: "http://elasticsearch:9200/"
Elasticsearch__User: user
Elasticsearch__Password: pass
SERVICE_BINDING_ROOT: /platform/bindings
volumes:
- ./https/ca-certificates:/platform/bindings/my-certificates
depends_on:
- "identity-server"
nginx:
Expand Down
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
version: '3.4'
services:
# === SQL SERVER ===
# TODO: Configure volumes
sql-server:
image: mcr.microsoft.com/mssql/server:${SQL_VERSION}
command: /bin/bash ./usr/config/entrypoint.sh
environment:
ACCEPT_EULA: Y
TZ: Europe/Kiev
MSSQL_COLLATION: Ukrainian_100_CI_AS
ports:
- 1433:1433
volumes:
- sql-data:/var/opt/mssql
- ./MSSQL/entrypoint.sh:/usr/config/entrypoint.sh:ro
- ./MSSQL/configure-db.sh:/usr/config/configure-db.sh:ro
- ./MSSQL/configure-db.sql:/usr/config/configure-db.sql:ro
networks:
- db
# === ELASTICSEARCH ===
Expand Down Expand Up @@ -81,6 +86,8 @@ services:
- web
# === VOLUMES ===
volumes:
sql-data:
driver: local
es-data:
driver: local
api-logs:
Expand Down