diff --git a/bitwarden.ps1 b/bitwarden.ps1 index 99d0eed..e57c7ad 100644 --- a/bitwarden.ps1 +++ b/bitwarden.ps1 @@ -10,6 +10,7 @@ param ( [switch] $updatedb, [switch] $updaterun, [switch] $updateself, + [switch] $uninstall, [switch] $help, [string] $output = "" ) @@ -23,25 +24,24 @@ if ($output -eq "") { } $scriptsDir = "${output}\scripts" -$bitwardenScriptUrl = "https://go.btwrdn.co/bw-ps" -$runScriptUrl = "https://go.btwrdn.co/bw-ps-run" +$githubBaseUrl = "https://raw.githubusercontent.com/bitwarden/server/master" # Please do not create pull requests modifying the version numbers. -$coreVersion = "1.45.2" -$webVersion = "2.25.0" -$keyConnectorVersion = "1.0.0" +$coreVersion = "1.46.2" +$webVersion = "2.26.1" +$keyConnectorVersion = "1.0.1" # Functions function Get-Self { - Invoke-RestMethod -OutFile $scriptPath -Uri $bitwardenScriptUrl + Invoke-RestMethod -OutFile $scriptPath -Uri "${githubBaseUrl}/scripts/bitwarden.ps1" } function Get-Run-File { if (!(Test-Path -Path $scriptsDir)) { New-Item -ItemType directory -Path $scriptsDir | Out-Null } - Invoke-RestMethod -OutFile $scriptsDir\run.ps1 -Uri $runScriptUrl + Invoke-RestMethod -OutFile $scriptsDir\run.ps1 -Uri "${githubBaseUrl}/scripts/run.ps1" } function Test-Output-Dir-Exists { @@ -69,6 +69,7 @@ Available commands: -updaterun -updateself -updateconf +-uninstall -renewcert -rebuild -help @@ -78,7 +79,7 @@ See more at https://bitwarden.com/help/article/install-on-premise/#script-comman } function Write-Line($str) { - if ($env:BITWARDEN_QUIET -ne "true") { + if($env:BITWARDEN_QUIET -ne "true") { Write-Host $str } } @@ -103,7 +104,7 @@ https://bitwarden.com, https://github.com/bitwarden =================================================== " -if ($env:BITWARDEN_QUIET -ne "true") { +if($env:BITWARDEN_QUIET -ne "true") { Write-Line "bitwarden.ps1 version ${coreVersion}" docker --version docker-compose --version @@ -156,6 +157,10 @@ elseif ($updateself) { Get-Self Write-Line "Updated self." } +elseif ($uninstall) { + Test-Output-Dir-Exists + Invoke-Expression "& `"$scriptsDir\run.ps1`" -uninstall -outputDir `"$output`" " +} elseif ($help) { Show-Commands } diff --git a/bitwarden.sh b/bitwarden.sh index 991e38d..013e7d2 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -29,26 +29,35 @@ if [ $# -eq 2 ] then OUTPUT=$2 fi +if command -v docker-compose &> /dev/null +then + dccmd='docker-compose' +else + dccmd='docker compose' +fi SCRIPTS_DIR="$OUTPUT/scripts" -BITWARDEN_SCRIPT_URL="https://go.btwrdn.co/bw-sh" -RUN_SCRIPT_URL="https://go.btwrdn.co/bw-sh-run" +GITHUB_BASE_URL="https://raw.githubusercontent.com/bitwarden/server/master" # Please do not create pull requests modifying the version numbers. -COREVERSION="1.45.2" -WEBVERSION="2.25.0" -KEYCONNECTORVERSION="1.0.0" +COREVERSION="1.46.2" +WEBVERSION="2.26.1" +KEYCONNECTORVERSION="1.0.1" echo "bitwarden.sh version $COREVERSION" docker --version -docker-compose --version +if [[ "$dccmd" == "docker compose" ]]; then + $dccmd version +else + $dccmd --version +fi echo "" # Functions function downloadSelf() { - if curl -L -s -w "http_code %{http_code}" -o $SCRIPT_PATH.1 $BITWARDEN_SCRIPT_URL | grep -q "^http_code 20[0-9]" + if curl -s -w "http_code %{http_code}" -o $SCRIPT_PATH.1 $GITHUB_BASE_URL/scripts/bitwarden.sh | grep -q "^http_code 20[0-9]" then mv $SCRIPT_PATH.1 $SCRIPT_PATH chmod u+x $SCRIPT_PATH @@ -62,7 +71,7 @@ function downloadRunFile() { then mkdir $SCRIPTS_DIR fi - curl -L -s -o $SCRIPTS_DIR/run.sh $RUN_SCRIPT_URL + curl -s -o $SCRIPTS_DIR/run.sh $GITHUB_BASE_URL/scripts/run.sh chmod u+x $SCRIPTS_DIR/run.sh rm -f $SCRIPTS_DIR/install.sh } @@ -96,6 +105,7 @@ updatedb updaterun updateself updateconf +uninstall renewcert rebuild help @@ -150,6 +160,10 @@ case $1 in "updateself") downloadSelf && echo "Updated self." && exit ;; + "uninstall") + checkOutputDirExists + $SCRIPTS_DIR/run.sh uninstall $OUTPUT + ;; "help") listCommands ;; diff --git a/run.ps1 b/run.ps1 index 78e560c..b56e658 100644 --- a/run.ps1 +++ b/run.ps1 @@ -9,6 +9,7 @@ param ( [switch] $stop, [switch] $pull, [switch] $updateconf, + [switch] $uninstall, [switch] $renewcert, [switch] $updatedb, [switch] $update @@ -183,6 +184,38 @@ function Update([switch] $withpull) { -keyconnectorv $keyConnectorVersion -q $setupQuiet } +function Uninstall() { + $keepDatabase = $(Write-Host "(WARNING: UNINSTALL STARTED) Would you like to save the database files? (y/n)" -f red -nonewline) + $(Read-host) + if ($keepDatabase -eq "y") { + Write-Host "Saving database." + Compress-Archive -Path "${outputDir}\mssql" -DestinationPath ".\bitwarden_database.zip" + Write-Host "(SAVED DATABASE FILES: YES) `n(WARNING: ALL DATA WILL BE REMOVED, INCLUDING THE FOLDER $outputDir) " -f red -nonewline + $uninstallAction = $( Read-Host "Are you sure you want to uninstall Bitwarden? (y/n)" ) + } else { + Write-Host "(WARNING: ALL DATA WILL BE REMOVED, INCLUDING THE FOLDER $outputDir) " -f red -nonewline + $uninstallAction = $( Read-Host "Are you sure you want to uninstall Bitwarden? (y/n)" ) + } + + + if ($uninstallAction -eq "y") { + Write-Host "uninstalling Bitwarden..." + Docker-Compose-Down + Write-Host "Removing $outputDir" + Remove-Item -Path $outputDir -Force -Recurse + Write-Host "Bitwarden uninstall complete!" + } else { + Write-Host "Bitwarden uninstall canceled." + Exit + } + + Write-Host "(!) " -f red -nonewline + $purgeAction = $( Read-Host "Would you like to purge all local Bitwarden container images? (y/n)" ) + + if ($purgeAction -eq "y") { + Docker-Prune + } +} + function Print-Environment { Pull-Setup docker run -it --rm --name setup -v ${outputDir}:/bitwarden bitwarden/setup:$coreVersion ` @@ -250,6 +283,10 @@ elseif ($update) { Start-Sleep -s 60 Update-Database } +elseif ($uninstall) { + Docker-Compose-Down + Uninstall +} elseif ($rebuild) { Docker-Compose-Down Update diff --git a/run.sh b/run.sh index a08a465..98a1956 100755 --- a/run.sh +++ b/run.sh @@ -2,8 +2,15 @@ set -e # Setup +if command -v docker-compose &> /dev/null +then + dccmd='docker-compose' +else + dccmd='docker compose' +fi CYAN='\033[0;36m' +RED='\033[1;31m' NC='\033[0m' # No Color DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" @@ -101,19 +108,19 @@ function install() { function dockerComposeUp() { dockerComposeFiles dockerComposeVolumes - docker-compose up -d + $dccmd up -d } function dockerComposeDown() { dockerComposeFiles - if [ $(docker-compose ps | wc -l) -gt 2 ]; then - docker-compose down + if [ $($dccmd ps | wc -l) -gt 2 ]; then + $dccmd down fi } function dockerComposePull() { dockerComposeFiles - docker-compose pull + $dccmd pull } function dockerComposeFiles() { @@ -180,7 +187,7 @@ function forceUpdateLetsEncrypt() { function updateDatabase() { pullSetup dockerComposeFiles - MSSQL_ID=$(docker-compose ps -q mssql) + MSSQL_ID=$($dccmd ps -q mssql) docker run -i --rm --name setup --network container:$MSSQL_ID \ -v $OUTPUT_DIR:/bitwarden --env-file $ENV_DIR/uid.env bitwarden/setup:$COREVERSION \ dotnet Setup.dll -update 1 -db 1 -os $OS -corev $COREVERSION -webv $WEBVERSION -keyconnectorv $KEYCONNECTORVERSION @@ -189,11 +196,11 @@ function updateDatabase() { function updatebw() { KEY_CONNECTOR_ENABLED=$(grep -A3 'enable_key_connector:' $OUTPUT_DIR/config.yml | tail -n1 | awk '{ print $2}') - CORE_ID=$(docker-compose ps -q admin) - WEB_ID=$(docker-compose ps -q web) - if [ $KEY_CONNECTOR_ENABLED = true ]; + CORE_ID=$($dccmd ps -q admin) + WEB_ID=$($dccmd ps -q web) + if [ "$KEY_CONNECTOR_ENABLED" = true ]; then - KEYCONNECTOR_ID=$(docker-compose ps -q key-connector) + KEYCONNECTOR_ID=$($dccmd ps -q key-connector) fi if [ $KEYCONNECTOR_ID ] && @@ -228,6 +235,46 @@ function update() { dotnet Setup.dll -update 1 -os $OS -corev $COREVERSION -webv $WEBVERSION -keyconnectorv $KEYCONNECTORVERSION } +function uninstall() { + echo -e -n "${RED}(WARNING: UNINSTALL STARTED) Would you like to save the database files? (y/n): ${NC}" + read KEEP_DATABASE + + if [ "$KEEP_DATABASE" == "y" ] + then + echo "Saving database files." + tar -cvzf "./bitwarden_database.tar.gz" "$OUTPUT_DIR/mssql" + echo -e -n "${RED}(SAVED DATABASE FILES: YES): WARNING: ALL DATA WILL BE REMOVED, INCLUDING THE FOLDER $OUTPUT_DIR): Are you sure you want to uninstall Bitwarden? (y/n): ${NC}" + read UNINSTALL_ACTION + else + echo -e -n "${RED}WARNING: ALL DATA WILL BE REMOVED, INCLUDING THE FOLDER $OUTPUT_DIR): Are you sure you want to uninstall Bitwarden? (y/n): ${NC}" + read UNINSTALL_ACTION + fi + + + if [ "$UNINSTALL_ACTION" == "y" ] + then + echo "Uninstalling Bitwarden..." + dockerComposeDown + echo "Removing $OUTPUT_DIR" + rm -R $OUTPUT_DIR + echo "Removing MSSQL docker volume." + docker volume prune --force --filter="label=com.bitwarden.product=bitwarden" + echo "Bitwarden uninstall complete!" + else + echo -e -n "${CYAN}(!) Bitwarden uninstall canceled. ${NC}" + exit 1 + fi + + echo -e -n "${RED}(!) Would you like to purge all local Bitwarden container images? (y/n): ${NC}" + read PURGE_ACTION + if [ "$PURGE_ACTION" == "y" ] + then + dockerPrune + echo -e -n "${CYAN}Bitwarden uninstall complete! ${NC}" + fi + +} + function printEnvironment() { pullSetup docker run -i --rm --name setup -v $OUTPUT_DIR:/bitwarden \ @@ -285,6 +332,10 @@ case $1 in updatebw updateDatabase ;; + "uninstall") + dockerComposeFiles + uninstall + ;; "rebuild") dockerComposeDown update nopull