From c924af2adeea18e6a01d08bf7955be265ed5cac4 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 18 Sep 2017 12:11:03 -0400 Subject: [PATCH] safety checks on output dir --- scripts/bitwarden.ps1 | 37 ++++++++++++++++++++++++----------- scripts/bitwarden.sh | 45 +++++++++++++++++++++++++++++++++---------- 2 files changed, 61 insertions(+), 21 deletions(-) diff --git a/scripts/bitwarden.ps1 b/scripts/bitwarden.ps1 index 31bd45c48f..57a087c412 100644 --- a/scripts/bitwarden.ps1 +++ b/scripts/bitwarden.ps1 @@ -40,29 +40,30 @@ if($output -eq "") { $output="${dir}\bitwarden" } -if(!(Test-Path -Path $output)) { - New-Item -ItemType directory -Path $output | Out-Null -} - $scriptsDir = "${output}\scripts" $dockerDir = "${output}\docker" $githubBaseUrl = "https://raw.githubusercontent.com/bitwarden/core/master" -if(!(Test-Path -Path $scriptsDir)) { - New-Item -ItemType directory -Path $scriptsDir | Out-Null -} - # Functions function Download-Self { + if(!(Test-Path -Path $scriptsDir)) { + New-Item -ItemType directory -Path $scriptsDir | Out-Null + } Invoke-RestMethod -OutFile $scriptPath -Uri "${githubBaseUrl}/scripts/bitwarden.ps1" } function Download-Install { + if(!(Test-Path -Path $scriptsDir)) { + New-Item -ItemType directory -Path $scriptsDir | Out-Null + } Invoke-RestMethod -OutFile $scriptsDir\install.ps1 ` -Uri "${githubBaseUrl}/scripts/install.ps1" } function Download-Run-File { + if(!(Test-Path -Path $scriptsDir)) { + New-Item -ItemType directory -Path $scriptsDir | Out-Null + } Invoke-RestMethod -OutFile $scriptsDir\run.ps1 -Uri "${githubBaseUrl}/scripts/run.ps1" } @@ -73,18 +74,28 @@ function Download-Docker-Files { Invoke-RestMethod -OutFile $dockerDir\mssql.env -Uri "${githubBaseUrl}/docker/mssql.env" } -function Download-All-Files { - Download-Run-File - Download-Docker-Files +function Check-Output-Dir-Exists { + if(!(Test-Path -Path $output)) { + throw "Cannot find a bitwarden installation at $output." + } +} + +function Check-Output-Dir-Not-Exists { + if(Test-Path -Path $output) { + throw "Looks like bitwarden is already installed at $output." + } } # Commands if($install) { + Check-Output-Dir-Not-Exists + New-Item -ItemType directory -Path $output | Out-Null Download-Install Invoke-Expression "$scriptsDir\install.ps1 -outputDir $output" } elseif($start -Or $restart) { + Check-Output-Dir-Exists if(!(Test-Path -Path $dockerDir)) { New-Item -ItemType directory -Path $dockerDir | Out-Null Download-All-Files @@ -93,6 +104,7 @@ elseif($start -Or $restart) { Invoke-Expression "$scriptsDir\run.ps1 -restart -outputDir $output -dockerDir $dockerDir" } elseif($update) { + Check-Output-Dir-Exists if(Test-Path -Path $dockerDir) { Remove-Item -Recurse -Force $dockerDir | Out-Null } @@ -103,12 +115,15 @@ elseif($update) { Invoke-Expression "$scriptsDir\run.ps1 -updatedb -outputDir $output -dockerDir $dockerDir" } elseif($updatedb) { + Check-Output-Dir-Exists Invoke-Expression "$scriptsDir\run.ps1 -updatedb -outputDir $output -dockerDir $dockerDir" } elseif($stop) { + Check-Output-Dir-Exists Invoke-Expression "$scriptsDir\run.ps1 -stop -outputDir $output -dockerDir $dockerDir" } elseif($updateself) { + Check-Output-Dir-Exists Download-Self echo "Updated self." } diff --git a/scripts/bitwarden.sh b/scripts/bitwarden.sh index 3f42dc3b32..a7066b5f05 100755 --- a/scripts/bitwarden.sh +++ b/scripts/bitwarden.sh @@ -35,11 +35,6 @@ then OUTPUT=$2 fi -if [ ! -d "$OUTPUT" ] -then - mkdir $OUTPUT -fi - OS="linux" if [ "$(uname)" == "Darwin" ] then @@ -50,24 +45,31 @@ SCRIPTS_DIR="$OUTPUT/scripts" DOCKER_DIR="$OUTPUT/docker" GITHUB_BASE_URL="https://raw.githubusercontent.com/bitwarden/core/master" -if [ ! -d "$SCRIPTS_DIR" ] -then - mkdir $SCRIPTS_DIR -fi - # Functions function downloadSelf() { + if [ ! -d "$SCRIPTS_DIR" ] + then + mkdir $SCRIPTS_DIR + fi curl -s -o $SCRIPT_PATH $GITHUB_BASE_URL/scripts/bitwarden.sh chmod u+x $SCRIPT_PATH } function downloadInstall() { + if [ ! -d "$SCRIPTS_DIR" ] + then + mkdir $SCRIPTS_DIR + fi curl -s -o $SCRIPTS_DIR/install.sh $GITHUB_BASE_URL/scripts/install.sh chmod u+x $SCRIPTS_DIR/install.sh } function downloadRunFile() { + if [ ! -d "$SCRIPTS_DIR" ] + then + mkdir $SCRIPTS_DIR + fi curl -s -o $SCRIPTS_DIR/run.sh $GITHUB_BASE_URL/scripts/run.sh chmod u+x $SCRIPTS_DIR/run.sh } @@ -84,14 +86,33 @@ function downloadAllFiles() { downloadDockerFiles } +function checkOutputDirExists() { + if [ ! -d "$OUTPUT" ] + then + echo "Cannot find a bitwarden installation at $OUTPUT." + exit 1 + fi +} + +function checkOutputDirNotExists() { + if [ -d "$OUTPUT" ] + then + echo "Looks like bitwarden is already installed at $OUTPUT." + exit 1 + fi +} + # Commands if [ "$1" == "install" ] then + checkOutputDirNotExists + mkdir $OUTPUT downloadInstall $SCRIPTS_DIR/install.sh $OUTPUT elif [ "$1" == "start" -o "$1" == "restart" ] then + checkOutputDirExists if [ ! -d "$DOCKER_DIR" ] then mkdir $DOCKER_DIR @@ -101,6 +122,7 @@ then $SCRIPTS_DIR/run.sh restart $OUTPUT $DOCKER_DIR elif [ "$1" == "update" ] then + checkOutputDirExists if [ -d "$DOCKER_DIR" ] then rm -rf $DOCKER_DIR @@ -112,12 +134,15 @@ then $SCRIPTS_DIR/run.sh updatedb $OUTPUT $DOCKER_DIR elif [ "$1" == "updatedb" ] then + checkOutputDirExists $SCRIPTS_DIR/run.sh updatedb $OUTPUT $DOCKER_DIR elif [ "$1" == "stop" ] then + checkOutputDirExists $SCRIPTS_DIR/run.sh stop $OUTPUT $DOCKER_DIR elif [ "$1" == "updateself" ] then + checkOutputDirExists downloadSelf echo "Updated self." else