def is_daemon_user(): """ Return True if the user is the current daemon user, False otherwise. """ from aiida.backends.utils import get_daemon_user from aiida.common.utils import get_configured_user_email daemon_user = get_daemon_user() this_user = get_configured_user_email() return daemon_user == this_user
def daemon_start(self, *args): """ Start 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.backends.settings import BACKEND from aiida.backends.profile import BACKEND_DJANGO, BACKEND_SQLA from aiida.backends.utils import get_daemon_user from aiida.common.utils import get_configured_user_email daemon_user = get_daemon_user() this_user = get_configured_user_email() if daemon_user != this_user: print "You are not the daemon user! I will not start the daemon." print "(The daemon user is '{}', you are '{}')".format( daemon_user, this_user) print "" print "** FOR ADVANCED USERS ONLY: **" print "To change the current default user, use 'verdi install --only-config'" print "To change the daemon user, use 'verdi daemon configureuser'" sys.exit(1) pid = self.get_daemon_pid() if pid is not None: print "Daemon already running, try asking for its status" return print "Clearing all locks ..." from aiida.orm.lock import LockManager LockManager().clear_all() print "Starting AiiDA Daemon ..." process = subprocess.Popen( "supervisord -c {}".format(self.conffile_full_path), shell=True, stdout=subprocess.PIPE) process.wait() if (process.returncode == 0): print "Daemon started"
def daemon_restart(self, *args): """ Restart the daemon. Before restarting, wait for the daemon to really shut down. """ 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.backends.utils import get_daemon_user from aiida.common.utils import get_configured_user_email daemon_user = get_daemon_user() this_user = get_configured_user_email() if daemon_user != this_user: print "You are not the daemon user! I will not restart the daemon." print "(The daemon user is '{}', you are '{}')".format( daemon_user, this_user) sys.exit(1) pid = self.get_daemon_pid() dead = True if pid is not None: dead = self.daemon_stop(wait_for_death=True) if not dead: print "Check the status and, when the daemon will be down, " print "you can restart it using:" print " verdi daemon start" else: self.daemon_start()
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)
def daemon_start(self, *args): """ Start the daemon """ if not is_dbenv_loaded(): from aiida.backends.utils import load_dbenv load_dbenv(process='daemon') from aiida.daemon.timestamps import get_last_daemon_timestamp, set_daemon_timestamp if args: print >> sys.stderr, ( "No arguments allowed for the '{}' command.".format( self.get_full_command_name())) sys.exit(1) from aiida.backends.utils import get_daemon_user from aiida.common.utils import get_configured_user_email daemon_user = get_daemon_user() this_user = get_configured_user_email() if daemon_user != this_user: print "You are not the daemon user! I will not start the daemon." print "(The daemon user is '{}', you are '{}')".format( daemon_user, this_user) print "" print "** FOR ADVANCED USERS ONLY: **" print "To change the current default user, use 'verdi install --only-config'" print "To change the daemon user, use 'verdi daemon configureuser'" sys.exit(1) pid = self.get_daemon_pid() if pid is not None: print "Daemon already running, try asking for its status" return print "Clearing all locks ..." from aiida.orm.lock import LockManager LockManager().clear_all() # rotate an existing log file out of the way if os.path.isfile(self.logfile): with open(self.logfile, 'rb') as curr_log_fh: with gzip.open(self.logfile + '.-1.gz', 'wb') as old_log_fh: shutil.copyfileobj(curr_log_fh, old_log_fh) os.remove(self.logfile) print "Starting AiiDA Daemon (log file: {})...".format(self.logfile) currenv = _get_env_with_venv_bin() process = subprocess.Popen([ "celery", "worker", "--app", "tasks", "--loglevel", "INFO", "--beat", "--schedule", self.celerybeat_schedule, "--logfile", self.logfile, "--pidfile", self._get_pid_full_path(), ], cwd=self.workdir, close_fds=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=currenv) # The following lines are needed for the workflow_stepper # (re-initialize the timestamps used to lock the task, in case # it crashed for some reason). # TODO: remove them when the old workflow system will be # taken away. try: if (get_last_daemon_timestamp('workflow', when='stop') - get_last_daemon_timestamp('workflow', when='start')) < timedelta(0): logger.info("Workflow stop timestamp was {}; re-initializing " "it to current time".format( get_last_daemon_timestamp('workflow', when='stop'))) print "Re-initializing workflow stepper stop timestamp" set_daemon_timestamp(task_name='workflow', when='stop') except TypeError: # when some timestamps are None (i.e. not present), we make # sure that at least the stop timestamp is defined print "Re-initializing workflow stepper stop timestamp" set_daemon_timestamp(task_name='workflow', when='stop') print "Daemon started"
def daemon_start(self, *args): """ Start the daemon """ if not is_dbenv_loaded(): from aiida.backends.utils import load_dbenv load_dbenv(process='daemon') from aiida.daemon.timestamps import get_last_daemon_timestamp, set_daemon_timestamp if args: print >> sys.stderr, ( "No arguments allowed for the '{}' command.".format( self.get_full_command_name())) sys.exit(1) from aiida.backends.utils import get_daemon_user from aiida.common.utils import get_configured_user_email daemon_user = get_daemon_user() this_user = get_configured_user_email() if daemon_user != this_user: print "You are not the daemon user! I will not start the daemon." print "(The daemon user is '{}', you are '{}')".format( daemon_user, this_user) print "" print "** FOR ADVANCED USERS ONLY: **" print "To change the current default user, use 'verdi install --only-config'" print "To change the daemon user, use 'verdi daemon configureuser'" sys.exit(1) pid = self.get_daemon_pid() if pid is not None: print "Daemon already running, try asking for its status" return print "Clearing all locks ..." from aiida.orm.lock import LockManager LockManager().clear_all() print "Starting AiiDA Daemon ..." currenv = _get_env_with_venv_bin() process = subprocess.Popen("supervisord -c {}".format( self.conffile_full_path), shell=True, stdout=subprocess.PIPE, env=currenv) process.wait() # The following lines are needed for the workflow_stepper # (re-initialize the timestamps used to lock the task, in case # it crashed for some reason). # TODO: remove them when the old workflow system will be # taken away. try: if (get_last_daemon_timestamp('workflow', when='stop') - get_last_daemon_timestamp('workflow', when='start')) < timedelta(0): logger.info("Workflow stop timestamp was {}; re-initializing " "it to current time".format( get_last_daemon_timestamp('workflow', when='stop'))) print "Re-initializing workflow stepper stop timestamp" set_daemon_timestamp(task_name='workflow', when='stop') except TypeError: # when some timestamps are None (i.e. not present), we make # sure that at least the stop timestamp is defined print "Re-initializing workflow stepper stop timestamp" set_daemon_timestamp(task_name='workflow', when='stop') if (process.returncode == 0): print "Daemon started"