예제 #1
0
def new_database_with_daemon(aiida_profile):
    """When you run something in aiida v1, it will be done in its own runner,
    which is a mini daemon in and of its own.
    However, in 0.12 the global daemon is the only one that can
    submit, update and retrieve job calculations.
    Therefore, we must configure and start it before running JobProcesses
    """
    if aiida_version() < cmp_version('1.0.0a1'):
        from aiida.backends.utils import set_daemon_user
        from aiida.cmdline.commands.daemon import Daemon
        from aiida.common import setup

        set_daemon_user(aiida_profile.email)
        daemon = Daemon()
        daemon.logfile = os.path.join(aiida_profile.config_dir,
                                      setup.LOG_SUBDIR, setup.CELERY_LOG_FILE)
        daemon.pidfile = os.path.join(aiida_profile.config_dir,
                                      setup.LOG_SUBDIR, setup.CELERY_PID_FILE)
        daemon.celerybeat_schedule = os.path.join(aiida_profile.config_dir,
                                                  setup.DAEMON_SUBDIR,
                                                  'celerybeat-schedule')

        if daemon.get_daemon_pid() is None:
            daemon.daemon_start()
        else:
            daemon.daemon_restart()
        yield aiida_profile
        daemon.kill_daemon()
        aiida_profile.reset_db()
    else:
        yield aiida_profile
        aiida_profile.reset_db()
예제 #2
0
    def configure_user(self, *args):
        """
        Configure the user that can run the daemon.
        """
        if not is_dbenv_loaded():
            from aiida.backends.utils import load_dbenv
            load_dbenv(process='daemon')

        if args:
            print >> sys.stderr, (
                "No arguments allowed for the '{}' command.".format(
                    self.get_full_command_name()))
            sys.exit(1)

        from aiida.utils import timezone
        from aiida.backends.utils import get_daemon_user, set_daemon_user
        from aiida.common.utils import (get_configured_user_email,
                                        query_yes_no, query_string)
        from aiida.daemon.timestamps import get_most_recent_daemon_timestamp
        from aiida.common.utils import str_timedelta
        from aiida.orm.user import User

        old_daemon_user = get_daemon_user()
        this_user = get_configured_user_email()

        print("> Current default user: {}".format(this_user))
        print("> Currently configured user who can run the daemon: {}".format(
            old_daemon_user))
        if old_daemon_user == this_user:
            print(
                "  (therefore, at the moment you are the user who can run "
                "the daemon)")
            pid = self.get_daemon_pid()
            if pid is not None:
                print("The daemon is running! I will not proceed.")
                sys.exit(1)
        else:
            print("  (therefore, you cannot run the daemon, at the moment)")

        most_recent_timestamp = get_most_recent_daemon_timestamp()

        print "*" * 76
        print "* {:72s} *".format("WARNING! Change this setting only if you "
                                  "are sure of what you are doing.")
        print "* {:72s} *".format("Moreover, make sure that the "
                                  "daemon is stopped.")

        if most_recent_timestamp is not None:
            timestamp_delta = timezone.now() - most_recent_timestamp
            last_check_string = (
                "[The most recent timestamp from the daemon was {}]".format(
                    str_timedelta(timestamp_delta)))
            print "* {:72s} *".format(last_check_string)

        print "*" * 76

        answer = query_yes_no(
            "Are you really sure that you want to change "
            "the daemon user?",
            default="no")
        if not answer:
            sys.exit(0)

        print ""
        print "Enter below the email of the new user who can run the daemon."
        new_daemon_user_email = query_string("New daemon user: "******"ERROR! The user you specified ({}) does "
                "not exist in the database!!".format(new_daemon_user_email))
            print("The available users are {}".format(
                [_.email for _ in User.search_for_users()]))
            sys.exit(1)

        set_daemon_user(new_daemon_user_email)

        print "The new user that can run the daemon is now {} {}.".format(
            found_users[0].first_name, found_users[0].last_name)