Skip to content

Commit

Permalink
[fix] Fixed bugs in auto-install script #299
Browse files Browse the repository at this point in the history
Updated CI to use auto-install script for spawning
containers.

Closes #299
  • Loading branch information
pandafy committed Jul 19, 2024
1 parent b62561b commit 0977f66
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 63 deletions.
21 changes: 20 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,26 @@ jobs:
- name: Build Images
run: make compose-build nfs-build

- name: Use auto-install script to start containers
# Do not remove the blank lines from the input.
run: |
GIT_BRANCH="${{ github.ref_name }}" SKIP_PULL=true sudo -E ./deploy/auto-install.sh << EOF
edge
dashboard.openwisp.org
api.openwisp.org
vpn.openwisp.org
[email protected]
EOF
- name: Test
run: make runtests || (docker compose logs && exit 1)
# The auto-install script installs docker-openwisp by default in
# /opt/openwisp/docker-openwisp. To ensure the test runs correctly
# and environment variables remain intact, it is essential to
# execute the test from this directory.
run: |
(make develop-pythontests && make stop) || (docker compose logs && exit 1)
working-directory: /opt/openwisp/docker-openwisp
env:
SELENIUM_HEADLESS: 1
27 changes: 15 additions & 12 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 Down
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)

Check failure

Code scanning / CodeQL

Clear-text storage of sensitive information High

This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.
This expression stores
sensitive data (secret)
as clear text.


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

0 comments on commit 0977f66

Please sign in to comment.