Skip to content

Commit

Permalink
[fix] Fixed bugs in auto-install script #299
Browse files Browse the repository at this point in the history
- Upgraded postgis to version 15-3.4-alpine
- Refactored build.py

Fixes #299
  • Loading branch information
pandafy authored and nemesifier committed Jul 22, 2024
1 parent 255bf2d commit f680e35
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 68 deletions.
35 changes: 18 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ SHELL := /bin/bash

default: compose-build

# Pull
USER = registry.gitlab.com/openwisp/docker-openwisp
TAG = latest
SKIP_PULL ?= false
SKIP_BUILD ?= false
SKIP_TESTS ?= false

# Pull
pull:
printf '\e[1;34m%-6s\e[m\n' "Downloading OpenWISP images..."
for image in 'openwisp-base' 'openwisp-nfs' 'openwisp-api' 'openwisp-dashboard' \
Expand Down Expand Up @@ -51,6 +55,9 @@ runtests: develop-runtests

develop-runtests:
docker compose up -d
make develop-pythontests

develop-pythontests:
python3 tests/runtests.py

# Development
Expand All @@ -72,25 +79,21 @@ clean:
`docker images | grep openwisp/docker-openwisp | tr -s ' ' | cut -d ' ' -f 3` &> /dev/null

# Production
USER = registry.gitlab.com/openwisp/docker-openwisp
TAG = latest
start: pull
start:
if [ "$(SKIP_PULL)" == "false" ]; then \
make pull; \
fi
printf '\e[1;34m%-6s\e[m\n' "Starting Services..."
docker compose --log-level WARNING up -d
docker --log-level WARNING compose up -d
printf '\e[1;32m%-6s\e[m\n' "Success: OpenWISP should be available at your dashboard domain in 2 minutes."

stop:
printf '\e[1;31m%-6s\e[m\n' "Stopping OpenWISP services..."
docker compose --log-level ERROR stop
docker compose --log-level ERROR down --remove-orphans
docker --log-level ERROR compose stop
docker --log-level ERROR compose down --remove-orphans
docker compose down --remove-orphans &> /dev/null

# Publish
USER = registry.gitlab.com/openwisp/docker-openwisp
TAG = latest
SKIP_BUILD = false
SKIP_TESTS = false

publish:
if [[ "$(SKIP_BUILD)" == "false" ]]; then \
make compose-build nfs-build; \
Expand All @@ -101,12 +104,10 @@ publish:
for image in 'openwisp-base' 'openwisp-nfs' 'openwisp-api' 'openwisp-dashboard' \
'openwisp-freeradius' 'openwisp-nginx' 'openwisp-openvpn' 'openwisp-postfix' \
'openwisp-websocket' ; do \
# Docker images built locally are tagged "latest" by default.
# This script updates the tag of each built image to a user-defined tag
# and pushes the newly tagged image to a Docker registry under the user's namespace.
docker tag openwisp/$${image}:latest $(USER)/$${image}:$(TAG); \
docker push $(USER)/$${image}:$(TAG); \
docker rmi $(USER)/$${image}:$(TAG); \
if [[ "$(TAG)" != "edge" ]] && [[ "$(TAG)" != "latest" ]]; then \
docker tag openwisp/$${image}:latest $(USER)/$${image}:latest; \
docker push $(USER)/$${image}:latest; \
docker rmi $(USER)/$${image}:latest; \
fi \
done
37 changes: 16 additions & 21 deletions build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
import sys


def randomize_key_value(key, value):
def update_env_file(key, value):
# Update the generated secret key
# in the .env file.

file_handle = open('.env', 'r')
file_string = file_handle.read()
file_handle.close()
with open('.env', 'r') as file_handle:
file_string = file_handle.read()
file_string = re.sub(fr'{key}=.*', fr'{key}={value}', file_string)
if file_string[-1] != '\n':
file_string += '\n'
if f'{key}' not in file_string:
file_string += f'{key}={value}'
file_handle = open('.env', 'w')
file_handle.write(file_string)
file_handle.close()


def get_secret_key():
chars = (
'abcdefghijklmnopqrstuvwxyz'
'ABCDEFGHIJKLMNOPQRSTUVXYZ'
'0123456789'
'#^[]-_*%&=+/'
)
with open('.env', 'w') as file_handle:
file_handle.write(file_string)


def get_secret_key(allow_special_chars=True):
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789'
if allow_special_chars:
chars += '#^[]-_*%&=+/'
keygen = ''.join([random.SystemRandom().choice(chars) for _ in range(50)])
print(keygen)
return keygen
Expand All @@ -42,11 +37,11 @@ def get_secret_key():
get_secret_key()
if 'change-secret-key' in arguments:
keygen = get_secret_key()
randomize_key_value('DJANGO_SECRET_KEY', keygen)
update_env_file('DJANGO_SECRET_KEY', keygen)
if 'default-secret-key' in arguments:
randomize_key_value('DJANGO_SECRET_KEY', 'default_secret_key')
update_env_file('DJANGO_SECRET_KEY', 'default_secret_key')
if 'change-database-credentials' in arguments:
keygen1 = get_secret_key()
keygen1 = get_secret_key(allow_special_chars=False)
keygen2 = get_secret_key()
randomize_key_value("DB_USER", keygen1)
randomize_key_value("DB_PASS", keygen2)
update_env_file('DB_USER', keygen1)
update_env_file('DB_PASS', keygen2)
50 changes: 21 additions & 29 deletions deploy/auto-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,22 @@ setup_docker() {
fi
}

setup_docker_compose() {
start_step "Install docker compose python library..."
python3 -m pip install docker compose &>>$LOG_FILE
docker compose version &>/dev/null
check_status $? "Docker compose installation failed."
download_docker_openwisp() {
local openwisp_version="$1"
start_step "Downloading docker-openwisp..."
if [[ -f $INSTALL_PATH/.env ]]; then
mv $INSTALL_PATH/.env $ENV_BACKUP &>>$LOG_FILE
rm -rf $INSTALL_PATH &>>$LOG_FILE
fi
if [ -z "$GIT_BRANCH" ]; then
if [[ "$openwisp_version" == "edge" ]]; then
GIT_BRANCH="master"
else
GIT_BRANCH="$openwisp_version"
fi
fi

git clone $GIT_PATH $INSTALL_PATH --depth 1 --branch $GIT_BRANCH &>>$LOG_FILE
}

setup_docker_openwisp() {
Expand All @@ -93,7 +104,7 @@ setup_docker_openwisp() {
domain=$(echo "$dashboard_domain" | cut -f2- -d'.')
# API Domain
echo -ne ${GRN}"(2/5) Enter API domain (blank for api.${domain}): "${NON}
read API_DOMAIN
read api_domain
# VPN domain
echo -ne ${GRN}"(3/5) Enter OpenVPN domain (blank for vpn.${domain}, N to disable module): "${NON}
read vpn_domain
Expand All @@ -108,17 +119,7 @@ setup_docker_openwisp() {
fi
echo ""

start_step "Downloading docker-openwisp..."
if [[ -f $INSTALL_PATH/.env ]]; then
mv $INSTALL_PATH/.env $ENV_BACKUP &>>$LOG_FILE
rm -rf $INSTALL_PATH &>>$LOG_FILE
fi

if [[ $openwisp_version -ne "edge" ]]; then
git clone $GIT_PATH $INSTALL_PATH --depth 1 --branch $openwisp_version &>>$LOG_FILE
else
git clone $GIT_PATH $INSTALL_PATH --depth 1 &>>$LOG_FILE
fi
download_docker_openwisp "$openwisp_version"

cd $INSTALL_PATH &>>$LOG_FILE
check_status $? "docker-openwisp download failed."
Expand All @@ -128,10 +129,10 @@ setup_docker_openwisp() {
# Dashboard Domain
set_env "DASHBOARD_DOMAIN" "$dashboard_domain"
# API Domain
if [[ -z "$API_DOMAIN" ]]; then
if [[ -z "$api_domain" ]]; then
set_env "API_DOMAIN" "api.${domain}"
else
set_env "API_DOMAIN" "$API_DOMAIN"
set_env "API_DOMAIN" "$api_domain"
fi
# Use Radius
if [[ -z "$USE_OPENWISP_RADIUS" ]]; then
Expand Down Expand Up @@ -182,15 +183,7 @@ upgrade_docker_openwisp() {
if [[ -z "$openwisp_version" ]]; then openwisp_version=latest; fi
echo ""

start_step "Downloading docker-openwisp..."
cp $INSTALL_PATH/.env $ENV_BACKUP &>>$LOG_FILE
rm -rf $INSTALL_PATH &>>$LOG_FILE

if [[ $openwisp_version -ne "edge" ]]; then
git clone $GIT_PATH $INSTALL_PATH --depth 1 --branch $openwisp_version &>>$LOG_FILE
else
git clone $GIT_PATH $INSTALL_PATH --depth 1 &>>$LOG_FILE
fi
download_docker_openwisp "$openwisp_version"

cd $INSTALL_PATH &>>$LOG_FILE
check_status $? "docker-openwisp download failed."
Expand Down Expand Up @@ -235,7 +228,6 @@ upgrade_debian() {
install_debian() {
apt_dependenices_setup
setup_docker
setup_docker_compose
setup_docker_openwisp
give_information_to_user
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ services:
- NET_ADMIN

postgres:
image: postgis/postgis:12-3.4
image: postgis/postgis:15-3.4-alpine
restart: always
environment:
- POSTGRES_DB=$DB_NAME
Expand Down

0 comments on commit f680e35

Please sign in to comment.