diff options
Diffstat (limited to 'src')
| -rwxr-xr-x | src/conf_mode/system_config-management.py | 20 | ||||
| -rwxr-xr-x | src/helpers/commit-confirm-notify.py | 48 | 
2 files changed, 52 insertions, 16 deletions
| diff --git a/src/conf_mode/system_config-management.py b/src/conf_mode/system_config-management.py index c681a8405..8de4e5342 100755 --- a/src/conf_mode/system_config-management.py +++ b/src/conf_mode/system_config-management.py @@ -22,6 +22,7 @@ from vyos.config import Config  from vyos.config_mgmt import ConfigMgmt  from vyos.config_mgmt import commit_post_hook_dir, commit_hooks +  def get_config(config=None):      if config:          conf = config @@ -36,22 +37,29 @@ def get_config(config=None):      return mgmt -def verify(_mgmt): + +def verify(mgmt): +    d = mgmt.config_dict +    confirm = d.get('commit_confirm', {}) +    if confirm.get('action', '') == 'reload' and 'commit_revisions' not in d: +        raise ConfigError('commit-confirm reload requires non-zero commit-revisions') +      return +  def generate(mgmt):      if mgmt is None:          return      mgmt.initialize_revision() +  def apply(mgmt):      if mgmt is None:          return      locations = mgmt.locations -    archive_target = os.path.join(commit_post_hook_dir, -                               commit_hooks['commit_archive']) +    archive_target = os.path.join(commit_post_hook_dir, commit_hooks['commit_archive'])      if locations:          try:              os.symlink('/usr/bin/config-mgmt', archive_target) @@ -68,8 +76,9 @@ def apply(mgmt):              raise ConfigError from exc      revisions = mgmt.max_revisions -    revision_target = os.path.join(commit_post_hook_dir, -                               commit_hooks['commit_revision']) +    revision_target = os.path.join( +        commit_post_hook_dir, commit_hooks['commit_revision'] +    )      if revisions > 0:          try:              os.symlink('/usr/bin/config-mgmt', revision_target) @@ -85,6 +94,7 @@ def apply(mgmt):          except OSError as exc:              raise ConfigError from exc +  if __name__ == '__main__':      try:          c = get_config() diff --git a/src/helpers/commit-confirm-notify.py b/src/helpers/commit-confirm-notify.py index 8d7626c78..af6167651 100755 --- a/src/helpers/commit-confirm-notify.py +++ b/src/helpers/commit-confirm-notify.py @@ -2,30 +2,56 @@  import os  import sys  import time +from argparse import ArgumentParser  # Minutes before reboot to trigger notification.  intervals = [1, 5, 15, 60] -def notify(interval): -    s = "" if interval == 1 else "s" +parser = ArgumentParser() +parser.add_argument( +    'minutes', type=int, help='minutes before rollback to trigger notification' +) +parser.add_argument( +    '--reboot', action='store_true', help="use 'soft' rollback instead of reboot" +) + + +def notify(interval, reboot=False): +    s = '' if interval == 1 else 's'      time.sleep((minutes - interval) * 60) -    message = ('"[commit-confirm] System is going to reboot in ' -               f'{interval} minute{s} to rollback the last commit.\n' -               'Confirm your changes to cancel the reboot."') -    os.system("wall -n " + message) +    if reboot: +        message = ( +            '"[commit-confirm] System will reboot in ' +            f'{interval} minute{s}\nto rollback the last commit.\n' +            'Confirm your changes to cancel the reboot."' +        ) +        os.system('wall -n ' + message) +    else: +        message = ( +            '"[commit-confirm] System will reload previous config in ' +            f'{interval} minute{s}\nto rollback the last commit.\n' +            'Confirm your changes to cancel the reload."' +        ) +        os.system('wall -n ' + message) + -if __name__ == "__main__": +if __name__ == '__main__':      # Must be run as root to call wall(1) without a banner. -    if len(sys.argv) != 2 or os.getuid() != 0: +    if os.getuid() != 0:          print('This script requires superuser privileges.', file=sys.stderr)          exit(1) -    minutes = int(sys.argv[1]) + +    args = parser.parse_args() + +    minutes = args.minutes +    reboot = args.reboot +      # Drop the argument from the list so that the notification      # doesn't kick in immediately.      if minutes in intervals:          intervals.remove(minutes)      for interval in sorted(intervals, reverse=True):          if minutes >= interval: -            notify(interval) -            minutes -= (minutes - interval) +            notify(interval, reboot=reboot) +            minutes -= minutes - interval      exit(0) | 
