Catch error during binds migration and delete the job if it is stuck during restart

remotes/1697617238042506578/master
Inex Code 2022-09-22 19:38:59 +03:00
parent e387e30983
commit 7a1e8af8fe
3 changed files with 142 additions and 56 deletions

View File

@ -27,4 +27,4 @@ async def get_token_header(
def get_api_version() -> str: def get_api_version() -> str:
"""Get API version""" """Get API version"""
return "2.0.8" return "2.0.9"

View File

@ -65,14 +65,27 @@ def move_folder(
else: else:
return return
data_path.mkdir(mode=0o750, parents=True, exist_ok=True) try:
data_path.mkdir(mode=0o750, parents=True, exist_ok=True)
except Exception as e:
print(f"Error creating data path: {e}")
return
shutil.chown(str(bind_path), user=user, group=group) try:
shutil.chown(str(data_path), user=user, group=group) shutil.chown(str(bind_path), user=user, group=group)
shutil.chown(str(data_path), user=user, group=group)
except LookupError:
pass
subprocess.run(["mount", "--bind", str(bind_path), str(data_path)], check=True) try:
subprocess.run(["mount", "--bind", str(bind_path), str(data_path)], check=True)
except subprocess.CalledProcessError as error:
print(error)
subprocess.run(["chown", "-R", f"{user}:{group}", str(data_path)], check=True) try:
subprocess.run(["chown", "-R", f"{user}:{group}", str(data_path)], check=True)
except subprocess.CalledProcessError as error:
print(error)
@huey.task() @huey.task()
@ -157,12 +170,17 @@ def migrate_to_binds(config: BindMigrationConfig, job: Job):
Nextcloud().stop() Nextcloud().stop()
move_folder( # If /volumes/sda1/nextcloud or /volumes/sdb/nextcloud exists, skip it.
data_path=pathlib.Path("/var/lib/nextcloud"), if not pathlib.Path("/volumes/sda1/nextcloud").exists():
bind_path=pathlib.Path(f"/volumes/{config.nextcloud_block_device}/nextcloud"), if not pathlib.Path("/volumes/sdb/nextcloud").exists():
user="nextcloud", move_folder(
group="nextcloud", data_path=pathlib.Path("/var/lib/nextcloud"),
) bind_path=pathlib.Path(
f"/volumes/{config.nextcloud_block_device}/nextcloud"
),
user="nextcloud",
group="nextcloud",
)
# Start Nextcloud # Start Nextcloud
Nextcloud().start() Nextcloud().start()
@ -178,21 +196,27 @@ def migrate_to_binds(config: BindMigrationConfig, job: Job):
Bitwarden().stop() Bitwarden().stop()
move_folder( if not pathlib.Path("/volumes/sda1/bitwarden").exists():
data_path=pathlib.Path("/var/lib/bitwarden"), if not pathlib.Path("/volumes/sdb/bitwarden").exists():
bind_path=pathlib.Path(f"/volumes/{config.bitwarden_block_device}/bitwarden"), move_folder(
user="vaultwarden", data_path=pathlib.Path("/var/lib/bitwarden"),
group="vaultwarden", bind_path=pathlib.Path(
) f"/volumes/{config.bitwarden_block_device}/bitwarden"
),
user="vaultwarden",
group="vaultwarden",
)
move_folder( if not pathlib.Path("/volumes/sda1/bitwarden_rs").exists():
data_path=pathlib.Path("/var/lib/bitwarden_rs"), if not pathlib.Path("/volumes/sdb/bitwarden_rs").exists():
bind_path=pathlib.Path( move_folder(
f"/volumes/{config.bitwarden_block_device}/bitwarden_rs" data_path=pathlib.Path("/var/lib/bitwarden_rs"),
), bind_path=pathlib.Path(
user="vaultwarden", f"/volumes/{config.bitwarden_block_device}/bitwarden_rs"
group="vaultwarden", ),
) user="vaultwarden",
group="vaultwarden",
)
# Start Bitwarden # Start Bitwarden
Bitwarden().start() Bitwarden().start()
@ -208,12 +232,14 @@ def migrate_to_binds(config: BindMigrationConfig, job: Job):
Gitea().stop() Gitea().stop()
move_folder( if not pathlib.Path("/volumes/sda1/gitea").exists():
data_path=pathlib.Path("/var/lib/gitea"), if not pathlib.Path("/volumes/sdb/gitea").exists():
bind_path=pathlib.Path(f"/volumes/{config.gitea_block_device}/gitea"), move_folder(
user="gitea", data_path=pathlib.Path("/var/lib/gitea"),
group="gitea", bind_path=pathlib.Path(f"/volumes/{config.gitea_block_device}/gitea"),
) user="gitea",
group="gitea",
)
Gitea().start() Gitea().start()
@ -228,19 +254,23 @@ def migrate_to_binds(config: BindMigrationConfig, job: Job):
MailServer().stop() MailServer().stop()
move_folder( if not pathlib.Path("/volumes/sda1/vmail").exists():
data_path=pathlib.Path("/var/vmail"), if not pathlib.Path("/volumes/sdb/vmail").exists():
bind_path=pathlib.Path(f"/volumes/{config.email_block_device}/vmail"), move_folder(
user="virtualMail", data_path=pathlib.Path("/var/vmail"),
group="virtualMail", bind_path=pathlib.Path(f"/volumes/{config.email_block_device}/vmail"),
) user="virtualMail",
group="virtualMail",
)
move_folder( if not pathlib.Path("/volumes/sda1/sieve").exists():
data_path=pathlib.Path("/var/sieve"), if not pathlib.Path("/volumes/sdb/sieve").exists():
bind_path=pathlib.Path(f"/volumes/{config.email_block_device}/sieve"), move_folder(
user="virtualMail", data_path=pathlib.Path("/var/sieve"),
group="virtualMail", bind_path=pathlib.Path(f"/volumes/{config.email_block_device}/sieve"),
) user="virtualMail",
group="virtualMail",
)
MailServer().start() MailServer().start()
@ -255,19 +285,27 @@ def migrate_to_binds(config: BindMigrationConfig, job: Job):
Pleroma().stop() Pleroma().stop()
move_folder( if not pathlib.Path("/volumes/sda1/pleroma").exists():
data_path=pathlib.Path("/var/lib/pleroma"), if not pathlib.Path("/volumes/sdb/pleroma").exists():
bind_path=pathlib.Path(f"/volumes/{config.pleroma_block_device}/pleroma"), move_folder(
user="pleroma", data_path=pathlib.Path("/var/lib/pleroma"),
group="pleroma", bind_path=pathlib.Path(
) f"/volumes/{config.pleroma_block_device}/pleroma"
),
user="pleroma",
group="pleroma",
)
move_folder( if not pathlib.Path("/volumes/sda1/postgresql").exists():
data_path=pathlib.Path("/var/lib/postgresql"), if not pathlib.Path("/volumes/sdb/postgresql").exists():
bind_path=pathlib.Path(f"/volumes/{config.pleroma_block_device}/postgresql"), move_folder(
user="postgres", data_path=pathlib.Path("/var/lib/postgresql"),
group="postgres", bind_path=pathlib.Path(
) f"/volumes/{config.pleroma_block_device}/postgresql"
),
user="postgres",
group="postgres",
)
Pleroma().start() Pleroma().start()

View File

@ -0,0 +1,48 @@
from selfprivacy_api.jobs import JobStatus, Jobs
from selfprivacy_api.migrations.migration import Migration
from selfprivacy_api.utils import WriteUserData
class CheckForFailedBindsMigration(Migration):
"""Mount volume."""
def get_migration_name(self):
return "check_for_failed_binds_migration"
def get_migration_description(self):
return "If binds migration failed, try again."
def is_migration_needed(self):
try:
jobs = Jobs.get_instance().get_jobs()
# If there is a job with type_id "migrations.migrate_to_binds" and status is not "FINISHED",
# then migration is needed and job is deleted
for job in jobs:
if (
job.type_id == "migrations.migrate_to_binds"
and job.status != JobStatus.FINISHED
):
return True
return False
except Exception as e:
print(e)
return False
def migrate(self):
# Get info about existing volumes
# Write info about volumes to userdata.json
try:
jobs = Jobs.get_instance().get_jobs()
for job in jobs:
if (
job.type_id == "migrations.migrate_to_binds"
and job.status != JobStatus.FINISHED
):
Jobs.get_instance().remove(job)
with WriteUserData() as userdata:
userdata["useBinds"] = False
print("Done")
except Exception as e:
print(e)
print("Error mounting volume")