def ensure_stopped( config ): """ Stop all syndicated instances for this mountpoint. """ mountpoint_dir = config['mountpoint_dir'] # is the daemon running? procs = watchdog.find_by_attrs( "syndicate-automount-daemon", {"mounts": mountpoint_dir} ) if len(procs) > 0: failed = signal_all( procs, signal.SIGTERM ) # wait for signals to be delivered time.sleep(1.0) procs = watchdog.find_by_attrs( "syndicate-automount-daemon", {"mounts": mountpoint_dir} ) if len(procs) > 0 or len(failed) > 0: failed = signal_all( procs, signal.SIGKILL ) if len(failed) > 0: log.error("Failed to stop automount daemons %s" % (",".join( [str(watchdog.get_proc_pid( p )) for p in procs] ))) return False return True
def ensure_credential_server_running(foreground=False, run_once=False): """ Instantiate our credential server and keep it running. """ # is the watchdog running? pids = syndicate_watchdog.find_by_attrs( "syndicate-credential-server-watchdog", {}) if len(pids) > 0: # it's running return True if foreground: # run in foreground if run_once: return credential_server_spawn(0) else: return syndicate_watchdog.main(credential_server_spawn, respawn_exit_statuses=range(1, 254)) # not running, and not foregrounding. fork a new one try: watchdog_pid = os.fork() except OSError, oe: logger.error("Failed to fork, errno = %s" % oe.errno) return False
def ensure_running( config ): """ Verify that there is an automount daemon servicing a mountpoint. If there isn't, start one. If we're configured to run in the foreground, this method never returns. """ mountpoint_dir = config['mountpoint_dir'] # is the daemon running? procs = watchdog.find_by_attrs( "syndicate-automount-daemon", {"mounts": mountpoint_dir} ) if len(procs) > 0: # it's running print "Syndicate automount daemon already running for %s (PID(s): %s)" % (mountpoint_dir, ",".join( [str(watchdog.get_proc_pid(p)) for p in procs] )) return True if config.get("foreground", None): main( config ) else: logfile_path = None pidfile_path = config.get("pidfile", None) if config.has_key("logdir"): logfile_path = os.path.join( config['logdir'], "syndicated.log" ) title = watchdog.attr_proc_title( "syndicate-automount-daemon", {"mounts" : mountpoint_dir} ) setproctitle.setproctitle( title ) daemon.daemonize( lambda: main(config), logfile_path=logfile_path, pidfile_path=pidfile_path ) return True
def ensure_RG_running( syndicate_url, principal_id, volume_name, gateway_name, key_password, user_pkey_pem, check_only=False, uid_name=None, gid_name=None, hostname=None, debug=False ): """ Ensure an RG is running. Return the PID on success. """ # is there an RG running for this volume? running_RGs = watchdog.find_by_attrs( SYNDICATE_RG_WATCHDOG_NAME, {"volume": volume_name} ) if len(running_RGs) == 1: # we're good! logging.info("RG for %s already running; PID = %s" % (volume_name, running_RGs[0].pid)) return running_RGs[0].pid elif len(running_RGs) > 1: # too many! probably in the middle of starting up logging.error("Multiple RGs running for %s...?" % (volume_name)) return -errno.EAGAIN else: logging.error("No RG running for %s" % (volume_name)) if not check_only: pid = start_RG( syndicate_url, principal_id, volume_name, gateway_name, key_password, user_pkey_pem, uid_name=uid_name, gid_name=gid_name, hostname=hostname, debug=debug ) if pid < 0: log.error("Failed to start RG in %s, rc = %s" % (volume_name, pid)) return pid else: # not running return -errno.ENOENT
def ensure_credential_server_running( foreground=False, run_once=False ): """ Instantiate our credential server and keep it running. """ # is the watchdog running? pids = syndicate_watchdog.find_by_attrs( "syndicate-credential-server-watchdog", {} ) if len(pids) > 0: # it's running return True if foreground: # run in foreground if run_once: return credential_server_spawn( 0 ) else: return syndicate_watchdog.main( credential_server_spawn, respawn_exit_statuses=range(1,254) ) # not running, and not foregrounding. fork a new one try: watchdog_pid = os.fork() except OSError, oe: logger.error("Failed to fork, errno = %s" % oe.errno) return False
def get_pids_of_daemons_for_dir( mountpoint_dir ): """ Get the PIDs of all automount daemons running on a mountpoint. """ procs = watchdog.find_by_attrs( "syndicate-automount-daemon", {"mounts": mountpoint_dir} ) ret = [ watchdog.get_proc_pid(p) for p in procs ] return ret
def stop_RG( volume_name ): # stop an RG running_RGs = watchdog.find_by_attrs( SYNDICATE_RG_WATCHDOG_NAME, {"volume": volume_name} ) if len(running_RGs) > 0: for proc in running_RGs: rc = stop_gateway_watchdog( proc.pid ) if rc != 0: return rc return 0
def list_running_gateways_by_volume(): """ Find the set of running gateways, grouped by volume. return a dictionary with the structure of: { volume_name : { gateway_type: { "pids": [gateway_pid] } } } """ watchdog_names = { "UG": SYNDICATE_UG_WATCHDOG_NAME, "RG": SYNDICATE_RG_WATCHDOG_NAME, "AG": SYNDICATE_AG_WATCHDOG_NAME } watchdog_name_to_type = dict( [(v, k) for (k, v) in watchdog_names.items()] ) ret = {} for gateway_type in ["UG", "RG", "AG"]: watchdog_name = watchdog_names[ gateway_type ] running_watchdog_procs = watchdog.find_by_attrs( watchdog_name, {} ) # from these, find out which volumes for running_watchdog_proc in running_watchdog_procs: cmdline = watchdog.get_proc_cmdline( running_watchdog_proc )[0] watchdog_attrs = watchdog.parse_proc_attrs( cmdline ) # find the volume name volume_name = watchdog_attrs.get("volume", None) if volume_name is None: # nothing to do continue if not ret.has_key( volume_name ): # add volume record ret[volume_name] = {} if not ret[volume_name].has_key( gateway_type ): # add gateway record ret[volume_name][gateway_type] = {} if not ret[volume_name][gateway_type].has_key( "pids" ): # add pids list ret[volume_name][gateway_type][pids] = [] ret[volume_name][gateway_type]["pids"].append( running_watchdog_proc.pid ) return ret
def stop_UG( volume_name, mountpoint=None ): # stop a UG, given its mountpoint and volume name # this method is idempotent query_attrs = { "volume": volume_name } if mountpoint is not None: query_attrs["mountpoint"] = mountpoint mounted_UGs = watchdog.find_by_attrs( SYNDICATE_UG_WATCHDOG_NAME, query_attrs ) if len(mounted_UGs) > 0: for proc in mounted_UGs: rc = stop_gateway_watchdog( proc.pid ) if rc != 0: return rc return 0
def ensure_UG_running( syndicate_url, principal_id, volume_name, gateway_name, key_password, user_pkey_pem, mountpoint=None, check_only=False, uid_name=None, gid_name=None, hostname=None, debug=False ): """ Ensure that a User Gateway is running on a particular mountpoint. Return 0 on success Return negative on error. """ if mountpoint is None: log.error("Missing mountpout. Pass mountpoint=...") return -errno.EINVAL # make sure a mountpoint exists rc = ensure_UG_mountpoint_exists( mountpoint, uid_name=uid_name, gid_name=gid_name ) if rc != 0: log.error("Failed to ensure mountpoint %s exists" % mountpoint) return rc # is there a UG running at this mountpoint? mounted_UGs = watchdog.find_by_attrs( SYNDICATE_UG_WATCHDOG_NAME, {"volume": volume_name, "mountpoint": mountpoint} ) if len(mounted_UGs) == 1: # we're good! logging.info("UG for %s at %s already running; PID = %s" % (volume_name, mountpoint, mounted_UGs[0].pid)) return mounted_UGs[0].pid elif len(mounted_UGs) > 1: # too many! probably in the middle of starting up logging.error("Multiple UGs running for %s on %s...?" % (volume_name, mountpoint)) return -errno.EAGAN else: logging.error("No UG running for %s on %s" % (volume_name, mountpoint)) if not check_only: pid = start_UG( syndicate_url, principal_id, volume_name, gateway_name, key_password, user_pkey_pem, mountpoint, uid_name=uid_name, gid_name=gid_name, hostname=hostname, debug=debug ) if pid < 0: log.error("Failed to start UG in %s at %s, rc = %s" % (volume_name, mountpoint, pid)) return pid else: return 0