def command(): # bootsrap <name> -i <ip>[:<port>] [-l <logpath>] [-k <keypath>] [-f] args = parser.parse_args() default_logpath = os.path.join(defaults.dir, 'logs', args.name) logpath = args.logpath or default_logpath port = args.port or get_port(args.name) utils.create_logdir(logpath, default_logpath, args.force) keys = [] for keypath in args.keypaths.split(','): if not os.path.isfile(keypath): sys.stderr.write("Error: bootsraping keypath %s does not exist.\n" % keypath) sys.exit(2) keys.append(Key.load(keypath)) log = Log(logpath) # TODO, name=args.name) ips = [] for ip in args.ips.split(','): if ':' not in ip: ip += ':%i' % port ips.append(ip) log.bootstrap(keys, ips) config = get_or_create_config(defaults) if args.name not in config: config[args.name] = {} config[args.name].update({ 'logpath': logpath, 'port': str(port), }) config.save() sys.stdout.write('Created log file %s\n' % logpath) sys.stdout.write('Network bootstraping will happen at:\n %s\n' % '\n '.join(ips)) sys.exit(0)
def get_filesystems(defaults): result = {} config = get_or_create_config(defaults) for section, content in config.items(): if section != 'DEFAULT': result[section] = utils.AttrDict( name=section, logpath=os.path.normpath(content['logpath']), port=int(content['port']), ) for log in os.listdir(defaults.logdir): if log not in result: name = log result[name] = utils.AttrDict( name=section, logpath=os.path.normpath(os.path.join(defaults.logdir, log)), port=get_port(name), ) return result
def get_cmd_port(args, mount_info, defaults): port = mount_info.port if mount_info else None name = args.name if not port and not name: name = get_default_name(defaults) if not port: config = get_or_create_config(defaults) if name not in config: logpath = os.path.join(defaults.logdir, name) if os.path.exists(logpath): info, __ = utils.get_mountpoint(logpath) if info: port = int(info.split(':')[1]) else: port = get_port(name) + 2 else: sys.stderr.write("Error: unknwon logname %s\n" % name) sys.exit(2) else: port = int(config[name].get('port', get_port(name))) + 2 return port
def get_cmd_port(args, mount_info, defaults): port = mount_info.port if mount_info else None name = args.name if not port and not name: name = get_default_name(defaults) if not port: config = get_or_create_config(defaults) if name not in config: logpath = os.path.join(defaults.logdir, name) if os.path.exists(logpath): info, __ = utils.get_mountpoint(logpath) if info: port = int(info.split(':')[1]) else: port = get_port(name)+2 else: sys.stderr.write("Error: unknwon logname %s\n" % name) sys.exit(2) else: port = int(config[name].get('port', get_port(name)))+2 return port
def command(mount=False, arg_parser=None): if arg_parser is None: # Invoking from code, not basefs bin set_parser(parser) arg_parser = parser args = arg_parser.parse_args() context = get_context(args.logpath, defaults) logpath = context.fs.logpath if context.mount_info: mountpoint = context.mount_info.mountpoint sys.stderr.write("Error: log %s already mounted in %s\n" % (logpath, mountpoint)) sys.exit(4) ip, *port = args.bind.split(':') if port: port = int(port[0]) else: port = context.fs.port if args.iface: iface_ip = utils.get_ip_address(args.iface) if ip != '0.0.0.0' and ip != iface_ip: sys.stderr.write("-bind and -iface ip addresses do not match %s != %s\n" % (ip, iface_ip)) sys.exit(9) ip = iface_ip # logpath = args.logpath config = get_or_create_config(defaults) hostname = args.hostname section = config[context.fs.name] if not hostname: if context.fs.name in config: hostname = section['hostname'] else: hostname = defaults.hostname rpc_port = port+1 sync_port = port+2 logpath = os.path.normpath(logpath) keypath = os.path.normpath(args.keypath) logging.basicConfig( level=logging.DEBUG if args.debug else logging.INFO, format='%(asctime)-15s [%(levelname)s] %(name)s: %(message)s', ) logpath = os.path.normpath(logpath) log = Log(logpath) log.load() if keypath == defaults.keypath and not os.path.exists(keypath): view = View(log) else: key = Key.load(keypath) view = View(log, key) view.build() serf = None serf_agent = None if args.serf: join = args.join.split(',') if args.join else [] serf, serf_agent = gossip.run(section, view, ip, port, hostname, join) if args.watcher: handler = handlers.Handler(args.watcher, view.log, state=serf.blockstate) else: if args.watcher: handler = handlers.Handler(args.watcher, view.log) if mount: init_function = lambda: None if args.serf: # Eventloop needs to run on a separated thread when using FUSE init_function = lambda: loop.run_thread(view, serf, port+2, config=section) mountpoint = args.mountpoint sys.stdout.write('Mounting %s into %s\n' % (logpath, mountpoint)) fs = FileSystem(view, serf=serf, serf_agent=serf_agent, init_function=init_function) fsname = '%s:%i' % (logpath, sync_port) foreground = args.foreground or args.debug FUSE(fs, mountpoint, fsname=fsname, nothreads=False, foreground=foreground) else: try: loop.run(view, serf, port+2, config=section) except KeyboardInterrupt: pass finally: serf_agent.stop()