Skip to content

Commit

Permalink
Merge pull request #37 from nsg/sync-python
Browse files Browse the repository at this point in the history
Re-implement sync service in pure python
  • Loading branch information
nsg committed Jun 18, 2023
2 parents f6edaa0 + 296fd05 commit 28a5ff6
Show file tree
Hide file tree
Showing 11 changed files with 309 additions and 321 deletions.
5 changes: 1 addition & 4 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@ fi
snapctl restart immich-distribution.postgres
snapctl restart immich-distribution.typesense
snapctl restart immich-distribution.haproxy

snapctl restart immich-distribution.sync-removed-fs
snapctl restart immich-distribution.sync-created-fs
snapctl restart immich-distribution.sync-database-watch
snapctl restart immich-distribution.sync-service
29 changes: 9 additions & 20 deletions snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,26 +132,8 @@ apps:
plugs:
- network

sync-removed-fs:
command: bin/sync-removed-watch
daemon: simple
restart-delay: 10s
after:
- immich-server
plugs:
- network

sync-created-fs:
command: bin/sync-created-watch
daemon: simple
restart-delay: 10s
after:
- immich-server
plugs:
- network

sync-database-watch:
command: bin/sync-database-watch
sync-service:
command: bin/sync-service.sh
daemon: simple
restart-delay: 10s
after:
Expand Down Expand Up @@ -373,6 +355,13 @@ parts:
- jq
- xz-utils

sync:
plugin: nil
override-build:
../../python/install/usr/local/bin/python3 ../../python/install/usr/local/bin/pip3 install watchfiles requests psycopg2-binary --target $SNAPCRAFT_PART_INSTALL/opt/pipsyncenv
after:
- python

patches:
source: patches
plugin: dump
Expand Down
33 changes: 14 additions & 19 deletions src/bin/load-env
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ snapctl() {

export LD_LIBRARY_PATH="$SNAP/usr/local/pgsql/lib:$SNAP/usr/lib/x86_64-linux-gnu/pulseaudio:$SNAP/usr/lib/x86_64-linux-gnu/libfswatch:$LD_LIBRARY_PATH"

export PGDATA="$SNAP_COMMON/pgsql/data"

export LC_ALL="C"
export LC_CTYPE="C"

Expand Down Expand Up @@ -61,33 +63,14 @@ immich_server_ready() {
curl -s "$IMMICH_SERVER_URL/server-info/ping" -o /dev/null
}

block_immich_server_ready() {
while ! immich_server_ready; do
sleep 2
done
}

immich_api() {
curl -s "$IMMICH_SERVER_URL/$2" -H 'Accept: application/json' -H "x-api-key: $1"
}

immich_api_delete() {
curl -s "$IMMICH_SERVER_URL/$2" -X DELETE -H "Accept: application/json" \
-H "Content-Type: application/json" -H "x-api-key: $1" \
-d "$3"
}

user_id() {
immich_api "$1" user/me | jq -r .id
}

immich_delete_asset() {
local key="$1"
local asset_id="$2"

immich_api_delete "$key" "asset" '{"ids": ["'$asset_id'"]}'
}

root_check() {
if [ x$UID != x0 ]; then
echo "You need to run this command as root (with sudo)"
Expand All @@ -104,3 +87,15 @@ banner() {
lego_staging_string() {
[ x$ACME_STAGING == xtrue ] && echo "--server=https://acme-staging-v02.api.letsencrypt.org/directory"
}

sync_enabled() {
snapctl get sync-enabled | grep -q "true"
}

query_db() {
if [ -z $SNAP ]; then
cat - | sudo immich-distribution.psql -d immich $@
else
cat - | $SNAP/bin/psql -d immich $@
fi
}
108 changes: 0 additions & 108 deletions src/bin/sync

This file was deleted.

51 changes: 0 additions & 51 deletions src/bin/sync-created-watch

This file was deleted.

53 changes: 0 additions & 53 deletions src/bin/sync-database-watch

This file was deleted.

19 changes: 6 additions & 13 deletions src/bin/sync-import-watch
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
#!/bin/bash

if [ -z $SNAP ]; then
. src/bin/sync
else
. $SNAP/bin/sync
fi

if [ -z $SNAP ]; then
echo "Abort: Only run this inside the snap"
exit 0
fi
. $SNAP/bin/load-env

if ! sync_enabled; then
echo "Folder sync disabled"
exit 0
fi

block_immich_server_ready
while ! immich_server_ready; do
sleep 2
done

for KEY in $(get_keys); do
for KEY in $(snapctl get sync); do
USER_ID="$(user_id $KEY)"

if [ ! -z "$USER_ID" ]; then
USER_PATH="$SNAP_COMMON/sync/$USER_ID"
mkdir -p "$USER_PATH"

echo "Full import of $USER_PATH for user $USER_ID"
upload "$KEY" --recursive "$USER_PATH"
$SNAP/bin/immich-cli upload --recursive --key "$KEY" --yes "$USER_PATH"
fi
done
Loading

0 comments on commit 28a5ff6

Please sign in to comment.