Skip to content

Commit

Permalink
working version, with settings.
Browse files Browse the repository at this point in the history
  • Loading branch information
remcoros committed Jul 26, 2024
1 parent 2bebb25 commit c58b521
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 168 deletions.
20 changes: 10 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@ ARG PLATFORM
ARG YQ_VERSION
ARG YQ_SHA

WORKDIR /app

COPY ./src /app

RUN \
dotnet restore && \
dotnet publish -c Release -o out

# Install necessary packages
RUN \
apt-get update && \
Expand All @@ -29,10 +21,18 @@ RUN \
echo "${YQ_SHA} /tmp/yq" | sha256sum -c || exit 1 && \
mv /tmp/yq /usr/local/bin/yq && chmod +x /usr/local/bin/yq

FROM mcr.microsoft.com/dotnet/runtime:8.0-bookworm-slim
WORKDIR /app

COPY ./src/PushTX .

RUN \
dotnet restore && \
dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:8.0-alpine

WORKDIR /app

COPY --from=build /app/out /app
COPY --from=build /usr/local/bin/yq /usr/local/bin/yq
COPY ./docker_entrypoint.sh /usr/local/bin/docker_entrypoint.sh
COPY --chmod=755 ./docker_entrypoint.sh /app/docker_entrypoint.sh
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ clean:
scripts/embassy.js: $(TS_FILES)
deno bundle scripts/embassy.ts scripts/embassy.js

docker-images/aarch64.tar: manifest.yaml Dockerfile docker_entrypoint.sh assets/nginx.conf
docker-images/aarch64.tar: manifest.yaml Dockerfile docker_entrypoint.sh
ifeq ($(ARCH),x86_64)
else
mkdir -p docker-images
Expand All @@ -51,7 +51,7 @@ else
--platform=linux/arm64 -o type=docker,dest=docker-images/aarch64.tar .
endif

docker-images/x86_64.tar: manifest.yaml Dockerfile docker_entrypoint.sh assets/nginx.conf
docker-images/x86_64.tar: manifest.yaml Dockerfile docker_entrypoint.sh
ifeq ($(ARCH),aarch64)
else
mkdir -p docker-images
Expand Down
153 changes: 11 additions & 142 deletions docker_entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -3,151 +3,20 @@
set -ea

echo
echo "Starting Labelbase..."
echo "Starting NFC Push TX..."
echo

# Setup MariaDB
mkdir -p /run/mysqld
chown -R mysql:mysql /run/mysqld

# check if the 'mysql' system database exists
if [ -d /var/lib/mysql/mysql ]; then
echo "[i] MariaDB directory already present, skipping creation"
chown -R mysql:mysql /var/lib/mysql

# get root password from passwords file, we'll need it later. The passwords file is created and updated on every run (and included in backups)
export MYSQL_ROOT_PASSWORD=$(yq e '.root' /root/data/start9/passwords.yaml)
export MYSQL_PASSWORD=$(yq e '.ulabelbase' /root/data/start9/passwords.yaml)
if [ $(yq e '.advanced.use_custom_node' /home/app/start9/config.yaml) = "false" ]; then
export RPC_HOST="bitcoind.embassy:8332"
export RPC_USERNAME=$(yq e '.bitcoind_rpcusername' /home/app/start9/config.yaml)
export RPC_PASSWORD=$(yq e '.bitcoind_rpcpassword' /home/app/start9/config.yaml)
echo "Use built-in Bitcoin Core node: $RPC_USERNAME@$RPC_HOST"
else
echo "[i] MariaDB data directory not found, creating initial DBs"

mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql

# install system db
mysql_install_db --user=mysql --ldata=/var/lib/mysql >/dev/null

# generate the root password
if [ "$MYSQL_ROOT_PASSWORD" = "" ]; then
export MYSQL_ROOT_PASSWORD=$(pwgen 16 1)
echo "[i] MariaDB root Password: $MYSQL_ROOT_PASSWORD"
fi

# create a database and give privileges
# note: Labelbase has the database and username hardcoded to 'labelbase / ulabelbase' with no way to change that
MYSQL_DATABASE=${MYSQL_DATABASE:-"labelbase"}
MYSQL_USER=${MYSQL_USER:-"ulabelbase"}
if [ "$MYSQL_PASSWORD" = "" ]; then
export MYSQL_PASSWORD=$(pwgen 16 1)
echo "[i] MariaDB $MYSQL_USER Password: $MYSQL_PASSWORD"
fi

tfile=$(mktemp)
if [ ! -f "$tfile" ]; then
return 1
fi

cat <<EOF >$tfile
USE mysql;
FLUSH PRIVILEGES ;
GRANT ALL ON *.* TO 'root'@'%' identified by '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION ;
GRANT ALL ON *.* TO 'root'@'localhost' identified by '$MYSQL_ROOT_PASSWORD' WITH GRANT OPTION ;
SET PASSWORD FOR 'root'@'localhost'=PASSWORD('${MYSQL_ROOT_PASSWORD}') ;
DROP DATABASE IF EXISTS test ;
FLUSH PRIVILEGES ;
EOF

echo "[i] Creating database: $MYSQL_DATABASE"
echo "[i] with character set: 'utf8' and collation: 'utf8_general_ci'"
echo "CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\` CHARACTER SET utf8 COLLATE utf8_general_ci;" >>$tfile

echo "[i] Creating user: $MYSQL_USER with password $MYSQL_PASSWORD"
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'%' IDENTIFIED BY '$MYSQL_PASSWORD';" >>$tfile
echo "GRANT ALL ON \`$MYSQL_DATABASE\`.* to '$MYSQL_USER'@'localhost' IDENTIFIED BY '$MYSQL_PASSWORD';" >>$tfile
echo "FLUSH PRIVILEGES;" >>$tfile

# run the script
/usr/sbin/mysqld --user=mysql --datadir='/var/lib/mysql' --bootstrap --verbose=0 --skip-networking=0 <$tfile
rm -f $tfile

echo
echo 'MariaDB init process done.'
echo
export RPC_HOST=$(yq e '.advanced.custom_rpchost' /home/app/start9/config.yaml)
export RPC_USERNAME=$(yq e '.advanced.custom_rpcusername' /home/app/start9/config.yaml)
export RPC_PASSWORD=$(yq e '.advanced.custom_rpcpassword' /home/app/start9/config.yaml)
echo "Use custom Bitcoin Core node: $RPC_USERNAME@$RPC_HOST"
fi

# Update stats (properties) file

mkdir -p /root/data/start9
cat <<EOF >/root/data/start9/stats.yaml
data:
MariaDB root password:
copyable: true
description: This is the MariaDB root password. Use it with caution!
masked: true
qr: false
type: string
value: $MYSQL_ROOT_PASSWORD
MariaDB ulabelbase password:
copyable: true
description: This is the MariaDB password for the 'ulabelbase' user. Use it with caution!
masked: true
qr: false
type: string
value: $MYSQL_PASSWORD
version: 2
EOF

cat <<EOF >/root/data/start9/passwords.yaml
root: $MYSQL_ROOT_PASSWORD
ulabelbase: $MYSQL_PASSWORD
EOF

# Run MariaDB

/usr/sbin/mysqld --user=mysql --datadir='/var/lib/mysql' --console --skip-networking=0 --bind-address=0.0.0.0 &
db_process=$!

# Loop until MariaDB is up
while ! mysql -h 127.0.0.1 -u"${MYSQL_USER}" -p"${MYSQL_PASSWORD}" -e "SELECT 1" >/dev/null 2>&1; do
echo "Waiting for MariaDB to be up..."
sleep 1
done

echo "MariaDB is up!"

# Run Labelbase

cd /app

# workaround: run 'manage.py help' to force generation of config.ini, without it, the next step (makemigrations) will fail.
# copy config.ini to a persistent volume and re-use it after restarts
if [ -f /root/data/config.ini ]; then
cp /root/data/config.ini /app
else
echo "Executing manage.py help"
python manage.py help
cp /app/config.ini /root/data
fi

python manage.py migrate --noinput
python manage.py process_tasks &
gunicorn labellabor.wsgi:application -b 127.0.0.1:8000 --reload &
app_process=$!

# Run nginx

echo "Starting nginx"

nginx -g "daemon off;" &
nginx_process=$!

# hook the TERM signal and wait for all our processes
_term() {
echo "Caught TERM signal!"
kill -TERM "$nginx_process" 2>/dev/null
kill -TERM "$app_process" 2>/dev/null
kill -TERM "$db_process" 2>/dev/null
}

trap _term TERM
wait $db_process $app_process $nginx_process
dotnet PushTX.dll
Binary file modified icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 16 additions & 10 deletions manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ assets:
main:
type: docker
image: main
entrypoint: "docker_entrypoint.sh"
entrypoint: "/app/docker_entrypoint.sh"
args: []
mounts:
main: /root/data
main: /home/app
gpu-acceleration: false
hardware-requirements:
arch:
Expand Down Expand Up @@ -54,15 +54,21 @@ interfaces:
lan-config:
443:
ssl: true
internal: 80
internal: 8080
tor-config:
port-mapping:
80: "80"
ui: true
80: "8080"
ui: false
protocols:
- tcp
- http
dependencies: {}
dependencies:
bitcoind:
version: ">=0.21.1.2 <29.0.0"
requirement:
type: "opt-out"
how: "Set 'Use custom Bitcoin node' to Enabled"
description: Use your own node for NFC Push TX
backup:
create:
type: docker
Expand All @@ -73,10 +79,10 @@ backup:
- duplicity
- create
- /mnt/backup
- /root/data
- /home/app
mounts:
BACKUP: /mnt/backup
main: /root/data
main: /home/app
restore:
type: docker
image: compat
Expand All @@ -86,10 +92,10 @@ backup:
- duplicity
- restore
- /mnt/backup
- /root/data
- /home/app
mounts:
BACKUP: /mnt/backup
main: /root/data
main: /home/app
migrations:
from:
"*":
Expand Down
1 change: 1 addition & 0 deletions scripts/embassy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { getConfig } from "./procedures/getConfig.ts";
export { migration } from "./procedures/migrations.ts";
export { health } from "./procedures/healthChecks.ts";
export { properties } from "./procedures/properties.ts";
export { dependencies } from "./procedures/dependencies.ts";
30 changes: 30 additions & 0 deletions scripts/procedures/dependencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { matches, types as T } from "../deps.ts";

const { shape, boolean } = matches;

const matchBitcoindConfig = shape({
rpc: shape({
enable: boolean,
}),
});

export const dependencies: T.ExpectedExports.dependencies = {
bitcoind: {
// deno-lint-ignore require-await
async check(effects, configInput) {
effects.info("check bitcoind");
const config = matchBitcoindConfig.unsafeCast(configInput);
if (!config.rpc.enable) {
return { error: "Must have RPC enabled" };
}
return { result: null };
},
// deno-lint-ignore require-await
async autoConfigure(effects, configInput) {
effects.info("autoconfigure bitcoind");
const config = matchBitcoindConfig.unsafeCast(configInput);
config.rpc.enable = true;
return { result: config };
},
},
};
Loading

0 comments on commit c58b521

Please sign in to comment.