Example #1
0
    def do_merge(self, options):
        '''
        merge live|FILE_PATH

        Merges the contents of FILE_PATH with the current configuration.
        In case of conflict, values from FILE_PATH will be used.
        If any error happens while doing so, the current configuration will
        be fully rolled back.

        If live is used instead of FILE_PATH, the configuration from the live
        system will be used instead.
        '''
        # TODO Add completion for filepath
        # TODO Add a filepath type to policy and also a parser we can use here
        tok_string = (pp.QuotedString('"')
                      | pp.QuotedString("'")
                      | pp.Word(pp.printables, excludeChars="{}#'\";"))
        options = self.parse(options, 'merge', tok_string)[1:]
        src = options[0]
        if src == 'live':
            if self.yes_no(
                    "Merge the running configuration with "
                    "the current configuration?", False) is not False:
                self.config.set(dump_live())
            else:
                log.info("Cancelled: configuration not modified")
        else:
            if self.yes_no("Merge %s with the current configuration?" % src,
                           False) is not False:
                self.config.update(src)
            else:
                log.info("Cancelled: configuration not modified")
Example #2
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))
Example #3
0
    def do_load(self, options):
        '''
        load live|FILE_PATH

        Replaces the current configuration with the contents of FILE_PATH.
        If any error happens while doing so, the current configuration will
        be fully rolled back.

        If live is used instead of FILE_PATH, the configuration from the live
        system will be used instead.
        '''
        # TODO Add completion for filepath
        # TODO Add a filepath type to policy and also a parser we can use here
        tok_string = (pp.QuotedString('"')
                      | pp.QuotedString("'")
                      | pp.Word(pp.printables, excludeChars="{}#'\";"))
        options = self.parse(options, 'load', tok_string)[1:]
        src = options[0]
        if src == 'live':
            if self.yes_no(
                    "Replace the current configuration with the "
                    "running configuration?", False) is not False:
                self.config.load_live()
            else:
                log.info("Cancelled: configuration not modified")
        else:
            if self.yes_no("Replace the current configuration with %s?" % src,
                           False) is not False:
                self.config.load(src)
            else:
                log.info("Cancelled: configuration not modified")
Example #4
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('')
Example #5
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")
Example #6
0
    def do_merge(self, options):
        '''
        merge live|FILE_PATH

        Merges the contents of FILE_PATH with the current configuration.
        In case of conflict, values from FILE_PATH will be used.
        If any error happens while doing so, the current configuration will
        be fully rolled back.

        If live is used instead of FILE_PATH, the configuration from the live
        system will be used instead.
        '''
        # TODO Add completion for filepath
        # TODO Add a filepath type to policy and also a parser we can use here
        tok_string = (pp.QuotedString('"')
                      | pp.QuotedString("'")
                      | pp.Word(pp.printables, excludeChars="{}#'\";"))
        options = self.parse(options, 'merge', tok_string)[1:]
        src = options[0]
        if src == 'live':
            if self.yes_no("Merge the running configuration with "
                           "the current configuration?", False) is not False:
                self.config.set(dump_live())
            else:
                log.info("Cancelled: configuration not modified")
        else:
            if self.yes_no("Merge %s with the current configuration?"
                           % src, False) is not False:
                self.config.update(src)
            else:
                log.info("Cancelled: configuration not modified")
Example #7
0
    def do_load(self, options):
        '''
        load live|FILE_PATH

        Replaces the current configuration with the contents of FILE_PATH.
        If any error happens while doing so, the current configuration will
        be fully rolled back.

        If live is used instead of FILE_PATH, the configuration from the live
        system will be used instead.
        '''
        # TODO Add completion for filepath
        # TODO Add a filepath type to policy and also a parser we can use here
        tok_string = (pp.QuotedString('"')
                      | pp.QuotedString("'")
                      | pp.Word(pp.printables, excludeChars="{}#'\";"))
        options = self.parse(options, 'load', tok_string)[1:]
        src = options[0]
        if src == 'live':
            if self.yes_no("Replace the current configuration with the "
                           "running configuration?", False) is not False:
                self.config.load_live()
            else:
                log.info("Cancelled: configuration not modified")
        else:
            if self.yes_no("Replace the current configuration with %s?"
                           % src, False) is not False:
                self.config.load(src)
            else:
                log.info("Cancelled: configuration not modified")
Example #8
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('')
Example #9
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))
Example #10
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")
Example #11
0
    def do_reload(self, options):
        '''
        reload

        Reloads the saved system configuration from disk and commits it to the
        running system.
        '''
        if not os.path.isfile(self.config_path):
            log.info("There is no on-disk system configuration: %s does "
                     "not exist" % self.config_path)
            return
        if self.yes_no(
                "Replace the current configuration with the saved "
                "system configuration?", False) is not False:
            log.info("Loading %s..." % self.config_path)
            self.config.load(self.config_path, allow_new_attrs=True)
            log.info("Commiting...")
            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)
        else:
            log.info("Cancelled: configuration not modified")
Example #12
0
    def do_reload(self, options):
        '''
        reload

        Reloads the saved system configuration from disk and commits it to the
        running system.
        '''
        if not os.path.isfile(self.config_path):
            log.info("There is no on-disk system configuration: %s does "
                     "not exist" % self.config_path)
            return
        if self.yes_no("Replace the current configuration with the saved "
                       "system configuration?", False) is not False:
            log.info("Loading %s..." % self.config_path)
            self.config.load(self.config_path, allow_new_attrs=True)
            log.info("Commiting...")
            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)
        else:
            log.info("Cancelled: configuration not modified")
Example #13
0
 def __init__(self, interactive=False):
     Cli.__init__(self, interactive, self.history_path)
     self.set_prompt()
     log.info("Syncing policy and configuration...")
     self.config = Config()
     self.config.load_live()
     self.edit_levels = ['']
     self.needs_save = False
     if interactive:
         log.warning("[edit] top-level")
Example #14
0
 def __init__(self, interactive=False):
     Cli.__init__(self, interactive, self.history_path)
     self.set_prompt()
     log.info("Syncing policy and configuration...")
     self.config = Config()
     self.config.load_live()
     self.edit_levels = ['']
     self.needs_save = False
     if interactive:
         log.warning("[edit] top-level")
Example #15
0
    def do_clear(self, options):
        '''
        clear

        Clears the current configuration. This removes all current objects and
        attributes from the configuration.
        '''
        options = self.parse(options, 'clear', '')[1:]

        self.config.clear()
        log.info("Configuration cleared")
Example #16
0
    def do_clear(self, options):
        '''
        clear

        Clears the current configuration. This removes all current objects and
        attributes from the configuration.
        '''
        options = self.parse(options, 'clear', '')[1:]

        self.config.clear()
        log.info("Configuration cleared")
Example #17
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")
Example #18
0
    def do_resync(self, options=''):
        '''
        resync

        Re-synchronizes the cli with the live running configuration. This
        could be useful in rare cases where manual changes have been made to
        the underlying configfs structure for debugging purposes.
        '''
        options = self.parse(options, 'resync', '')
        log.info("Syncing policy and configuration...")
        # FIXME Investigate bug in ConfigTree code: error if loading live twice
        # without recreating the Config object.
        self.config = Config()
        self.config.load_live()
Example #19
0
    def do_initialize_system(self, options):
        '''
        initialize_system

        Loads and commits the system startup configuration if it exists.
        '''
        self.config.load(CliConfig.config_path)
        do_it = self.yes_no("Load and commit the system startup configuration?"
                            , False)
        if do_it is not False:
            log.info("Initializing LIO target...")
            for msg in self.config.apply():
                log.info(msg)
        self.config.load_live()
Example #20
0
    def do_diff(self, options):
        '''
        diff

        Shows all differences between the current configuration and the live
        running configuration.
        '''
        options = self.parse(options, 'diff', '')[1:]
        diff = self.config.diff_live()
        has_diffs = False
        if diff['removed']:
            has_diffs = True
            log.warning("Objects removed in the current configuration:")
            for node in diff['removed']:
                log.info("    %s" % node.path_str)
        if diff['created']:
            has_diffs = True
            log.warning("New objects in the current configuration:")
            for node in diff['created']:
                log.info("    %s" % node.path_str)
        if diff['major']:
            has_diffs = True
            log.warning("Major attribute changes in the current configuration:")
            for node in diff['major']:
                log.info("    %s" % node.path_str)
        if diff['minor']:
            has_diffs = True
            log.warning("Minor attribute changes in the current configuration:")
            for node in diff['minor']:
                log.info("    %s" % node.path_str)
        if not has_diffs:
            log.warning("Current configuration is in sync with live system")
        else:
            sys.stdout.write("\n")
Example #21
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")
Example #22
0
 def save_running_config(cls):
     if os.path.isfile(cls.config_path):
         # TODO remove/rotate older backups
         ts = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
         backup_path = "%s/backup-%s.lio" % (cls.backup_dir, ts)
         log.info("Performing backup of startup configuration: %s" %
                  backup_path)
         shutil.copyfile(cls.config_path, backup_path)
     log.info("Saving new startup configuration")
     # We reload the config from live before saving it, in
     # case this kernel has new attributes not yet in our
     # policy files
     config = Config()
     config.load_live()
     config.save(cls.config_path)
Example #23
0
 def save_running_config(cls):
     if os.path.isfile(cls.config_path):
         # TODO remove/rotate older backups
         ts = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
         backup_path = "%s/backup-%s.lio" % (cls.backup_dir, ts)
         log.info("Performing backup of startup configuration: %s"
                  % backup_path)
         shutil.copyfile(cls.config_path, backup_path)
     log.info("Saving new startup configuration")
     # We reload the config from live before saving it, in
     # case this kernel has new attributes not yet in our
     # policy files
     config = Config()
     config.load_live()
     config.save(cls.config_path)
Example #24
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
Example #25
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
Example #26
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))
Example #27
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))
Example #28
0
    def do_undo(self, options):
        '''
        undo

        Undo the last configuration change done during this config mode
        session. The lio cli has unlimited undo levels capabilities within a
        session.
        
        To restore a previously commited configuration, see the rollback
        command.
        '''
        options = self.parse(options, 'undo', '')[1:]
        data_src = self.config.current.data['source']
        self.config.undo()
        self.needs_save = True

        # TODO Implement info option to view all previous ops
        # TODO Implement last N option for multiple undo

        log.info("[undo] %s" % self.fmt_data_src(data_src))
Example #29
0
    def do_undo(self, options):
        '''
        undo

        Undo the last configuration change done during this config mode
        session. The lio cli has unlimited undo levels capabilities within a
        session.
        
        To restore a previously commited configuration, see the rollback
        command.
        '''
        options = self.parse(options, 'undo', '')[1:]
        data_src = self.config.current.data['source']
        self.config.undo()
        self.needs_save = True

        # TODO Implement info option to view all previous ops
        # TODO Implement last N option for multiple undo

        log.info("[undo] %s" % self.fmt_data_src(data_src))
Example #30
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")
Example #31
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")
Example #32
0
    def do_debug(self, options):
        '''
        debug [off|cli|api|all]

        Controls the debug messages level:

            off disables all debug message
            cli enables only cli debug messages
            api also enables Config API messages
            all adds even more details to api debug

        With no option, displays the current debug level.
        '''
        syntax = pp.Optional(pp.oneOf(["off", "cli", "api", "all"]))
        options = self.parse(options, 'debug', syntax)[1:]

        if not options:
            log.info("Current debug level: %s" % self.debug_level)
        else:
            self.debug_level = options[0]
            if self.debug_level == 'off':
                log.setLevel(self.log_levels['info'])
                rtslib.config.log.setLevel(self.log_levels['info'])
                rtslib.config_tree.log.setLevel(self.log_levels['info'])
            elif self.debug_level == 'cli':
                log.setLevel(self.log_levels['debug'])
                rtslib.config.log.setLevel(self.log_levels['info'])
                rtslib.config_tree.log.setLevel(self.log_levels['info'])
            elif self.debug_level == 'api':
                log.setLevel(self.log_levels['debug'])
                rtslib.config.log.setLevel(self.log_levels['debug'])
                rtslib.config_tree.log.setLevel(self.log_levels['info'])
            elif self.debug_level == 'all':
                log.setLevel(self.log_levels['debug'])
                rtslib.config.log.setLevel(self.log_levels['debug'])
                rtslib.config_tree.log.setLevel(self.log_levels['debug'])

            log.info("Debug level is now: %s" % self.debug_level)
Example #33
0
    def do_diff(self, options):
        '''
        diff

        Shows all differences between the current configuration and the live
        running configuration.
        '''
        options = self.parse(options, 'diff', '')[1:]
        diff = self.config.diff_live()
        has_diffs = False
        if diff['removed']:
            has_diffs = True
            log.warning("Objects removed in the current configuration:")
            for node in diff['removed']:
                log.info("    %s" % node.path_str)
        if diff['created']:
            has_diffs = True
            log.warning("New objects in the current configuration:")
            for node in diff['created']:
                log.info("    %s" % node.path_str)
        if diff['major']:
            has_diffs = True
            log.warning(
                "Major attribute changes in the current configuration:")
            for node in diff['major']:
                log.info("    %s" % node.path_str)
        if diff['minor']:
            has_diffs = True
            log.warning(
                "Minor attribute changes in the current configuration:")
            for node in diff['minor']:
                log.info("    %s" % node.path_str)
        if not has_diffs:
            log.warning("Current configuration is in sync with live system")
        else:
            sys.stdout.write("\n")