Skip to content

Commit

Permalink
Fixed autodeploy and auto execution
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalCz committed Jul 17, 2023
1 parent 3d7bd11 commit 58d357b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 31 deletions.
4 changes: 2 additions & 2 deletions config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
IMG_NAME="rasppi-os"
STAGE_LIST=${STAGE_LIST:-"stage0 stage1 stage2 stage99-sth"}

WORK_PARTITION_SIZE=16384
DEPLOY_PARTITION_SIZE=8192
WORK_PARTITION_SIZE=8192
DEPLOY_PARTITION_SIZE=1024

DEPLOY_COMPRESSION=none
2 changes: 1 addition & 1 deletion stage99-sth/02-install-node/00-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ echo "Installing node.js $NODE_VERSION"

on_chroot << EOF
curl -sL https://deb.nodesource.com/setup_${NODE_VERSION}.x | bash -
sudo apt-get install -y nodejs jq
sudo apt-get install -y nodejs jq python3-pip python-is-python3
EOF
1 change: 1 addition & 0 deletions stage99-sth/03-install-sth/00-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

on_chroot << EOF
npm i -g @scramjet/sth
pip install pyee==9.0.4 scramjet-framework-py
EOF
11 changes: 6 additions & 5 deletions stage99-sth/04-setup-systemd/00-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ install -d "${ROOTFS_DIR}/opt/sth/deploy"
install -d "${ROOTFS_DIR}/opt/sth/deploy/conf"
install -d "${ROOTFS_DIR}/opt/sth/deploy/sequences"

install -m 755 files/start-sth "${ROOTFS_DIR}/usr/bin/start-sth"
install -d "${ROOTFS_DIR}/etc/sth"
install -m 644 files/sth-config.json "${ROOTFS_DIR}/etc/sth/"
install -m 644 files/sth.service "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/sth-deploy-config.json "${ROOTFS_DIR}/opt/sth/deploy/conf"
install -m 755 files/start-sth "${ROOTFS_DIR}/usr/bin/start-sth"
install -d "${ROOTFS_DIR}/etc/sth"
install -d "${ROOTFS_DIR}/etc/sth/sequences"
install -m 644 files/sth-config "${ROOTFS_DIR}/etc/sth/"
install -m 644 files/sth.service "${ROOTFS_DIR}/etc/systemd/system/"
install -m 644 files/sth-config-deploy.json "${ROOTFS_DIR}/opt/sth/deploy/conf/sth-config.json"

on_chroot << EOF
chown -R sth:sth /usr/lib/sth
Expand Down
82 changes: 71 additions & 11 deletions stage99-sth/04-setup-systemd/files/start-sth
Original file line number Diff line number Diff line change
@@ -1,18 +1,78 @@
#!/bin/bash

DEPLOY_PATH=/opt/sth/deploy
WORK_PATH=/srv/sth
CONFIG_LOCATION=/etc/sth/sth-config.json
DEPLOY_CONFIG_LOCATION="${DEPLOY_PATH}/conf/sth-config.json"
RUNTIME_CONFIG_LOCATION="${WORK_PATH}/sth-config.json"

if [[ -e "${DEPLOY_CONFIG_LOCATION}" ]]; then
jq -s '.[0] * .[1]' "${CONFIG_LOCATION}" "${DEPLOY_CONFIG_LOCATION}"
else
cp "${CONFIG_LOCATION}" "${RUNTIME_CONFIG_LOCATION}"
. /etc/sth/sth-config

shopt -s extglob

findfirstconf () {
TARGET=$1
for FILE in "${TARGET%%/}".{json,yaml,yml}; do
[[ -e "$FILE" ]] && echo $FILE && break;
done
}

findsequences() {
TARGET=$1
for FILE in "${TARGET%%/}"/*.{tgz,tar.gz}; do
[[ -e "$FILE" ]] && echo $FILE
done
}

settargetname () {
BASE=$(basename "$1")
TARGET=$2

echo ${TARGET%%/}/$BASE
}

DEPLOY_CONFIG_LOCATION=$(findfirstconf "${DEPLOY_PATH}/conf/sth-config")
HAS_SEQUENCES=0
EXTRA_OPTS=("--no-colors" "-D" "$WORK_PATH/sequences")

if [[ -n "${DEPLOY_CONFIG_LOCATION}" ]]; then
RUNTIME_CONFIG_LOCATION=$(settargetname "$DEPLOY_CONFIG_LOCATION" "$WORK_PATH")
cp "${DEPLOY_CONFIG_LOCATION}" "${RUNTIME_CONFIG_LOCATION}"
EXTRA_OPTS+=("--config=${RUNTIME_CONFIG_LOCATION}")

echo "Using STH config from ${DEPLOY_CONFIG_LOCATION}" | logger -e
fi

if [[ -e "${DEPLOY_PATH}/conf/purge.txt" ]]; then
echo "Purging all sequences" | logger -e
rm -rf "$WORK_PATH/sequences"
mkdir "$WORK_PATH/sequences"
fi

SEQUENCES="$(findsequences ${DEPLOY_PATH}/sequences/)"
if [[ -n "$SEQUENCES" ]]; then
function unpack() {
PACKAGE=$1
TARGET=$2

mkdir -p "$TARGET"
tar -C "$TARGET" -zxf "$PACKAGE"
}

STARTUP_CONFIG=$(findfirstconf "${DEPLOY_PATH}/conf/startup-config")
if [[ -n "${STARTUP_CONFIG}" ]]; then
RUNTIME_CONFIG_LOCATION=$(settargetname "$STARTUP_CONFIG" "$WORK_PATH")
cp "${STARTUP_CONFIG}" "${RUNTIME_CONFIG_LOCATION}"
EXTRA_OPTS+=("--startup-config=${RUNTIME_CONFIG_LOCATION}")

echo "Using startup config from ${DEPLOY_CONFIG_LOCATION}" | logger -e
fi

sth --config="${RUNTIME_CONFIG_LOCATION}" 2>&1 | logger -e

for SEQ_FILE in "$(findsequences ${DEPLOY_PATH}/sequences/)"; do
BASENAME=`basename "${SEQ_FILE}"`
TARGET="${BASENAME%.@(tar.gz|tgz)}"

echo "Unpacking sequence '${BASENAME}' to '$WORK_PATH/sequences/$TARGET'" | logger -e
unpack "$SEQ_FILE" "$WORK_PATH/sequences/$TARGET"

EXTRA_OPTS+=("-E")
done
fi

echo Running STH with opts: "${EXTRA_OPTS[@]}"
sth "${EXTRA_OPTS[@]}" 2>&1 | logger -e
4 changes: 4 additions & 0 deletions stage99-sth/04-setup-systemd/files/sth-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LOGCOLORS="--no-colors"
SEQUENCES_ROOT="/srv/sth/sequences"
DEPLOY_PATH=/opt/sth/deploy
WORK_PATH=/srv/sth
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"host": {
"id": "mini-pi-1"
},
"safeOperationLimit": 64,
"instanceRequirements": {
"freeMem": 128
Expand Down
12 changes: 0 additions & 12 deletions stage99-sth/04-setup-systemd/files/sth-config.json

This file was deleted.

0 comments on commit 58d357b

Please sign in to comment.