Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docker-compose installation script and modified scripts #754

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions database-restore.sh

This file was deleted.

3 changes: 0 additions & 3 deletions restart.sh

This file was deleted.

2 changes: 1 addition & 1 deletion database-backup.sh → scripts/database-backup.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash
export $(grep -v '^#' .env | xargs)
export $(grep -v '^#' ../.env | xargs)
docker exec -i mongodb-praise /usr/bin/mongodump --authenticationDatabase admin --archive -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --db praise_db > database-backup-$(date +"%F-%T").archive
10 changes: 10 additions & 0 deletions scripts/database-restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
if ! test -f "$1" ; then
echo "Could not find database backup"
echo "Usage: database-restore.sh filename\n"
fi
export $(grep -v '^#' ../.env | xargs)
docker-compose -f ../docker-compose.production.yml down -v
docker-compose -f ../docker-compose.production.yml up mongodb -d --remove-orphans
docker exec -i mongodb-praise sh -c 'mongorestore --authenticationDatabase admin --nsInclude=praise_db.* --uri="mongodb://$MONGO_INITDB_ROOT_USERNAME:$MONGO_INITDB_ROOT_PASSWORD@mongodb:$MONGO_PORT/?authSource=admin" --drop --preserveUUID --archive' < $1
docker-compose -f ../docker-compose.production.yml up -d --remove-orphans
10 changes: 10 additions & 0 deletions scripts/docker-compose-latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

sudo apt remove docker-compose -y
sudo rm /usr/local/bin/docker-compose

VERSION=$(curl --silent https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K.*\d')
DESTINATION=/usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m) -o $DESTINATION
sudo chmod 755 $DESTINATION
docker-compose version
171 changes: 171 additions & 0 deletions scripts/multi-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#!/bin/bash

# Define Variables
WORKING_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PRAISE_HOME="$(dirname "${WORKING_DIR}")"

# Define Random Passwords
MONGO_INITDB_ROOT_PASSWORD=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-32} | head -n 1)
MONGO_PASSWORD=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-32} | head -n 1)
JWT_SECRET=$(cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-32} | head -n 1)

# Define Random Ports
MONGO_PORT=$(comm -23 <(seq 27017 27100 | sort) <(ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1)
API_PORT=$(comm -23 <(seq 8080 8180 | sort) <(ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1)
FRONTEND_PORT=$(comm -23 <(seq 3000 3100 | sort) <(ss -Htan | awk '{print $4}' | cut -d':' -f2 | sort -u) | shuf | head -n 1)

installQuestions() {

echo "Welcome to the Praise Setup!"
echo "I need to ask you a few questions before starting the setup."

# Detect public IPv4 or IPv6 address and pre-fill for the user
PUBLIC_IP=$(curl ifconfig.me)
read -rp "IPv4 or IPv6 public address: " -e -i "${PUBLIC_IP}" PUBLIC_IP

# Ask User for the Desirted Domain Name for the Praise Bot Server
HOST=""
read -rp "What would you like the Praise Bot Domain Name to be? " -e -i "${HOST}" HOST
# Ask the user for the DISCORD TOKEN
DISCORD_TOKEN=""
read -rp "What is your Discord Token? " -e -i "${DISCORD_TOKEN}" DISCORD_TOKEN
# Ask the user for the DISCORD CLIENT ID
DISCORD_CLIENT_ID=""
read -rp "What is your Discord Client ID? " -e -i "${DISCORD_CLIENT_ID}" DISCORD_CLIENT_ID
# Ask the user for the DISCORD GUILD ID
DISCORD_GUILD_ID=""
read -rp "What is your Discord Guild ID? " -e -i "${DISCORD_GUILD_ID}" DISCORD_GUILD_ID
# Ask the user for the ADMINS wallet Address
ADMINS=""
read -rp "What will your praise bot server admin wallet address be (Please insert the addresses separated by comma)? " -e -i "${ADMINS}" ADMINS
echo
echo "Okay, that was all I needed. We are ready to setup your Praise server now."
read -n1 -r -p "Press any key to continue..."
}

## Configure Praise
configure_praise () {
echo "
###########################################################################
## GENERAL ##

# Running through Docker: NODE_ENV=production
NODE_ENV=$PRAISE_ENV

###########################################################################
## DATABASE ##

# Running through Docker: MONGO_HOST=mongodb
# Running outside Docker: MONGO_HOST=localhost
MONGO_HOST=mongodb
MONGO_DB=praise_db
MONGO_PORT=$MONGO_PORT
MONGO_INITDB_ROOT_USERNAME=praiseDbRootUsername
MONGO_INITDB_ROOT_PASSWORD=$MONGO_INITDB_ROOT_PASSWORD
MONGO_USERNAME=praiseDbUsername
MONGO_PASSWORD=$MONGO_PASSWORD


###########################################################################
## HOST ##

# The fully qualified domain name for the host where you are running Praise
# For development: HOST=localhost
HOST=$HOST

###########################################################################
## API ##

# Full URL to the host where the API is running.
# When running in development, the URL should also include API_PORT
API_URL=https://$HOST

# The API is accessed on this port. In production this port is not exposed
# externally but API is accessed on {$API_URL}/api
API_PORT=$API_PORT

# Comma separated list of ethereum addresses with admnin access to the API
ADMINS=$ADMINS

# API authentication
JWT_SECRET=$JWT_SECRET
# expires after 1 hour of inactivity, or 3 days
JWT_ACCESS_EXP=3600
JWT_REFRESH_EXP=25920000

###########################################################################
## FRONTEND ##

# Full URL to the host (and optionally port) where frontend is being served
FRONTEND_URL=https://$HOST

## FRONTEND - DEVELOPMENT ONLY ##

# Full URL to host where API is running. This variable is not currently used in production.
# Why? The frontend is built as a static website and cannot easily accept
# env variables. There are workarounds but we haven't prioritised to implement them yet.
#
# https://jakobzanker.de/blog/inject-environment-variables-into-a-react-app-docker-on-runtime/
REACT_APP_SERVER_URL=https://$HOST

# Port number used when running frontend for development, outside of Docker
FRONTEND_PORT=$FRONTEND_PORT

###########################################################################
## DISCORD_BOT ##

DISCORD_TOKEN=$DISCORD_TOKEN
DISCORD_CLIENT_ID=$DISCORD_CLIENT_ID
DISCORD_GUILD_ID=$DISCORD_GUILD_ID

###########################################################################
## LOGGING ##

# options: error, warn, info, http, debug
LOGGER_LEVEL=warn" > $PRAISE_HOME/.env
}

praise_env () {
echo "Please choose which env you want to install"
echo "1) production (Stable)"
echo "2) staging (Dev)"
read -rp "Please select which number [1/2]: " -e -i "1" env
until [[ "$env" =~ ^[12]*$ ]]; do
echo "$env: invalid selection."
read -rp "Confirm Praise env? [1/2]: " -e -i "1" env
done
if [[ "$env" =~ ^[1]$ ]]; then
export PRAISE_ENV=production
echo $PRAISE_ENV
sleep 1
elif [[ "$env" =~ ^[2]$ ]]; then
export PRAISE_ENV=development
echo $PRAISE_ENV
sleep 1
else
echo "Invalid Option, Praise Setup Aborted"
sleep 1
exit 0
fi
}

configure_compose () {
# Create a different copy of the compose file that has modified configs for each instance
cp $PRAISE_HOME/docker-compose.$PRAISE_ENV.yml $PRAISE_HOME/docker-compose.multi.yml

sed -i "s/- 80:80//" $PRAISE_HOME/docker-compose.multi.yml
sed -i "s/- 443:443/- \$FRONTEND_PORT:\$FRONTEND_PORT/" $PRAISE_HOME/docker-compose.multi.yml
sed -i "s/container_name: .*mongodb-praise/container_name: ${HOST}-mongodb-praise/" $PRAISE_HOME/docker-compose.multi.yml
sed -i "s/container_name: .*api-praise/container_name: ${HOST}-api-praise/" $PRAISE_HOME/docker-compose.multi.yml
sed -i "s/container_name: .*discord-bot-praise/container_name: ${HOST}-discord-bot-praise/" $PRAISE_HOME/docker-compose.multi.yml
sed -i "s/container_name: .*frontend-praise/container_name: ${HOST}-frontend-praise/" $PRAISE_HOME/docker-compose.multi.yml
}

main () {
praise_env
installQuestions
configure_compose
configure_praise
}

main
28 changes: 28 additions & 0 deletions scripts/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Praise Scripts
Praise comes with a few bash scripts to simplify the management of a Praise installation.
Run scripts using:
```
bash scriptname.sh
```
## docker-compose-latest.sh
Installs the latest version of docker-compose automatically.
**Note:** This is a prerequisite to run all praise services
## setup.sh
Builds and runs the Praise setup script. Run this script before starting Praise the first time.
## start.sh
Starts all Praise services.
## restart.sh
Restarts all Praise services. Run this command after changing the server settings.
## upgrade.sh
Downloads new server images and restarts Praise to perform the upgrade.
## database-backup.sh
Makes a full backup of the database. Backup is saved as a file in the current folder. Script uses login information in .env.
## database-restore.sh
**Usage:**
```
bash database-restore.sh [filename]
```
Deletes the currently active database and replaces it with data from the backup.
## reset.sh
Shuts down all running Praise services, deletes all containers and images.
**Warning**: Use with caution. N.b. The server setting are not reset, all database passwords etc are left untouched.
8 changes: 2 additions & 6 deletions reset.sh → scripts/reset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo "Shutting down containers..."
echo "Shutting down containers, and removing volumes..."
echo
docker compose -f ./docker-compose.production.yml down
docker-compose -f ../docker-compose.production.yml down -v
echo
echo "Deleting images..."
echo
docker rmi -f $(docker images -aq)
echo
echo "Deleting volumes..."
echo
docker volume rm $(docker volume ls -q)
fi
3 changes: 3 additions & 0 deletions scripts/restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker-compose -f ../docker-compose.production.yml down
docker-compose -f ../docker-compose.production.yml up
Loading