<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
import pwd
import shutil
import os
import logging

from defence360agent.contracts.config import Core

logger = logging.getLogger(__name__)


def migrate(migrator, database, fake=False, **kwargs):
    try:
        for user in pwd.getpwall():
            try:
                src = os.path.join(user.pw_dir, Core.USER_CONFIG_FILE_NAME)
                if os.path.isfile(src) and not os.path.islink(src):
                    dst_dir = os.path.join(Core.USER_CONFDIR, user.pw_name)
                    os.mkdir(dst_dir)
                    os.chown(dst_dir, 0, user.pw_gid)
                    os.chmod(dst_dir, 0o750)
                    dst_file = os.path.join(
                        dst_dir, Core.USER_CONFIG_FILE_NAME
                    )
                    shutil.move(src, dst_file)
                    os.chown(dst_file, 0, user.pw_gid)
                    os.chmod(dst_file, 0o640)
            except OSError as e:
                logger.warning("Something went wrong: %s", str(e))
    except Exception:
        logger.exception("Failed to migrate config for %s", user)


def rollback(migrator, database, fake=False, **kwargs):
    pass
