def heartbeat_content(self): ''' What should we write to our heartbeat counter? Enough data that an admin could figure out who's hogging the lock. If enough time has elapsed since the last call to this function, updates the content string. Returns a tuple containing the current and previous content strings. ''' # get the seconds since the epoch in UTC now = calendar.timegm(time.gmtime()) if self.__heartbeat_time and now - self.__heartbeat_time < HEART_RATE: return self.__heartbeat_content, self.__heartbeat_content self.__heartbeat_time = now # tabs are used as separators for easy parsing in check_holder_alive() val = "{host}\t{process}\t{time}\t{argv}" \ .format( host = p4gf_util.get_hostname() , process = os.getpid() , time = int(self.__heartbeat_time) , argv = ' '.join(sys.argv) ) last = self.__heartbeat_content self.__heartbeat_content = val return self.__heartbeat_content, last
def set_server_id_counter(server_id): '''Set the server_id_counter value to the hostname to identify GF hosts. ''' if server_id: p4.run('counter', '-u', p4gf_const.P4GF_COUNTER_SERVER_ID + server_id, p4gf_util.get_hostname())
def _get_counter_name(): """Generate the name of the counter for keeping track of the last change that this server has retrieved for the user keys. """ global CounterName if not CounterName: host = p4gf_util.get_hostname() CounterName = COUNTER_UPDATE_AUTH_KEYS.format(host) return CounterName
def ensure_server_id(): """Write this machine's permanent server-id assignment to P4GF_HOME/server-id . NOP if we already have a server-id stored in that file. We'll just keep using it. """ id_from_file = p4gf_util.read_server_id_from_file() server_id = ID_FROM_ARGV if ID_FROM_ARGV else p4gf_util.get_hostname() # when re-running super_init, do not replace the server-id file when # the server_id file exists an no --id parameter is present # assume in the case that the existing file is correct if id_from_file and not ID_FROM_ARGV: server_id = id_from_file do_reset = True if server_id_counter_exists(server_id): do_reset = False if id_from_file == server_id: Verbosity.report(Verbosity.INFO, _("Git Fusion server ID already set to '{0}'.").format(id_from_file)) else: if not OVERRULE_SERVERID_CONFLICT: Verbosity.report(Verbosity.INFO, _("Git Fusion server ID is already assigned: " \ "'{0}' set on host on '{1}'.\n" \ "Retry with a different --id server_id.") \ .format(server_id, server_id_counter_exists(server_id))) Verbosity.report(Verbosity.INFO, _("If you are certain no other Git Fusion instance is using this server ID," "\nyou may overrule this conflict and set the local server-id file to" "\n'{0}' with:" "\n p4gf_super_init.py --force").format(server_id)) if id_from_file: Verbosity.report(Verbosity.INFO, _("Git Fusion server ID already set to '{0}'.").format(id_from_file)) else: Verbosity.report(Verbosity.INFO, _("This Git Fusion's server ID is unset. Stopping.")) show_all_server_ids() sys.exit(0) else: do_reset = True if do_reset: if id_from_file and id_from_file != server_id: # delete the previous counter if server_id_counter_exists(id_from_file): unset_server_id_counter(id_from_file) set_server_id_counter(server_id) p4gf_util.write_server_id_to_file(server_id) Verbosity.report(Verbosity.INFO, _("Git Fusion server ID set to '{0}' in file '{1}'") .format(server_id, p4gf_util.server_id_file_path())) show_all_server_ids()
def main(): """Process command line arguments and call functions to do the real work of cleaning up the Git mirror and Perforce workspaces. """ # Set up argument parsing. parser = p4gf_util.create_arg_parser( "Convert 2012.2 Git Fusion configured Perforce Server for use with" " 2013.1 Git Fusion.\nThis is not reversable.") parser.add_argument("-y", "--convert", action="store_true", help="perform the conversion") parser.add_argument("-d", "--delete", action="store_true", help="skip obliterate and show delete command") parser.add_argument( '--id', nargs=1, help="Set this Git Fusion server's unique id. Default is hostname.") args = parser.parse_args() # Do not run as root, this is very git account specific user = getpass.getuser() if user == "root": print("This script should be run using the Git dedicated account") return 2 if args.convert: try: global LOG_FILE # pylint:disable=W1501 # "x" is not a valid mode for open. # Yes it is. Pylint 1.0.0 is incorrect here. LOG_FILE = open("p4gf_convert_v12_2.log", "x") # pylint:enable=W1501 print("Logging to p4gf_convert_v12_2.log") except IOError: print("Please remove or rename p4gf_convert_v12_2.log") sys.exit(1) client_name = p4gf_const.P4GF_OBJECT_CLIENT_PREFIX + p4gf_util.get_hostname( ) try: p4 = p4gf_create_p4.create_p4(client=client_name) except RuntimeError as e: sys.stderr.write("{}\n".format(e)) sys.exit(1) if not p4: return 2 # Sanity check the connection (e.g. user logged in?) before proceeding. try: p4.fetch_client() view = [ '//{depot}/... //{client}/...'.format(depot=p4gf_const.P4GF_DEPOT, client=client_name) ] spec = { 'Host': '', 'Root': os.path.join(os.environ.get("HOME"), p4gf_const.P4GF_DIR), 'View': view } p4gf_util.ensure_spec_values(p4, 'client', client_name, spec) except P4.P4Exception as e: sys.stderr.write("P4 exception occurred: {}".format(e)) sys.exit(1) try: convert(args, p4) except P4.P4Exception as e: sys.stderr.write("{}\n".format(e)) sys.exit(1) if not args.convert: print("This was report mode. Use -y to make changes.") else: print("Commands run were logged to p4gf_convert_v12_2.log.") if args.delete: print("You must now run: p4 delete //.git-fusion/objects/...") print(" p4 submit") print(" Use a client which has this location in its view") LOG_FILE.write( "Need to run: p4 delete //.git-fusion/objects/...\n") LOG_FILE.write(" p4 submit\n") LOG_FILE.close()
def ensure_server_id(): """Write this machine's permanent server-id assignment to P4GF_HOME/server-id. NOP if we already have a server-id stored in that file. We'll just keep using it. """ # pylint: disable=too-many-branches if ID_FROM_ARGV: if re.search(r'[/@#*%]|\.\.\.', ID_FROM_ARGV): msg = _('Special characters (*, #, %) not allowed in Git Fusion server ID {server_id}.') Verbosity.report(Verbosity.ERROR, msg.format(server_id=ID_FROM_ARGV)) sys.exit(1) id_from_file = p4gf_util.read_server_id_from_file() if id_from_file and ID_FROM_ARGV and id_from_file != ID_FROM_ARGV: if not OVERRULE_SERVERID_CONFLICT: # msg = _("Git Fusion server ID already set to '{0}', cannot initialize again.") Verbosity.report( Verbosity.ERROR, _("Git Fusion server ID already set to '{server_id}'. " + "To reinitialize Git Fusion with '{id_from_argv}' use the --force option.") .format(server_id=id_from_file, id_from_argv=ID_FROM_ARGV)) sys.exit(1) server_id = ID_FROM_ARGV if ID_FROM_ARGV else p4gf_util.get_hostname() # when re-running super_init, do not replace the server-id file when # the server_id file exists an no --id parameter is present # assume in the case that the existing file is correct if id_from_file and not ID_FROM_ARGV: server_id = id_from_file check_for_localhost(id_from_file, server_id) do_reset = True if server_id_p4key_exists(server_id): do_reset = False if id_from_file == server_id: Verbosity.report( Verbosity.INFO, _("Git Fusion server ID already set to '{server_id}'.") .format(server_id=id_from_file)) else: if not OVERRULE_SERVERID_CONFLICT: Verbosity.report( Verbosity.INFO, _("Git Fusion server ID is already assigned: " "'{server_id}' set on host on '{exists}'.\n" "Retry with a different --id server_id.") .format(server_id=server_id, exists=server_id_p4key_exists(server_id))) Verbosity.report( Verbosity.INFO, _("If you are certain no other Git Fusion instance is using this server ID," "\nyou may overrule this conflict and set the local server-id file to" "\n'{server_id}' with:" "\n configure-git-fusion.sh --id") .format(server_id=server_id)) if id_from_file: Verbosity.report( Verbosity.INFO, _("Git Fusion server ID already set to '{server_id}'.") .format(server_id=id_from_file)) else: Verbosity.report( Verbosity.INFO, _("This Git Fusion's server ID is unset. Stopping.")) show_all_server_ids() sys.exit(1) else: do_reset = True if do_reset: if id_from_file and id_from_file != server_id: # delete the previous p4key if server_id_p4key_exists(id_from_file): unset_server_id_p4key(id_from_file) set_server_id_p4key(server_id) p4gf_util.write_server_id_to_file(server_id) Verbosity.report( Verbosity.INFO, _("Git Fusion server ID set to '{server_id}' in file '{path}'") .format(server_id=server_id, path=p4gf_util.server_id_file_path())) show_all_server_ids()
def set_server_id_p4key(server_id): """Set the server-id p4key value to the hostname to identify GF hosts.""" if server_id: P4Key.set(p4, p4gf_const.P4GF_P4KEY_SERVER_ID + server_id, p4gf_util.get_hostname())
def ensure_server_id(): """Write this machine's permanent server-id assignment to P4GF_HOME/server-id . NOP if we already have a server-id stored in that file. We'll just keep using it. """ id_from_file = p4gf_util.read_server_id_from_file() server_id = ID_FROM_ARGV if ID_FROM_ARGV else p4gf_util.get_hostname() # when re-running super_init, do not replace the server-id file when # the server_id file exists an no --id parameter is present # assume in the case that the existing file is correct if id_from_file and not ID_FROM_ARGV: server_id = id_from_file do_reset = True if server_id_counter_exists(server_id): do_reset = False if id_from_file == server_id: Verbosity.report( Verbosity.INFO, _("Git Fusion server ID already set to '{0}'.").format( id_from_file)) else: if not OVERRULE_SERVERID_CONFLICT: Verbosity.report(Verbosity.INFO, _("Git Fusion server ID is already assigned: " \ "'{0}' set on host on '{1}'.\n" \ "Retry with a different --id server_id.") \ .format(server_id, server_id_counter_exists(server_id))) Verbosity.report( Verbosity.INFO, _("If you are certain no other Git Fusion instance is using this server ID," "\nyou may overrule this conflict and set the local server-id file to" "\n'{0}' with:" "\n p4gf_super_init.py --force").format(server_id)) if id_from_file: Verbosity.report( Verbosity.INFO, _("Git Fusion server ID already set to '{0}'.").format( id_from_file)) else: Verbosity.report( Verbosity.INFO, _("This Git Fusion's server ID is unset. Stopping.")) show_all_server_ids() sys.exit(0) else: do_reset = True if do_reset: if id_from_file and id_from_file != server_id: # delete the previous counter if server_id_counter_exists(id_from_file): unset_server_id_counter(id_from_file) set_server_id_counter(server_id) p4gf_util.write_server_id_to_file(server_id) Verbosity.report( Verbosity.INFO, _("Git Fusion server ID set to '{0}' in file '{1}'").format( server_id, p4gf_util.server_id_file_path())) show_all_server_ids()
def main(): """Process command line arguments and call functions to do the real work of cleaning up the Git mirror and Perforce workspaces. """ # Set up argument parsing. parser = p4gf_util.create_arg_parser( "Convert 2012.2 Git Fusion configured Perforce Server for use with" " 2013.1 Git Fusion.\nThis is not reversable.") parser.add_argument("-y", "--convert", action="store_true", help="perform the conversion") parser.add_argument("-d", "--delete", action="store_true", help="skip obliterate and show delete command") parser.add_argument('--id', nargs=1, help="Set this Git Fusion server's unique id. Default is hostname.") args = parser.parse_args() # Do not run as root, this is very git account specific user = getpass.getuser() if user == "root": print("This script should be run using the Git dedicated account") return 2 if args.convert: try: global LOG_FILE # pylint:disable=W1501 # "x" is not a valid mode for open. # Yes it is. Pylint 1.0.0 is incorrect here. LOG_FILE = open("p4gf_convert_v12_2.log", "x") # pylint:enable=W1501 print("Logging to p4gf_convert_v12_2.log") except IOError: print("Please remove or rename p4gf_convert_v12_2.log") sys.exit(1) client_name = p4gf_const.P4GF_OBJECT_CLIENT_PREFIX + p4gf_util.get_hostname() try: p4 = p4gf_create_p4.create_p4(client=client_name) except RuntimeError as e: sys.stderr.write("{}\n".format(e)) sys.exit(1) if not p4: return 2 # Sanity check the connection (e.g. user logged in?) before proceeding. try: p4.fetch_client() view = ['//{depot}/... //{client}/...'.format( depot=p4gf_const.P4GF_DEPOT, client=client_name)] spec = {'Host': '', 'Root': os.path.join(os.environ.get("HOME"), p4gf_const.P4GF_DIR), 'View': view} p4gf_util.ensure_spec_values(p4, 'client', client_name, spec) except P4.P4Exception as e: sys.stderr.write("P4 exception occurred: {}".format(e)) sys.exit(1) try: convert(args, p4) except P4.P4Exception as e: sys.stderr.write("{}\n".format(e)) sys.exit(1) if not args.convert: print("This was report mode. Use -y to make changes.") else: print("Commands run were logged to p4gf_convert_v12_2.log.") if args.delete: print("You must now run: p4 delete //.git-fusion/objects/...") print(" p4 submit") print(" Use a client which has this location in its view") LOG_FILE.write("Need to run: p4 delete //.git-fusion/objects/...\n") LOG_FILE.write(" p4 submit\n") LOG_FILE.close()