def configure_devices(): cli.info_1("Already configured writer devices") try: writers = get_writers() except Exception as exp: logger.error(exp) display_error( "Configured devices are not present! " "Reseting devices conf and disabling host.\n" "Please configure devices and re-enable it." ) reset_writers() disable_host() for writer in writers: cli.info( cli.blue, " *", cli.reset, cli.bold, "{slot}:/dev/{device}".format(**writer), cli.reset, "({name} at {device_id})".format(**writer), ) cli.info("") menu = OrderedDict( [ ("reset-writers", ("Reset writers config (remove ALL)", reset_devices)), ("add-device", ("Add one device", add_device)), ] ) display_menu("Choose:", menu=menu, with_cancel=True)
def get_valid_string(label, validator, default=None): """ shortcut to request a string validated via a callback """ value, error = None, None while value is None: if error: cli.info(cli.yellow, error) value, error = validator(cli.ask_string(label, default=default)) return value
def add_device(): cli.info_2("Please remove all SD-cards from all writers.") ready = cli.ask_yes_no("Ready?", default=False) if not ready: return cli.info( "Great! Now please insert", cli.bold, "one", cli.reset, "SD-card into the writer you want to configure.", ) cli.info_3("waiting for card", end="") block_name = None while block_name is None: time.sleep(1) cli.dot() block_name = find_device() cli.info("FOUND") # we now have a new DEVICE. device_id = get_device_id(block_name) slot = get_next_slot() # update configured writers list writers = read_conf().get("writers", {}) writers.update({slot: device_id}) if update_conf({"writers": writers}): display_success( "Found your", humanfriendly.format_size(get_block_size(block_name), binary=True), "card on", cli.bold, block_name, cli.reset, "({})".format(get_display_name(block_name)), ".\n", "Assigned slot:", cli.bold, slot, ) else: display_error("Failed to configure a slot for", cli.bold, block_name) pause()
def configure_iface(): # pick interface ifaces = list(get_interfaces()) iface = display_menu("Choose Interface:", choices=ifaces, with_cancel=True) cli.info("You selected", cli.bold, iface) iface_config = get_iface_config(iface) from pprint import pprint as pp pp(iface_config) # select method (dhcp, fixed) dhcp = "dhcp" fixed = "fixed" methods = [dhcp, fixed] method = display_menu("Connection method:", choices=methods, with_cancel=True) if method == dhcp: if save_network_config(iface, dhcp=True): cli.info_2( "Saved your", cli.bold, iface, cli.reset, "configuration as", cli.bold, "DHCP", ) else: display_error("Unable to save DHCP network config for", cli.bold, iface) pause() return # fixed method config # get address address = get_valid_string("IP Address", ipadress_validator, default=iface_config["address"]) cli.info("You entered", cli.bold, address) netmask = get_valid_string("Netmask", ipadress_validator, default=iface_config["netmask"]) cli.info("You entered", cli.bold, netmask) gateway = get_valid_string("Gateway", ipadress_validator, default=iface_config["gateway"]) cli.info("You entered", cli.bold, gateway) if save_network_config(iface, dhcp=False, address=address, netmask=netmask, gateway=gateway): cli.info_2( "Saved your", cli.bold, iface, cli.reset, "configuration as", cli.bold, address, cli.reset, "/", cli.bold, netmask, cli.reset, "/", cli.bold, gateway, ) else: display_error("Unable to save fixed network config for", cli.bold, iface) pause()
def pause(*message): cli.info(message if message else "hit ENTER to continue…") cli.read_input()
def display_error(*message): """ standardized way of displaying an error message """ cli.info(cli.red, *message)