Ejemplo n.º 1
0
    def do_dump(self, options):
        '''
        dump FILE_PATH [PATH|all]

        Dumps a copy of either the current configuration level or the
        configuration at PATH to FILE_PATH. If PATH is 'all', then the
        top-level configuration will be dumped.
        '''
        options = options.split()
        if len(options) < 1:
            raise CliError("Syntax error: expected at least one option")
        filepath = options.pop(0)
        if not filepath.startswith('/'):
            raise CliError("Expected an absolute file path")
        path = " ".join(options)
        if path.strip() == 'all':
            path = ''
        else:
            path = ("%s %s" % (self.edit_levels[-1], path)).strip()

        self.config.save(filepath, path)
        if not path:
            path_desc = 'all'
        else:
            path_desc = path
        # FIXME Accept "half-node" path
        log.info("Dumped [%s] to %s" % (path_desc, filepath))
Ejemplo n.º 2
0
    def do_edit(self, options):
        '''
        edit PATH

        Changes the current configuration edit level to PATH, relative to the
        current configuration edit level. If PATH does not exist currently, it
        will be created.
        '''
        level = self.edit_levels[-1]
        nodes = self.config.search("%s %s" % (level, options))
        if not nodes:
            nodes_beyond = self.config.search("%s %s .*" % (level, options))
            if nodes_beyond:
                raise CliError("Incomplete path: [%s]" % options)
            else:
                statement = "%s %s" % (self.edit_levels[-1], options)
                log.debug("Setting statement '%s'" % statement)
                self.config.set(statement)
                self.needs_save = True
                node = self.config.search(statement)[0]
                log.info("Created configuration level: %s" % node.path_str)
                self.add_edit_level(node.path_str)
                self.do_missing('')
        elif len(nodes) > 1:
            raise CliError("Ambiguous path: [%s]" % options)
        else:
            self.add_edit_level(nodes[0].path_str)
            self.do_missing('')
Ejemplo n.º 3
0
    def do_commit(self, options):
        '''
        commit [check|interactive]

        Saves the current configuration to the system startup configuration
        file, after applying the changes to the running system.
      
        If the check option is provided, the current configuration will be
        checked but not saved or applied.

        If the interactive option is provided, the user will be able to confirm
        or skip every modification to the live system.
        '''
        # TODO Add [as DESCRIPTION] option
        # TODO Change to commit only current level unless 'all' option
        syntax = pp.Optional(pp.oneOf("check interactive"))
        options = self.parse(options, 'commit', syntax)[1:]

        if self.attrs_missing:
            self.do_missing('')
            raise CliError("Cannot validate configuration: "
                           "required attributes not set")

        if not self.needs_commit:
            raise CliError("No changes to commit!")

        log.info("Validating configuration")
        for msg in self.config.verify():
            log.info(msg)
        if 'check' in options:
            return

        do_it = self.yes_no(
            "Apply changes and overwrite system "
            "configuration ?", False)
        if do_it is not False:
            log.info("Applying configuration")
            for msg in self.config.apply():
                if 'interactive' in options:
                    apply = self.yes_no("%s\nPlease confirm" % msg, True)
                    if apply is False:
                        log.warning("Aborted commit on user request: "
                                    "please verify system status")
                        return
                else:
                    log.info(msg)
            self.save_running_config()
            self.needs_save = False
        else:
            log.info("Cancelled configuration commit")
Ejemplo n.º 4
0
    def do_missing(self, options):
        '''
        missing [PATH]

        Shows all missing required attribute values in the current candidate
        configuration for PATH, relative to the current edit level. 
        '''
        node_filter = filter_only_missing
        path = ("%s %s" % (self.edit_levels[-1], options)).strip()
        if not path:
            path = '.*'
        trees = self.config.search(path)
        if not trees:
            trees = self.config.search("%s .*" % path)
        if not trees:
            raise CliError("No such path: %s" % path)

        missing = []
        for tree in trees:
            for attr in tree.walk(node_filter):
                missing.append(attr)

        if not options:
            path = "current configuration"

        if not missing:
            log.warning("No missing attributes values under %s" % path)
        else:
            log.warning("Missing attributes values under %s:" % path)
            for attr in missing:
                log.info("    %s" % attr.path_str)
            sys.stdout.write("\n")
Ejemplo n.º 5
0
    def del_edit_level(self):
        if len(self.edit_levels) == 1:
            raise CliError("Already at top-level")

        self.edit_levels.pop()
        if len(self.edit_levels) == 1:
            log.warning("[edit] top-level")
        else:
            log.warning("[edit] %s" % self.edit_levels[-1])
        self.set_prompt(self.edit_levels[-1])
Ejemplo n.º 6
0
    def do_delete(self, options):
        '''
        delete [PATH]

        Deletes either all LIO configuration objects at the current edit level,
        or only those under PATH relative to the current level.
        '''
        path = "%s %s" % (self.edit_levels[-1], options)
        if not path.strip():
            raise CliError("Cannot delete top-level configuration")

        nodes = self.config.search(path)
        if not nodes:
            # TODO Replace all "%s .*" forms with a try_hard arg to search
            nodes.extend(self.config.search("%s .*" % path))
        if not nodes:
            raise CliError("No configuration objects at path: %s" %
                           path.strip())

        # FIXME Use a real tree walk with filter
        obj_no = 0
        for node in nodes:
            if node.data['type'] == 'obj':
                obj_no += 1

        if obj_no == 0:
            raise CliError("Can't delete attributes, only objects: %s" %
                           path.strip())

        do_it = self.yes_no(
            "Delete %d objects(s) from current configuration?" % len(nodes),
            False)
        if do_it is not False:
            deleted = self.config.delete(path)
            if not deleted:
                deleted = self.config.delete("%s .*" % path)
            self.needs_save = True
            log.info("Deleted %d configuration object(s)" % obj_no)
        else:
            log.info("Cancelled: configuration not modified")
Ejemplo n.º 7
0
    def do_set(self, options):
        '''
        set [PATH] OBJECT IDENTIFIER
        set [PATH] ATTRIBUTE VALUE

        Sets either an OBJECT IDENTIFIER (i.e. "disk mydisk") or an ATTRIBUTE
        VALUE (i.e. "enable yes").
        '''
        if not options:
            raise CliError("Missing required options")
        statement = "%s %s" % (self.edit_levels[-1], options)
        log.debug("Setting statement '%s'" % statement)
        created = self.config.set(statement)
        for node in created:
            log.info("[%s] has been set" % node.path_str)
        if not created:
            log.info("Ignored: Current configuration already match statement")
        else:
            self.needs_save = True
Ejemplo n.º 8
0
    def do_info(self, options):
        '''
        info [PATH]

        Displays edit history information about the current configuration level
        or all configuration items matching PATH.
        '''
        # TODO Add node type information
        path = "%s %s" % (self.edit_levels[-1], options)
        if not path.strip():
            # This is just a test for tables
            table = pt.PrettyTable()
            table.hrules = pt.ALL
            table.field_names = ["change", "date", "type", "data"]
            table.align['data'] = 'l'
            changes = []
            nb_ver = len(self.config._configs)
            for idx, cfg in enumerate(reversed(self.config._configs)):
                lst_src = self.lst_data_src(cfg.data['source'])
                table.add_row(["%03d" % (idx + 1)] + lst_src)
            # FIXME Use term width to compute these
            table.max_width["date"] = 10
            table.max_width["data"] = 43
            sys.stdout.write("%s\n" % table.get_string())
        else:
            nodes = self.config.search(path)
            if not nodes:
                # TODO Replace all "%s .*" forms with a try_hard arg to search
                nodes.extend(self.config.search("%s .*" % path))
            if not nodes:
                raise CliError("Path does not exist: %s" % path.strip())
            infos = []
            for node in nodes:
                if node.data.get('required'):
                    req = "(required attribute) "
                else:
                    req = ""
                path = node.path_str
                infos.append(
                    "%s[%s]\nLast change: %s" %
                    (req, path, self.fmt_data_src(node.data['source'])))
            log.info("\n\n".join(infos))
Ejemplo n.º 9
0
    def do_configure(self, options):
        '''
        configure

        Switch to config mode. In this mode, you can safely edit a candidate
        configuration for the system, and commit it only when it is ready.
        '''
        options = self.parse(options, 'configure', '')
        if not self.interactive:
            raise CliError("Cannot switch to config mode when running "
                           "non-interactively.")
        else:
            self.save_history()
            self.clear_history()
            # FIXME Preserve CliConfig session state, notably undo history
            CliConfig(interactive=True).cmdloop()
            self.clear_history()
            self.load_history()
            self.do_resync()
            log.warning("[live] Back to live mode")