Exemple #1
0
def detect_log_config(arguments):
    """
    Detect access log config (path and format) of nginx. Offer user to select if multiple access logs are detected.
    :return: path and format of detected / selected access log
    """
    config = arguments['--config']
    if config is None:
        config = detect_config_path()
    if not os.path.exists(config):
        error_exit('Nginx config file not found: %s' % config)

    with open(config) as f:
        config_str = f.read()
    access_logs = dict(get_access_logs(config_str))
    if not access_logs:
        error_exit('Access log file is not provided and ngxtop cannot detect it from your config file (%s).' % config)

    log_formats = dict(get_log_formats(config_str))
    if len(access_logs) == 1:
        log_path, format_name = access_logs.items()[0]
        if format_name == 'combined':
            return log_path, LOG_FORMAT_COMBINED
        if format_name not in log_formats:
            error_exit('Incorrect format name set in config for access log file "%s"' % log_path)
        return log_path, log_formats[format_name]

    # multiple access logs configured, offer to select one
    print('Multiple access logs detected in configuration:')
    log_path = choose_one(access_logs.keys(), 'Select access log file to process: ')
    format_name = access_logs[log_path]
    if format_name not in log_formats:
        error_exit('Incorrect format name set in config for access log file "%s"' % log_path)
    return log_path, log_formats[format_name]
def detect_log_config(arguments):
    """
    Detect access log config (path and format) of nginx. Offer user to select if multiple access logs are detected.
    :return: path and format of detected / selected access log
    """
    config = arguments['--config']
    if config is None:
        config = detect_config_path()
    if not os.path.exists(config):
        error_exit('Nginx config file not found: %s' % config)

    with open(config) as f:
        config_str = f.read()
    access_logs = dict(get_access_logs(config_str))
    if not access_logs:
        error_exit('Access log file is not provided and ngxtop cannot detect it from your config file (%s).' % config)

    log_formats = dict(get_log_formats(config_str))
    if len(access_logs) == 1:
        log_path, format_name = list(access_logs.items())[0]
        if format_name == 'combined':
            return log_path, LOG_FORMAT_COMBINED
        if format_name not in log_formats:
            error_exit('Incorrect format name set in config for access log file "%s"' % log_path)
        return log_path, log_formats[format_name]

    # multiple access logs configured, offer to select one
    print('Multiple access logs detected in configuration:')
    log_path = choose_one(list(access_logs.keys()), 'Select access log file to process: ')
    format_name = access_logs[log_path]
    if format_name not in log_formats:
        error_exit('Incorrect format name set in config for access log file "%s"' % log_path)
    return log_path, log_formats[format_name]
def main(argv=None):
    if argv is None:
        argv = sys.argv
    if (len(argv) > 5) or (len(argv) < 4):
        print "Usage: " + argv[
            0] + " <config file> <domain> <v_node> [YYYY_MM_DD]"
        print "The config file is the same format as used for backups, backup dir, snapshot name and swift credentials are used"
        print 'The domain is the domain to be restored from swift and the v_node is the vertica node name to restore data for'
        print 'If the year/month/day is specified the most recent backup on that day will be downloaded rather than prompting'
        return 1

    config_file = argv[1]
    domain = argv[2]
    v_node_name = argv[3]
    if len(argv) == 5:
        day = argv[4]
    else:
        day = None
    config = yaml.load(open(config_file, 'r'))

    # Setup logging
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
    log = logging.getLogger(__name__)

    with LogTime(log.info, "Restore download completed"):

        # Setup swift/paths
        base_dir, prefix_dir = calculate_paths(config, v_node_name)
        swift_store = SwiftStore(config['swift_key'],
                                 config['swift_region'],
                                 config['swift_tenant'],
                                 config['swift_url'],
                                 config['swift_user'],
                                 prefix_dir,
                                 domain=domain,
                                 vnode=v_node_name)
        fs_store = FSStore(base_dir, prefix_dir)

        # Get the metadata from the last restore (if any)
        current_metadata = DirectoryMetadata(fs_store)

        # Grab the swift metadata we want to restore
        if day is None:
            pickle = choose_one(swift_store.list_pickles(),
                                "Please choose a pickle to restore from")
        else:
            # Since the list is sorted this will find the newest that matches the given day, or None otherwise
            pickle = None
            for option in swift_store.list_pickles():
                if option.startswith(day):
                    pickle = option

        if pickle is None:
            log.error('No backups found in swift.')
            sys.exit(1)
        swift_metadata = DirectoryMetadata.load_pickle(swift_store, pickle)

        # Compare the files in the current restore and swift and download/delete as necessary
        with LogTime(log.debug, "Diff completed", seconds=True):
            to_download, to_del = swift_metadata.diff(current_metadata)

        size_downloaded = 0
        with LogTime(log.info, "Download Completed"):
            for relative_path in to_download:
                size_downloaded += swift_store.download(
                    relative_path, base_dir)
        log.info("\tDownloaded %s in %d items" %
                 (sizeof_fmt(size_downloaded), len(to_download)))

        with LogTime(log.info, "Deleted %d items" % len(to_del)):
            for relative_path in to_del:
                fs_store.delete(relative_path)

        EpochFiles(os.path.join(base_dir, prefix_dir), config['snapshot_name'],
                   swift_metadata.date).restore()

        # Save the swift metadata to the local fs, to indicate the restore is done
        swift_metadata.save(fs_store)

    delete_pickles(fs_store)
def main(argv=None):
    if argv is None:
        argv = sys.argv
    if (len(argv) > 6) or (len(argv) < 5):
        print "Usage: " + argv[0] + " <config file> <domain> <v_node> <hostname> [YYYY_MM_DD]"
        print "The config file is the same format as used for backups, backup dir, snapshot name and swift credentials are used"
        print 'The domain is the domain to be restored from swift and the v_node is the vertica node name to restore data for'
        print 'If the year/month/day is specified the most recent backup on that day will be downloaded rather than prompting'
        return 1

    config_file = argv[1]
    domain = argv[2]
    v_node_name = argv[3]
    hostname = argv[4]
    if len(argv) == 6:
        day = argv[5]
    else:
        day = None
    config = yaml.load(open(config_file, 'r'))

    # Setup logging
    logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
    log = logging.getLogger(__name__)

    with LogTime(log.info, "Restore download completed"):

        # Setup swift/paths
        base_dir, prefix_dir = calculate_paths(config, v_node_name)
        swift_store = SwiftStore(config['swift_key'], config['swift_region'], config['swift_tenant'],
                                 config['swift_url'], config['swift_user'], prefix_dir, domain=domain,
                                 vnode=v_node_name, hostname=hostname)
        fs_store = FSStore(base_dir, prefix_dir)

        # Get the metadata from the last restore (if any)
        current_metadata = DirectoryMetadata(fs_store)

        # Grab the swift metadata we want to restore
        if day is None:
            pickle = choose_one(swift_store.list_pickles(), "Please choose a pickle to restore from")
        else:
            # Since the list is sorted this will find the newest that matches the given day, or None otherwise
            pickle = None
            for option in swift_store.list_pickles():
                if option.startswith(day):
                    pickle = option

        if pickle is None:
            log.error('No backups found in swift.')
            sys.exit(1)
        swift_metadata = DirectoryMetadata.load_pickle(swift_store, pickle)

        # Compare the files in the current restore and swift and download/delete as necessary
        with LogTime(log.debug, "Diff completed", seconds=True):
            to_download, to_del = swift_metadata.diff(current_metadata)

        size_downloaded = 0
        with LogTime(log.info, "Download Completed"):
            for relative_path in to_download:
                size_downloaded += swift_store.download(relative_path, base_dir)
        log.info("\tDownloaded %s in %d items" % (sizeof_fmt(size_downloaded), len(to_download)))

        with LogTime(log.info, "Deleted %d items" % len(to_del)):
            for relative_path in to_del:
                fs_store.delete(relative_path)

        EpochFiles(os.path.join(base_dir, prefix_dir), config['catalog_dir'], config['snapshot_name'], swift_metadata.date).restore()

        # Save the swift metadata to the local fs, to indicate the restore is done
        swift_metadata.save(fs_store)

    delete_pickles(fs_store)
Exemple #5
0
def render_battle_1():
    session.monster_name = "Ugly Orc"
    turn = "player"
    player_defense = 0
    monster_defense = 0
    time_to_battle = session.monster_hp > 0 and session.player_hp > 0
    while time_to_battle:
        screen.clear_screen()
        screen.print_general_screen()
        screen.print_battle_screen()
        if turn == "player":
            dice = utils.rolls_dice()
            option = ""
            while option != "q":
                screen.print_battle_options(["Attack", "Defend"])
                option = raw_input("Choose an option (q to quit): ")
                if option == "1":
                    if monster_defense > 0:
                        damage = (dice + session.player_ap) - (
                            session.monster_dp + monster_defense)
                    else:
                        damage = (dice +
                                  session.player_ap) - session.monster_dp
                    if damage < 0:
                        damage = 0
                    screen.clear_screen()
                    screen.print_general_screen()
                    screen.print_battle_screen()
                    screen.print_battle_info(
                        "Player attacks, rolls %s, dealing %s of damage" %
                        (dice, damage))
                    raw_input("Press any key to continue...")
                    session.monster_hp = session.monster_hp - damage
                    player_defense = 0
                    turn = "monster"
                    break
                elif option == "2":
                    player_defense = dice + session.player_dp
                    screen.clear_screen()
                    screen.print_general_screen()
                    screen.print_battle_screen()
                    screen.print_battle_info(
                        "Player defends, rolls %s, getting %s of defense" %
                        (dice, player_defense))
                    raw_input("Press any key to continue...")
                    turn = "monster"
                    break
                else:
                    screen.print_error("Choose a valid option")
        elif turn == "monster":
            dice = utils.rolls_dice()
            option = utils.choose_one(["1", "2"])
            if option == "1":
                if player_defense > 0:
                    damage = (dice + session.monster_ap) - (session.player_dp +
                                                            player_defense)
                else:
                    damage = (dice + session.monster_ap) - session.player_dp
                if damage < 0:
                    damage = 0
                screen.clear_screen()
                screen.print_general_screen()
                screen.print_battle_screen()
                screen.print_battle_info(
                    "Monster attacks, rolls %s, dealing %s of damage" %
                    (dice, damage))
                raw_input("Press any key to continue...")
                session.player_hp = session.player_hp - damage
                monster_defense = 0
                turn = "player"
            elif option == "2":
                monster_defense = dice + session.monster_dp
                screen.clear_screen()
                screen.print_general_screen()
                screen.print_battle_screen()
                screen.print_battle_info(
                    "Monster defends, rolls %s, getting %s of defense" %
                    (dice, monster_defense))
                raw_input("Press any key to continue...")
                turn = "player"
            else:
                screen.print_error("Unknown monster action")
        else:
            screen.print_error("Unknown turn")

        if session.monster_hp <= 0:
            screen.print_room_notice("You won!")
            screen.print_room_notice("You found %sGP" % session.monster_gp)
            session.player_gp = session.player_gp + session.monster_gp
            rooms.render_room_5()
        elif session.player_hp <= 0:
            screen.print_room_notice("You died!")
            screen.print_game_over()
        else:
            continue
def render_battle_1():
    session.monster_name = "Ugly Orc"
    turn = "player"
    player_defense = 0
    monster_defense = 0
    time_to_battle = session.monster_hp > 0 and session.player_hp > 0
    while time_to_battle:
        screen.clear_screen()
        screen.print_general_screen()
        screen.print_battle_screen()
        if turn == "player":
            dice = utils.rolls_dice()
            option = ""
            while option != "q":
                screen.print_battle_options([
                    "Attack",
                    "Defend"
                ])
                option = raw_input("Choose an option (q to quit): ")
                if option == "1":
                    if monster_defense > 0:
                        damage = (dice + session.player_ap) - (session.monster_dp + monster_defense)
                    else:
                        damage = (dice + session.player_ap) - session.monster_dp
                    if damage < 0:
                        damage = 0
                    screen.clear_screen()
                    screen.print_general_screen()
                    screen.print_battle_screen()
                    screen.print_battle_info(
                        "Player attacks, rolls %s, dealing %s of damage" % (dice, damage)
                    )
                    raw_input("Press any key to continue...")
                    session.monster_hp = session.monster_hp - damage
                    player_defense = 0
                    turn = "monster"
                    break
                elif option == "2":
                    player_defense = dice + session.player_dp
                    screen.clear_screen()
                    screen.print_general_screen()
                    screen.print_battle_screen()
                    screen.print_battle_info(
                        "Player defends, rolls %s, getting %s of defense" % (dice, player_defense)
                    )
                    raw_input("Press any key to continue...")
                    turn = "monster"
                    break
                else:
                    screen.print_error("Choose a valid option")
        elif turn == "monster":
            dice = utils.rolls_dice()
            option = utils.choose_one(["1", "2"])
            if option == "1":
                if player_defense > 0:
                    damage = (dice + session.monster_ap) - (session.player_dp + player_defense)
                else:
                    damage = (dice + session.monster_ap) - session.player_dp
                if damage < 0:
                    damage = 0
                screen.clear_screen()
                screen.print_general_screen()
                screen.print_battle_screen()
                screen.print_battle_info(
                    "Monster attacks, rolls %s, dealing %s of damage" % (dice, damage)
                )
                raw_input("Press any key to continue...")
                session.player_hp = session.player_hp - damage
                monster_defense = 0
                turn = "player"
            elif option == "2":
                monster_defense = dice + session.monster_dp
                screen.clear_screen()
                screen.print_general_screen()
                screen.print_battle_screen()
                screen.print_battle_info(
                    "Monster defends, rolls %s, getting %s of defense" % (dice, monster_defense)
                )
                raw_input("Press any key to continue...")
                turn = "player"
            else:
                screen.print_error("Unknown monster action")
        else:
            screen.print_error("Unknown turn")

        if session.monster_hp <= 0:
            screen.print_room_notice("You won!")
            screen.print_room_notice("You found %sGP" % session.monster_gp)
            session.player_gp = session.player_gp + session.monster_gp
            rooms.render_room_5()
        elif session.player_hp <= 0:
            screen.print_room_notice("You died!")
            screen.print_game_over()
        else:
            continue