main: Added docker for the master server
All checks were successful
Build Docker / Build Master (push) Successful in 1m9s

This commit is contained in:
Nathan Woodburn 2023-08-16 17:22:54 +10:00
parent aed9fea0f1
commit 14ee680583
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
4 changed files with 87 additions and 23 deletions

View File

@ -0,0 +1,29 @@
name: Build Docker
run-name: Build Docker Images
on: [push]
jobs:
Build Master:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Docker
run : |
apt-get install ca-certificates curl gnupg
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
chmod a+r /etc/apt/keyrings/docker.gpg
echo "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
apt-get update
apt-get install docker-ce-cli -y
- name: Build Docker image
run : |
cd master
echo "${{ secrets.DOCKERGIT_TOKEN }}" | docker login git.woodburn.au -u nathanwoodburn --password-stdin
tag_num=$(git rev-parse --short HEAD)
docker build -t hnshosting-master:$tag_num .
docker tag hnshosting-master:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-master:$tag_num
docker push git.woodburn.au/nathanwoodburn/hnshosting-master:$tag_num
docker tag hnshosting-master:$tag_num git.woodburn.au/nathanwoodburn/hnshosting-master:latest
docker push git.woodburn.au/nathanwoodburn/hnshosting-master:latest

View File

@ -7,14 +7,20 @@ This is done to make it easier to manage multiple wordpress sites on multiple se
## Master server install
Install prerequisites:
Docker is the easiest way to install the master server.
```
chmod +x install.sh
./install.sh
docker run -d -p 5000:5000 -e LICENCE-API=your-api-key -e WORKER_KEY=your-api-key --name hnshosting-master git.woodburn.au/nathanwoodburn/hnshosting-master:latest -v ./data:/data
```
You can also mount a docker volume to /data to store the files instead of mounting a host directory.
This will create the service to run the master server.
Alternatively you can install it manually.
Set your .env file.
```
cd master
python3 -m pip install -r requirements.txt
python3 main.py
```
## Worker server install

29
master/Dockerfile Normal file
View File

@ -0,0 +1,29 @@
FROM --platform=$BUILDPLATFORM python:3.10-alpine AS builder
WORKDIR /app
COPY requirements.txt /app
RUN --mount=type=cache,target=/root/.cache/pip \
pip3 install -r requirements.txt
COPY . /app
# Add mount point for data volume
VOLUME /data
ENTRYPOINT ["python3"]
CMD ["main.py"]
FROM builder as dev-envs
# RUN <<EOF
# apk update
# apk add git
# EOF
# RUN <<EOF
# addgroup -S docker
# adduser -S --shell /bin/bash --ingroup docker vscode
# EOF
# # install Docker tools (cli, buildx, compose)
# COPY --from=gloursdocker/docker / /

View File

@ -20,7 +20,7 @@ def add_license():
licence_key = os.urandom(16).hex()
# Add license key to file
key_file = open('licence_key.txt', 'a')
key_file = open('/data/licence_key.txt', 'a')
key_file.write(licence_key + '\n')
key_file.close()
@ -38,7 +38,7 @@ def new_site():
return jsonify({'error': 'Invalid API key or domain', 'success': 'false'})
# Check if API key is a valid site key
if api_key not in open('licence_key.txt', 'r').read():
if api_key not in open('/data/licence_key.txt', 'r').read():
return jsonify({'error': 'Invalid API key', 'success': 'false'})
# Check if domain already exists
@ -53,7 +53,7 @@ def new_site():
# Check if worker file exists
workers = None
try:
worker_file = open('workers.txt', 'r')
worker_file = open('/data/workers.txt', 'r')
workers = worker_file.readlines()
worker_file.close()
except FileNotFoundError:
@ -74,15 +74,15 @@ def new_site():
# Add domain to file
sites_file = open('sites.txt', 'a')
sites_file = open('/data/sites.txt', 'a')
sites_file.write(domain + ':' + worker.split(':')[0] + '\n')
sites_file.close()
# Use key
key_file = open('licence_key.txt', 'r')
key_file = open('/data/licence_key.txt', 'r')
lines = key_file.readlines()
key_file.close()
key_file = open('licence_key.txt', 'w')
key_file = open('/data/licence_key.txt', 'w')
for line in lines:
if line.strip("\n") != api_key:
key_file.write(line)
@ -108,11 +108,11 @@ def add_worker():
# Check worker file
try:
workers_file = open('workers.txt', 'r')
workers_file = open('/data/workers.txt', 'r')
except FileNotFoundError:
workers_file = open('workers.txt', 'w')
workers_file = open('/data/workers.txt', 'w')
workers_file.close()
workers_file = open('workers.txt', 'r')
workers_file = open('/data/workers.txt', 'r')
# Check if worker already exists
if worker in workers_file.read():
@ -121,7 +121,7 @@ def add_worker():
workers_file.close()
# Add worker to file
workers_file = open('workers.txt', 'a')
workers_file = open('/data/workers.txt', 'a')
workers_file.write(worker + ":" + worker_IP + '\n')
workers_file.close()
@ -136,11 +136,11 @@ def add_worker():
def get_sites_count():
# If file doesn't exist, create it
try:
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
except FileNotFoundError:
sites_file = open('sites.txt', 'w')
sites_file = open('/data/sites.txt', 'w')
sites_file.close()
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
num=len(sites_file.readlines())
sites_file.close()
# Return number of lines in file
@ -149,11 +149,11 @@ def get_sites_count():
def site_exists(domain):
# If file doesn't exist, create it
try:
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
except FileNotFoundError:
sites_file = open('sites.txt', 'w')
sites_file = open('/data/sites.txt', 'w')
sites_file.close()
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
contains_site = False
for line in sites_file.readlines():
@ -167,11 +167,11 @@ def site_exists(domain):
def site_worker(domain):
# If file doesn't exist, create it
try:
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
except FileNotFoundError:
sites_file = open('sites.txt', 'w')
sites_file = open('/data/sites.txt', 'w')
sites_file.close()
sites_file = open('sites.txt', 'r')
sites_file = open('/data/sites.txt', 'r')
worker = None
for line in sites_file.readlines():