Last active
July 21, 2024 17:44
-
-
Save Qwizi/1fa94f127c69d79fc173951a314427dc to your computer and use it in GitHub Desktop.
SharkServers manage.sh
This file contains hidden or 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 | |
| # SharkServers Management Script | |
| # This script provides utilities for managing the SharkServers application environment. | |
| set -e | |
| set -o pipefail | |
| # Function to display the help message | |
| function help_message() { | |
| echo -e "\033[1mUSAGE:\033[0m" | |
| echo -e " \033[1m./manage.sh\033[0m --install [dev] \t Install the application (optional 'dev' for development environment)." | |
| echo -e " \033[1m./manage.sh\033[0m --migrate \t\t Run database migrations." | |
| echo -e " \033[1m./manage.sh\033[0m --create-superuser \t Create a superuser account." | |
| echo -e " \033[1m./manage.sh\033[0m --load-data \t\t Load initial data into the database." | |
| echo -e " \033[1m./manage.sh\033[0m --uninstall \t\t Uninstall the application." | |
| echo -e " \033[1m./manage.sh\033[0m --help \t\t Display this help message." | |
| echo | |
| echo -e "\033[1mDESCRIPTION:\033[0m" | |
| echo " This script is designed to simplify the management of the SharkServers application." | |
| echo " It allows for easy installation, database migration, superuser creation, data loading, and uninstallation." | |
| echo | |
| } | |
| # Function definitions for script operations | |
| function migrate() { | |
| echo -e "\033[1mRunning migrations...\033[0m" | |
| docker compose exec app python manage.py migrate sourcebans --database=sourcebans | |
| docker compose exec app python manage.py migrate | |
| echo -e "\033[1mMigrations ran successfully.\033[0m" | |
| } | |
| function create_groups() { | |
| echo -e "\033[1mCreating groups...\033[0m" | |
| GROUP_COUNT=$(docker compose exec app python manage.py shell -c "from accounts.models import Group; print(Group.objects.all().count());") | |
| if [ "$GROUP_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata groups | |
| echo -e "\033[1mGroups created successfully.\033[0m" | |
| else | |
| echo -e "\033[1mGroups already exist.\033[0m" | |
| fi | |
| } | |
| function create_test_superuser() { | |
| echo -e "\033[1mCreating superuser...\033[0m" | |
| SUPERUSER_COUNT=$(docker compose exec -T app python manage.py shell -c 'from django.contrib.auth import get_user_model; User = get_user_model(); print(User.objects.filter(is_superuser=True).count());') | |
| if [ "$SUPERUSER_COUNT" = "0" ]; then | |
| echo "Creating superuser" | |
| # Create superuser | |
| docker compose exec -T app python manage.py shell -c 'from django.contrib.auth import get_user_model; from accounts.models import Group, UserGroupOrder; admin_group = Group.objects.get(id="group_1vjj1DC9QJHMyxcCtEw1tb"); User = get_user_model(); admin_user = User.objects.create_superuser(username="admin", password="admin", email=None); admin_user.groups.add(admin_group); UserGroupOrder.objects.create(user=admin_user, group=admin_group, order=1);' | |
| echo "Superuser admin with password admin created" | |
| echo -e "\033[1mSuperuser admin with password admin created.\033[0m" | |
| else | |
| echo -e "\033[1mSuperuser already exists.\033[0m" | |
| fi | |
| } | |
| function create_sourcebans_stuff() { | |
| echo -e "\033[1mCreating SourceBans stuff...\033[0m" | |
| SB_ADMIN_COUNT=$(docker compose exec app python manage.py shell -c "from sourcebans.models import Admin; print(Admin.objects.all().count());") | |
| if [ "$SB_ADMIN_COUNT" = "0" ]; then | |
| docker compose exec -T app python manage.py loaddata --database=sourcebans admins | |
| echo -e "\033[1mAdmins loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mAdmins already exist.\033[0m" | |
| fi | |
| SB_GROUP_COUNT=$(docker compose exec app python manage.py shell -c "from sourcebans.models import Group; print(Group.objects.all().count());") | |
| if [ "$SB_GROUP_COUNT" = "0" ]; then | |
| docker compose exec -T app python manage.py loaddata --database=sourcebans sbgroups | |
| echo -e "\033[1mGroups loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mGroups already exist.\033[0m" | |
| fi | |
| SB_SRVGROUP_COUNT=$(docker compose exec app python manage.py shell -c "from sourcebans.models import SRVGroup; print(SRVGroup.objects.all().count());") | |
| if [ "$SB_SRVGROUP_COUNT" = "0" ]; then | |
| docker compose exec -T app python manage.py loaddata --database=sourcebans srvgroups | |
| echo -e "\033[1mServer groups loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mServer groups already exist.\033[0m" | |
| fi | |
| SB_OVERRIDES_COUNT=$(docker compose exec app python manage.py shell -c "from sourcebans.models import Override; print(Override.objects.all().count());") | |
| if [ "$SB_OVERRIDES_COUNT" = "0" ]; then | |
| docker compose exec -T app python manage.py loaddata --database=sourcebans overrides | |
| echo -e "\033[1mOverrides loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mOverrides already exist.\033[0m" | |
| fi | |
| echo -e "\033[1mSourceBans stuff created successfully.\033[0m" | |
| } | |
| function create_rooms() { | |
| echo -e "\033[1mCreating rooms...\033[0m" | |
| ROOMS_COUNT=$(docker compose exec app python manage.py shell -c "from sharkbox.models import Room; print(Room.objects.all().count());") | |
| if [ "$ROOMS_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata rooms | |
| fi | |
| echo -e "\033[1mRooms created successfully.\033[0m" | |
| } | |
| function load_data() { | |
| echo -e "\033[1mLoading data...\033[0m" | |
| docker compose exec app python manage.py loaddata accounts | |
| echo -e "\033[1mUsers loaded successfully.\033[0m" | |
| EMAILS_COUNT=$(docker compose exec app python manage.py shell -c "from allauth.account.models import EmailAddress; print(EmailAddress.objects.all().count());") | |
| if [ "$EMAILS_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata emails | |
| echo -e "\033[1mEmails loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mEmails already exist.\033[0m" | |
| fi | |
| CATEGORY_COUNT=$(docker compose exec app python manage.py shell -c "from forum.models import Category; print(Category.objects.all().count());") | |
| if [ "$CATEGORY_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata categories | |
| echo -e "\033[1mCategories loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mCategories already exist.\033[0m" | |
| fi | |
| POSTS_COUNT=$(docker compose exec app python manage.py shell -c "from forum.models import Post; print(Post.objects.all().count());") | |
| if [ "$POSTS_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata posts | |
| echo -e "\033[1mPosts loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mPosts already exist.\033[0m" | |
| fi | |
| THREADS_COUNT=$(docker compose exec app python manage.py shell -c "from forum.models import Thread; print(Thread.objects.all().count());") | |
| if [ "$THREADS_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata threads | |
| echo -e "\033[1mThreads loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mThreads already exist.\033[0m" | |
| fi | |
| MESSAGE_COUNT=$(docker compose exec app python manage.py shell -c "from sharkbox.models import Message; print(Message.objects.all().count());") | |
| if [ "$MESSAGE_COUNT" = "0" ]; then | |
| docker compose exec app python manage.py loaddata messages | |
| echo -e "\033[1mMessages loaded successfully.\033[0m" | |
| else | |
| echo -e "\033[1mMessages already exist.\033[0m" | |
| fi | |
| echo -e "\033[1mData loaded successfully.\033[0m" | |
| } | |
| function wait_for_sourcebans_db() { | |
| echo -e "\033[1mWaiting for SourceBans database to be ready...\033[0m" | |
| bash scripts/wait-for-it.sh db_sbpp:3306 --timeout=10 -- echo "db_sbpp is up" | |
| echo -e "\033[1mSourceBans database is ready.\033[0m" | |
| } | |
| function wait_for_app_db() { | |
| echo -e "\033[1mWaiting for application database to be ready...\033[0m" | |
| bash scripts/wait-for-it.sh localhost:5437 --timeout=20 -- echo "db is up" | |
| echo -e "\033[1mApplication database is ready.\033[0m" | |
| } | |
| function wait_for_database() { | |
| wait_for_app_db | |
| wait_for_sourcebans_db | |
| } | |
| function install_dev() { | |
| echo -e "\033[1mInstalling development environment...\033[0m" | |
| docker compose up -d --build | |
| wait_for_database | |
| migrate | |
| create_groups | |
| create_test_superuser | |
| create_sourcebans_stuff | |
| create_rooms | |
| echo -e "\033[1mDevelopment environment installed successfully.\033[0m" | |
| } | |
| function uninstall() { | |
| echo -e "\033[1mUninstalling development environment...\033[0m" | |
| docker compose down | |
| echo -e "\033[1mUninstallation complete.\033[0m" | |
| } | |
| # shellcheck disable=SC2120 | |
| function create_superuser_cli() { | |
| if [ $# -lt 2 ]; then | |
| echo -e "\033[1mError: Username and password arguments are required.\033[0m" | |
| return 1 | |
| fi | |
| local username=$1 | |
| local password=$2 | |
| echo -e "\033[1mCreating superuser with username: $username\033[0m" | |
| docker compose exec -T app python manage.py shell -c "from django.contrib.auth import get_user_model; from accounts.models import Group, UserGroupOrder; admin_group = Group.objects.get(id='group_1vjj1DC9QJHMyxcCtEw1tb'); User = get_user_model(); admin_user = User.objects.create_superuser(username='$username', password='$password', email=None); admin_user.groups.add(admin_group); UserGroupOrder.objects.create(user=admin_user, group=admin_group, order=1);" | |
| echo -e "\033[1mSuperuser $username created successfully.\033[0m" | |
| } | |
| function install_prod() { | |
| echo -e "\033[1mInstalling production environment...\033[0m" | |
| local name="" | |
| local project_uuid="" | |
| local server_uuid="" | |
| local gh="" | |
| while [[ "$#" -gt 0 ]]; do | |
| case $1 in | |
| --name) name="$2"; shift ;; | |
| --project) project_uuid="$2"; shift ;; | |
| --server) server_uuid="$2"; shift ;; | |
| --gh) gh="$2"; shift ;; | |
| *) echo "Unknown parameter passed: $1"; exit 1 ;; | |
| esac | |
| shift | |
| done | |
| if [[ -z "$name" ]]; then | |
| echo -e "\033[1mError: Name is required for production environment installation.\033[0m" | |
| exit 1 | |
| fi | |
| if [[ -z "$project_uuid" ]]; then | |
| echo -e "\033[1mError: Project UUID is required for production environment installation.\033[0m" | |
| exit 1 | |
| fi | |
| if [[ -z "$server_uuid" ]]; then | |
| echo -e "\033[1mError: Server UUID is required for production environment installation.\033[0m" | |
| exit 1 | |
| fi | |
| if [[ -z "$gh" ]]; then | |
| echo -e "\033[1mError: GitHub App UUID is required for production environment installation.\033[0m" | |
| exit 1 | |
| fi | |
| local json_payload=$(jq -n \ | |
| --arg project_uuid "$project_uuid" \ | |
| --arg server_uuid "$server_uuid" \ | |
| --arg name "$name" \ | |
| --arg gh "$gh" \ | |
| '{ | |
| project_uuid: $project_uuid, | |
| server_uuid: $server_uuid, | |
| environment_name: "production", | |
| github_app_uuid: $gh, | |
| git_repository: "https://github.com/Qwizi/SharkServersReborn/", | |
| git_branch: "master", | |
| build_pack: "dockercompose", | |
| name: $name, | |
| description: $name, | |
| base_directory: "/", | |
| instant_deploy: false, | |
| docker_compose_location: "docker-compose.prod.yml", | |
| }') | |
| response=$(curl --request POST \ | |
| --url "$COOLIFY_HOSTNAME/api/v1/applications/private-github-app" \ | |
| --header 'Authorization: Bearer '"$COOLIFY_KEY"'' \ | |
| --header 'Content-Type: application/json' \ | |
| --data "$json_payload") | |
| local app_uuid=$(echo "$response" | jq -r '.uuid') | |
| local json_env_payload=$(jq -n \ | |
| --arg app_uuid "$app_uuid" \ | |
| '{ | |
| key: "COOLIFY_APP_UUID", | |
| value: $app_uuid, | |
| "is_preview": true, | |
| "is_build_time": true, | |
| "is_literal": true, | |
| "is_multiline": false, | |
| "is_shown_once": false | |
| }') | |
| response=$(curl --request PATCH \ | |
| --url "$COOLIFY_HOSTNAME/api/v1/applications/$app_uuid/envs" \ | |
| --header 'Authorization: Bearer '"$COOLIFY_KEY"'' \ | |
| --header 'Content-Type: application/json' \ | |
| --data "$json_payload") | |
| echo -e "\033[1mProduction environment installed successfully.\033[0m" | |
| echo -e "\033[1mApplication created with UUID: $app_uuid\033[0m" | |
| } | |
| # Main script logic to parse arguments and call the appropriate function | |
| case "$1" in | |
| --install) | |
| shift | |
| if [[ "$1" == "--name" ]]; then | |
| install_prod "$@" | |
| else | |
| install_dev | |
| fi | |
| ;; | |
| --migrate) | |
| migrate | |
| ;; | |
| --create-superuser) | |
| shift # Remove the first argument, which is --create-superuser | |
| create_superuser_cli "$@" | |
| ;; | |
| --load-data) | |
| load_data | |
| ;; | |
| --uninstall) | |
| uninstall | |
| ;; | |
| --help) | |
| help_message | |
| ;; | |
| *) | |
| echo -e "\033[1mUnknown option: $1\033[0m" | |
| help_message | |
| exit 1 | |
| ;; | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment