From a34e19206ca502df551fbefc5c43ee3cc3baedab Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 23 Aug 2017 16:15:42 -0400 Subject: [PATCH] condense scripts into run script --- scripts/bitwarden.ps1 | 50 ++++++++++++++++++++++++------- scripts/bitwarden.sh | 57 ++++++++++++++++++++++++++---------- scripts/run.ps1 | 57 ++++++++++++++++++++++++++++++++++++ scripts/run.sh | 68 +++++++++++++++++++++++++++++++++++++++++++ scripts/start.ps1 | 23 --------------- scripts/start.sh | 37 ----------------------- scripts/stop.ps1 | 13 --------- scripts/stop.sh | 20 ------------- scripts/update-db.ps1 | 8 ----- scripts/update-db.sh | 13 --------- 10 files changed, 206 insertions(+), 140 deletions(-) create mode 100644 scripts/run.ps1 create mode 100644 scripts/run.sh delete mode 100644 scripts/start.ps1 delete mode 100644 scripts/start.sh delete mode 100644 scripts/stop.ps1 delete mode 100644 scripts/stop.sh delete mode 100644 scripts/update-db.ps1 delete mode 100644 scripts/update-db.sh diff --git a/scripts/bitwarden.ps1 b/scripts/bitwarden.ps1 index 4c02f2c8db..c6e62509a5 100644 --- a/scripts/bitwarden.ps1 +++ b/scripts/bitwarden.ps1 @@ -5,6 +5,7 @@ param ( [switch] $stop, [switch] $update, [switch] $updatedb, + [switch] $updateself, [string] $output = "" ) @@ -22,8 +23,13 @@ Write-Host " Open source password management solutions Copyright 2015-${year}, 8bit Solutions LLC https://bitwarden.com, https://github.com/bitwarden + +=================================================== " +# Setup + +$scriptName = $MyInvocation.MyCommand.Name $dir = Split-Path -Parent $MyInvocation.MyCommand.Path if($output -eq "") { $output="${dir}\bitwarden" @@ -41,26 +47,45 @@ if(!(Test-Path -Path $scriptsDir)) { New-Item -ItemType directory -Path $scriptsDir | Out-Null } -function Download-Run-Files { - Invoke-RestMethod -OutFile $scriptsDir\start.ps1 -Uri "${githubBaseUrl}/scripts/start.ps1" - Invoke-RestMethod -OutFile $scriptsDir\stop.ps1 -Uri "${githubBaseUrl}/scripts/stop.ps1" +# Functions + +function Download-Self { + Invoke-RestMethod -OutFile $scriptsDir\$scriptName -Uri "${githubBaseUrl}/scripts/bitwarden.ps1" +} + +function Download-Install { + Invoke-RestMethod -OutFile $scriptsDir\install.ps1 ` -Uri "${githubBaseUrl}/scripts/install.ps1" +} + +function Download-Run-File { + Invoke-RestMethod -OutFile $scriptsDir\run.ps1 -Uri "${githubBaseUrl}/scripts/run.ps1" +} + +function Download-Docker-Files { Invoke-RestMethod -OutFile $dockerDir\docker-compose.yml -Uri "${githubBaseUrl}/docker/docker-compose.yml" Invoke-RestMethod -OutFile $dockerDir\docker-compose.macwin.yml ` -Uri "${githubBaseUrl}/docker/docker-compose.macwin.yml" Invoke-RestMethod -OutFile $dockerDir\global.env -Uri "${githubBaseUrl}/docker/global.env" Invoke-RestMethod -OutFile $dockerDir\mssql.env -Uri "${githubBaseUrl}/docker/mssql.env" } +function Download-All-Files { + Download-Run-File + Download-Docker-Files +} + +# Commands + if($install) { - Invoke-RestMethod -OutFile $scriptsDir\install.ps1 ` -Uri "${githubBaseUrl}/scripts/install.ps1" + Download-Install Invoke-Expression "$scriptsDir\install.ps1 -outputDir $output" } elseif($start -Or $restart) { if(!(Test-Path -Path $dockerDir)) { New-Item -ItemType directory -Path $dockerDir | Out-Null - Download-Run-Files + Download-All-Files } - Invoke-Expression "$scriptsDir\start.ps1 -outputDir $output -dockerDir $dockerDir" + Invoke-Expression "$scriptsDir\run.ps1 -restart -outputDir $output -dockerDir $dockerDir" } elseif($update) { if(Test-Path -Path $dockerDir) { @@ -68,15 +93,18 @@ elseif($update) { } New-Item -ItemType directory -Path $dockerDir | Out-Null - Download-Run-Files - Invoke-Expression "$scriptsDir\start.ps1 -outputDir $output -dockerDir $dockerDir" + Download-All-Files + Invoke-Expression "$scriptsDir\run.ps1 -restart -outputDir $output -dockerDir $dockerDir" } elseif($updatedb) { - Invoke-RestMethod -OutFile $scriptsDir\update-db.ps1 -Uri "${githubBaseUrl}/scripts/update-db.ps1" - Invoke-Expression "$scriptsDir\update-db.ps1 -outputDir $output" + Invoke-Expression "$scriptsDir\run.ps1 -updatedb -outputDir $output -dockerDir $dockerDir" } elseif($stop) { - Invoke-Expression "$scriptsDir\stop.ps1 -dockerDir $dockerDir" + Invoke-Expression "$scriptsDir\run.ps1 -stop -outputDir $output -dockerDir $dockerDir" +} +elseif($updateself) { + Download-Self + echo "Updated self." } else { echo "No command found." diff --git a/scripts/bitwarden.sh b/scripts/bitwarden.sh index 3fa46690de..13e3e00f2d 100755 --- a/scripts/bitwarden.sh +++ b/scripts/bitwarden.sh @@ -15,8 +15,13 @@ Open source password management solutions Copyright 2015-$(date +'%Y'), 8bit Solutions LLC https://bitwarden.com, https://github.com/bitwarden +=================================================== + EOF +# Setup + +SCRIPT_NAME=`basename "$0"` DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" OUTPUT="$DIR/bitwarden" if [ $# -eq 2 ] @@ -44,30 +49,50 @@ then mkdir $SCRIPTS_DIR fi -function downloadRunFiles() { - curl -s -o $SCRIPTS_DIR/start.sh $GITHUB_BASE_URL/scripts/start.sh - chmod u+x $SCRIPTS_DIR/start.sh - curl -s -o $SCRIPTS_DIR/stop.sh $GITHUB_BASE_URL/scripts/stop.sh - chmod u+x $SCRIPTS_DIR/stop.sh +# Functions + +function downloadSelf() { + curl -s -o $SCRIPTS_DIR/$SCRIPT_NAME $GITHUB_BASE_URL/scripts/bitwarden.sh + chmod u+x $SCRIPTS_DIR/$SCRIPT_NAME +} + +function downloadInstall() { + curl -s -o $SCRIPTS_DIR/install.sh $GITHUB_BASE_URL/scripts/install.sh + chmod u+x $SCRIPTS_DIR/install.sh +} + +function downloadRunFile() { + curl -s -o $SCRIPTS_DIR/run.sh $GITHUB_BASE_URL/scripts/run.sh + chmod u+x $SCRIPTS_DIR/run.sh +} + +function downloadDockerFiles() { curl -s -o $DOCKER_DIR/docker-compose.yml $GITHUB_BASE_URL/docker/docker-compose.yml curl -s -o $DOCKER_DIR/docker-compose.$OS.yml $GITHUB_BASE_URL/docker/docker-compose.$OS.yml curl -s -o $DOCKER_DIR/global.env $GITHUB_BASE_URL/docker/global.env curl -s -o $DOCKER_DIR/mssql.env $GITHUB_BASE_URL/docker/mssql.env } +function downloadAllFiles() { + downloadRunFile + downloadDockerFiles +} + +# Commands + if [ "$1" == "install" ] then - curl -s -o $SCRIPTS_DIR/install.sh $GITHUB_BASE_URL/scripts/install.sh - chmod u+x $SCRIPTS_DIR/install.sh + downloadInstall $SCRIPTS_DIR/install.sh $OUTPUT elif [ "$1" == "start" -o "$1" == "restart" ] then if [ ! -d "$DOCKER_DIR" ] then mkdir $DOCKER_DIR - downloadRunFiles + downloadAllFiles fi - $SCRIPTS_DIR/start.sh $OUTPUT $DOCKER_DIR + + $SCRIPTS_DIR/run.sh restart $OUTPUT $DOCKER_DIR elif [ "$1" == "update" ] then if [ -d "$DOCKER_DIR" ] @@ -76,16 +101,18 @@ then fi mkdir $DOCKER_DIR - downloadRunFiles - $SCRIPTS_DIR/start.sh $OUTPUT $DOCKER_DIR + downloadAllFiles + $SCRIPTS_DIR/run.sh restart $OUTPUT $DOCKER_DIR elif [ "$1" == "updatedb" ] then - curl -s -o $SCRIPTS_DIR/update-db.sh $GITHUB_BASE_URL/scripts/update-db.sh - chmod u+x $SCRIPTS_DIR/update-db.sh - $SCRIPTS_DIR/update-db.sh $OUTPUT + $SCRIPTS_DIR/run.sh updatedb $OUTPUT $DOCKER_DIR elif [ "$1" == "stop" ] then - $SCRIPTS_DIR/stop.sh $DOCKER_DIR + $SCRIPTS_DIR/run.sh stop $OUTPUT $DOCKER_DIR +elif [ "$1" == "updateself" ] +then + downloadSelf + echo "Updated self." else echo "No command found." fi diff --git a/scripts/run.ps1 b/scripts/run.ps1 new file mode 100644 index 0000000000..b6c090d9f8 --- /dev/null +++ b/scripts/run.ps1 @@ -0,0 +1,57 @@ +param ( + [string]$outputDir = "../.", + [string]$dockerDir = "", + [switch] $start, + [switch] $restart, + [switch] $stop, + [switch] $updatedb +) + +# Setup + +$dir = Split-Path -Parent $MyInvocation.MyCommand.Path +if($dockerDir -eq "") { + $dockerDir="${dir}\..\docker" +} + +# Functions + +function Docker-Compose-Up { + docker-compose -f ${dockerDir}\docker-compose.yml -f ${dockerDir}\docker-compose.macwin.yml up -d +} + +function Docker-Compose-Down { + docker-compose -f ${dockerDir}\docker-compose.yml -f ${dockerDir}\docker-compose.macwin.yml down +} + +function Docker-Prune { + docker image prune -f +} + +function Update-Lets-Encrypt { + if(Test-Path -Path "${outputDir}/letsencrypt") { + docker run -it --rm --name certbot -p 443:443 -p 80:80 -v $outputDir/letsencrypt:/etc/letsencrypt/ certbot/certbot ` + renew --logs-dir /etc/letsencrypt/logs + } +} + +function Update-Database { + docker run -it --rm --name setup --network container:mssql -v ${outputDir}:/bitwarden bitwarden/setup ` + dotnet Setup.dll -update 1 -db 1 + echo "Database update complete" +} + +# Commands + +if($start -Or $restart) { + Docker-Compose-Down + Update-Lets-Encrypt + Docker-Compose-Up + Docker-Prune +} +elseif($stop) { + Docker-Compose-Down +} +elseif($updatedb) { + Update-Database +} diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100644 index 0000000000..b0fb823ee5 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +set -e + +# Setup + +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +OUTPUT_DIR="../." +if [ $# -eq 2 ] +then + OUTPUT_DIR=$2 +fi + +DOCKER_DIR=$DIR/../docker +if [ $# -eq 3 ] +then + DOCKER_DIR=$3 +fi + +OS="linux" +if [ "$(uname)" == "Darwin" ] +then + OS="macwin" +fi + +# Functions + +function dockerComposeUp() { + docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.$OS.yml up -d +} + +function dockerComposeDown() { + docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.$OS.yml down +} + +function dockerPrune() { + docker image prune -f +} + +function updateLetsEncrypt() { + if [ -d "${outputDir}/letsencrypt" ] + then + docker run -it --rm --name certbot -p 443:443 -p 80:80 -v $OUTPUT_DIR/letsencrypt:/etc/letsencrypt/ certbot/certbot \ + renew --logs-dir /etc/letsencrypt/logs + fi +} + +function updateDatabase() { + docker run -it --rm --name setup --network container:mssql -v $OUTPUT_DIR:/bitwarden bitwarden/setup \ + dotnet Setup.dll -update 1 -db 1 + echo "Database update complete" +} + +# Commands + +if [ "$1" == "start" -o "$1" == "restart" ] +then + dockerComposeDown + updateLetsEncrypt + dockerComposeUp + dockerPrune +elif [ "$1" == "stop" ] +then + dockerComposeDown +elif [ "$1" == "upadtedb" ] +then + updateDatabase +fi diff --git a/scripts/start.ps1 b/scripts/start.ps1 deleted file mode 100644 index 8ce56b7d60..0000000000 --- a/scripts/start.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -param ( - [string]$outputDir = "../.", - [string]$dockerDir = "" -) - -$dir = Split-Path -Parent $MyInvocation.MyCommand.Path -if($dockerDir -eq "") { - $dockerDir="${dir}\..\docker" -} - -docker --version -docker-compose --version - -docker-compose -f ${dockerDir}\docker-compose.yml -f ${dockerDir}\docker-compose.macwin.yml down - -$letsEncryptPath = "${outputDir}/letsencrypt" -if(Test-Path -Path $letsEncryptPath) { - docker run -it --rm --name certbot -p 443:443 -p 80:80 -v $outputDir/letsencrypt:/etc/letsencrypt/ certbot/certbot ` - renew --logs-dir /etc/letsencrypt/logs -} - -docker-compose -f ${dockerDir}\docker-compose.yml -f ${dockerDir}\docker-compose.macwin.yml up -d -docker image prune -f diff --git a/scripts/start.sh b/scripts/start.sh deleted file mode 100644 index fa57bd6d6d..0000000000 --- a/scripts/start.sh +++ /dev/null @@ -1,37 +0,0 @@ -#!/usr/bin/env bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" - -OUTPUT_DIR="../." -if [ $# -eq 1 ] -then - OUTPUT_DIR=$1 -fi - -DOCKER_DIR=$DIR/../docker -if [ $# -eq 2 ] -then - DOCKER_DIR=$2 -fi - -OS="linux" -if [ "$(uname)" == "Darwin" ] -then - OS="macwin" -fi - -docker --version -docker-compose --version - -docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.$OS.yml down - -LETS_ENCRYPT_PATH="${outputDir}/letsencrypt" -if [ -d "${LETS_ENCRYPT_PATH}" ] -then - docker run -it --rm --name certbot -p 443:443 -p 80:80 -v $OUTPUT_DIR/letsencrypt:/etc/letsencrypt/ certbot/certbot \ - renew --logs-dir /etc/letsencrypt/logs -fi - -docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.$OS.yml up -d -docker image prune -f diff --git a/scripts/stop.ps1 b/scripts/stop.ps1 deleted file mode 100644 index 29ab436f6a..0000000000 --- a/scripts/stop.ps1 +++ /dev/null @@ -1,13 +0,0 @@ -param ( - [string] $dockerDir = "" -) - -$dir = Split-Path -Parent $MyInvocation.MyCommand.Path -if($dockerDir -eq "") { - $dockerDir="${dir}\..\docker" -} - -docker --version -docker-compose --version - -docker-compose -f ${dockerDir}\docker-compose.yml -f ${dockerDir}\docker-compose.macwin.yml down diff --git a/scripts/stop.sh b/scripts/stop.sh deleted file mode 100644 index c85c09ca55..0000000000 --- a/scripts/stop.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash -set -e - -DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -DOCKER_DIR=$DIR/../docker -if [ $# -eq 1 ] -then - DOCKER_DIR=$1 -fi - -OS="linux" -if [ "$(uname)" == "Darwin" ] -then - OS="macwin" -fi - -docker --version -docker-compose --version - -docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.$OS.yml down diff --git a/scripts/update-db.ps1 b/scripts/update-db.ps1 deleted file mode 100644 index 916d692a3e..0000000000 --- a/scripts/update-db.ps1 +++ /dev/null @@ -1,8 +0,0 @@ -param ( - [string]$outputDir = "../." -) - -docker run -it --rm --name setup --network container:mssql -v ${outputDir}:/bitwarden bitwarden/setup ` - dotnet Setup.dll -update 1 -db 1 - -echo "Database update complete" diff --git a/scripts/update-db.sh b/scripts/update-db.sh deleted file mode 100644 index 893ad42803..0000000000 --- a/scripts/update-db.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash -set -e - -OUTPUT_DIR="../." -if [ $# -eq 1 ] -then - OUTPUT_DIR=$1 -fi - -docker run -it --rm --name setup --network container:mssql -v $OUTPUT_DIR:/bitwarden bitwarden/setup \ - dotnet Setup.dll -update 1 -db 1 - -echo "Database update complete"