def test_persist_instance_data_writes_canonical_cloud_id_and_symlink(self): """canonical-cloud-id class attribute is set, persist to json.""" tmp = self.tmp_dir() datasource = DataSourceTestSubclassNet(self.sys_cfg, self.distro, Paths({"run_dir": tmp})) cloud_id_link = os.path.join(tmp, "cloud-id") cloud_id_file = os.path.join(tmp, "cloud-id-my-cloud") cloud_id2_file = os.path.join(tmp, "cloud-id-my-cloud2") for filename in (cloud_id_file, cloud_id_link, cloud_id2_file): self.assertFalse(os.path.exists(filename), "Unexpected link found {filename}") with mock.patch("cloudinit.sources.canonical_cloud_id", return_value="my-cloud"): datasource.get_data() self.assertEqual("my-cloud\n", util.load_file(cloud_id_link)) # A symlink with the generic /run/cloud-init/cloud-id link is present self.assertTrue(util.is_link(cloud_id_link)) # When cloud-id changes, symlink and content change with mock.patch("cloudinit.sources.canonical_cloud_id", return_value="my-cloud2"): datasource.persist_instance_data() self.assertEqual("my-cloud2\n", util.load_file(cloud_id2_file)) # Previous cloud-id-<cloud-type> file removed self.assertFalse(os.path.exists(cloud_id_file)) # Generic link persisted which contains canonical-cloud-id as content self.assertTrue(util.is_link(cloud_id_link)) self.assertEqual("my-cloud2\n", util.load_file(cloud_id_link))
def remove_artifacts(remove_logs, remove_seed=False): """Helper which removes artifacts dir and optionally log files. @param: remove_logs: Boolean. Set True to delete the cloud_dir path. False preserves them. @param: remove_seed: Boolean. Set True to also delete seed subdir in paths.cloud_dir. @returns: 0 on success, 1 otherwise. """ init = Init(ds_deps=[]) init.read_cfg() if remove_logs: for log_file in get_config_logfiles(init.cfg): del_file(log_file) if not os.path.isdir(init.paths.cloud_dir): return 0 # Artifacts dir already cleaned seed_path = os.path.join(init.paths.cloud_dir, 'seed') for path in glob.glob('%s/*' % init.paths.cloud_dir): if path == seed_path and not remove_seed: continue try: if os.path.isdir(path) and not is_link(path): del_dir(path) else: del_file(path) except OSError as e: error('Could not remove {0}: {1}'.format(path, str(e))) return 1 return 0
def remove_artifacts(remove_logs, remove_seed=False): """Helper which removes artifacts dir and optionally log files. @param: remove_logs: Boolean. Set True to delete the cloud_dir path. False preserves them. @param: remove_seed: Boolean. Set True to also delete seed subdir in paths.cloud_dir. @returns: 0 on success, 1 otherwise. """ init = Init(ds_deps=[]) init.read_cfg() if remove_logs: for log_file in get_config_logfiles(init.cfg): del_file(log_file) if not os.path.isdir(init.paths.cloud_dir): return 0 # Artifacts dir already cleaned seed_path = os.path.join(init.paths.cloud_dir, "seed") for path in glob.glob("%s/*" % init.paths.cloud_dir): if path == seed_path and not remove_seed: continue try: if os.path.isdir(path) and not is_link(path): del_dir(path) else: del_file(path) except OSError as e: error("Could not remove {0}: {1}".format(path, str(e))) return 1 return 0
def _create_network_symlink(interface_name): file_path = '/etc/init.d/net.{name}'.format(name=interface_name) if not util.is_link(file_path): util.sym_link('/etc/init.d/net.lo', file_path)