From fd96920837810c5e97de7f397a7df989a41165f3 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Tue, 3 Jul 2012 20:24:01 -0700 Subject: Add a check on 'chownbyname' that catches the keyerror and logs it (instead of failing) --- cloudinit/util.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/util.py b/cloudinit/util.py index 4c29432b..d7dd20b5 100644 --- a/cloudinit/util.py +++ b/cloudinit/util.py @@ -917,7 +917,8 @@ def pipe_in_out(in_fh, out_fh, chunk_size=1024, chunk_cb=None): def chownbyid(fname, uid=None, gid=None): - if uid is None and gid is None: + if uid in [None, -1] and gid in [None, -1]: + # Nothing to do return LOG.debug("Changing the ownership of %s to %s:%s", fname, uid, gid) os.chown(fname, uid, gid) @@ -926,11 +927,17 @@ def chownbyid(fname, uid=None, gid=None): def chownbyname(fname, user=None, group=None): uid = -1 gid = -1 - if user: - uid = pwd.getpwnam(user).pw_uid - if group: - gid = grp.getgrnam(group).gr_gid + try: + if user: + uid = pwd.getpwnam(user).pw_uid + if group: + gid = grp.getgrnam(group).gr_gid + except KeyError: + logexc(LOG, ("Failed changing the ownership of %s using username %s and" + " groupname %s (do they exist?)"), fname, user, group) + return False chownbyid(fname, uid, gid) + return True # Always returns well formated values -- cgit v1.2.3