def main(): state = util.load_command_environment() target = state['target'] if target is None: print("Target was not provided in the environment.") sys.exit(1) fstab = state['fstab'] if fstab is None: print("/etc/fstab output was not provided in the environment.") sys.exit(1) bootmac = get_boot_mac() if bootmac is None: print("Unable to determine boot interface.") sys.exit(1) devices = get_block_devices(target) if not devices: print("Unable to find block device for: %s" % target) sys.exit(1) write_fstab(target, fstab) grub_root = get_grub_root(target) write_grub_conf(target, grub_root, extra=get_extra_kernel_parameters()) grub_install(target, grub_root) set_autorelabel(target) if state.get('config'): cfg = config.load_config(state['config']) else: cfg = {} handle_cloudconfig(cfg, target) apply_networking(cfg, target, bootmac)
def test_cmd_preserve_source(self): """check if cloud-init was prevented from overwriting""" self.output_files_exist(["curtin-preserve-sources.cfg"]) # For earlier than xenial 'apt_preserve_sources_list' is expected self.assertEqual( {'apt': {'preserve_sources_list': True}}, load_config(self.collect_path("curtin-preserve-sources.cfg")))
def from_state_file(state_file): network_state = None state = curtin_config.load_config(state_file) network_state = NetworkState() network_state.load(state) return network_state
def test_old_cloudinit_version(self, m_get_pkg_ver): """Test installed old cloud-init version.""" m_get_pkg_ver.return_value = distro.parse_dpkg_version('0.7.7-0') apt_config.apply_preserve_sources_list(self.tmp) m_get_pkg_ver.assert_has_calls( [mock.call('cloud-init', target=self.tmp)]) self.assertEqual(load_config(self.tmp_cfg), {'apt_preserve_sources_list': True})
def load_and_validate(config_path): """Load and validate storage config file.""" config = curtin_config.load_config(config_path) if 'storage' not in config: LOG.info('Skipping %s, missing "storage" key' % config_path) return return validate_config(config.get('storage'), sourcefile=config_path)
def parse_net_config(path): """Parses a curtin network configuration file and return network state""" ns = None net_config = config.load_config(path) if 'network' in net_config: ns = parse_net_config_data(net_config.get('network')) return ns
def test_new_cloudinit_version(self, m_get_pkg_ver): """Test cloud-init > 1.0 with new apt format.""" m_get_pkg_ver.return_value = distro.parse_dpkg_version('17.1-0ubuntu1') apt_config.apply_preserve_sources_list(self.tmp) m_get_pkg_ver.assert_has_calls( [mock.call('cloud-init', target=self.tmp)]) self.assertEqual(load_config(self.tmp_cfg), {'apt': { 'preserve_sources_list': True }})
def get_fs_entries(self): """Return a dictionary of fs entires in config by 'partX'.""" stgcfg = config.load_config(self.conf_file)['storage']['config'] fs_entries = {} for entry in stgcfg: if not entry['id'].startswith("fs"): continue part = "part%d" % int(entry['id'][2:]) fs_entries[part] = entry.copy() return fs_entries
def test_cloudinit_network_passthrough(self): cc_passthrough = "cloud.cfg.d/50-curtin-networking.cfg" avail_str = self.load_collect_file('cloudinit_passthrough_available') available = int(avail_str) == 1 print('avail_str=%s available=%s' % (avail_str, available)) if not available: raise SkipTest('not available on %s' % self.__class__) print('passthrough was available') pt_file = os.path.join(self.td.collect, 'etc_cloud', cc_passthrough) print('checking if passthrough file written: %s' % pt_file) self.assertTrue(os.path.exists(pt_file)) # compare original = { 'network': config.load_config(self.conf_file).get('network') } intarget = config.load_config(pt_file) self.assertEqual(original, intarget)
def apply_net(target, network_state=None, network_config=None): if network_state is None and network_config is None: raise ValueError("Must provide network_config or network_state") if target is None: raise ValueError("target cannot be None.") passthrough = False if network_state: # NB: we cannot support passthrough until curtin can convert from # network_state to network-config yaml ns = net.network_state.from_state_file(network_state) raise ValueError('Not Supported; curtin lacks a network_state to ' 'network_config converter.') elif network_config: netcfg = config.load_config(network_config) # curtin will pass-through the netconfig into the target # for rendering at runtime unless the target OS does not # support NETWORK_CONFIG_V2 feature. LOG.info( 'Checking cloud-init in target [%s] for network ' 'configuration passthrough support.', target) try: passthrough = net.netconfig_passthrough_available(target) except util.ProcessExecutionError: LOG.warning('Failed to determine if passthrough is available') if passthrough: LOG.info('Passing network configuration through to target: %s', target) net.render_netconfig_passthrough(target, netconfig=netcfg) else: ns = net.parse_net_config_data(netcfg.get('network', {})) if ns is None: return if not passthrough: LOG.info('Rendering network configuration in target') net.render_network_state(target=target, network_state=ns) _maybe_remove_legacy_eth0(target) _disable_ipv6_privacy_extensions(target) _patch_ifupdown_ipv6_mtu_hook(target)
def test_cloudinit_network_disabled(self): cc_disabled = 'cloud.cfg.d/curtin-disable-cloudinit-networking.cfg' avail_str = self.load_collect_file('cloudinit_passthrough_available') available = int(avail_str) == 1 print('avail_str=%s available=%s' % (avail_str, available)) if available: raise SkipTest('passthrough available on %s' % self.__class__) print('passthrough not available') cc_disable_file = os.path.join(self.td.collect, 'etc_cloud', cc_disabled) print('checking if network:disable file written: %s' % cc_disable_file) self.assertTrue(os.path.exists(cc_disable_file)) # compare original = {'network': {'config': 'disabled'}} intarget = config.load_config(cc_disable_file) print('checking cloud-init network-cfg content') self.assertEqual(original, intarget)
def test_preserve_source(self): """test_preserve_source - no clobbering sources.list by cloud-init""" # For earlier than xenial 'apt_preserve_sources_list' is expected self.assertEqual({'apt': { 'preserve_sources_list': True }}, load_config(self.collect_path("curtin-preserve-sources.cfg")))
with open(ns1_state, "w+") as f: f.write(ns1_dump) print("Loading state from file") ns2 = from_state_file(ns1_state) print( "NS1 == NS2 ?=> {}".format(ns1.network_state == ns2.network_state)) def test_output(network_config): (version, config) = load_config(network_config) ns1 = NetworkState(version=version, config=config) ns1.parse_config() random.shuffle(config) ns2 = NetworkState(version=version, config=config) ns2.parse_config() print( "NS1 == NS2 ?=> {}".format(ns1.network_state == ns2.network_state)) eni_1 = net.render_interfaces(ns1.network_state) eni_2 = net.render_interfaces(ns2.network_state) print(eni_1) print(eni_2) print("eni_1 == eni_2 ?=> {}".format(eni_1 == eni_2)) y = curtin_config.load_config(sys.argv[1]) network_config = y.get('network') test_parse(network_config) test_dump_and_load(network_config) test_output(network_config) # vi: ts=4 expandtab syntax=python