Esempio n. 1
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")
Esempio n. 2
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")
Esempio n. 3
0
    def do_exit(self, options):
        '''
        exit [now]

        Exits the current configuration edit level, and goes back to the
        previous edit level. If run on the top-level configuration, then exits
        config mode.

        If the now option is provided, no confirmation will be asked if there
        are uncommitted changes in the current candidate configuration when
        exiting the config mode.
        '''
        options = self.parse(options, 'exit', pp.Optional('now'))[1:]

        if self.edit_levels[-1]:
            self.del_edit_level()
            exit = False
        elif self.needs_commit:
            log.warning("[edit] All non-commited changes will be lost!")
            if 'now' in options:
                log.warning("[edit] exiting anyway, as requested")
                exit = True
            else:
                exit = self.yes_no("Exit config mode anyway?", False)
        else:
            exit = True
        return exit
Esempio n. 4
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")
Esempio n. 5
0
    def do_exit(self, options):
        '''
        exit [now]

        Exits the current configuration edit level, and goes back to the
        previous edit level. If run on the top-level configuration, then exits
        config mode.

        If the now option is provided, no confirmation will be asked if there
        are uncommitted changes in the current candidate configuration when
        exiting the config mode.
        '''
        options = self.parse(options, 'exit', pp.Optional('now'))[1:]

        if self.edit_levels[-1]:
            self.del_edit_level()
            exit = False
        elif self.needs_commit:
            log.warning("[edit] All non-commited changes will be lost!")
            if 'now' in options:
                log.warning("[edit] exiting anyway, as requested")
                exit = True
            else:
                exit = self.yes_no("Exit config mode anyway?", False)
        else:
            exit = True
        return exit
Esempio n. 6
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")
Esempio n. 7
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")
Esempio n. 8
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])
Esempio n. 9
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")
Esempio n. 10
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])
Esempio n. 11
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")
Esempio n. 12
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")
Esempio n. 13
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")
Esempio n. 14
0
    def do_rollback(self, options):
        '''
        rollback

        Return to the last committed configuration. Only the current
        configuration is affected. The commit command can then be used to apply
        the rolled-back configuration to the running system.
        '''
        # TODO Add more control to directly rollback the n-th version, view
        # backup infos before rollback, etc.
        backups = sorted(n for n in os.listdir(self.backup_dir)
                         if n.endswith(".lio"))
        if not backups:
            raise ConfigError("No backup found")
        else:
            backup_path = "%s/%s" % (self.backup_dir, backups[-1])
            self.config.load(backup_path)
            os.remove(backup_path)
            log.warning("Rolled-back to %s" % backup_path)
Esempio n. 15
0
    def do_rollback(self, options):
        '''
        rollback

        Return to the last committed configuration. Only the current
        configuration is affected. The commit command can then be used to apply
        the rolled-back configuration to the running system.
        '''
        # TODO Add more control to directly rollback the n-th version, view
        # backup infos before rollback, etc.
        backups = sorted(n for n in os.listdir(self.backup_dir)
                         if n.endswith(".lio"))
        if not backups:
            raise ConfigError("No backup found")
        else:
            backup_path = "%s/%s" % (self.backup_dir, backups[-1])
            self.config.load(backup_path)
            os.remove(backup_path)
            log.warning("Rolled-back to %s" % backup_path)
Esempio n. 16
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")
Esempio n. 17
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")
Esempio n. 18
0
 def add_edit_level(self, path):
     self.edit_levels.append(path)
     log.warning("[edit] %s" % self.edit_levels[-1])
     self.set_prompt(self.edit_levels[-1])
Esempio n. 19
0
 def add_edit_level(self, path):
     self.edit_levels.append(path)
     log.warning("[edit] %s" % self.edit_levels[-1])
     self.set_prompt(self.edit_levels[-1])