Added migration comment to mastodon

This commit is contained in:
Kevin Veen-Birkenbach 2024-11-07 23:50:46 -03:00
parent 3e49bd84dc
commit e243d91145
2 changed files with 53 additions and 19 deletions

View File

@ -32,9 +32,20 @@ After setting up mastodon you need to give the rights
docker-compose exec -it -u root web chown -R 991:991 public docker-compose exec -it -u root web chown -R 991:991 public
``` ```
### Running Database Migrations
To ensure all required database tables and structures are in place after an update, you can run database migrations directly within the Mastodon web container. Use the following command:
```bash
docker compose exec -it web bash -c "RAILS_ENV=production bin/rails db:migrate"
```
This command enters the web container and runs migrations in production mode, updating the database structure to match the current version of the Mastodon software. If your Mastodon web container is named differently in your `docker-compose.yml`, replace `web` with the appropriate container name.
## further information ## further information
- https://goneuland.de/mastodon-mit-docker-und-traefik-installieren/ - https://goneuland.de/mastodon-mit-docker-und-traefik-installieren/
- https://gist.github.com/TrillCyborg/84939cd4013ace9960031b803a0590c4 - https://gist.github.com/TrillCyborg/84939cd4013ace9960031b803a0590c4
- https://www.2daygeek.com/linux-command-check-website-is-up-down-alive/ - https://www.2daygeek.com/linux-command-check-website-is-up-down-alive/
- https://vitobotta.com/2022/11/07/setting-up-a-personal-mastodon-instance/ - https://vitobotta.com/2022/11/07/setting-up-a-personal-mastodon-instance/
- https://www.digitalocean.com/community/tutorials/how-to-scale-your-mastodon-server - https://www.digitalocean.com/community/tutorials/how-to-scale-your-mastodon-server

View File

@ -25,12 +25,6 @@ def run_command(command):
def git_pull(): def git_pull():
""" """
Checks whether the Git repository in the specified directory is up to date and performs a git pull if necessary. Checks whether the Git repository in the specified directory is up to date and performs a git pull if necessary.
Args:
directory (str): The path to the directory of the Git repository.
Returns:
bool: True if a git pull was performed, otherwise False.
""" """
print(f"Checking if the git repository is up to date.") print(f"Checking if the git repository is up to date.")
local = subprocess.check_output("git rev-parse @", shell=True).decode().strip() local = subprocess.check_output("git rev-parse @", shell=True).decode().strip()
@ -39,12 +33,15 @@ def git_pull():
if local != remote: if local != remote:
print("Repository is not up to date. Performing git pull.") print("Repository is not up to date. Performing git pull.")
run_command("git pull") run_command("git pull")
return True; return True
print("Repository is already up to date.") print("Repository is already up to date.")
return False; return False
def get_image_digests(directory): def get_image_digests(directory):
"""
Retrieves the image digests for all images in the specified Docker Compose project.
"""
compose_project = os.path.basename(directory) compose_project = os.path.basename(directory)
try: try:
images_output = subprocess.check_output( images_output = subprocess.check_output(
@ -59,16 +56,18 @@ def get_image_digests(directory):
raise # Other errors are still raised raise # Other errors are still raised
def is_any_service_up(): def is_any_service_up():
"""
Checks if any Docker services are currently running.
"""
process = subprocess.Popen("docker-compose ps -q", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) process = subprocess.Popen("docker-compose ps -q", shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output, _ = process.communicate() output, _ = process.communicate()
service_ids = output.decode().strip().splitlines() service_ids = output.decode().strip().splitlines()
return bool(service_ids)
# Check if there are any service containers up
if not service_ids:
return False # No services are up
return True # At least one service is up
def pull_docker_images(): def pull_docker_images():
"""
Pulls the latest Docker images for the project.
"""
print("Pulling docker images.") print("Pulling docker images.")
try: try:
run_command("docker-compose pull") run_command("docker-compose pull")
@ -82,6 +81,9 @@ def pull_docker_images():
return False return False
def update_docker(directory): def update_docker(directory):
"""
Checks for updates to Docker images and rebuilds containers if necessary.
"""
print(f"Checking for updates to Docker images in {directory}.") print(f"Checking for updates to Docker images in {directory}.")
before_digests = get_image_digests(directory) before_digests = get_image_digests(directory)
need_to_build = pull_docker_images() need_to_build = pull_docker_images()
@ -96,7 +98,18 @@ def update_docker(directory):
else: else:
print("Docker images are up to date. No rebuild necessary.") print("Docker images are up to date. No rebuild necessary.")
def update_mastodon():
"""
Runs the database migration for Mastodon to ensure all required tables are up to date.
"""
print("Starting Mastodon database migration.")
run_command("docker compose exec -T web bash -c 'RAILS_ENV=production bin/rails db:migrate'")
print("Mastodon database migration complete.")
def update_nextcloud(): def update_nextcloud():
"""
Performs the necessary Nextcloud update procedures, including maintenance and app updates.
"""
print("Start Nextcloud update procedure.") print("Start Nextcloud update procedure.")
update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ upgrade") update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ upgrade")
update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ maintenance:repair") update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ maintenance:repair")
@ -104,12 +117,17 @@ def update_nextcloud():
update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ maintenance:mode --off") update_procedure("docker-compose exec -T -u www-data application /var/www/html/occ maintenance:mode --off")
def update_discourse(directory): def update_discourse(directory):
"""
Updates Discourse by running the rebuild command on the launcher script.
"""
os.chdir(directory) os.chdir(directory)
print("Start Discourse update procedure.") print("Start Discourse update procedure.")
update_procedure("./launcher rebuild app") update_procedure("./launcher rebuild app")
# This procedure waits until the container is up
def update_procedure(command): def update_procedure(command):
"""
Attempts to execute a command up to a maximum number of retries.
"""
max_attempts = 3 max_attempts = 3
for attempt in range(max_attempts): for attempt in range(max_attempts):
try: try:
@ -120,10 +138,13 @@ def update_procedure(command):
print(f"Attempt {attempt + 1} failed, retrying in 60 seconds...") print(f"Attempt {attempt + 1} failed, retrying in 60 seconds...")
time.sleep(60) # Wait for 60 seconds before retrying time.sleep(60) # Wait for 60 seconds before retrying
else: else:
print("All attempts to update Nextcloud apps have failed.") print("All attempts to update have failed.")
raise # Re-raise the last exception after all attempts fail raise # Re-raise the last exception after all attempts fail
def start_docker(directory): def start_docker(directory):
"""
Starts or restarts Docker services in the specified directory.
"""
if is_any_service_up(): if is_any_service_up():
print(f"Restarting containers in {directory}.") print(f"Restarting containers in {directory}.")
run_command("docker-compose up -d --force-recreate") run_command("docker-compose up -d --force-recreate")
@ -151,7 +172,7 @@ if __name__ == "__main__":
update_discourse(dir_path) update_discourse(dir_path)
else: else:
print("Discourse update skipped. No changes in git repository.") print("Discourse update skipped. No changes in git repository.")
if os.path.basename(dir_path) == "matrix": elif os.path.basename(dir_path) == "matrix":
# No autoupdate for matrix is possible atm, # No autoupdate for matrix is possible atm,
# due to the reason that the role has to be executed every time. # due to the reason that the role has to be executed every time.
# The update has to be executed in the role # The update has to be executed in the role
@ -164,3 +185,5 @@ if __name__ == "__main__":
# Nextcloud needs additional update procedures # Nextcloud needs additional update procedures
if os.path.basename(dir_path) == "nextcloud": if os.path.basename(dir_path) == "nextcloud":
update_nextcloud() update_nextcloud()
elif os.path.basename(dir_path) == "mastodon":
update_mastodon()