Merge ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:testability-2 into ubuntu-mirror-charm:master

Proposed by Paul Collins
Status: Merged
Approved by: Haw Loeung
Approved revision: f1079acceb49180c754cbdbba58a1da1dc9e7062
Merged at revision: 9e0ddc52aa26875a2a24a95944320a9df62aa1bc
Proposed branch: ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:testability-2
Merge into: ubuntu-mirror-charm:master
Diff against target: 256 lines (+49/-38)
1 file modified
hooks/hooks.py (+49/-38)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+388436@code.launchpad.net

Commit message

refactor, eliminating globals

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 9e0ddc52aa26875a2a24a95944320a9df62aa1bc

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 fb28d13..c4d3d47 100755
3--- a/hooks/hooks.py
4+++ b/hooks/hooks.py
5@@ -46,10 +46,6 @@ from Cheetah.Template import Template
6 from Config import Config
7
8 hooks = Hooks()
9-hostname = socket.gethostname()
10-fqdn = socket.getfqdn()
11-execd_dir = os.path.join(charm_dir(), 'exec.d')
12-conf = Config()
13
14 apache_tls_settings = {
15 'ssl_cipher_suite': 'EECDH+AESGCM+AES128:EDH+AESGCM+AES128:EECDH+AES128:EDH+AES128:ECDH+AESGCM+AES128:aRSA+AESGCM+AES128:ECDH+AES128:DH+AES128:aRSA+AES128:EECDH+AESGCM:EDH+AESGCM:EECDH:EDH:ECDH+AESGCM:aRSA+AESGCM:ECDH:DH:aRSA:HIGH:!MEDIUM:!aNULL:!NULL:!LOW:!3DES:!DSS:!EXP:!PSK:!SRP', # noqa: E501
16@@ -148,7 +144,7 @@ def check_output(cmd):
17 return None
18
19
20-def configure_scripts():
21+def configure_scripts(conf, hostname):
22 mkdir(conf.script_dir())
23 for script in scripts_to_copy:
24 src = os.path.join(charm_dir(), "files", script)
25@@ -195,7 +191,7 @@ def get_ssh_keyfile(username):
26 # Creation of any necessary destination directories is the responsibility
27 # of the sync script specified in ${mirror}_command
28 #
29-def configure_rsync_client(): # noqa: C901
30+def configure_rsync_client(conf, hostname): # noqa: C901
31 log("CHARM: Configuring rsync client")
32 mirror_userinfo = conf.mirror_userinfo()
33 mirror_confdir = os.path.join(mirror_userinfo.pw_dir, "mirror-config")
34@@ -264,7 +260,7 @@ def configure_rsync_client(): # noqa: C901
35 # config with our own to give connection limits and motd.
36 # Change to update the file rather than overwriting.
37 #
38-def configure_rsync_server():
39+def configure_rsync_server(conf, hostname):
40 log("CHARM: Configuring rsync server")
41
42 roles = conf.roles()
43@@ -325,7 +321,7 @@ def get_role_config(roles, hostname, role):
44 #
45 # Set up apache config files - adding or removing vhosts as needed
46 #
47-def configure_apache(): # noqa: C901
48+def configure_apache(conf, hostname): # noqa: C901
49 log("CHARM: Configuring apache")
50 ensure_package_status(service_affecting_packages, conf.package_status())
51 apache_dir = "/etc/apache2"
52@@ -418,7 +414,7 @@ def configure_apache(): # noqa: C901
53 log("CHARM: Unable to delete vhost {}".format(vhost))
54
55 # Set up server limits
56- restart_for_mpm = configure_apache_mpm()
57+ restart_for_mpm = configure_apache_mpm(conf, hostname)
58
59 # Configure status module
60 tmpl_data = {}
61@@ -455,14 +451,14 @@ def configure_apache(): # noqa: C901
62 service_reload("apache2")
63
64 # Notify any interested parties
65- configure_extra_mirrors()
66+ configure_extra_mirrors(conf, hostname)
67 log("CHARM: Finished configuring apache")
68
69
70 # Set up the mpm config
71 # 2.4 defaults to event
72 # Returns True is something changed, False otherwise
73-def configure_apache_mpm(): # noqa: C901
74+def configure_apache_mpm(conf, hostname): # noqa: C901
75 log("CHARM: Configuring mpm")
76 restart_required = False
77 apache_version = get_pkg_version("apache2")[0:3]
78@@ -548,7 +544,7 @@ def configure_apache_mpm(): # noqa: C901
79 #
80 # Set up vsftp
81 #
82-def configure_vsftp():
83+def configure_vsftp(conf, hostname):
84 log("CHARM: Configuring vsftp")
85
86 # Set the ftp user's homedir where we want it
87@@ -572,7 +568,7 @@ def configure_vsftp():
88 #
89 # Set up any external ssh sync triggers
90 #
91-def configure_triggers():
92+def configure_triggers(conf, hostname):
93 roles = conf.roles()
94 if hostname not in roles:
95 return
96@@ -602,7 +598,7 @@ def configure_triggers():
97 write_file(keyfile, contents)
98
99
100-def configure_nrpe(): # noqa: C901
101+def configure_nrpe(conf, hostname): # noqa: C901
102 if not conf.nagios_relation_ok():
103 log("CHARM: Nagios relation not established - not configuring nrpe")
104 return
105@@ -665,6 +661,7 @@ def configure_nrpe(): # noqa: C901
106 tmpl_data["rsync_crit"] = int(conf.rsync_max_connections() + 4)
107 tmpl_data["rsync_warn"] = int(conf.rsync_max_connections() + 1) # all client slots used
108
109+ fqdn = socket.getfqdn()
110 if role == "ports":
111 tmpl_data["tracepath"] = os.path.join(mirror["path"], "project", "trace", fqdn)
112 else:
113@@ -746,7 +743,7 @@ def configure_nrpe(): # noqa: C901
114 service_reload('nagios-nrpe-server')
115
116
117-def configure_log_archiving():
118+def configure_log_archiving(conf, hostname):
119 settings = {}
120 logdirs = []
121
122@@ -768,7 +765,7 @@ def configure_log_archiving():
123 relation_set(r, settings)
124
125
126-def configure_extra_mirrors():
127+def configure_extra_mirrors(conf, hostname):
128 settings = {}
129 if is_relation_made("ubuntu-mirror"):
130 settings["apache_logdir"] = conf.logdir("apache")
131@@ -807,7 +804,7 @@ def is_lxc():
132 return False
133
134
135-def configure_sysctl():
136+def configure_sysctl(conf, hostname):
137 # In a container, these keys exist but can't be changed. sysctl
138 # seems to realize that if setting these fail as root, something
139 # is beyond our control and exits 0. Only add such keys here.
140@@ -833,14 +830,14 @@ def configure_sysctl():
141 sysctl.create(yaml.dump(non_container_settings), '/etc/sysctl.d/90-ubuntu-mirror-non-container.conf')
142
143
144-def configure_user():
145+def configure_user(conf, hostname):
146 if not user_exists(conf.mirror_user()):
147 mirror_userinfo = adduser(conf.mirror_user(), system_user=True)
148 if not os.path.isdir(mirror_userinfo.pw_dir):
149 mkdir(mirror_userinfo.pw_dir, owner=conf.mirror_user())
150
151
152-def configure_directories():
153+def configure_directories(conf, hostname):
154 if not os.path.isdir("/srv/ftp.root"):
155 mkdir("/srv/ftp.root")
156 chownr("/srv/ftp.root", conf.mirror_user(), conf.mirror_user(), follow_links=False)
157@@ -848,66 +845,80 @@ def configure_directories():
158
159 @hooks.hook("install.real")
160 def install():
161+ conf = Config()
162+ hostname = socket.gethostname()
163 log("CHARM: Installing {}".format(conf.app_name()))
164 apt_install(required_pkgs, options=['--force-yes'])
165- configure_user()
166- configure_scripts()
167- configure_directories()
168+ configure_user(conf, hostname)
169+ configure_scripts(conf, hostname)
170+ configure_directories(conf, hostname)
171 for module in apache_modules:
172 check_call(["/usr/sbin/a2enmod", module])
173
174
175 @hooks.hook("config-changed")
176 def config_changed():
177+ conf = Config()
178+ hostname = socket.gethostname()
179 log("CHARM: Configuring {}".format(conf.app_name()))
180- configure_user()
181- configure_scripts()
182- configure_directories()
183- configure_rsync_client()
184- configure_rsync_server()
185- configure_apache()
186- configure_vsftp()
187- configure_triggers()
188- configure_nrpe()
189- configure_log_archiving()
190- configure_sysctl()
191+ configure_user(conf, hostname)
192+ configure_scripts(conf, hostname)
193+ configure_directories(conf, hostname)
194+ configure_rsync_client(conf, hostname)
195+ configure_rsync_server(conf, hostname)
196+ configure_apache(conf, hostname)
197+ configure_vsftp(conf, hostname)
198+ configure_triggers(conf, hostname)
199+ configure_nrpe(conf, hostname)
200+ configure_log_archiving(conf, hostname)
201+ configure_sysctl(conf, hostname)
202
203
204 @hooks.hook("upgrade-charm")
205 def upgrade_charm():
206+ conf = Config()
207 log("CHARM: Upgrading {}".format(conf.app_name()))
208 install()
209
210
211 @hooks.hook("nrpe-external-master-relation-changed")
212 def nrpe_external_master_relation_changed():
213+ conf = Config()
214+ hostname = socket.gethostname()
215 log("CHARM: Changing nrpe relation for {}".format(conf.app_name()))
216- configure_nrpe()
217- configure_extra_mirrors()
218+ configure_nrpe(conf, hostname)
219+ configure_extra_mirrors(conf, hostname)
220
221
222 @hooks.hook("local-monitors-relation-changed")
223 def local_monitors_relation_changed():
224+ conf = Config()
225+ hostname = socket.gethostname()
226 log("CHARM: Changing monitors relation for {}".format(conf.app_name()))
227- configure_nrpe()
228+ configure_nrpe(conf, hostname)
229
230
231 @hooks.hook("log-archive-relation-changed")
232 def log_archive_relation_changed():
233+ conf = Config()
234+ hostname = socket.gethostname()
235 log("CHARM: Changing log-archive relation for {}".format(conf.app_name()))
236- configure_log_archiving()
237+ configure_log_archiving(conf, hostname)
238
239
240 @hooks.hook("ubuntu-mirror-relation-changed")
241 def ubuntu_mirror_relation_changed():
242+ conf = Config()
243+ hostname = socket.gethostname()
244 log("CHARM: Changing ubuntu-mirror relation for {}".format(conf.app_name()))
245- configure_extra_mirrors()
246+ configure_extra_mirrors(conf, hostname)
247
248
249 if __name__ == "__main__":
250 hook_name = os.path.basename(sys.argv[0])
251 if hook_name.endswith('.real'):
252 hook_name = hook_name[:-5]
253+ execd_dir = os.path.join(charm_dir(), 'exec.d')
254 execd_run(('charm-pre-%s' % hook_name), execd_dir=execd_dir)
255 hooks.execute(sys.argv)
256 execd_run(('charm-post-%s' % hook_name), execd_dir=execd_dir)

Subscribers

People subscribed via source and target branches