Beispiel #1
0
def configure_dexbot(config, ctx):
    whiptail = get_whiptail('DEXBot configure')
    workers = config.get('workers', {})
    if not workers:
        while True:
            txt = whiptail.prompt("Your name for the worker")
            config['workers'] = {txt: configure_worker(whiptail, {})}
            if not whiptail.confirm(
                    "Set up another worker?\n(DEXBot can run multiple workers in one instance)"
            ):
                break
        setup_systemd(whiptail, config)
    else:
        bitshares_instance = ctx.bitshares
        action = whiptail.menu(
            "You have an existing configuration.\nSelect an action:",
            [('NEW', 'Create a new worker'), ('DEL', 'Delete a worker'),
             ('EDIT', 'Edit a worker'), ('CONF', 'Redo general config')])

        if action == 'EDIT':
            worker_name = whiptail.menu("Select worker to edit",
                                        [(index, index) for index in workers])
            config['workers'][worker_name] = configure_worker(
                whiptail, config['workers'][worker_name])

            strategy = StrategyBase(worker_name,
                                    bitshares_instance=bitshares_instance,
                                    config=config)
            strategy.clear_all_worker_data()
        elif action == 'DEL':
            worker_name = whiptail.menu("Select worker to delete",
                                        [(index, index) for index in workers])
            del config['workers'][worker_name]

            strategy = StrategyBase(worker_name,
                                    bitshares_instance=bitshares_instance,
                                    config=config)
            strategy.clear_all_worker_data()
        elif action == 'NEW':
            txt = whiptail.prompt("Your name for the new worker")
            config['workers'][txt] = configure_worker(whiptail, {})
        elif action == 'CONF':
            choice = whiptail.node_radiolist(
                msg="Choose node",
                items=select_choice(config['node'][0],
                                    [(index, index)
                                     for index in config['node']]))
            # Move selected node as first item in the config file's node list
            config['node'].remove(choice)
            config['node'].insert(0, choice)

            setup_systemd(whiptail, config)
    whiptail.clear()
    return config
Beispiel #2
0
def configure_dexbot(config):
    d = get_whiptail()
    workers = config.get('workers', {})
    if not workers:
        while True:
            txt = d.prompt("Your name for the worker")
            config['workers'] = {txt: configure_worker(d, {})}
            if not d.confirm(
                    "Set up another worker?\n(DEXBot can run multiple workers in one instance)"
            ):
                break
        setup_systemd(d, config)
    else:
        action = d.menu(
            "You have an existing configuration.\nSelect an action:",
            [('NEW', 'Create a new worker'), ('DEL', 'Delete a worker'),
             ('EDIT', 'Edit a worker'), ('CONF', 'Redo general config')])
        if action == 'EDIT':
            worker_name = d.menu("Select worker to edit",
                                 [(i, i) for i in workers])
            config['workers'][worker_name] = configure_worker(
                d, config['workers'][worker_name])
            bitshares_instance = BitShares(config['node'])
            strategy = BaseStrategy(worker_name,
                                    bitshares_instance=bitshares_instance)
            strategy.purge()
        elif action == 'DEL':
            worker_name = d.menu("Select worker to delete",
                                 [(i, i) for i in workers])
            del config['workers'][worker_name]
            bitshares_instance = BitShares(config['node'])
            strategy = BaseStrategy(worker_name,
                                    bitshares_instance=bitshares_instance)
            strategy.purge()
        elif action == 'NEW':
            txt = d.prompt("Your name for the new worker")
            config['workers'][txt] = configure_worker(d, {})
        elif action == 'CONF':
            config['node'] = d.prompt("BitShares node to use",
                                      default=config['node'])
            setup_systemd(d, config)
    d.clear()
    return config
Beispiel #3
0
def configure_dexbot(config):
    d = get_whiptail()
    bots = config.get('bots', {})
    if len(bots) == 0:
        ping_results = start_pings()
        while True:
            txt = d.prompt("Your name for the bot")
            config['bots'] = {txt: configure_bot(d, {})}
            if not d.confirm(
                    "Set up another bot?\n(DEXBOt can run multiple bots in one instance)"
            ):
                break
        setup_reporter(d, config)
        setup_systemd(d, config)
        node = best_node(ping_results)
        if node:
            config['node'] = node
        else:
            # search failed, ask the user
            config['node'] = d.prompt(
                "Search for best BitShares node failed.\n\nPlease enter wss:// url of chosen node."
            )
    else:
        action = d.menu(
            "You have an existing configuration.\nSelect an action:",
            [('NEW', 'Create a new bot'), ('DEL', 'Delete a bot'),
             ('EDIT', 'Edit a bot'), ('CONF', 'Redo general config')])
        if action == 'EDIT':
            botname = d.menu("Select bot to edit", [(i, i) for i in bots])
            config['bots'][botname] = configure_bot(d, config['bots'][botname])
        elif action == 'DEL':
            botname = d.menu("Select bot to delete", [(i, i) for i in bots])
            del config['bots'][botname]
        if action == 'NEW':
            txt = d.prompt("Your name for the new bot")
            config['bots'][txt] = configure_bot(d, {})
        else:
            setup_reporter(d, config)
            config['node'] = d.prompt("BitShares node to use",
                                      default=config['node'])
    d.clear()
    return config
Beispiel #4
0
def configure_dexbot(config, ctx):
    """ Main `cli configure` entrypoint

        :param dexbot.config.Config config: dexbot config
    """
    whiptail = get_whiptail('DEXBot configure')
    workers = config.get('workers', {})
    bitshares_instance = ctx.bitshares
    validator = ConfigValidator(bitshares_instance)

    if not workers:
        while True:
            txt = whiptail.prompt("Your name for the worker")
            if len(txt) == 0:
                whiptail.alert("Worker name cannot be blank. ")
            else:
                config['workers'] = {
                    txt: configure_worker(whiptail, {}, bitshares_instance)
                }
                if not whiptail.confirm(
                        "Set up another worker?\n(DEXBot can run multiple workers in one instance)"
                ):
                    break
        setup_systemd(whiptail, config)
    else:
        while True:
            action = whiptail.menu(
                "You have an existing configuration.\nSelect an action:",
                [('LIST', 'List your workers'), ('NEW', 'Create a new worker'),
                 ('EDIT', 'Edit a worker'), ('DEL_WORKER', 'Delete a worker'),
                 ('ADD', 'Add a bitshares account'),
                 ('DEL_ACCOUNT', 'Delete a bitshares account'),
                 ('SHOW', 'Show bitshares accounts'),
                 ('NODES', 'Edit Node Selection'),
                 ('ADD_NODE', 'Add Your Node'), ('HELP', 'Where to get help'),
                 ('EXIT', 'Quit this application')])

            my_workers = [(index, index) for index in workers]

            if action == 'EXIT':
                # Cancel will also exit the application. but this is a clearer label
                # Todo: modify cancel to be "Quit" or "Exit" for the whiptail menu item.
                break
            elif action == 'LIST':
                if len(my_workers):
                    # List workers, then provide option to list config of workers
                    worker_name = whiptail.menu(
                        "List of Your Workers. Select to view Configuration.",
                        my_workers)
                    content = config['workers'][worker_name]
                    text = '\n'
                    for key, value in content.items():
                        text += '{}: {}\n'.format(key, value)
                    whiptail.view_text(text, pager=False)
                else:
                    whiptail.alert('No workers to view.')
            elif action == 'EDIT':
                if len(my_workers):
                    worker_name = whiptail.menu("Select worker to edit",
                                                my_workers)
                    config['workers'][worker_name] = configure_worker(
                        whiptail, config['workers'][worker_name],
                        bitshares_instance)
                else:
                    whiptail.alert('No workers to edit.')
            elif action == 'DEL_WORKER':
                if len(my_workers):
                    worker_name = whiptail.menu("Select worker to delete",
                                                my_workers)
                    del config['workers'][worker_name]
                    # Pass ctx.config which is a loaded config (see ui.py configfile()), while `config` in a Config()
                    # instance, which is empty dict, but capable of returning keys via __getitem__(). We need to pass
                    # loaded config into StrategyBase to avoid loading a default config and preserve `--configfile`
                    # option
                    strategy = StrategyBase(
                        worker_name,
                        bitshares_instance=bitshares_instance,
                        config=ctx.config)
                    strategy.clear_all_worker_data()
                else:
                    whiptail.alert('No workers to delete.')
            elif action == 'NEW':
                worker_name = whiptail.prompt("Your name for the new worker. ")
                if not worker_name:
                    whiptail.alert("Worker name cannot be blank. ")
                elif not validator.validate_worker_name(worker_name):
                    whiptail.alert(
                        'Worker name needs to be unique. "{}" is already in use.'
                        .format(worker_name))
                else:
                    config['workers'][worker_name] = configure_worker(
                        whiptail, {}, bitshares_instance)
            elif action == 'ADD':
                add_account(whiptail, bitshares_instance)
            elif action == 'DEL_ACCOUNT':
                del_account(whiptail, bitshares_instance)
            elif action == 'SHOW':
                account_list = list_accounts(bitshares_instance)
                if account_list:
                    action = whiptail.menu(
                        "Bitshares Account List (Name - Type)", account_list)
                else:
                    whiptail.alert(
                        'You do not have any bitshares accounts in the wallet')
            elif action == 'ADD_NODE':
                txt = whiptail.prompt(
                    "Your name for the new node: e.g. wss://dexnode.net/ws")
                # Insert new node on top of the list
                config['node'].insert(0, txt)
            elif action == 'NODES':
                choice = whiptail.node_radiolist(
                    msg="Choose your preferred node",
                    items=select_choice(config['node'][0],
                                        [(index, index)
                                         for index in config['node']]))
                # Move selected node as first item in the config file's node list
                config['node'].remove(choice)
                config['node'].insert(0, choice)
                setup_systemd(whiptail, config)
            elif action == 'HELP':
                whiptail.alert(
                    "Please see https://github.com/Codaone/DEXBot/wiki")

    whiptail.clear()
    return config
Beispiel #5
0
def configure_dexbot(config, shell=False):
    global bitshares_instance
    d = get_whiptail()
    workers = config.get('workers', {})
    if len(workers) == 0:
        d.view_text("""Welcome to the DEXBot text-based configuration.
You will be asked to set up at least one bot, this will then run in the background.
You can use the arrow keys or TAB to move between buttons, and RETURN to select, the mouse does not work. Use Space to select an item in a list. Selecting Cancel will exit the program.
""")
        while True:
            txt = d.prompt("Your name for the new worker")
            config['workers'] = {txt: configure_worker(d, {})}
            if not d.confirm("Set up another worker?\n(DEXBot can run multiple workers in one instance)"):
                break
        setup_systemd(d, config, shell)
        add_key(d, bitshares_instance)
        return True
    else:
        menu = [('NEW', 'Create a new bot'),
                ('DEL', 'Delete a bot'),
                ('EDIT', 'Edit a bot'),
                ('REPORT', 'Configure reporting'),
                ('NODE', 'Set the BitShares node'),
                ('KEY', 'Add a private key'),
                ('WIPE', 'Wipe all private keys'),
                ('LOG', 'Show the DEXBot event log'),
                ('SHELL', 'Escape to the Linux command shell')]
        if shell:
            menu.extend([('PASSWD', 'Set the account password'),
                         ('LOGOUT', 'Logout of the server')])
        else:
            menu.append(('QUIT', 'Quit without saving'))
        action = d.menu("Select an action:", menu)
        if action == 'EDIT':
            worker_name = d.menu("Select worker to edit", [(i, i) for i in workers])
            config['workers'][worker_name] = configure_worker(d, config['workers'][worker_name])
            strategy = BaseStrategy(worker_name, bitshares_instance=bitshares_instance)
            if not bitshares_instance:
                bitshares_instance = BitShares(config['node'])
            unlock_wallet(d, bitshares_instance)
            strategy.purge()
            return True
        elif action == 'DEL':
            worker_name = d.menu("Select worker to delete", [(i, i) for i in workers])
            del config['workers'][worker_name]
            if not bitshares_instance:
                bitshares_instance = BitShares(config['node'])
            unlock_wallet(d, bitshares_instance)
            strategy = BaseStrategy(worker_name, bitshares_instance=bitshares_instance)
            strategy.purge()
            return True
        elif action == 'NEW':
            txt = d.prompt("Your name for the new worker")
            config['workers'][txt] = configure_worker(d, {})
            return True
        elif action == 'REPORT':
            setup_reporter(d, config)
            return True
        elif action == 'NODE':
            config['node'] = d.prompt(
                "BitShares node to use",
                default=config['node'])
            return True
        elif action == 'KEY':
            add_key(d, bitshares_instance)
            return False
        elif action == 'WIPE':
            if not bitshares_instance:
                bitshares_instance = BitShares()
            if bitshares_instance.wallet.created():
                if d.confirm("Really wipe your wallet of keys?", default='no'):
                    unlock_wallet(d, bitshares_instance)
                    bitshares_instance.wallet.wipe(True)
            else:
                d.alert("No wallet to wipe")
            return False
        elif action == 'LOG':
            os.system("journalctl --user-unit=dexbot -n 1000 -e")
            return False
        elif action == 'LOGOUT':
            sys.exit()
        elif action == 'PASSWD':
            os.system("passwd")
            return False
        elif action == 'SHELL':
            os.system("/bin/bash")
            return False
        elif action == 'QUIT':
            return False
        else:
            d.prompt("Unknown command {}".format(action))
            return False