Created
May 18, 2023 09:21
-
-
Save richban/2310df15b3c8898b3be18e858d074b06 to your computer and use it in GitHub Desktop.
Get authtoken from a Django App running inside a docker container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Get the container ID | |
container_id=$(docker ps -aqf "name=geo-django-1") | |
# If container is not running | |
if [ -z "$container_id" ]; then | |
echo "Container is not running." | |
exit 1 | |
fi | |
# Command to be run inside the Django shell | |
django_shell_command=$( | |
cat <<END | |
from api.models import User | |
from rest_framework.authtoken.models import Token | |
from django.db import IntegrityError | |
try: | |
user = User.objects.get(id=1) | |
token, created = Token.objects.get_or_create(user=user) | |
except IntegrityError: | |
token = Token.objects.get(user=user) | |
print(token.key) | |
END | |
) | |
# Run the command inside the Django shell | |
token=$(docker exec -i $container_id bash -c "echo \"$django_shell_command\" | python manage.py shell") | |
echo "Token $token" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment