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

try: add new 'netplan try' command #15

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b196fd9
try: implement a new 'netplan try' command
Mar 23, 2018
a50682d
tests: add tests for 'netplan try' command
Mar 26, 2018
3e70bc4
tests: move to using python3-nose for test discovery
Mar 26, 2018
33c8d53
Revert "tests: move to using python3-nose for test discovery"
Mar 26, 2018
69fb831
tests: more ConfigManager tests, to run using nose
Mar 26, 2018
bc474af
Factor out Terminal mangling to a separate class; clarify ConfigManag…
Mar 27, 2018
a488aab
terminal: add docstrings
Mar 27, 2018
0790b70
terminal: fix custom message display
Mar 27, 2018
db5e523
try: make the input timeout configurable
Mar 27, 2018
991ee0b
netplan try: disable echo again
Mar 28, 2018
560c3fa
terminal: really check for the input being ENTER
Mar 28, 2018
e8bf817
netplan try: prettify the seconds remaining
Mar 28, 2018
8197195
terminal: clarify / fixup get_confirmation_input()
Mar 29, 2018
2671125
tests: more terminal tests fixes
Apr 13, 2018
bcd4bef
tests: terminal: don't compare before/after attributes and flags in u…
Apr 14, 2018
26f65fd
apply: Make it possible to run daemon restarts synchronously
Apr 16, 2018
f704b6b
'netplan try': make sure a config is revertable before applying changes
Apr 16, 2018
745c795
utils: make both NetworkManager and networkd systemctl calls sync-cap…
Apr 16, 2018
a8576fd
'netplan try': better handle reversion when 'netplan apply' fails
Apr 16, 2018
10e4113
'netplan try': do a better job at merging and checking for unsupporte…
Apr 16, 2018
15c73f9
'netplan try': Don't need to explicitly import ConfigurationError
Apr 16, 2018
0863b9e
configmanager: more fixes to parsing/merging configs
Apr 16, 2018
3d83f90
tests: fix ConfigManager tests for the empty dict case
Apr 16, 2018
26b6bb0
'netplan try': attempt to revert newly added virtual devices
Apr 17, 2018
610b4f6
'netplan try': make sure to only try removing virtual devices
Apr 17, 2018
87dfbcd
'netplan try': linting fixes
Apr 17, 2018
27eebab
terminal: correct handling of save/restore for terminal flags/settings
Apr 17, 2018
66a7368
terminal: disable echo while we're waiting for timeout/user input
Apr 17, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -36,6 +36,7 @@ clean:
check: default linting
tests/generate.py
tests/cli.py
nosetests3 -v --with-coverage

linting:
$(PYFLAKES3) $(PYCODE)
Expand Down
1 change: 1 addition & 0 deletions debian/control
Expand Up @@ -14,6 +14,7 @@ Build-Depends: debhelper (>= 11),
systemd,
pyflakes3,
pycodestyle | pep8,
python3-nose,
pandoc,
Vcs-Git: https://github.com/CanonicalLtd/netplan
Vcs-Browser: https://github.com/CanonicalLtd/netplan
Expand Down
2 changes: 2 additions & 0 deletions netplan/cli/commands/__init__.py
Expand Up @@ -19,10 +19,12 @@
from netplan.cli.commands.generate import NetplanGenerate
from netplan.cli.commands.ip import NetplanIp
from netplan.cli.commands.migrate import NetplanMigrate
from netplan.cli.commands.try_command import NetplanTry

__all__ = [
'NetplanApply',
'NetplanGenerate',
'NetplanIp',
'NetplanMigrate',
'NetplanTry',
]
28 changes: 17 additions & 11 deletions netplan/cli/commands/apply.py
Expand Up @@ -24,6 +24,7 @@
import subprocess

import netplan.cli.utils as utils
from netplan.configmanager import ConfigurationError


class NetplanApply(utils.NetplanCommand):
Expand All @@ -34,14 +35,18 @@ def __init__(self):
leaf=True)

def run(self): # pragma: nocover (covered in autopkgtest)
self.func = self.command_apply
self.func = NetplanApply.command_apply

self.parse_args()
self.run_command()

def command_apply(self): # pragma: nocover (covered in autopkgtest)
if subprocess.call([utils.get_generator_path()]) != 0:
sys.exit(1)
@staticmethod
def command_apply(run_generate=True, sync=False, exit_on_error=True): # pragma: nocover (covered in autopkgtest)
if run_generate and subprocess.call([utils.get_generator_path()]) != 0:
if exit_on_error:
sys.exit(os.EX_CONFIG)
else:
raise ConfigurationError("the configuration could not be generated")

devices = os.listdir('/sys/class/net')

Expand All @@ -51,7 +56,7 @@ def command_apply(self): # pragma: nocover (covered in autopkgtest)
# stop backends
if restart_networkd:
logging.debug('netplan generated networkd configuration exists, restarting networkd')
subprocess.check_call(['systemctl', 'stop', '--no-block', 'systemd-networkd.service', 'netplan-wpa@*.service'])
utils.systemctl_networkd('stop', sync=sync, extra_services=['netplan-wpa@*.service'])
else:
logging.debug('no netplan generated networkd configuration exists')

Expand All @@ -66,7 +71,7 @@ def command_apply(self): # pragma: nocover (covered in autopkgtest)
except subprocess.CalledProcessError:
pass

utils.systemctl_network_manager('stop')
utils.systemctl_network_manager('stop', sync=sync)
else:
logging.debug('no netplan generated NM configuration exists')

Expand All @@ -75,7 +80,7 @@ def command_apply(self): # pragma: nocover (covered in autopkgtest)
for device in devices:
if not os.path.islink('/sys/class/net/' + device):
continue
if self.replug(device):
if NetplanApply.replug(device):
any_replug = True
else:
# if the interface is up, we can still apply .link file changes
Expand All @@ -90,12 +95,13 @@ def command_apply(self): # pragma: nocover (covered in autopkgtest)

# (re)start backends
if restart_networkd:
subprocess.check_call(['systemctl', 'start', '--no-block', 'systemd-networkd.service'] +
[os.path.basename(f) for f in glob.glob('/run/systemd/system/*.wants/netplan-wpa@*.service')])
netplan_wpa = [os.path.basename(f) for f in glob.glob('/run/systemd/system/*.wants/netplan-wpa@*.service')]
utils.systemctl_networkd('start', sync=sync, extra_services=netplan_wpa)
if restart_nm:
utils.systemctl_network_manager('start')
utils.systemctl_network_manager('start', sync=sync)

def replug(self, device): # pragma: nocover (covered in autopkgtest)
@staticmethod
def replug(device): # pragma: nocover (covered in autopkgtest)
'''Unbind and rebind device if it is down'''

devdir = os.path.join('/sys/class/net', device)
Expand Down
169 changes: 169 additions & 0 deletions netplan/cli/commands/try_command.py
@@ -0,0 +1,169 @@
#!/usr/bin/python3
#
# Copyright (C) 2018 Canonical, Ltd.
# Author: Mathieu Trudel-Lapierre <mathieu.trudel-lapierre@canonical.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

'''netplan try command line'''

import os
import time
import signal
import sys
import logging
import subprocess

from netplan.configmanager import ConfigManager
import netplan.cli.utils as utils
from netplan.cli.commands.apply import NetplanApply
import netplan.terminal

# Keep a timeout long enough to allow the network to converge, 60 seconds may
# be slightly short given some complex configs, i.e. if STP must reconverge.
DEFAULT_INPUT_TIMEOUT = 120


class NetplanTry(utils.NetplanCommand):

def __init__(self):
super().__init__(command_id='try',
description='Try to apply a new netplan config to running '
'system, with automatic rollback',
leaf=True)
self.configuration_changed = False
self.new_interfaces = None
self.config_manager = ConfigManager()

def run(self): # pragma: nocover (requires user input)
self.parser.add_argument('--config-file',
help='Apply the config file in argument in addition to current configuration.')
self.parser.add_argument('--timeout',
type=int, default=DEFAULT_INPUT_TIMEOUT,
help="Maximum number of seconds to wait for the user's confirmation")

self.func = self.command_try

self.parse_args()
self.run_command()

def command_try(self): # pragma: nocover (requires user input)
if not self.is_revertable():
sys.exit(os.EX_CONFIG)

try:
fd = sys.stdin.fileno()
t = netplan.terminal.Terminal(fd)

# we really don't want to be interrupted while doing backup/revert operations
signal.signal(signal.SIGINT, self._signal_handler)

self.backup()
self.setup()

NetplanApply.command_apply(run_generate=True, sync=True, exit_on_error=False)

t.get_confirmation_input(timeout=self.timeout)
except netplan.terminal.InputRejected:
print("\nReverting.")
self.revert()
except netplan.terminal.InputAccepted:
print("\nConfiguration accepted.")
except Exception as e:
print("\nAn error occured: %s" % e)
print("\nReverting.")
self.revert()
finally:
self.cleanup()

def backup(self): # pragma: nocover (requires user input)
backup_config_dir = False
if self.config_file:
backup_config_dir = True
self.config_manager.backup(backup_config_dir=backup_config_dir)

def setup(self): # pragma: nocover (requires user input)
if self.config_file:
dest_dir = os.path.join("/", "etc", "netplan")
dest_name = os.path.basename(self.config_file).rstrip('.yaml')
dest_suffix = time.time()
dest_path = os.path.join(dest_dir, "{}.{}.yaml".format(dest_name, dest_suffix))
self.config_manager.add({self.config_file: dest_path})
self.configuration_changed = True

def revert(self): # pragma: nocover (requires user input)
self.config_manager.revert()
NetplanApply.command_apply(run_generate=False, sync=True, exit_on_error=False)
for ifname in self.new_interfaces:
if ifname not in self.config_manager.bonds and \
ifname not in self.config_manager.bridges and \
ifname not in self.config_manager.vlans:
logging.debug("{} will not be removed: not a virtual interface".format(ifname))
continue
try:
cmd = ['ip', 'link', 'del', ifname]
subprocess.check_call(cmd)
except subprocess.CalledProcessError:
logging.warn("Could not revert (remove) new interface '{}'".format(ifname))

def cleanup(self): # pragma: nocover (requires user input)
self.config_manager.cleanup()

def is_revertable(self): # pragma: nocover (requires user input)
'''
Check if the configuration is revertable, if it doesn't contain bits
that we know are likely to render the system unstable if we apply it,
or if we revert.

Returns True if the parsed config is "revertable", meaning that we
can actually rely on backends to re-apply /all/ of the relevant
configuration to interfaces when their config changes.

Returns False if the parsed config contains options that are known
to not cleanly revert via the backend.
'''

# Parse; including any new config file passed on the command-line:
# new config might include things we can't revert.
extra_config = []
if self.config_file:
extra_config.append(self.config_file)
self.config_manager.parse(extra_config=extra_config)
self.new_interfaces = self.config_manager.new_interfaces

logging.debug("New interfaces: {}".format(self.new_interfaces))

revert_unsupported = []

# Bridges and bonds are special. They typically include (or could include)
# more than one device in them, and they can be set with special parameters
# to tweak their behavior, which are really hard to "revert", especially
# as systemd-networkd doesn't necessarily touch them when config changes.
multi_iface = {}
multi_iface.update(self.config_manager.bridges)
multi_iface.update(self.config_manager.bonds)
for ifname, settings in multi_iface.items():
if settings and 'parameters' in settings:
reason = "reverting custom parameters for bridges and bonds is not supported"
revert_unsupported.append((ifname, reason))

if revert_unsupported:
for ifname, reason in revert_unsupported:
print("{}: {}".format(ifname, reason))
print("\nPlease carefully review the configuration and use 'netplan apply' directly.")
return False
return True

def _signal_handler(self, signal, frame): # pragma: nocover (requires user input)
if self.configuration_changed:
raise netplan.terminal.InputRejected()
24 changes: 22 additions & 2 deletions netplan/cli/utils.py
Expand Up @@ -51,15 +51,35 @@ def nm_running(): # pragma: nocover (covered in autopkgtest)
return False


def systemctl_network_manager(action): # pragma: nocover (covered in autopkgtest)
def systemctl_network_manager(action, sync=False): # pragma: nocover (covered in autopkgtest)
service_name = NM_SERVICE_NAME

command = ['systemctl', action]
if not sync:
command.append('--no-block')

# If the network-manager snap is installed use its service
# name rather than the one of the deb packaged NetworkManager
if is_nm_snap_enabled():
service_name = NM_SNAP_SERVICE_NAME

subprocess.check_call(['systemctl', action, '--no-block', service_name])
command.append(service_name)

subprocess.check_call(command)


def systemctl_networkd(action, sync=False, extra_services=[]): # pragma: nocover (covered in autopkgtest)
command = ['systemctl', action]

if not sync:
command.append('--no-block')

command.append('systemd-networkd.service')

for service in extra_services:
command.append(service)

subprocess.check_call(command)


class NetplanCommand(argparse.Namespace):
Expand Down