Skip to content

Commit

Permalink
Address #90, #89 and #87
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanVaughn committed Mar 18, 2022
1 parent c1304fe commit 9f3d745
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 60 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ jobs:
python3 -m pip install -r requirements.txt
- name: Check For Updates
#run: echo "::set-env name=WT_UPDATE::$(python3 updater.py --check)"
run: echo "WT_UPDATE=$(python3 updater.py --check)" >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -44,7 +43,6 @@ jobs:

- name: Determine Week
if: env.WT_UPDATE == 'true'
# run: echo "::set-env name=WEEK::$(date +%V)"
run: echo "WEEK=$(date +%V)" >> $GITHUB_ENV

- name: Cache Docker Layers
Expand Down Expand Up @@ -98,4 +96,4 @@ jobs:
run: python3 updater.py
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
#BUILDX_PLATFORMS: ${{ steps.buildx.outputs.platforms }}
# BUILDX_PLATFORMS: ${{ steps.buildx.outputs.platforms }}
27 changes: 14 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@ FROM docker.io/library/php:7.4-apache
ARG BUILD_DATE
ARG VCS_REF

ENV WEBTREES_VERSION=2.1.0-alpha.2
ENV WEBTREES_HOME="/var/www/webtrees"

WORKDIR $WEBTREES_HOME

RUN apt-get update
# install pre-reqs
RUN apt-get update
RUN apt-get install -y \
curl \
libmagickwand-dev \
libpq-dev \
libzip-dev \
mariadb-client \
patch \
python3 \
unzip \
--no-install-recommends
Expand All @@ -24,20 +23,25 @@ RUN pecl install imagick \
&& docker-php-ext-enable imagick \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) pdo pdo_mysql pdo_pgsql zip intl gd exif
# install webtrees and disable version update prompt
RUN curl -s -L https://github.com/fisharebest/webtrees/releases/download/${WEBTREES_VERSION}/webtrees-${WEBTREES_VERSION}.zip -o webtrees.zip \
&& unzip -q webtrees.zip -d /var/www/ && rm webtrees.zip \
&& chown -R www-data:www-data $WEBTREES_HOME \
&& perl -0777 -i -pe 's/public\s+function\s+isUpgradeAvailable[\S\s]+?{[\S\s]+?}/public function isUpgradeAvailable(){ return false; }/' $WEBTREES_HOME/app/Services/UpgradeService.php
# remove old apt stuff
RUN apt-get purge gcc g++ make zip unzip -y \
RUN apt-get purge gcc g++ make -y \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/tmp/* /etc/apache2/sites-enabled/000-*.conf /var/lib/apt/lists/*

# install webtrees and disable version update prompt
ENV WEBTREES_VERSION=2.0.19
RUN curl -s -L https://github.com/fisharebest/webtrees/releases/download/${WEBTREES_VERSION}/webtrees-${WEBTREES_VERSION}.zip -o webtrees.zip \
&& unzip -q webtrees.zip -d /var/www/ && rm webtrees.zip \
&& chown -R www-data:www-data ./ \
&& perl -0777 -i -pe "s/private\s+function\s+fetchLatestVersion[\S\s]+?{[\S\s]+?{[\S\s]+?{[\S\s]+?{[\S\s]+?}[\S\s]+?}[\S\s]+?}[\S\s]+?}[\S\s]+?}/private function fetchLatestVersion(): string { return Site::getPreference('LATEST_WT_VERSION'); }/" app/Services/UpgradeService.php

# enable apache modules
RUN a2enmod rewrite && a2enmod ssl && rm -rf /var/www/html

# copy apache/php configs
COPY php.ini /usr/local/etc/php/php.ini
COPY .htaccess $WEBTREES_HOME
COPY .htaccess ./
COPY apache/ /etc/apache2/sites-available/

# entrypoint
Expand All @@ -47,9 +51,6 @@ COPY docker-entrypoint.py /
COPY docker-healthcheck.sh /
RUN chmod +x /docker-healthcheck.sh

# enable apache modules
RUN a2enmod rewrite && a2enmod ssl

# final Docker config
EXPOSE 80 443
VOLUME ["$WEBTREES_HOME/data", "$WEBTREES_HOME/media"]
Expand Down
8 changes: 4 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ services:
WT_NAME: "Full Name"
WT_PASS: "mybadpassword"
WT_EMAIL: "me@example.com"
image: ghcr.io/nathanvaughn/webtrees:latest
# build: .
# image: ghcr.io/nathanvaughn/webtrees:latest
build: .
ports:
- 80:80
- 443:443
# - 443:443
restart: unless-stopped
volumes:
- ~/certs:/certs/
# - ~/certs:/certs/
- app_data:/var/www/webtrees/data/
- app_media:/var/www/webtrees/media/

Expand Down
105 changes: 67 additions & 38 deletions docker-entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_env(
if key in os.environ:
value = os.environ[key]

print(f"{key} found in environment variables")
print2(f"{key} found in environment variables")
return value

# try to find file version of variable
Expand All @@ -57,7 +57,7 @@ def get_env(
with open(os.environ[file_key], "r") as f:
value = f.read().strip()

print(f"{file_key} found in environment variables")
print2(f"{file_key} found in environment variables")
return value

# try to find alternate variable
Expand All @@ -67,13 +67,19 @@ def get_env(
return a

# return default value
print2(f"{key} NOT found in environment variables, using default: {default}")
return default


def set_config_value(key: str, value: str) -> None:
def set_config_value(key: str, value: Optional[str]) -> None:
"""
In the config file, make sure the given key is set to the given value.
"""
if value is None:
return

print2(f"Setting value for {key} in config")

if not os.path.isfile(CONFIG_FILE):
print2(f"WARNING: {CONFIG_FILE} does not exist")
return
Expand All @@ -83,7 +89,7 @@ def set_config_value(key: str, value: str) -> None:
lines = fp.readlines()

# replace matching line
replacement = f'{key}="{value}"'
replacement = f'{key}="{value}"\n'
found = False

for i, line in enumerate(lines):
Expand Down Expand Up @@ -111,16 +117,18 @@ def enable_apache_site(
"""
all_sites = ["webtrees", "webtrees-redir", "webtrees-ssl"]

# perl complains about locale to stderr, so disable that

# disable the other sites
for s in all_sites:
if s not in enable_sites:
print2(f"Disabling site {s}")
subprocess.check_call(["a2dissite", s])
subprocess.check_call(["a2dissite", s], stderr=subprocess.DEVNULL)

# enable the desired sites
for s in enable_sites:
print2(f"Enabling site {s}")
subprocess.check_call(["a2ensite", s])
subprocess.check_call(["a2ensite", s], stderr=subprocess.DEVNULL)


def perms():
Expand All @@ -131,6 +139,9 @@ def perms():
subprocess.check_call(["chown", "-R", "www-data:www-data", "media"])
subprocess.check_call(["chmod", "-R", "755", "media"])

if os.path.isfile(CONFIG_FILE):
subprocess.check_call(["chmod", "700", CONFIG_FILE])


def setup_wizard():
print2("Attempting to automate setup wizard")
Expand All @@ -157,21 +168,27 @@ def setup_wizard():
if os.path.isfile(CONFIG_FILE):
print2("Config file already exists")

# make sure all the variables we need are set
if not all([db_host, db_port, db_user, db_pass, db_name, base_url]):
print2("WARNING: Not all required variables were found for config update")
# make sure all the variables we need are not set to None
try:
if db_type in ["mysql", "pgsql"]:
assert db_host is not None
assert db_port is not None
assert db_user is not None
assert db_pass is not None
elif db_type == "sqlite":
assert db_name is not None
db_host = ""
db_port = ""
db_user = ""
db_pass = ""
else:
raise ValueError(f"Unknown database type: {db_type}")

except AssertionError:
print2("WARNING: Not all required variables were found for database update")
return

print2("Updating config file")

assert db_host is not None
assert db_port is not None
assert db_user is not None
assert db_pass is not None
assert db_name is not None
assert table_prefix is not None
assert base_url is not None

set_config_value("dbhost", db_host)
set_config_value("dbport", db_port)
set_config_value("dbuser", db_user)
Expand All @@ -183,28 +200,39 @@ def setup_wizard():
else:
print2("Config file does NOT exist")

# make sure all the variables we need are set
if not all(
[
lang,
db_type,
db_host,
db_port,
db_user,
db_pass,
db_name,
base_url,
wt_name,
wt_user,
wt_pass,
wt_email,
]
):
# make sure all the variables we need are not set to None
try:
assert lang is not None
assert table_prefix is not None
assert base_url is not None

assert wt_name is not None
assert wt_user is not None
assert wt_pass is not None
assert wt_email is not None

assert db_type is not None
assert db_name is not None

if db_type in ["mysql", "pgsql"]:
assert db_host is not None
assert db_port is not None
assert db_user is not None
assert db_pass is not None
elif db_type == "sqlite":
assert db_name is not None
db_host = ""
db_port = ""
db_user = ""
db_pass = ""
else:
raise ValueError(f"Unknown database type: {db_type}")

except AssertionError:
print2("WARNING: Not all required variables were found for setup wizard")
return

print2("Automating setup wizard")

print2("Starting Apache in background")
# set us up to a known HTTP state
enable_apache_site(["webtrees"])
Expand Down Expand Up @@ -236,14 +264,14 @@ def setup_wizard():
urlencode(
{
"lang": lang,
"tblpfx": table_prefix,
"baseurl": base_url,
"dbtype": db_type,
"dbhost": db_host,
"dbport": db_port,
"dbuser": db_user,
"dbpass": db_pass,
"dbname": db_name,
"tblpfx": table_prefix,
"baseurl": base_url,
"wtname": wt_name,
"wtuser": wt_user,
"wtpass": wt_pass,
Expand Down Expand Up @@ -301,6 +329,7 @@ def main():
pretty_urls()
https()
htaccess()
perms()

print2("Starting Apache")
subprocess.run(["apache2-foreground"])
Expand Down
3 changes: 1 addition & 2 deletions updater.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import argparse
import datetime
import getpass
import json
import os
import subprocess
import sys
import urllib.request
from typing import List, Tuple
from typing import List

import github
import github.Repository
Expand Down

0 comments on commit 9f3d745

Please sign in to comment.