Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T5249: Add rollback-soft feature #2596

Merged
merged 1 commit into from
Dec 17, 2023
Merged

Conversation

sever-sever
Copy link
Member

@sever-sever sever-sever commented Dec 9, 2023

Change Summary

Add the ability to rollback configs without rebooting

rollback-soft 1

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes)
  • Migration from an old Vyatta component to vyos-1x, please link to related PR inside obsoleted component
  • Other (please describe):

Related Task(s)

Related PR(s)

vyos/vyatta-cfg#69

Component(s) name

Proposed changes

How to test

vyos@r4# rollback-soft 1

vyos@r4# sudo /usr/bin/config-mgmt rollback_soft --rev 1
[edit]
vyos@r4# compare 
+ firewall {
+     ipv4 {
+         input {
+             filter {
+                 default-action "accept"
+                 rule 10 {
+                     action "drop"
+                     inbound-interface {
+                         name "lo"
+                     }
+                 }
+             }
+         }
+     }
+ }

[edit]
vyos@r4# 


Smoketest result

Checklist:

  • I have read the CONTRIBUTING document
  • I have linked this PR to one or more Phabricator Task(s)
  • I have run the components SMOKETESTS if applicable
  • My commit headlines contain a valid Task id
  • My change requires a change to the documentation
  • I have updated the documentation accordingly

@vyosbot vyosbot requested review from a team, dmbaturin, sarthurdev and zdc and removed request for a team December 9, 2023 13:33
@vyosbot vyosbot requested review from jestabro and c-po December 9, 2023 13:33
@sever-sever sever-sever marked this pull request as draft December 9, 2023 13:33
@sever-sever sever-sever marked this pull request as ready for review December 9, 2023 15:36
@vyosbot vyosbot requested a review from a team December 9, 2023 15:36
@jestabro
Copy link
Contributor

jestabro commented Dec 11, 2023

Based on our discussions on this and the issue of migration of archived configs, the following should make a simpler interface, using either the 'legacy' loadFIle or an explicit version, as indicated by keyword arg. It needs one minor fix before PR, but I've tested with your patch here (and fix for vyos/vyatta-cfg#69) and it all looks great.

#2608

@jestabro
Copy link
Contributor

After merge of minor bug:
#2613
rebase and merge of
#2608
one can then use the following patch to simplify your work above:

From de795908c6694429e555e1cc6eb5228dbb01c1e9 Mon Sep 17 00:00:00 2001
From: John Estabrook <[email protected]>
Date: Sun, 10 Dec 2023 18:27:30 -0600
Subject: [PATCH] T5249: use load_config.load for soft rollback

---
 python/vyos/config_mgmt.py | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/python/vyos/config_mgmt.py b/python/vyos/config_mgmt.py
index 22b1998df..2de3d1831 100644
--- a/python/vyos/config_mgmt.py
+++ b/python/vyos/config_mgmt.py
@@ -30,6 +30,7 @@

 from vyos.config import Config
 from vyos.configtree import ConfigTree, ConfigTreeError, show_diff
+from vyos.load_config import load, LoadConfigError
 from vyos.defaults import directories
 from vyos.version import get_full_version_data
 from vyos.utils.io import ask_yes_no
@@ -268,20 +269,14 @@ def rollback_soft(self, rev: int):
         msg = ''

         if not self._check_revision_number(rev):
-            msg = f'Invalid revision number {rev}: must be 0 < rev < {self.max_revisions}'
+            msg = f'Invalid revision number {rev}: must be 0 < rev < {self.num_revisions}'
             return msg, 1

-        path = os.path.join(archive_dir, f'config.boot.{rev}.gz')
-        with gzip.open(path, 'rb') as f:
-            config = f.read()
-
-        # Write config to laod
-        with open(rollback_config, 'wb') as f:
-            f.write(config)
-
-        rc, out = rc_cmd(f'/bin/cli-shell-api loadFile {rollback_config}')
-        if rc != 0:
-            raise ConfigMgmtError(out)
+        rollback_ct = self._get_config_tree_revision(rev)
+        try:
+            load(rollback_ct, switch='explicit')
+        except LoadConfigError as e:
+            raise ConfigMgmtError(e) from e

         return msg, 0

@@ -486,13 +481,10 @@ def show_commit_file(self, rev: int) -> str:

     # utility functions
     #
-    @staticmethod
-    def _strip_version(s):
-        return re.split(r'(^//)', s, maxsplit=1, flags=re.MULTILINE)[0]

     def _get_saved_config_tree(self):
         with open(config_file) as f:
-            c = self._strip_version(f.read())
+            c = f.read()
         return ConfigTree(c)

     def _get_file_revision(self, rev: int):
@@ -504,7 +496,7 @@ def _get_file_revision(self, rev: int):
         return r

     def _get_config_tree_revision(self, rev: int):
-        c = self._strip_version(self._get_file_revision(rev))
+        c = self._get_file_revision(rev)
         return ConfigTree(c)

     def _add_logrotate_conf(self):
-- 
2.42.0

Add the ability to rollback configs without rebooting
```
sudo /usr/bin/config-mgmt rollback_soft --rev 1

rollback-soft 1
```
@sever-sever sever-sever merged commit fc173e3 into vyos:current Dec 17, 2023
7 checks passed
@sever-sever
Copy link
Member Author

@Mergifyio backport sagitta

Copy link
Contributor

mergify bot commented Dec 18, 2023

backport sagitta

✅ Backports have been created

dmbaturin added a commit that referenced this pull request Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants