Skip to content

Commit

Permalink
feat: major updates to install scripts, capture sigterms
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Nov 3, 2023
1 parent a84806b commit a68bb9b
Show file tree
Hide file tree
Showing 4 changed files with 412 additions and 152 deletions.
91 changes: 53 additions & 38 deletions scripts/gen-env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pretty_echo() {
echo ""
}

cleanup_and_exit() {
echo
echo "CTRL+C received, exiting..."
# Add extra cleanup actions here
exit 1
}

install_envsubst_if_missing() {
if ! command -v curl &> /dev/null; then
sudo apt-get update
Expand All @@ -44,46 +51,51 @@ check_existing_dotenv() {
then
echo "WARNING: ${DOTENV_PATH} file already exists."
echo "This script will overwrite the content of this file."
until [ "$conf" = "y" -o "$conf" = "n" ]
echo
echo "Do you want to overwrite "${DOTENV_PATH}"? y/n"
until [ "$overwrite" = "y" -o "$overwrite" = "n" ]
do
read -e -p "Do you want to overwrite it? y/n " conf
if [ "$conf" = "y" ]
read -e -p "Enter 'y' to overwrite, anything else to continue: " overwrite

if [ "$overwrite" = "y" ]
then
conf=""
break
elif [ "$conf" = "n" ]
return 1
elif [ "$overwrite" = "n" ]
then
echo "Aborting."
exit 0
echo "Continuing with existing .env file."
return 0
else
echo "Invalid input!"
fi
done
fi

return 1
}

check_debug() {
pretty_echo "Local Deployment?"
check_existing_dotenv() {
if [ -f "${DOTENV_PATH}" ]
then
echo "WARNING: ${DOTENV_PATH} file already exists."
echo "This script will overwrite the content of this file."
echo
echo "Do you want to overwrite file '"${DOTENV_PATH}"'? y/n"
echo
while true
do
read -e -p "Enter 'y' to overwrite, anything else to continue: " overwrite

echo "Is this a local test deployment?"
while true
do
read -e -p "Enter y if yes, anything else to continue: " debug
if [[ "$overwrite" = "y" || "$overwrite" = "yes" ]]
then
return 1
else
echo "Continuing with existing .env file."
return 0
fi
done
fi

if [[ "$debug" = "y" || "$debug" = "yes" ]]
then
IS_DEBUG=true
export DEBUG=True
export LOG_LEVEL="DEBUG"
echo "Using debug configuration."
else
IS_DEBUG=false
export DEBUG=False
export LOG_LEVEL="INFO"
break
fi
break
done
return 1
}

set_deploy_env() {
Expand Down Expand Up @@ -292,8 +304,6 @@ set_osm_credentials() {
echo
read -e -p "Client Secret: " OSM_CLIENT_SECRET
echo
read -e -p "Secret Key: " OSM_SECRET_KEY
echo
echo "Login redirect URI (default http://127.0.0.1:7051/osmauth/): "
while true
do
Expand All @@ -313,7 +323,8 @@ set_osm_credentials() {

export OSM_CLIENT_ID=${OSM_CLIENT_ID}
export OSM_CLIENT_SECRET=${OSM_CLIENT_SECRET}
export OSM_SECRET_KEY=${OSM_SECRET_KEY}
secret_key=$(tr -dc 'a-zA-Z0-9' </dev/urandom | head -c 50)
export OSM_SECRET_KEY=${secret_key}
export OSM_LOGIN_REDIRECT_URI=${OSM_LOGIN_REDIRECT_URI}
}

Expand All @@ -340,20 +351,23 @@ generate_dotenv() {

if [ -f ./.env.example ]; then
echo ".env.example already exists. Continuing."

echo "substituting variables from .env.example --> ${DOTENV_PATH}"
./envsubst < .env.example > ${DOTENV_PATH}
else
echo "Downloading .env.example from repo."
echo
curl -LO "https://raw.githubusercontent.com/hotosm/fmtm/${BRANCH_NAME:-development}/.env.example"
fi

echo "substituting variables from .env.example --> ${DOTENV_PATH}"
./envsubst < .env.example > ${DOTENV_PATH}
echo "substituting variables from .env.example --> ${DOTENV_PATH}"
./envsubst < .env.example > ${DOTENV_PATH}

echo "Deleting .env.example"
rm .env.example
echo "Deleting .env.example"
rm .env.example
fi
}

prompt_user_for_dotenv() {
prompt_user_gen_dotenv() {
pretty_echo "Generate dotenv config for FMTM"
check_existing_dotenv
install_envsubst_if_missing
Expand Down Expand Up @@ -387,4 +401,5 @@ prompt_user_for_dotenv() {
pretty_echo "Completed dotenv file generation"
}

prompt_user_for_dotenv
trap cleanup_and_exit INT
prompt_user_gen_dotenv
9 changes: 9 additions & 0 deletions scripts/renew-certs-manual.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

set -euo pipefail

cleanup_and_exit() {
echo
echo "CTRL+C received, exiting..."
exit 1
}

# Capture CTRL+C
trap cleanup_and_exit INT

# Prompt the user for input and set the BRANCH_NAME variable
read -p "Enter the environment (dev/staging/prod): " ENVIRONMENT

Expand Down
65 changes: 55 additions & 10 deletions scripts/setup/docker.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/bin/bash

# Tested for Debian 11 Bookworm & Ubuntu 22.04 LTS
# Note: this script must be run as a non-root user
# Note: The user must be logged in directly (not via su)

IS_DEBIAN=false
OS_NAME="debian"

pretty_echo() {
local message="$1"
Expand All @@ -20,17 +22,25 @@ pretty_echo() {
echo ""
}

cleanup_and_exit() {
echo
echo "CTRL+C received, exiting..."
# Add extra cleanup actions here
exit 1
}

check_os() {
pretty_echo "Checking Current OS"

if [ -e /etc/os-release ]; then
source /etc/os-release
case "$ID" in
debian)
IS_DEBIAN=true
export OS_NAME=${ID}
echo "Current OS is ${PRETTY_NAME}."
;;
ubuntu)
export OS_NAME=${ID}
echo "Current OS is ${PRETTY_NAME}."
;;
*)
Expand Down Expand Up @@ -70,15 +80,15 @@ install_dependencies() {
dbus-user-session \
slirp4netns

if [ "$IS_DEBIAN" = true ]; then
if [ "$OS_NAME" = "debian" ]; then
sudo apt-get install -y fuse-overlayfs
fi
}

add_gpg_key() {
pretty_echo "Adding docker gpg key"
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/${id}/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
curl -fsSL https://download.docker.com/linux/${ID}/gpg | sudo gpg --yes --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg
echo "Done"
}
Expand All @@ -100,7 +110,25 @@ apt_install_docker() {
docker-ce-cli \
containerd.io \
docker-buildx-plugin \
docker-compose-plugin
docker-compose-plugin \
docker-ce-rootless-extras
}

check_user_not_root() {
pretty_echo "Use non-root user"

if [ "$(id -u)" -eq 0 ]; then
if id "fmtm" &>/dev/null; then
echo "Current user is root. Switching to existing non-privileged user 'fmtm'."
else
echo "Current user is root. Creating a non-privileged user 'fmtm'."
useradd -m -s /bin/bash fmtm
fi

echo "Rerunning this script as user 'fmtm'."
sudo -u fmtm bash -c "$0 $*"
exit 0
fi
}

update_to_rootless() {
Expand All @@ -121,14 +149,29 @@ EOF
}

add_vars_to_bashrc() {
pretty_echo "Adding rootless DOCKER_HOST to bashrc"
heading_echo "Adding rootless DOCKER_HOST to bashrc"

user_id=$(id -u)
export DOCKER_HOST="unix:///run/user/$user_id//docker.sock"
echo "export DOCKER_HOST=unix:///run/user/$user_id//docker.sock" >> ~/.bashrc
docker_host_var="export DOCKER_HOST=unix:///run/user/$user_id//docker.sock"
dc_alias_cmd="alias dc='docker compose'"

# Check if DOCKER_HOST is already defined
if ! grep -q "$docker_host_var" ~/.bashrc; then
echo "Adding rootless DOCKER_HOST var to ~/.bashrc."
echo "$docker_host_var" >> ~/.bashrc
fi

echo "Done"
echo

heading_echo "Adding dc='docker compose' alias"

# Check if the alias already exists
if ! grep -q "$dc_alias_cmd" ~/.bashrc; then
echo "Adding 'dc' alias to ~/.bashrc."
echo "$dc_alias_cmd" >> ~/.bashrc
fi

pretty_echo "Adding dc='docker compose' alias"
echo "alias dc='docker compose'" >> ~/.bashrc
echo "Done"
}

Expand All @@ -144,4 +187,6 @@ install_docker() {
add_vars_to_bashrc
}

check_user_not_root
trap cleanup_and_exit INT
install_docker
Loading

0 comments on commit a68bb9b

Please sign in to comment.