Support different output formats + metadata cleaning
This commit is contained in:
parent
8a1d7e0d46
commit
d67aadd038
38
genconfdrv
38
genconfdrv
|
@ -25,6 +25,7 @@ class ConfigDrive:
|
||||||
self._interfaces = []
|
self._interfaces = []
|
||||||
self._pubkeys = []
|
self._pubkeys = []
|
||||||
self._verbose = verbose
|
self._verbose = verbose
|
||||||
|
self._clean_metadata = False
|
||||||
|
|
||||||
self._added_resolv_module_call = False
|
self._added_resolv_module_call = False
|
||||||
|
|
||||||
|
@ -92,6 +93,9 @@ class ConfigDrive:
|
||||||
self.add_command("cloud-init single --name cc_resolv_conf", True)
|
self.add_command("cloud-init single --name cc_resolv_conf", True)
|
||||||
self._added_resolv_module_call = True
|
self._added_resolv_module_call = True
|
||||||
|
|
||||||
|
def set_clean_metadata(self, do_clean_metadata):
|
||||||
|
self._clean_metadata = do_clean_metadata
|
||||||
|
|
||||||
def add_user(self, name, keys=None, gecos=None, sudo=False, password=None):
|
def add_user(self, name, keys=None, gecos=None, sudo=False, password=None):
|
||||||
if "users" not in self._user_data:
|
if "users" not in self._user_data:
|
||||||
self._user_data["users"] = []
|
self._user_data["users"] = []
|
||||||
|
@ -168,8 +172,10 @@ class ConfigDrive:
|
||||||
meta_data["files"].append({"content_path": "/content/0001", "path": "/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg"})
|
meta_data["files"].append({"content_path": "/content/0001", "path": "/etc/cloud/cloud.cfg.d/99-disable-network-config.cfg"})
|
||||||
self.add_text("/openstack/content/0001", "network: {config: disabled}")
|
self.add_text("/openstack/content/0001", "network: {config: disabled}")
|
||||||
|
|
||||||
#meta_data["files"].append({"content_path": "/content/0002", "path": "/etc/cloud/cloud.cfg.d/10-resolv-conf.cfg"})
|
# do not look for datasource on every boot
|
||||||
#self.add_text("/openstack/content/0002", "cloud_init_modules:\n - resolv-conf\n")
|
if self._clean_metadata:
|
||||||
|
meta_data["files"].append({"content_path": "/content/0002", "path": "/etc/cloud/cloud.cfg.d/99-manual-cache-clean.cfg"})
|
||||||
|
self.add_text("/openstack/content/0002", "manual_cache_clean: True")
|
||||||
|
|
||||||
if self._pubkeys:
|
if self._pubkeys:
|
||||||
meta_data["public_keys"] = {}
|
meta_data["public_keys"] = {}
|
||||||
|
@ -204,10 +210,18 @@ class ConfigDrive:
|
||||||
def _write_userdata(self):
|
def _write_userdata(self):
|
||||||
self.add_text("/openstack/latest/user_data", "#cloud-config\n" + json.dumps(self._user_data, indent=4))
|
self.add_text("/openstack/latest/user_data", "#cloud-config\n" + json.dumps(self._user_data, indent=4))
|
||||||
|
|
||||||
def write_iso(self, path):
|
def write_drive(self, path, fmt):
|
||||||
self._write_metadata()
|
self._write_metadata()
|
||||||
self._write_userdata()
|
self._write_userdata()
|
||||||
|
|
||||||
|
if fmt == "iso":
|
||||||
|
self._write_iso(path)
|
||||||
|
elif fmt == "tgz":
|
||||||
|
self._write_tgz(path)
|
||||||
|
else:
|
||||||
|
raise ValueError("Unknown format")
|
||||||
|
|
||||||
|
def _write_iso(self, path):
|
||||||
p = subprocess.Popen([self._genisoimage,
|
p = subprocess.Popen([self._genisoimage,
|
||||||
"-J", "-r", "-q",
|
"-J", "-r", "-q",
|
||||||
"-V", "config-2",
|
"-V", "config-2",
|
||||||
|
@ -220,6 +234,10 @@ class ConfigDrive:
|
||||||
|
|
||||||
return p.wait()
|
return p.wait()
|
||||||
|
|
||||||
|
def _write_tgz(self, path):
|
||||||
|
p = subprocess.Popen(["tar", "cfz", path, "-C", self._tmpfs.getsyspath(""), "."])
|
||||||
|
return p.wait()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self._tmpfs:
|
if self._tmpfs:
|
||||||
self._tmpfs.close()
|
self._tmpfs.close()
|
||||||
|
@ -254,6 +272,14 @@ def main():
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if not args.format:
|
||||||
|
if args.output.endswith(".tar.gz") or args.output.endswith(".tgz"):
|
||||||
|
args.format = "tgz"
|
||||||
|
elif args.output.endswith(".iso"):
|
||||||
|
args.format = "iso"
|
||||||
|
else:
|
||||||
|
parser.error("Could not infer output format from output file extension")
|
||||||
|
|
||||||
cfgdrv = None
|
cfgdrv = None
|
||||||
try:
|
try:
|
||||||
cfgdrv = ConfigDrive(verbose=args.verbose)
|
cfgdrv = ConfigDrive(verbose=args.verbose)
|
||||||
|
@ -261,9 +287,6 @@ def main():
|
||||||
cfgdrv.set_hostname(args.hostname)
|
cfgdrv.set_hostname(args.hostname)
|
||||||
|
|
||||||
for net in args.networks:
|
for net in args.networks:
|
||||||
if ";" in net:
|
|
||||||
net = net.split(";")
|
|
||||||
else:
|
|
||||||
net = net.split(":")
|
net = net.split(":")
|
||||||
cfgdrv.conf_network(*net)
|
cfgdrv.conf_network(*net)
|
||||||
|
|
||||||
|
@ -311,8 +334,7 @@ def main():
|
||||||
cfgdrv.add_user(user[0], keys, sudo=sudo, gecos=gecos, password=password)
|
cfgdrv.add_user(user[0], keys, sudo=sudo, gecos=gecos, password=password)
|
||||||
|
|
||||||
if args.output:
|
if args.output:
|
||||||
ret = cfgdrv.write_iso(args.output)
|
cfgdrv.write_iso(args.output)
|
||||||
sys.exit(ret)
|
|
||||||
finally:
|
finally:
|
||||||
if cfgdrv:
|
if cfgdrv:
|
||||||
cfgdrv.close()
|
cfgdrv.close()
|
||||||
|
|
Loading…
Reference in New Issue