def network_finish(self, config): log.debug("network config: \n%s", yaml.dump(sanitize_config(config), default_flow_style=False)) netplan_path = self.netplan_path while True: try: tmppath = '%s.%s' % (netplan_path, random.randrange(0, 1000)) fd = os.open(tmppath, os.O_WRONLY | os.O_EXCL | os.O_CREAT, 0o0600) except FileExistsError: continue else: break w = os.fdopen(fd, 'w') with w: w.write("# This is the network config written by '{}'\n".format( self.opts.project)) w.write(yaml.dump(config)) os.rename(tmppath, netplan_path) self.model.parse_netplan_configs(self.root) if self.opts.dry_run: tasks = [ ('one', BackgroundProcess(['sleep', '0.1'])), ('two', PythonSleep(0.1)), ('three', BackgroundProcess(['sleep', '0.1'])), ] if os.path.exists('/lib/netplan/generate'): # If netplan appears to be installed, run generate to at # least test that what we wrote is acceptable to netplan. tasks.append(('generate', BackgroundProcess( ['netplan', 'generate', '--root', self.root]))) if not self.tried_once: tasks.append( ('timeout', WaitForDefaultRouteTask(3, self.network_event_receiver))) tasks.append(('fail', BackgroundProcess(['false']))) self.tried_once = True else: tasks = [ ('generate', BackgroundProcess(['/lib/netplan/generate'])), ('apply', BackgroundProcess(['netplan', 'apply'])), ('timeout', WaitForDefaultRouteTask(30, self.network_event_receiver)), ] def cancel(): self.cs.cancel() self.task_error('canceled') self.acw = ApplyingConfigWidget(len(tasks), cancel) self.ui.frame.body.show_overlay(self.acw, min_width=60) self.cs = TaskSequence(self.run_in_bg, tasks, self) self.cs.run()
def _write_config(self): config = self.model.render_config() log.debug("network config: \n%s", yaml.dump(sanitize_config(config), default_flow_style=False)) for p in netplan.configs_in_root(self.root, masked=True): if p == self.netplan_path: continue os.rename(p, p + ".dist-" + self.opts.project) write_file(self.netplan_path, self.model.stringify_config(config), omode="w") self.parse_netplan_configs()
def _write_config(self): config = self.model.render() log.debug("network config: \n%s", yaml.dump(sanitize_config(config), default_flow_style=False)) for p in netplan.configs_in_root(self.root, masked=True): if p == self.netplan_path: continue os.rename(p, p + ".dist-" + self.opts.project) write_file(self.netplan_path, '\n'.join(( ("# This is the network config written by '%s'" % self.opts.project), yaml.dump(config, default_flow_style=False))), omode="w") self.model.parse_netplan_configs(self.root)
def network_finish(self, config): log.debug("network config: \n%s", yaml.dump(sanitize_config(config), default_flow_style=False)) for p in netplan.configs_in_root(self.root, masked=True): if p == self.netplan_path: continue os.rename(p, p + ".dist-" + self.opts.project) write_file(self.netplan_path, '\n'.join(( ("# This is the network config written by '%s'" % self.opts.project), yaml.dump(config))), omode="w") self.model.parse_netplan_configs(self.root) if self.opts.dry_run: tasks = [ ('one', BackgroundProcess(['sleep', '0.1'])), ('two', PythonSleep(0.1)), ('three', BackgroundProcess(['sleep', '0.1'])), ] if os.path.exists('/lib/netplan/generate'): # If netplan appears to be installed, run generate to at # least test that what we wrote is acceptable to netplan. tasks.append(('generate', BackgroundProcess(['netplan', 'generate', '--root', self.root]))) if not self.tried_once: tasks.append( ('timeout', WaitForDefaultRouteTask(3, self.network_event_receiver)) ) tasks.append(('fail', BackgroundProcess(['false']))) self.tried_once = True else: devs_to_down = [] for dev in self.model.get_all_netdevs(): devcfg = self.model.config.config_for_device(dev._net_info) if dev._configuration != devcfg: devs_to_down.append(dev) tasks = [] if devs_to_down: tasks.extend([ ('stop-networkd', BackgroundProcess(['systemctl', 'stop', 'systemd-networkd.service'])), ('down', DownNetworkDevices(self.observer.rtlistener, devs_to_down)), ]) tasks.extend([ ('apply', BackgroundProcess(['netplan', 'apply'])), ('timeout', WaitForDefaultRouteTask(30, self.network_event_receiver)), ]) def cancel(): self.cs.cancel() self.task_error('canceled') self.acw = ApplyingConfigWidget(len(tasks), cancel) self.ui.frame.body.show_overlay(self.acw, min_width=60) self.cs = TaskSequence(self.run_in_bg, tasks, self) self.cs.run()
def apply_config(self, silent=False): log.debug("apply_config silent=%s", silent) if self.dhcp_check_handle is not None: self.loop.remove_alarm(self.dhcp_check_handle) self.dhcp_check_handle = None config = self.model.render() devs_to_delete = [] devs_to_down = [] dhcp_device_versions = [] for dev in self.model.get_all_netdevs(include_deleted=True): for v in 4, 6: if dev.dhcp_enabled(v): if not silent: dev.set_dhcp_state(v, "PENDING") self.network_event_receiver.update_link(dev.ifindex) else: dev.set_dhcp_state(v, "RECONFIGURE") dhcp_device_versions.append((dev, v)) if dev.info is None: continue if dev.is_virtual: devs_to_delete.append(dev) continue if dev.config != self.model.config.config_for_device(dev.info): devs_to_down.append(dev) log.debug("network config: \n%s", yaml.dump(sanitize_config(config), default_flow_style=False)) for p in netplan.configs_in_root(self.root, masked=True): if p == self.netplan_path: continue os.rename(p, p + ".dist-" + self.opts.project) write_file(self.netplan_path, '\n'.join((("# This is the network config written by '%s'" % self.opts.project), yaml.dump(config, default_flow_style=False))), omode="w") self.model.parse_netplan_configs(self.root) if self.opts.dry_run: delay = 0.1 / self.app.scale_factor tasks = [ ('one', BackgroundProcess(['sleep', str(delay)])), ('two', PythonSleep(delay)), ('three', BackgroundProcess(['sleep', str(delay)])), ] if os.path.exists('/lib/netplan/generate'): # If netplan appears to be installed, run generate to at # least test that what we wrote is acceptable to netplan. tasks.append(('generate', BackgroundProcess( ['netplan', 'generate', '--root', self.root]))) else: tasks = [] if devs_to_down or devs_to_delete: tasks.extend([ ('stop-networkd', BackgroundProcess( ['systemctl', 'stop', 'systemd-networkd.service'])), ('down', DownNetworkDevices(self.observer.rtlistener, devs_to_down, devs_to_delete)), ]) tasks.extend([ ('apply', BackgroundProcess(['netplan', 'apply'])), ]) if not silent: self.view.show_apply_spinner() ts = TaskSequence(self.run_in_bg, tasks, ApplyWatcher(self.view, self)) ts.run() if dhcp_device_versions: self.dhcp_check_handle = self.loop.set_alarm_in( 10, lambda loop, ud: self.check_dchp_results(ud), dhcp_device_versions)