2017-08-23 22:15:42 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# Setup
|
|
|
|
|
2018-05-31 18:05:26 +02:00
|
|
|
CYAN='\033[0;36m'
|
|
|
|
NC='\033[0m' # No Color
|
|
|
|
|
2017-08-23 22:15:42 +02:00
|
|
|
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
|
|
|
2018-05-31 18:05:26 +02:00
|
|
|
OUTPUT_DIR=".."
|
2017-08-23 22:51:36 +02:00
|
|
|
if [ $# -gt 1 ]
|
2017-08-23 22:15:42 +02:00
|
|
|
then
|
|
|
|
OUTPUT_DIR=$2
|
|
|
|
fi
|
|
|
|
|
2017-11-09 04:24:23 +01:00
|
|
|
COREVERSION="latest"
|
2017-08-24 02:05:53 +02:00
|
|
|
if [ $# -gt 2 ]
|
2017-08-23 22:15:42 +02:00
|
|
|
then
|
2017-11-09 04:24:23 +01:00
|
|
|
COREVERSION=$3
|
2017-08-23 22:15:42 +02:00
|
|
|
fi
|
|
|
|
|
2017-11-09 04:24:23 +01:00
|
|
|
WEBVERSION="latest"
|
|
|
|
if [ $# -gt 3 ]
|
|
|
|
then
|
|
|
|
WEBVERSION=$4
|
|
|
|
fi
|
|
|
|
|
|
|
|
OS="lin"
|
2018-05-31 18:05:26 +02:00
|
|
|
[ "$(uname)" == "Darwin" ] && OS="mac"
|
|
|
|
ENV_DIR="$OUTPUT_DIR/env"
|
|
|
|
DOCKER_DIR="$OUTPUT_DIR/docker"
|
|
|
|
|
2018-05-31 19:49:41 +02:00
|
|
|
# Initialize UID/GID which will be used to run services from within containers
|
2018-05-31 18:05:26 +02:00
|
|
|
if ! grep -q "^LOCAL_UID=" $ENV_DIR/uid.env 2>/dev/null || ! grep -q "^LOCAL_GID=" $ENV_DIR/uid.env 2>/dev/null
|
2017-08-23 22:15:42 +02:00
|
|
|
then
|
2018-05-31 18:05:26 +02:00
|
|
|
LUID="LOCAL_UID=`id -u $USER`"
|
|
|
|
[ "$LUID" == "LOCAL_UID=0" ] && LUID="LOCAL_UID=65534"
|
|
|
|
LGID="LOCAL_GID=`id -g $USER`"
|
|
|
|
[ "$LGID" == "LOCAL_GID=0" ] && LGID="LOCAL_GID=65534"
|
|
|
|
mkdir -p $ENV_DIR
|
|
|
|
echo $LUID >$ENV_DIR/uid.env
|
|
|
|
echo $LGID >>$ENV_DIR/uid.env
|
2017-08-23 22:15:42 +02:00
|
|
|
fi
|
|
|
|
|
2018-05-31 19:49:41 +02:00
|
|
|
# Backwards compat GID/UID for pre-1.20.0 installations
|
2018-05-31 18:05:26 +02:00
|
|
|
if [[ "$COREVERSION" == *.*.* ]] &&
|
|
|
|
echo -e "1.19.0\n$COREVERSION" | sort -t '.' -k 1,1 -k 2,2 -k 3,3 -n | awk 'END {if($0!="1.19.0") {exit 1}}'
|
|
|
|
then
|
|
|
|
LUID="LOCAL_UID=`id -u $USER`"
|
|
|
|
LGID="LOCAL_GID=`awk -F: '$1=="docker" {print $3}' /etc/group`"
|
|
|
|
if [ "$OS" == "mac" ]
|
|
|
|
then
|
|
|
|
LUID="LOCAL_UID=999"
|
|
|
|
LGID="LOCAL_GID=999"
|
|
|
|
fi
|
|
|
|
echo $LUID >$ENV_DIR/uid.env
|
|
|
|
echo $LGID >>$ENV_DIR/uid.env
|
|
|
|
fi
|
2017-10-04 05:20:09 +02:00
|
|
|
|
2017-08-23 22:15:42 +02:00
|
|
|
# Functions
|
|
|
|
|
2018-05-31 18:05:26 +02:00
|
|
|
function install() {
|
|
|
|
LETS_ENCRYPT="n"
|
2018-06-01 03:47:06 +02:00
|
|
|
echo -e -n "${CYAN}(!)${NC} Enter the domain name for your Bitwarden instance (ex. bitwarden.company.com): "
|
2018-05-31 18:05:26 +02:00
|
|
|
read DOMAIN
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
if [ "$DOMAIN" == "" ]
|
|
|
|
then
|
|
|
|
DOMAIN="localhost"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$DOMAIN" != "localhost" ]
|
|
|
|
then
|
|
|
|
echo -e -n "${CYAN}(!)${NC} Do you want to use Let's Encrypt to generate a free SSL certificate? (y/n): "
|
|
|
|
read LETS_ENCRYPT
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
if [ "$LETS_ENCRYPT" == "y" ]
|
|
|
|
then
|
|
|
|
echo -e -n "${CYAN}(!)${NC} Enter your email address (Let's Encrypt will send you certificate expiration reminders): "
|
|
|
|
read EMAIL
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
mkdir -p $OUTPUT_DIR/letsencrypt
|
|
|
|
docker pull certbot/certbot
|
|
|
|
docker run -it --rm --name certbot -p 80:80 -v $OUTPUT_DIR/letsencrypt:/etc/letsencrypt/ certbot/certbot \
|
2018-05-31 19:49:41 +02:00
|
|
|
certonly --standalone --noninteractive --agree-tos --preferred-challenges http \
|
|
|
|
--email $EMAIL -d $DOMAIN --logs-dir /etc/letsencrypt/logs
|
2018-05-31 18:05:26 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
pullSetup
|
2018-05-31 19:49:41 +02:00
|
|
|
docker run -it --rm --name setup -v $OUTPUT_DIR:/bitwarden \
|
|
|
|
--env-file $ENV_DIR/uid.env bitwarden/setup:$COREVERSION \
|
|
|
|
dotnet Setup.dll -install 1 -domain $DOMAIN -letsencrypt $LETS_ENCRYPT -os $OS \
|
|
|
|
-corev $COREVERSION -webv $WEBVERSION
|
2018-05-31 18:05:26 +02:00
|
|
|
|
|
|
|
echo ""
|
|
|
|
echo "Setup complete"
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:15:42 +02:00
|
|
|
function dockerComposeUp() {
|
2018-02-20 17:15:16 +01:00
|
|
|
if [ -f "${DOCKER_DIR}/docker-compose.override.yml" ]
|
2018-02-20 17:09:47 +01:00
|
|
|
then
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.override.yml up -d
|
|
|
|
else
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml up -d
|
|
|
|
fi
|
2017-08-23 22:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function dockerComposeDown() {
|
2018-02-20 17:15:16 +01:00
|
|
|
if [ -f "${DOCKER_DIR}/docker-compose.override.yml" ]
|
2018-02-20 17:09:47 +01:00
|
|
|
then
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.override.yml down
|
|
|
|
else
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml down
|
|
|
|
fi
|
2017-08-23 22:15:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-27 04:36:25 +02:00
|
|
|
function dockerComposePull() {
|
2018-02-20 17:15:16 +01:00
|
|
|
if [ -f "${DOCKER_DIR}/docker-compose.override.yml" ]
|
2018-02-20 17:09:47 +01:00
|
|
|
then
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml -f $DOCKER_DIR/docker-compose.override.yml pull
|
|
|
|
else
|
|
|
|
docker-compose -f $DOCKER_DIR/docker-compose.yml pull
|
|
|
|
fi
|
2017-08-27 04:36:25 +02:00
|
|
|
}
|
|
|
|
|
2017-08-23 22:15:42 +02:00
|
|
|
function dockerPrune() {
|
2018-08-08 16:04:03 +02:00
|
|
|
docker image prune -f --filter="label=com.bitwarden.product=bitwarden"
|
2017-08-23 22:15:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateLetsEncrypt() {
|
2017-11-09 04:24:23 +01:00
|
|
|
if [ -d "${OUTPUT_DIR}/letsencrypt/live" ]
|
2017-08-23 22:15:42 +02:00
|
|
|
then
|
2017-08-27 04:54:10 +02:00
|
|
|
docker pull certbot/certbot
|
2018-03-27 20:55:33 +02:00
|
|
|
docker run -i --rm --name certbot -p 443:443 -p 80:80 \
|
|
|
|
-v $OUTPUT_DIR/letsencrypt:/etc/letsencrypt/ certbot/certbot \
|
2017-08-23 22:15:42 +02:00
|
|
|
renew --logs-dir /etc/letsencrypt/logs
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDatabase() {
|
2017-10-25 23:21:35 +02:00
|
|
|
pullSetup
|
2018-05-31 18:05:26 +02:00
|
|
|
docker run -i --rm --name setup --network container:bitwarden-mssql \
|
|
|
|
-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
|
2017-08-23 22:15:42 +02:00
|
|
|
echo "Database update complete"
|
|
|
|
}
|
|
|
|
|
2017-10-25 23:21:35 +02:00
|
|
|
function update() {
|
|
|
|
pullSetup
|
2018-05-31 18:05:26 +02:00
|
|
|
docker run -i --rm --name setup -v $OUTPUT_DIR:/bitwarden \
|
|
|
|
--env-file $ENV_DIR/uid.env bitwarden/setup:$COREVERSION \
|
|
|
|
dotnet Setup.dll -update 1 -os $OS -corev $COREVERSION -webv $WEBVERSION
|
2017-10-25 23:21:35 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 17:16:01 +02:00
|
|
|
function printEnvironment() {
|
2017-10-25 23:21:35 +02:00
|
|
|
pullSetup
|
2018-05-31 18:05:26 +02:00
|
|
|
docker run -i --rm --name setup -v $OUTPUT_DIR:/bitwarden \
|
|
|
|
--env-file $ENV_DIR/uid.env bitwarden/setup:$COREVERSION \
|
|
|
|
dotnet Setup.dll -printenv 1 -os $OS -corev $COREVERSION -webv $WEBVERSION
|
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
|
|
|
dockerComposeDown
|
2017-08-27 04:36:25 +02:00
|
|
|
dockerComposePull
|
2017-08-23 22:15:42 +02:00
|
|
|
updateLetsEncrypt
|
|
|
|
dockerComposeUp
|
|
|
|
dockerPrune
|
2017-08-24 17:16:01 +02:00
|
|
|
printEnvironment
|
2017-10-25 23:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function pullSetup() {
|
2017-11-09 04:24:23 +01:00
|
|
|
docker pull bitwarden/setup:$COREVERSION
|
2017-10-25 23:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Commands
|
|
|
|
|
2018-05-31 18:05:26 +02:00
|
|
|
if [ "$1" == "install" ]
|
|
|
|
then
|
|
|
|
install
|
|
|
|
elif [ "$1" == "start" -o "$1" == "restart" ]
|
2017-10-25 23:21:35 +02:00
|
|
|
then
|
|
|
|
restart
|
2017-08-27 04:36:25 +02:00
|
|
|
elif [ "$1" == "pull" ]
|
|
|
|
then
|
|
|
|
dockerComposePull
|
2017-08-23 22:15:42 +02:00
|
|
|
elif [ "$1" == "stop" ]
|
|
|
|
then
|
|
|
|
dockerComposeDown
|
2017-08-23 22:42:39 +02:00
|
|
|
elif [ "$1" == "updatedb" ]
|
2017-08-23 22:15:42 +02:00
|
|
|
then
|
|
|
|
updateDatabase
|
2017-10-25 23:21:35 +02:00
|
|
|
elif [ "$1" == "update" ]
|
|
|
|
then
|
|
|
|
dockerComposeDown
|
|
|
|
update
|
|
|
|
restart
|
2018-03-11 05:53:21 +01:00
|
|
|
echo "Pausing 60 seconds for database to come online. Please wait..."
|
|
|
|
sleep 60
|
2017-10-25 23:21:35 +02:00
|
|
|
updateDatabase
|
2017-08-23 22:15:42 +02:00
|
|
|
fi
|