Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
dezren39 committed Mar 12, 2024
1 parent ecab6b9 commit 5136f6f
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 33 deletions.
2 changes: 2 additions & 0 deletions sources/identity/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
./init
./.init
.link
./.link
./provider/static/init
./provider/data/
./provider/stream/
Expand Down
28 changes: 25 additions & 3 deletions sources/identity/bootstrap-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,40 @@ if [ -n "$5" ]; then
BOOTSTRAP_SCRIPT="$5"
fi
if [ -z "$BOOTSTRAP_SCRIPT" ]; then
# BOOTSTRAP_SCRIPT="bootstrap.sh"
BOOTSTRAP_SCRIPT="init.template.sh"
BOOTSTRAP_SCRIPT="bootstrap.sh"
fi
if [ -n "$6" ]; then
BOOTSTRAP_HOST="$6"
fi
if [ -z "$BOOTSTRAP_HOST" ]; then
BOOTSTRAP_HOST="localhost:3333"
fi
if [ -n "$7" ]; then
BOOTSTRAP_PROVIDER_SCRIPT="$7"
fi
if [ -z "$BOOTSTRAP_PROVIDER_SCRIPT" ]; then
BOOTSTRAP_PROVIDER_SCRIPT="provider.sh"
fi
if [ -n "$8" ]; then
TURSO_HOST=$8
fi
if [ -z "$TURSO_HOST" ]; then
echo "TURSO_HOST not set"
exit 1
fi
if [ -n "$9" ]; then
TURSO_AUTH_TOKEN=$9 # really shouldn't do this it's viewable in ps, etc.
fi
if [ -z "$TURSO_AUTH_TOKEN" ]; then
echo "TURSO_AUTH_TOKEN not set"
exit 1
fi
apt update
apt install -y git
git clone "$BOOTSTRAP_REPO"
cd "$BOOTSTRAP_DIR"
chmod +x "./$BOOTSTRAP_PROVIDER_SCRIPT"
"TURSO_AUTH_TOKEN=$TURSO_AUTH_TOKEN" "TURSO_HOST=$TURSO_HOST" "PORT=3333" "INIT_URL=$BOOTSTRAP_HOST/init" ./"$BOOTSTRAP_PROVIDER_SCRIPT" &
sleep 15
chmod +x "./$BOOTSTRAP_SCRIPT"
"./$BOOTSTRAP_SCRIPT" "$BOOTSTRAP_HOST"
source "./$BOOTSTRAP_SCRIPT" "$BOOTSTRAP_HOST"
2 changes: 1 addition & 1 deletion sources/identity/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ apt update
apt install -y curl
curl -o "$BOOTSTRAP_SCRIPT" "http://$BOOTSTRAP_HOST/$BOOTSTRAP_URL_PATH/$BOOTSTRAP_SCRIPT"
chmod +x "./$BOOTSTRAP_SCRIPT"
"./$BOOTSTRAP_SCRIPT"
source "./$BOOTSTRAP_SCRIPT"
2 changes: 1 addition & 1 deletion sources/identity/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
gopkg.in/go-jose/go-jose.v2 v2.6.3
)

replace github.com/charmbracelet/charm => github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626
replace github.com/charmbracelet/charm => github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac

require (
github.com/FZambia/eagle v0.1.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions sources/identity/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626 h1:VGhRmEVrZAzupFbnlwBHa+JpwffnJCukj4jtojsQI94=
github.com/developing-today-forks/charm v0.12.7-0.20240310061729-3b7e199cf626/go.mod h1:6xzxrach+U21VLDQYKlWRMq+Basc95gD4AQ8v7LLEhE=
github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac h1:tPq59hN7UOMb0D4LKjS2ZG+OYFUvzKf/A4pxCDarZYQ=
github.com/developing-today-forks/charm v0.12.7-0.20240312075138-411f9ff4f8ac/go.mod h1:6xzxrach+U21VLDQYKlWRMq+Basc95gD4AQ8v7LLEhE=
github.com/dgraph-io/badger/v3 v3.2103.5 h1:ylPa6qzbjYRQMU6jokoj4wzcaweHylt//CH0AKt0akg=
github.com/dgraph-io/badger/v3 v3.2103.5/go.mod h1:4MPiseMeDQ3FNCYwRbbcBOGJLf5jsE0PPFzRiKjtcdw=
github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWajOK8=
Expand Down
60 changes: 50 additions & 10 deletions sources/identity/init.template.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
# must be bash because we source a bashrc file
if [ -n "$1" ]; then
CHARM_URL="$1"
fi
Expand All @@ -14,7 +15,7 @@ export CHARM_URL
if [ -n "$2" ]; then
CHARM_LINK_URL="$2"
fi
if [ -n "{{CHARM_LINK_URL}}" ] && [ "{{CHARM_LINK_URL}}" != "\{\{CHARM_LINK_URL\}\}" ]; then
if [ -z "$CHARM_LINK_URL" ] && [ -n "{{CHARM_LINK_URL}}" ] && [ "{{CHARM_LINK_URL}}" != "\{\{CHARM_LINK_URL\}\}" ]; then
CHARM_LINK_URL="{{CHARM_LINK_URL}}"
fi
if [ -z "$CHARM_LINK_URL" ] || [ "$CHARM_LINK_URL" = "\{\{CHARM_LINK_URL\}\}" ]; then
Expand All @@ -25,16 +26,21 @@ if [ -z "$CHARM_LINK_URL" ] || [ "$CHARM_LINK_URL" = "\{\{CHARM_LINK_URL\}\}" ];
IP="127.0.0.1"
fi
fi
PORT=3333
CHARM_LINK_URL="http://$IP:$PORT/link"
echo "No charm link provided"
echo "Using default link: $CHARM_LINK_URL"
fi
export CHARM_LINK_URL
/boot/dietpi/dietpi-software uninstall 103 104 # ramlog dropbear
/boot/dietpi/dietpi-software install 188 # go (git by dependency)
source /etc/bash.bashrc
if [ -f /etc/bash.bashrc ]; then
source /etc/bash.bashrc
else
echo "/etc/bash.bashrc does not exist, continuing without sourcing it."
fi
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --batch --yes --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_21.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt update
apt install -y curl nodejs npm ucspi-tcp unzip
Expand All @@ -55,15 +61,49 @@ if [ ! -d "code" ]; then
else
echo "code directory already exists"
fi
cd code/source/identity
cd code/src/identity
chmod +x *.ps1
./build-libsql.ps1
CHARM_LINK=$(curl -sL "$CHARM_LINK_URL")
if [ -z "$CHARM_LINK" ]; then
echo "Failed to obtain charm link"
exit 1
"CHARM_LINK_URL=$CHARM_LINK_URL" ./provider.sh &
get_http_status() {
local url=$1
curl -o /dev/null -s -w "%{http_code}\n" "$url"
}

start_time=$(date +%s)

while : ; do
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))

if [ "$elapsed_time" -ge 60 ]; then
echo "1 minute has elapsed, stopping."
break
fi

http_status=$(get_http_status "$CHARM_LINK_URL")
echo "Checking URL: $CHARM_LINK_URL - HTTP status: $http_status"

if [ "$http_status" -eq 405 ]; then
echo "Received 405 code, exiting."
break
fi

sleep 2
done
if [ "$elapsed_time" -ge 60 ]; then
echo "Failed to obtain charm link"
exit 1
fi
response=$(curl -sL "$CHARM_LINK_URL" --data-urlencode "keys=$(./identity charm keys --simple | tr '\n' ',' | sed 's/,$//')")

if [ -n "$response" ]; then
CHARM_LINK=$response
else
echo "Failed to obtain charm link"
exit 1
fi
./identity charm link "$CHARM_LINK"
./identity charm link -d "$CHARM_LINK"
./identity charm kv sync
./identity charm kv get dt.identity.init > .init
chmod +x .init
Expand Down
21 changes: 13 additions & 8 deletions sources/identity/provider.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
if [ -n "$1" ]; then
CHARM_DIR="$1"
fi
random() {
echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p)
}
if [ -z "$CHARM_DIR" ]; then
CHARM_DIR=~$USER/code/source/identity/data/charms/$(random)
fi
Expand Down Expand Up @@ -31,17 +34,19 @@ if [ -z "$TURSO_AUTH_TOKEN" ]; then
echo "TURSO_AUTH_TOKEN not set"
exit 1
fi
random() {
echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p)
}
IP=$(hostname -I | awk '{print $1}')
if [ "$(expr substr "$IP" 1 4)" = "172." ]; then
IP=$(hostname -I | awk '{print $2}')
if [ -n "$6" ]; then
CHARM_LINK_URL=$6
fi
if [ -z "$CHARM_LINK_URL" ]; then
IP=$(hostname -I | awk '{print $1}')
if [ "$(expr substr "$IP" 1 4)" = "172." ]; then
IP="127.0.0.1"
IP=$(hostname -I | awk '{print $2}')
if [ "$(expr substr "$IP" 1 4)" = "172." ]; then
IP="127.0.0.1"
fi
fi
CHARM_LINK_URL="http://$IP:$PORT/link"
fi
CHARM_LINK_URL="http://$IP:$PORT/link"
cp -f ./init.template.sh ./provider/static/init
sed -i "s|{{CHARM_DIR}}|$CHARM_DIR|g" ./provider/static/init
sed -i "s|{{CHARM_LINK_URL}}|$CHARM_LINK_URL|g" ./provider/static/init
Expand Down
28 changes: 23 additions & 5 deletions sources/identity/provider/pages/link.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ random() {
echo $(dd if=/dev/urandom bs=1 count=64 2>/dev/null | xxd -p)
}
for key in "${!FORM_DATA[@]}"; do
if [[ "$key" == "link" ]]; then
echo "CHARM_DIR=$CHARM_DIR/$(random)"
CHARM_DIR=$CHARM_DIR/$(random) ~$USER/code/source/identity/identity charm link ${FORM_DATA[$key]}
if [[ $? -eq 0 ]]; then
respond 200 "${FORM_DATA[$key]}"
if [[ "$key" == "keys" ]]; then
CHARM_DIR=$CHARM_DIR/$(random)
mkdir -p "$CHARM_DIR"
LINK_CODE_PATH=$CHARM_DIR/.link
rm -rf "$LINK_CODE_PATH"
./identity charm link -d -o "$LINK_CODE_PATH" -keys "${FORM_DATA[$key]}" &

max_wait=60 # seconds
wait_interval=1 # seconds
elapsed_time=0

while [[ ! -f "$LINK_CODE_PATH" && $elapsed_time -lt $max_wait ]]; do
sleep $wait_interval
((elapsed_time+=wait_interval))
done

if [[ -f "$LINK_CODE_PATH" ]]; then
LINK_CODE=$(cat "$LINK_CODE_PATH")
if [[ -z "$LINK_CODE" ]]; then
respond 405 "Failure."
else
respond 200 "$LINK_CODE"
fi
else
respond 405 "Failure."
fi
Expand Down
4 changes: 2 additions & 2 deletions sources/identity/web/templates/main_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sources/identity/web/templates/pages/index_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5136f6f

Please sign in to comment.