cloudinit: Rework file building

* Ensure files are cleaned up if we fail mid run
* Ensure temp user-data and meta-data files are cleaned up
* Move dest file naming into cloudinit.py

Signed-off-by: Cole Robinson <crobinso@redhat.com>
This commit is contained in:
Cole Robinson 2019-11-22 22:57:43 -05:00
parent ed6436e455
commit a67237b221
2 changed files with 31 additions and 27 deletions

View File

@ -1,7 +1,9 @@
import tempfile
import os
import random
import string
import re
import string
import tempfile
from ..logger import log
@ -38,21 +40,13 @@ class CloudInitData():
return self._get_password(self.ssh_key)
def create_metadata(scratchdir, cloudinit_data):
def _create_metadata_content(cloudinit_data):
content = ""
if cloudinit_data.meta_data:
log.debug("Using meta-data content from path=%s",
cloudinit_data.meta_data)
content = open(cloudinit_data.meta_data).read()
fileobj = tempfile.NamedTemporaryFile(
prefix="virtinst-", suffix="-metadata",
dir=scratchdir, delete=False)
filename = fileobj.name
with open(filename, "w") as f:
f.write(content)
return filename
return content
def _create_userdata_content(cloudinit_data):
@ -90,14 +84,24 @@ def _create_userdata_content(cloudinit_data):
return content
def create_userdata(scratchdir, cloudinit_data):
content = _create_userdata_content(cloudinit_data)
def create_files(scratchdir, cloudinit_data):
metadata = _create_metadata_content(cloudinit_data)
userdata = _create_userdata_content(cloudinit_data)
fileobj = tempfile.NamedTemporaryFile(
prefix="virtinst-", suffix="-userdata",
dir=scratchdir, delete=False)
filename = fileobj.name
filepairs = []
try:
for content, destfile in [(metadata, "meta-data"),
(userdata, "user-data")]:
fileobj = tempfile.NamedTemporaryFile(
prefix="virtinst-", suffix=("-%s" % destfile),
dir=scratchdir, delete=False)
filename = fileobj.name
filepairs.append((filename, destfile))
with open(filename, "w+") as f:
f.write(content)
return filename
with open(filename, "w+") as f:
f.write(content)
except Exception: # pragma: no cover
for filepair in filepairs:
os.unlink(filepair[0])
return filepairs

View File

@ -8,6 +8,7 @@
import os
from . import cloudinit
from . import unattended
from .installertreemedia import InstallerTreeMedia
from .installerinject import perform_cdrom_injections
@ -16,7 +17,6 @@ from ..devices import DeviceDisk
from ..osdict import OSDB
from ..logger import log
from .. import progress
from .cloudinit import create_metadata, create_userdata
def _make_testsuite_path(path):
@ -423,14 +423,14 @@ class Installer(object):
self._cloudinit_data = cloudinit_data
def _install_cloudinit(self, guest):
metadata = create_metadata(guest.conn.get_app_cache_dir(),
self._cloudinit_data)
userdata = create_userdata(guest.conn.get_app_cache_dir(),
filepairs = cloudinit.create_files(
guest.conn.get_app_cache_dir(),
self._cloudinit_data)
for filepair in filepairs:
self._tmpfiles.append(filepair[0])
iso = perform_cdrom_injections(
[(metadata, "meta-data"), (userdata, "user-data")],
guest.conn.get_app_cache_dir(), cloudinit=True)
filepairs, guest.conn.get_app_cache_dir(), cloudinit=True)
self._tmpfiles.append(iso)
self._add_unattended_install_cdrom_device(guest, iso)