Skip to content

Commit

Permalink
fix: update sync script (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
vncsna authored Sep 19, 2023
1 parent 5c5d852 commit 0144318
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 58 deletions.
8 changes: 4 additions & 4 deletions basedosdados_api/core/management/commands/loadfixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ def handle(self, *args, **options) -> str | None:
return None

print("Purge previous database if exists")
DB_PATH.unlink(missing_ok=True)
call_command("flush", interactive=False)

print("Migrate development database")
call_command("migrate")

print("Purge database restrictions")
with connection.cursor() as cursor:
cursor.execute(f"{DB_STATEMENT} auth_permission")
cursor.execute(f"{DB_STATEMENT} django_admin_log")
cursor.execute(f"{DB_STATEMENT} django_content_type")
cursor.execute(f"{DB_STATEMENT} auth_permission CASCADE")
cursor.execute(f"{DB_STATEMENT} django_admin_log CASCADE")
cursor.execute(f"{DB_STATEMENT} django_content_type CASCADE")

print("Load fixtures")
response = super().handle(*args, **options)
Expand Down
33 changes: 0 additions & 33 deletions scripts/dump.sh

This file was deleted.

67 changes: 46 additions & 21 deletions scripts/sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,67 @@ set -euxo pipefail
# kubectl config set-context --current --namespace=,,,

# Set file
FILE=fixtures
FILE="${FILE:-fixtures}"

# Set destination environment
SRC_ENV=api-prod
DST_ENV=api-staging
# If dump doesn't exist
if [ ! -f "$FILE.tar.gz" ]; then
# Set source env
SRC_ENV="${SRC_ENV:-api-prod}"

# Get source pod
SRC_POD=$(kubectl get po -l app.kubernetes.io/name=$SRC_ENV -o name | \
sed "s/^.\{4\}//" | \
head -n 1\
)

# Dump fixtures in source pod
kubectl exec $SRC_POD -- /bin/bash -c "\
python manage.py dumpfixture --output $FILE.json && \
tar -czvf $FILE.tar.gz $FILE.json\
"

# Copy fixtures from source pod
kubectl cp --retries=2 $SRC_POD:/app/$FILE.tar.gz ./$FILE.tar.gz

# Remove fixtures from source pod
kubectl exec $SRC_POD -- rm $FILE.json $FILE.tar.gz
fi

# Set destination env
DST_ENV=""
if [[ $# -eq 1 ]]; then
# Local
if [[ $1 = l* ]]; then
exit 0
fi
# Staging
if [[ $1 = s* ]]; then
DST_ENV=api-staging
fi
# Development
if [[ $1 = d* ]]; then
DST_ENV=api-development
fi
fi
if [[ $DST_ENV = "" ]]; then
echo "Please specify environment"
exit 1
fi

# Get source and destination pods
SRC_POD=$(kubectl get po -l app.kubernetes.io/name=$SRC_ENV -o name | \
sed "s/^.\{4\}//" | \
head -n 1\
)
# Get destination pod
DST_POD=$(kubectl get po -l app.kubernetes.io/name=$DST_ENV -o name | \
sed "s/^.\{4\}//" | \
head -n 1\
)

# Dump fixtures in source pod
kubectl exec $SRC_POD -- /bin/bash -c "\
python manage.py dumpfixture --output $FILE.json && \
tar -czvf $FILE.tar.gz $FILE.json\
"

# Copy fixtures from source pod to destination pod
kubectl cp $SRC_POD:/app/$FILE.tar.gz $DST_POD:/app/$FILE.tar.gz
# Copy fixtures to destination pod
kubectl cp --retries=2 ./$FILE.tar.gz $DST_POD:/app/$FILE.tar.gz

# Load fixtures in destination pod
kubectl exec $SRC_POD -- /bin/bash -c "\
kubectl exec $DST_POD -- /bin/bash -c "\
tar -xzvf $FILE.tar.gz && \
python manage.py loadfixture $FILE.json && \
python manage.py loadfixture $FILE.json\
"

# Remove fixtures from pods
kubectl exec $SRC_POD -- rm $FILE.json $FILE.tar.gz
# Remove fixtures from destination
kubectl exec $DST_POD -- rm $FILE.json $FILE.tar.gz

0 comments on commit 0144318

Please sign in to comment.