diff --git a/scripts/bitwarden.ps1 b/scripts/bitwarden.ps1 index b8bb13f5c3..e57c7ad526 100644 --- a/scripts/bitwarden.ps1 +++ b/scripts/bitwarden.ps1 @@ -10,6 +10,7 @@ param ( [switch] $updatedb, [switch] $updaterun, [switch] $updateself, + [switch] $uninstall, [switch] $help, [string] $output = "" ) @@ -68,6 +69,7 @@ Available commands: -updaterun -updateself -updateconf +-uninstall -renewcert -rebuild -help @@ -155,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/scripts/bitwarden.sh b/scripts/bitwarden.sh index 200b2d256d..280a5abec0 100755 --- a/scripts/bitwarden.sh +++ b/scripts/bitwarden.sh @@ -95,6 +95,7 @@ updatedb updaterun updateself updateconf +uninstall renewcert rebuild help @@ -149,6 +150,10 @@ case $1 in "updateself") downloadSelf && echo "Updated self." && exit ;; + "uninstall") + checkOutputDirExists + $SCRIPTS_DIR/run.sh uninstall $OUTPUT + ;; "help") listCommands ;; diff --git a/scripts/run.ps1 b/scripts/run.ps1 index 78e560c61b..b56e65811c 100644 --- a/scripts/run.ps1 +++ b/scripts/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/scripts/run.sh b/scripts/run.sh index 64856b81a7..656619ff57 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -4,6 +4,7 @@ set -e # Setup CYAN='\033[0;36m' +RED='\033[1;31m' NC='\033[0m' # No Color DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" @@ -228,6 +229,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 +326,10 @@ case $1 in updatebw updateDatabase ;; + "uninstall") + dockerComposeFiles + uninstall + ;; "rebuild") dockerComposeDown update nopull diff --git a/util/Setup/Templates/DockerCompose.hbs b/util/Setup/Templates/DockerCompose.hbs index c5e9f84df7..e13cfa844d 100644 --- a/util/Setup/Templates/DockerCompose.hbs +++ b/util/Setup/Templates/DockerCompose.hbs @@ -214,6 +214,8 @@ services: volumes: mssql_data: + labels: + com.bitwarden.product: bitwarden {{/if}} networks: