1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-28 13:15:12 +01:00
bitwarden-server/scripts/run.ps1

252 lines
7.4 KiB
PowerShell
Raw Normal View History

2017-08-23 22:15:42 +02:00
param (
[string]$outputDir = "../.",
2017-11-09 04:24:23 +01:00
[string]$coreVersion = "latest",
[string]$webVersion = "latest",
2018-05-31 19:42:00 +02:00
[switch] $install,
2017-08-23 22:15:42 +02:00
[switch] $start,
[switch] $restart,
[switch] $stop,
2017-08-27 04:36:25 +02:00
[switch] $pull,
[switch] $updateconf,
2020-06-17 15:05:35 +02:00
[switch] $renewcert,
2017-10-25 23:21:35 +02:00
[switch] $updatedb,
[switch] $update
2017-08-23 22:15:42 +02:00
)
# Setup
2018-05-31 19:43:04 +02:00
$dockerDir = "${outputDir}\docker"
2019-03-25 21:46:32 +01:00
$setupQuiet = 0
$qFlag = ""
$quietPullFlag = ""
$certbotHttpPort = "80"
$certbotHttpsPort = "443"
if ($env:BITWARDEN_QUIET -eq "true") {
2019-03-25 21:46:32 +01:00
$setupQuiet = 1
$qFlag = " -q"
$quietPullFlag = " --quiet-pull"
}
if ("${env:BITWARDEN_CERTBOT_HTTP_PORT}" -ne "") {
2019-03-25 21:46:32 +01:00
$certbotHttpPort = $env:BITWARDEN_CERTBOT_HTTP_PORT
}
if ("${env:BITWARDEN_CERTBOT_HTTPS_PORT}" -ne "") {
2019-03-25 21:46:32 +01:00
$certbotHttpsPort = $env:BITWARDEN_CERTBOT_HTTPS_PORT
}
2017-08-23 22:15:42 +02:00
# Functions
2018-05-31 19:42:00 +02:00
function Install() {
[string]$letsEncrypt = "n"
Write-Host "(!) " -f cyan -nonewline
[string]$domain = $( Read-Host "Enter the domain name for your Bitwarden instance (ex. bitwarden.example.com)" )
2018-05-31 19:42:00 +02:00
echo ""
2018-05-31 19:43:04 +02:00
if ($domain -eq "") {
2018-05-31 19:42:00 +02:00
$domain = "localhost"
}
2018-05-31 19:43:04 +02:00
if ($domain -ne "localhost") {
2018-05-31 19:42:00 +02:00
Write-Host "(!) " -f cyan -nonewline
$letsEncrypt = $( Read-Host "Do you want to use Let's Encrypt to generate a free SSL certificate? (y/n)" )
echo ""
2018-05-31 19:43:04 +02:00
if ($letsEncrypt -eq "y") {
2018-05-31 19:42:00 +02:00
Write-Host "(!) " -f cyan -nonewline
[string]$email = $( Read-Host ("Enter your email address (Let's Encrypt will send you certificate " +
"expiration reminders)") )
2018-05-31 19:42:00 +02:00
echo ""
$letsEncryptPath = "${outputDir}/letsencrypt"
2018-05-31 19:43:04 +02:00
if (!(Test-Path -Path $letsEncryptPath )) {
2018-05-31 19:42:00 +02:00
New-Item -ItemType directory -Path $letsEncryptPath | Out-Null
}
2019-03-25 21:46:32 +01:00
Invoke-Expression ("docker pull{0} certbot/certbot" -f "") #TODO: qFlag
$certbotExp = "docker run -it --rm --name certbot -p ${certbotHttpsPort}:443 -p ${certbotHttpPort}:80 " + `
"-v ${outputDir}/letsencrypt:/etc/letsencrypt/ certbot/certbot " + `
"certonly{0} --standalone --noninteractive --agree-tos --preferred-challenges http " + `
"--email ${email} -d ${domain} --logs-dir /etc/letsencrypt/logs"
Invoke-Expression ($certbotExp -f $qFlag)
2018-05-31 19:42:00 +02:00
}
}
Write-Host "(!) " -f cyan -nonewline
[string]$database = $( Read-Host "Enter the database name for your Bitwarden instance (ex. vault): ")
echo ""
if ($database -eq "") {
$database = "vault"
}
2018-05-31 19:42:00 +02:00
Pull-Setup
docker run -it --rm --name setup -v ${outputDir}:/bitwarden bitwarden/setup:$coreVersion `
dotnet Setup.dll -install 1 -domain ${domain} -letsencrypt ${letsEncrypt} `
-os win -corev $coreVersion -webv $webVersion -q $setupQuiet -dbname "$database"
2018-05-31 19:42:00 +02:00
}
2017-08-23 22:15:42 +02:00
function Docker-Compose-Up {
2018-08-31 15:16:14 +02:00
Docker-Compose-Files
Docker-Compose-Volumes
2019-03-25 21:46:32 +01:00
Invoke-Expression ("docker-compose up -d{0}" -f $quietPullFlag)
2017-08-23 22:15:42 +02:00
}
function Docker-Compose-Down {
2018-08-31 15:16:14 +02:00
Docker-Compose-Files
if ((Invoke-Expression ("docker-compose ps{0}" -f "") | Measure-Object -Line).lines -gt 2 ) {
Invoke-Expression ("docker-compose down{0}" -f "") #TODO: qFlag
}
2017-08-23 22:15:42 +02:00
}
2017-08-27 04:36:25 +02:00
function Docker-Compose-Pull {
2018-08-31 15:16:14 +02:00
Docker-Compose-Files
2019-03-25 21:46:32 +01:00
Invoke-Expression ("docker-compose pull{0}" -f $qFlag)
2018-08-31 15:16:14 +02:00
}
function Docker-Compose-Files {
2018-05-31 19:43:04 +02:00
if (Test-Path -Path "${dockerDir}\docker-compose.override.yml" -PathType leaf) {
2018-08-31 15:16:14 +02:00
$env:COMPOSE_FILE = "${dockerDir}\docker-compose.yml;${dockerDir}\docker-compose.override.yml"
}
else {
2018-08-31 15:16:14 +02:00
$env:COMPOSE_FILE = "${dockerDir}\docker-compose.yml"
}
2018-11-09 03:32:23 +01:00
$env:COMPOSE_HTTP_TIMEOUT = "300"
2017-08-27 04:36:25 +02:00
}
function Docker-Compose-Volumes {
Create-Dir "core"
Create-Dir "core/attachments"
Create-Dir "logs"
Create-Dir "logs/admin"
Create-Dir "logs/api"
Create-Dir "logs/events"
Create-Dir "logs/icons"
Create-Dir "logs/identity"
Create-Dir "logs/mssql"
Create-Dir "logs/nginx"
Create-Dir "logs/notifications"
2020-09-14 19:08:43 +02:00
Create-Dir "logs/sso"
Create-Dir "mssql/backups"
Create-Dir "mssql/data"
}
function Create-Dir($str) {
$outPath = "${outputDir}/$str"
if (!(Test-Path -Path $outPath )) {
Write-Line "Creating directory $outPath"
New-Item -ItemType directory -Path $outPath | Out-Null
}
}
2017-08-23 22:15:42 +02:00
function Docker-Prune {
2018-11-07 15:51:16 +01:00
docker image prune --all --force --filter="label=com.bitwarden.product=bitwarden" `
--filter="label!=com.bitwarden.project=setup"
2017-08-23 22:15:42 +02:00
}
function Update-Lets-Encrypt {
2018-05-31 19:43:04 +02:00
if (Test-Path -Path "${outputDir}\letsencrypt\live") {
2019-03-25 21:46:32 +01:00
Invoke-Expression ("docker pull{0} certbot/certbot" -f "") #TODO: qFlag
$certbotExp = "docker run -it --rm --name certbot -p ${certbotHttpsPort}:443 -p ${certbotHttpPort}:80 " + `
"-v ${outputDir}/letsencrypt:/etc/letsencrypt/ certbot/certbot " + `
2019-03-25 21:46:32 +01:00
"renew{0} --logs-dir /etc/letsencrypt/logs" -f $qFlag
Invoke-Expression $certbotExp
2017-08-23 22:15:42 +02:00
}
}
2020-06-17 15:05:35 +02:00
function Force-Update-Lets-Encrypt {
if (Test-Path -Path "${outputDir}\letsencrypt\live") {
Invoke-Expression ("docker pull{0} certbot/certbot" -f "") #TODO: qFlag
$certbotExp = "docker run -it --rm --name certbot -p ${certbotHttpsPort}:443 -p ${certbotHttpPort}:80 " + `
"-v ${outputDir}/letsencrypt:/etc/letsencrypt/ certbot/certbot " + `
2020-06-17 15:05:35 +02:00
"renew{0} --logs-dir /etc/letsencrypt/logs --force-renew" -f $qFlag
Invoke-Expression $certbotExp
}
}
2017-08-23 22:15:42 +02:00
function Update-Database {
2017-10-25 23:21:35 +02:00
Pull-Setup
Docker-Compose-Files
$mssqlId = docker-compose ps -q mssql
docker run -it --rm --name setup --network container:$mssqlId `
2018-05-31 19:42:00 +02:00
-v ${outputDir}:/bitwarden bitwarden/setup:$coreVersion `
2019-03-25 21:46:32 +01:00
dotnet Setup.dll -update 1 -db 1 -os win -corev $coreVersion -webv $webVersion -q $setupQuiet
Write-Line "Database update complete"
2017-08-23 22:15:42 +02:00
}
2018-08-30 22:25:33 +02:00
function Update([switch] $withpull) {
if ($withpull) {
Pull-Setup
}
2017-11-09 04:24:23 +01:00
docker run -it --rm --name setup -v ${outputDir}:/bitwarden bitwarden/setup:$coreVersion `
2019-03-25 21:46:32 +01:00
dotnet Setup.dll -update 1 -os win -corev $coreVersion -webv $webVersion -q $setupQuiet
2017-10-25 23:21:35 +02:00
}
2017-08-24 17:16:01 +02:00
function Print-Environment {
2017-10-25 23:21:35 +02:00
Pull-Setup
2017-11-09 04:24:23 +01:00
docker run -it --rm --name setup -v ${outputDir}:/bitwarden bitwarden/setup:$coreVersion `
2019-03-25 21:46:32 +01:00
dotnet Setup.dll -printenv 1 -os win -corev $coreVersion -webv $webVersion -q $setupQuiet
2017-08-24 17:16:01 +02:00
}
2017-10-25 23:21:35 +02:00
function Restart {
2017-08-23 22:15:42 +02:00
Docker-Compose-Down
2017-08-27 04:36:25 +02:00
Docker-Compose-Pull
2017-08-23 22:15:42 +02:00
Update-Lets-Encrypt
Docker-Compose-Up
2017-08-24 17:16:01 +02:00
Print-Environment
2017-08-23 22:15:42 +02:00
}
2017-10-25 23:21:35 +02:00
2020-06-17 15:12:01 +02:00
function Cert-Restart {
2020-06-17 15:05:35 +02:00
Docker-Compose-Down
Docker-Compose-Pull
Force-Update-Lets-Encrypt
Docker-Compose-Up
Print-Environment
}
2017-10-25 23:21:35 +02:00
function Pull-Setup {
2019-03-25 21:46:32 +01:00
Invoke-Expression ("docker pull{0} bitwarden/setup:${coreVersion}" -f "") #TODO: qFlag
}
function Write-Line($str) {
if ($env:BITWARDEN_QUIET -ne "true") {
2019-03-25 21:46:32 +01:00
Write-Host $str
}
2017-10-25 23:21:35 +02:00
}
# Commands
2018-05-31 19:43:04 +02:00
if ($install) {
2018-05-31 19:42:00 +02:00
Install
}
2018-05-31 19:43:04 +02:00
elseif ($start -Or $restart) {
2017-10-25 23:21:35 +02:00
Restart
}
2018-05-31 19:43:04 +02:00
elseif ($pull) {
2017-08-27 04:36:25 +02:00
Docker-Compose-Pull
}
2018-05-31 19:43:04 +02:00
elseif ($stop) {
2017-08-23 22:15:42 +02:00
Docker-Compose-Down
}
2020-06-17 15:05:35 +02:00
elseif ($renewcert) {
2020-06-17 15:12:01 +02:00
Cert-Restart
2020-06-17 15:05:35 +02:00
}
elseif ($updateconf) {
Docker-Compose-Down
Update -withpull
}
2018-05-31 19:43:04 +02:00
elseif ($updatedb) {
2017-08-23 22:15:42 +02:00
Update-Database
}
2018-05-31 19:43:04 +02:00
elseif ($update) {
2017-10-25 23:21:35 +02:00
Docker-Compose-Down
2018-08-30 22:25:33 +02:00
Update -withpull
2017-10-25 23:21:35 +02:00
Restart
Docker-Prune
2019-03-25 21:46:32 +01:00
Write-Line "Pausing 60 seconds for database to come online. Please wait..."
2018-03-11 05:53:21 +01:00
Start-Sleep -s 60
2017-10-25 23:21:35 +02:00
Update-Database
}
2018-08-30 22:25:33 +02:00
elseif ($rebuild) {
Docker-Compose-Down
Update
}