diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-07-03 20:24:01 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2012-07-03 20:24:01 -0700 |
commit | fd96920837810c5e97de7f397a7df989a41165f3 (patch) | |
tree | 9751e9306c625f7a6460b83788087838ed4ec601 /cloudinit | |
parent | 32ea55364e7688110646818dd651ed476b3c57f1 (diff) | |
download | vyos-cloud-init-fd96920837810c5e97de7f397a7df989a41165f3.tar.gz vyos-cloud-init-fd96920837810c5e97de7f397a7df989a41165f3.zip |
Add a check on 'chownbyname' that catches the keyerror and logs it (instead of failing)
Diffstat (limited to 'cloudinit')
-rw-r--r-- | cloudinit/util.py | 17 |
1 files changed, 12 insertions, 5 deletions
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 |