use only util methods for reading/loading/appending/peeking
Use only util methods for reading/loading/appending/peeking at files since it is likely soon that we will add a new way of adjusting the root of files read, also it is useful for debugging to track what is being read/written in a central fashion.
This commit is contained in:
commit
47c95ed210
|
@ -335,8 +335,7 @@ class Distro(object):
|
||||||
util.write_file(sudo_file, content, 0440)
|
util.write_file(sudo_file, content, 0440)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
with open(sudo_file, 'a') as f:
|
util.append_file(sudo_file, content)
|
||||||
f.write(content)
|
|
||||||
except IOError as e:
|
except IOError as e:
|
||||||
util.logexc(LOG, "Failed to write %s" % sudo_file, e)
|
util.logexc(LOG, "Failed to write %s" % sudo_file, e)
|
||||||
raise e
|
raise e
|
||||||
|
|
|
@ -73,13 +73,11 @@ def read_user_data_callback(mount_dir):
|
||||||
|
|
||||||
# First try deltacloud_user_data_file. On failure try user_data_file.
|
# First try deltacloud_user_data_file. On failure try user_data_file.
|
||||||
try:
|
try:
|
||||||
with open(deltacloud_user_data_file, 'r') as user_data_f:
|
user_data = util.load_file(deltacloud_user_data_file).strip()
|
||||||
user_data = user_data_f.read().strip()
|
except IOError:
|
||||||
except:
|
|
||||||
try:
|
try:
|
||||||
with open(user_data_file, 'r') as user_data_f:
|
user_data = util.load_file(user_data_file).strip()
|
||||||
user_data = user_data_f.read().strip()
|
except IOError:
|
||||||
except:
|
|
||||||
util.logexc(LOG, ('Failed accessing user data file.'))
|
util.logexc(LOG, ('Failed accessing user data file.'))
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@ -157,11 +155,10 @@ class DataSourceAltCloud(sources.DataSource):
|
||||||
|
|
||||||
if os.path.exists(CLOUD_INFO_FILE):
|
if os.path.exists(CLOUD_INFO_FILE):
|
||||||
try:
|
try:
|
||||||
cloud_info = open(CLOUD_INFO_FILE)
|
cloud_type = util.load_file(CLOUD_INFO_FILE).strip().upper()
|
||||||
cloud_type = cloud_info.read().strip().upper()
|
except IOError:
|
||||||
cloud_info.close()
|
util.logexc(LOG, 'Unable to access cloud info file at %s.',
|
||||||
except:
|
CLOUD_INFO_FILE)
|
||||||
util.logexc(LOG, 'Unable to access cloud info file.')
|
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
cloud_type = self.get_cloud_type()
|
cloud_type = self.get_cloud_type()
|
||||||
|
|
|
@ -307,19 +307,19 @@ def read_config_drive_dir_v2(source_dir, version="2012-08-10"):
|
||||||
found = False
|
found = False
|
||||||
if os.path.isfile(fpath):
|
if os.path.isfile(fpath):
|
||||||
try:
|
try:
|
||||||
with open(fpath) as fp:
|
data = util.load_file(fpath)
|
||||||
data = fp.read()
|
except IOError:
|
||||||
except Exception as exc:
|
raise BrokenConfigDriveDir("Failed to read: %s" % fpath)
|
||||||
raise BrokenConfigDriveDir("failed to read: %s" % fpath)
|
|
||||||
found = True
|
found = True
|
||||||
elif required:
|
elif required:
|
||||||
raise NonConfigDriveDir("missing mandatory %s" % fpath)
|
raise NonConfigDriveDir("Missing mandatory path: %s" % fpath)
|
||||||
|
|
||||||
if found and process:
|
if found and process:
|
||||||
try:
|
try:
|
||||||
data = process(data)
|
data = process(data)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise BrokenConfigDriveDir("failed to process: %s" % fpath)
|
raise BrokenConfigDriveDir(("Failed to process "
|
||||||
|
"path: %s") % fpath)
|
||||||
|
|
||||||
if found:
|
if found:
|
||||||
results[name] = data
|
results[name] = data
|
||||||
|
@ -335,8 +335,7 @@ def read_config_drive_dir_v2(source_dir, version="2012-08-10"):
|
||||||
# do not use os.path.join here, as content_path starts with /
|
# do not use os.path.join here, as content_path starts with /
|
||||||
cpath = os.path.sep.join((source_dir, "openstack",
|
cpath = os.path.sep.join((source_dir, "openstack",
|
||||||
"./%s" % item['content_path']))
|
"./%s" % item['content_path']))
|
||||||
with open(cpath) as fp:
|
return util.load_file(cpath)
|
||||||
return(fp.read())
|
|
||||||
|
|
||||||
files = {}
|
files = {}
|
||||||
try:
|
try:
|
||||||
|
@ -350,7 +349,7 @@ def read_config_drive_dir_v2(source_dir, version="2012-08-10"):
|
||||||
if item:
|
if item:
|
||||||
results['network_config'] = read_content_path(item)
|
results['network_config'] = read_content_path(item)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
raise BrokenConfigDriveDir("failed to read file %s: %s" % (item, exc))
|
raise BrokenConfigDriveDir("Failed to read file %s: %s" % (item, exc))
|
||||||
|
|
||||||
# to openstack, user can specify meta ('nova boot --meta=key=value') and
|
# to openstack, user can specify meta ('nova boot --meta=key=value') and
|
||||||
# those will appear under metadata['meta'].
|
# those will appear under metadata['meta'].
|
||||||
|
@ -465,8 +464,7 @@ def get_previous_iid(paths):
|
||||||
# hasn't declared itself found.
|
# hasn't declared itself found.
|
||||||
fname = os.path.join(paths.get_cpath('data'), 'instance-id')
|
fname = os.path.join(paths.get_cpath('data'), 'instance-id')
|
||||||
try:
|
try:
|
||||||
with open(fname) as fp:
|
return util.load_file(fname)
|
||||||
return fp.read()
|
|
||||||
except IOError:
|
except IOError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
@ -336,8 +336,7 @@ if __name__ == "__main__":
|
||||||
'token_secret': args.tsec, 'consumer_secret': args.csec}
|
'token_secret': args.tsec, 'consumer_secret': args.csec}
|
||||||
|
|
||||||
if args.config:
|
if args.config:
|
||||||
with open(args.config) as fp:
|
cfg = util.read_conf(args.config)
|
||||||
cfg = util.load_yaml(fp.read())
|
|
||||||
if 'datasource' in cfg:
|
if 'datasource' in cfg:
|
||||||
cfg = cfg['datasource']['MAAS']
|
cfg = cfg['datasource']['MAAS']
|
||||||
for key in creds.keys():
|
for key in creds.keys():
|
||||||
|
@ -346,7 +345,7 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
def geturl(url, headers_cb):
|
def geturl(url, headers_cb):
|
||||||
req = urllib2.Request(url, data=None, headers=headers_cb(url))
|
req = urllib2.Request(url, data=None, headers=headers_cb(url))
|
||||||
return(urllib2.urlopen(req).read())
|
return (urllib2.urlopen(req).read())
|
||||||
|
|
||||||
def printurl(url, headers_cb):
|
def printurl(url, headers_cb):
|
||||||
print "== %s ==\n%s\n" % (url, geturl(url, headers_cb))
|
print "== %s ==\n%s\n" % (url, geturl(url, headers_cb))
|
||||||
|
|
|
@ -204,9 +204,8 @@ def transport_iso9660(require_iso=True):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# See if we can read anything at all...??
|
# See if we can read anything at all...??
|
||||||
with open(fullp, 'rb') as fp:
|
util.peek_file(fullp, 512)
|
||||||
fp.read(512)
|
except IOError:
|
||||||
except:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -983,6 +983,12 @@ def find_devs_with(criteria=None, oformat='device',
|
||||||
return entries
|
return entries
|
||||||
|
|
||||||
|
|
||||||
|
def peek_file(fname, max_bytes):
|
||||||
|
LOG.debug("Peeking at %s (max_bytes=%s)", fname, max_bytes)
|
||||||
|
with open(fname, 'rb') as ifh:
|
||||||
|
return ifh.read(max_bytes)
|
||||||
|
|
||||||
|
|
||||||
def load_file(fname, read_cb=None, quiet=False):
|
def load_file(fname, read_cb=None, quiet=False):
|
||||||
LOG.debug("Reading from %s (quiet=%s)", fname, quiet)
|
LOG.debug("Reading from %s (quiet=%s)", fname, quiet)
|
||||||
ofh = StringIO()
|
ofh = StringIO()
|
||||||
|
@ -1328,6 +1334,10 @@ def uptime():
|
||||||
return uptime_str
|
return uptime_str
|
||||||
|
|
||||||
|
|
||||||
|
def append_file(path, content):
|
||||||
|
write_file(path, content, omode="ab", mode=None)
|
||||||
|
|
||||||
|
|
||||||
def ensure_file(path, mode=0644):
|
def ensure_file(path, mode=0644):
|
||||||
write_file(path, content='', omode="ab", mode=mode)
|
write_file(path, content='', omode="ab", mode=mode)
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@ from cloudinit import util
|
||||||
|
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
|
||||||
# Makes the old path start
|
# Makes the old path start
|
||||||
# with new base instead of whatever
|
# with new base instead of whatever
|
||||||
# it previously had
|
# it previously had
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
# Allow running this test individually
|
# Allow running this test individually
|
||||||
top_dir = os.path.join(os.path.dirname(__file__), os.pardir, "helpers.py")
|
top_dir = os.path.join(os.path.dirname(__file__), os.pardir, "helpers.py")
|
||||||
|
@ -10,10 +10,9 @@ if os.path.exists(top_dir):
|
||||||
|
|
||||||
import helpers
|
import helpers
|
||||||
|
|
||||||
from cloudinit import util
|
|
||||||
from cloudinit import stages
|
|
||||||
|
|
||||||
from cloudinit.settings import (PER_INSTANCE)
|
from cloudinit.settings import (PER_INSTANCE)
|
||||||
|
from cloudinit import stages
|
||||||
|
from cloudinit import util
|
||||||
|
|
||||||
|
|
||||||
class TestSimpleRun(helpers.FilesystemMockingTestCase):
|
class TestSimpleRun(helpers.FilesystemMockingTestCase):
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
ci_files='cloud*.py cloudinit/*.py cloudinit/config/*.py'
|
ci_files='cloudinit/*.py cloudinit/config/*.py'
|
||||||
test_files=$(find tests -name "*.py")
|
test_files=$(find tests -name "*.py")
|
||||||
def_files="$ci_files $test_files"
|
def_files="$ci_files $test_files"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue