Merge lp:~harlowja/cloud-init/ensure-sudo-dir-ready into lp:~cloud-init-dev/cloud-init/trunk

Proposed by Joshua Harlow
Status: Merged
Merged at revision: 691
Proposed branch: lp:~harlowja/cloud-init/ensure-sudo-dir-ready
Merge into: lp:~cloud-init-dev/cloud-init/trunk
Diff against target: 63 lines (+35/-2)
1 file modified
cloudinit/distros/__init__.py (+35/-2)
To merge this branch: bzr merge lp:~harlowja/cloud-init/ensure-sudo-dir-ready
Reviewer Review Type Date Requested Status
cloud-init Commiters Pending
Review via email: mp+127086@code.launchpad.net
To post a comment you must log in.
677. By Joshua Harlow

Dir should be 0755, not 0440

678. By Joshua Harlow

Update the log statement used here to be
a little more relevant.

679. By Joshua Harlow

Ensure that the include dir starts the line
and is not a part of a comment or other
part of the line.

Revision history for this message
Scott Moser (smoser) wrote :

Only comment here is that we should log.warn if we're modifying /etc/sudoers

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/distros/__init__.py'
2--- cloudinit/distros/__init__.py 2012-09-28 21:21:02 +0000
3+++ cloudinit/distros/__init__.py 2012-09-30 20:49:21 +0000
4@@ -251,7 +251,7 @@
5 if util.is_user(name):
6 LOG.warn("User %s already exists, skipping." % name)
7 else:
8- LOG.debug("Creating name %s" % name)
9+ LOG.debug("Adding user named %s", name)
10 try:
11 util.subp(adduser_cmd, logstring=x_adduser_cmd)
12 except Exception as e:
13@@ -299,6 +299,38 @@
14
15 return True
16
17+ def ensure_sudo_dir(self, path, sudo_base='/etc/sudoers'):
18+ # Ensure the dir is included and that
19+ # it actually exists as a directory
20+ sudoers_contents = ''
21+ if os.path.exists(sudo_base):
22+ sudoers_contents = util.load_file(sudo_base)
23+ found_include = False
24+ for line in sudoers_contents.splitlines():
25+ line = line.strip()
26+ include_match = re.search(r"^#includedir\s+(.*)$", line)
27+ if not include_match:
28+ continue
29+ included_dir = include_match.group(1).strip()
30+ if not included_dir:
31+ continue
32+ included_dir = os.path.abspath(included_dir)
33+ if included_dir == path:
34+ found_include = True
35+ break
36+ if not found_include:
37+ sudoers_contents += "\n#includedir %s\n" % (path)
38+ try:
39+ if not os.path.exists(sudo_base):
40+ util.write_file(sudo_base, sudoers_contents, 0440)
41+ else:
42+ with open(sudo_base, 'a') as f:
43+ f.write(sudoers_contents)
44+ except IOError as e:
45+ util.logexc(LOG, "Failed to write %s" % sudo_base, e)
46+ raise e
47+ util.ensure_dir(path, 0755)
48+
49 def write_sudo_rules(self,
50 user,
51 rules,
52@@ -314,9 +346,10 @@
53 content += "%s %s\n" % (user, rule)
54 content += "\n"
55
56+ self.ensure_sudo_dir(os.path.dirname(sudo_file))
57+
58 if not os.path.exists(sudo_file):
59 util.write_file(sudo_file, content, 0440)
60-
61 else:
62 try:
63 with open(sudo_file, 'a') as f: