Merge ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:symlink-following into ubuntu-mirror-charm:master

Proposed by Paul Collins
Status: Merged
Approved by: Haw Loeung
Approved revision: 0053527401169898f725d20c32374caf12b25c14
Merged at revision: 3ee1f300437dcb10eb7620358e717e207bfaae47
Proposed branch: ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:symlink-following
Merge into: ubuntu-mirror-charm:master
Diff against target: 68 lines (+26/-3)
1 file modified
hooks/hooks.py (+26/-3)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+390874@code.launchpad.net

Commit message

stop creating extra symlinks in mirror directories

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision 3ee1f300437dcb10eb7620358e717e207bfaae47

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hooks/hooks.py b/hooks/hooks.py
2index 2757b35..4c272f4 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -5,6 +5,7 @@
6
7 import base64
8 import datetime
9+import errno
10 import json
11 import os
12 import os.path
13@@ -24,7 +25,6 @@ from charmhelpers.core.host import (
14 mkdir,
15 service_reload,
16 service_restart,
17- symlink,
18 user_exists,
19 write_file,
20 )
21@@ -103,6 +103,29 @@ GET_DEFAULT_ADDRESS_DESTINATIONS = {
22 }
23
24
25+def ensure_symlink(target, link_path):
26+ """Ensure that link_path is a symlink to target. Do not follow link_path.
27+ Raise an exception if link_path exists but is not a symlink."""
28+ need_unlink = False
29+ try:
30+ current = os.readlink(link_path)
31+ if current == target:
32+ log("Skipping {} - symlink exists and already points to {}".format(link_path, current))
33+ return
34+ need_unlink = True
35+ except OSError as ose:
36+ if ose.errno != errno.ENOENT:
37+ raise
38+ except Exception:
39+ raise
40+
41+ # There's no non-racy way to replace a symlink. Even `ls -sfn target link_path` does this.
42+ if need_unlink:
43+ os.unlink(link_path)
44+ os.symlink(target, link_path)
45+ log("Symlink {} at {}, pointing to {}".format("replaced" if need_unlink else "created", link_path, target))
46+
47+
48 def get_default_address(af):
49 """Determine our default address for the given address family."""
50 try:
51@@ -486,7 +509,7 @@ def configure_apache(conf, hostname): # noqa: C901
52 tmpl_data["ports"] = [80, 443] if role_config.get('https') else [80]
53 all_ports.update(tmpl_data["ports"])
54 file_from_template(mirror['tmpl_file'], sites_available, tmpl_data)
55- symlink(sites_available, sites_enabled)
56+ ensure_symlink(sites_available, sites_enabled)
57
58 # And make sure the docroot exists
59 if role == "ubuntu":
60@@ -502,7 +525,7 @@ def configure_apache(conf, hostname): # noqa: C901
61 mkdir(mirror["path"])
62 os.chmod(mirror["path"], 0o755)
63 os.chown(mirror["path"], mirror_userinfo.pw_uid, mirror_userinfo.pw_gid)
64- symlink(mirror["path"], linkdest)
65+ ensure_symlink(mirror["path"], linkdest)
66
67 # Update ports file
68 all_addresses.discard('*') # archive.ubuntu.com must not open port 443.

Subscribers

People subscribed via source and target branches