Esempio n. 1
0
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)
Esempio n. 2
0
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
Esempio n. 3
0
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
Esempio n. 4
0
def command():
    # get <domainTXT/domain:port/ip:port> [-l <logpath>] [-n <name>] [-p] [-f] [-d]
    args = parser.parse_args()
    default_logpath = os.path.join(defaults.dir, 'logs', args.name)
    logpath = args.logpath or default_logpath
    if logpath != '-':
        create_logdir(logpath, default_logpath, args.force)
    ip, *port = args.addr.split(':')
    try:
        ip = str(ipaddress.ip_address(ip))
    except ValueError:
        nslookup = """nslookup -q=txt %s|grep 'text =' | sed -E 's/.*text = "([^"]+)".*/\\1/'""" % ip
        lookup = subprocess.Popen(nslookup, shell=True, stdout=subprocess.PIPE)
        lookup = lookup.stdout.read().decode().strip()
        if lookup:
            ip, *port = lookup.split(':')
        else:
            nslookup = "nslookup %s -q=a | grep Address|tail -n1 | awk {'print $2'}" % ip
            lookup = subprocess.Popen(nslookup, shell=True, stdout=subprocess.PIPE)
            lookup = lookup.stdout.read().decode().strip()
            if not '#' in lookup:
                ip = lookup
    if not port:
        port = get_port(args.name) + 2
    else:
        port = int(port[0])
    sys.stderr.write("Connecting to %s:%i\n" % (ip, port))
    received = False
    if logpath == '-':
        for data in utils.netcat(ip, port, b'cGET'):
            sys.stdout.write(data)
            received = True
    else:
        try:
            with open(logpath, 'w') as handler:
                for data in utils.netcat(ip, port, b'cGET'):
                    handler.write(data)
                    received = True
        except ConnectionRefusedError:
            os.remove(logpath)
            sys.stderr.write("Error: connection refused, bad port?\n")
            sys.exit(3)
    if not received:
        sys.stderr.write("Error: nothing has been received, bad port?\n")
        sys.exit(2)
    elif logpath != '-':
        log = Log(logpath)
        try:
            log.load(validate=True)
        except exceptions.ValidationError as e:
            sys.stderr.write(str(e) + '\n')
        sys.stdout.write("%s log created\n" % logpath)
Esempio n. 5
0
def command():
    # get <domainTXT/domain:port/ip:port> [-l <logpath>] [-n <name>] [-p] [-f] [-d]
    args = parser.parse_args()
    default_logpath = os.path.join(defaults.dir, "logs", args.name)
    logpath = args.logpath or default_logpath
    if logpath != "-":
        create_logdir(logpath, default_logpath, args.force)
    ip, *port = args.addr.split(":")
    try:
        ip = str(ipaddress.ip_address(ip))
    except ValueError:
        nslookup = """nslookup -q=txt %s|grep 'text =' | sed -E 's/.*text = "([^"]+)".*/\\1/'""" % ip
        lookup = subprocess.Popen(nslookup, shell=True, stdout=subprocess.PIPE)
        lookup = lookup.stdout.read().decode().strip()
        if lookup:
            ip, *port = lookup.split(":")
        else:
            nslookup = "nslookup %s -q=a | grep Address|tail -n1 | awk {'print $2'}" % ip
            lookup = subprocess.Popen(nslookup, shell=True, stdout=subprocess.PIPE)
            lookup = lookup.stdout.read().decode().strip()
            if not "#" in lookup:
                ip = lookup
    if not port:
        port = get_port(args.name) + 2
    else:
        port = int(port[0])
    sys.stderr.write("Connecting to %s:%i\n" % (ip, port))
    received = False
    if logpath == "-":
        for data in utils.netcat(ip, port, b"cGET"):
            sys.stdout.write(data)
            received = True
    else:
        try:
            with open(logpath, "w") as handler:
                for data in utils.netcat(ip, port, b"cGET"):
                    handler.write(data)
                    received = True
        except ConnectionRefusedError:
            os.remove(logpath)
            sys.stderr.write("Error: connection refused, bad port?\n")
            sys.exit(3)
    if not received:
        sys.stderr.write("Error: nothing has been received, bad port?\n")
        sys.exit(2)
    elif logpath != "-":
        log = Log(logpath)
        try:
            log.load(validate=True)
        except exceptions.ValidationError as e:
            sys.stderr.write(str(e) + "\n")
        sys.stdout.write("%s log created\n" % logpath)
Esempio n. 6
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
Esempio n. 7
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