Merge lp:~pjdc/charm-haproxy/do-not-clobber-logrotate-on-upgrade into lp:charm-haproxy

Proposed by Paul Collins
Status: Merged
Approved by: Paul Collins
Approved revision: 139
Merged at revision: 136
Proposed branch: lp:~pjdc/charm-haproxy/do-not-clobber-logrotate-on-upgrade
Merge into: lp:charm-haproxy
Diff against target: 131 lines (+55/-13)
2 files modified
config.yaml (+3/-1)
hooks/hooks.py (+52/-12)
To merge this branch: bzr merge lp:~pjdc/charm-haproxy/do-not-clobber-logrotate-on-upgrade
Reviewer Review Type Date Requested Status
Tom Haddon Approve
Paul Collins Needs Resubmitting
Canonical IS Reviewers Pending
Review via email: mp+369560@code.launchpad.net

Commit message

only configure logrotate if the config item is not empty, and restore a clobbered config file to the default

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
Tom Haddon (mthaddon) :
review: Needs Information
Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM, but see comment about empty logrotate config

review: Approve
137. By Paul Collins

configure_logrotate: write default config if file on disk is zero length

Revision history for this message
Paul Collins (pjdc) wrote :

I fiddled with the comment a bit before deciding to bite the bullet and restore the default config if the file on disk is zero-length.

review: Needs Resubmitting
138. By Paul Collins

configure_logrotate: typo in comment

Revision history for this message
Tom Haddon (mthaddon) wrote :

LGTM, but per comment inline there's an odd looking indentation that should be fixed before landing.

review: Approve
139. By Paul Collins

hooks.py: indentation fix

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

Change successfully merged at revision 136

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'config.yaml'
2--- config.yaml 2019-06-07 10:09:25 +0000
3+++ config.yaml 2019-07-08 01:16:49 +0000
4@@ -100,7 +100,9 @@
5 description: |
6 Override package logrotate configuration.
7 .
8- For example.
9+ Warning: Setting this value back to the empty string will leave the previous config in place on disk.
10+ .
11+ Example value:
12 .
13 /var/log/haproxy.log {
14 weekly
15
16=== modified file 'hooks/hooks.py'
17--- hooks/hooks.py 2019-06-07 09:24:02 +0000
18+++ hooks/hooks.py 2019-07-08 01:16:49 +0000
19@@ -1,15 +1,16 @@
20 #!/usr/bin/env python
21
22 import base64
23+import errno
24 import glob
25 import os
26+import pwd
27 import re
28+import shutil
29 import socket
30-import shutil
31 import subprocess
32 import sys
33 import yaml
34-import pwd
35
36 from itertools import izip, tee
37 from operator import itemgetter
38@@ -52,7 +53,6 @@
39 default_haproxy_lib_dir = "/var/lib/haproxy"
40 metrics_cronjob_path = "/etc/cron.d/haproxy_metrics"
41 metrics_script_path = "/usr/local/bin/haproxy_to_statsd.sh"
42-log_rotate_config_path = "/etc/logrotate.d/haproxy"
43 service_affecting_packages = ['haproxy']
44 apt_backports_template = (
45 "deb http://archive.ubuntu.com/ubuntu %(release)s-backports "
46@@ -102,6 +102,29 @@
47 ]
48
49
50+logrotate_config_path = "/etc/logrotate.d/haproxy"
51+
52+logrotate_config_header = '''\
53+# This file is managed by the haproxy charm (config item logrotate_config).
54+# Manual changes may be reverted at any time. Use Juju to change it.
55+'''
56+
57+# This is used to roll back zero-length configs deployed by a earlier buggy
58+# version of the charm. Extracted from haproxy 1.6.3-1ubuntu0.2.
59+default_packaged_haproxy_logrotate_config = '''\
60+/var/log/haproxy.log {
61+ daily
62+ rotate 52
63+ missingok
64+ notifempty
65+ compress
66+ delaycompress
67+ postrotate
68+ invoke-rc.d rsyslog rotate >/dev/null 2>&1 || true
69+ endscript
70+}
71+'''
72+
73 class InvalidRelationDataError(Exception):
74 """Invalid data has been provided in the relation."""
75
76@@ -962,9 +985,6 @@
77 ensure_package_status(service_affecting_packages, config_data['package_status'])
78 enable_haproxy()
79
80- if config_data.get('logrotate_config'):
81- logrotate_configuration()
82-
83
84 def config_changed():
85 config_data = config_get()
86@@ -983,7 +1003,7 @@
87 raise ValueError('{} is not a valid port/proto value'.format(port_plus_proto))
88
89 if config_data.changed('logrotate_config'):
90- logrotate_configuration()
91+ configure_logrotate(config_data.get('logrotate_config'))
92
93 old_stanzas = get_listen_stanzas()
94 haproxy_globals = create_haproxy_globals()
95@@ -1395,11 +1415,31 @@
96 user=monitoring_username)
97
98
99-def logrotate_configuration():
100- config = config_get()
101- logrotate_config = config['logrotate_config']
102- with open(log_rotate_config_path, 'w') as f:
103- f.write(logrotate_config)
104+def configure_logrotate(logrotate_config):
105+ # NOTE(pjdc): This function is a trapdoor -- setting logrotate_config and
106+ # then changing it to an empty string leaves the last value in place, but
107+ # if we ship a default config and always write *something*, we'd clobber
108+ # manual configs. Since this is a relatively new charm feature, we
109+ # probably can't lay exclusive claim to the logrotate config file yet.
110+ final_logrotate_config = None
111+
112+ if logrotate_config:
113+ final_logrotate_config = logrotate_config_header + logrotate_config
114+ else:
115+ try:
116+ # If the live config is zero-length, it was probably clobbered by a
117+ # (fixed) bug in the previous version of the charm (LP:1834980).
118+ if os.path.getsize(logrotate_config_path) == 0:
119+ final_logrotate_config = default_packaged_haproxy_logrotate_config
120+ except OSError as ose:
121+ # If the file or directory is missing, just carry on. Either
122+ # someone deliberately deleted it, or logrotate isn't installed.
123+ if ose.errno != errno.ENOENT:
124+ raise
125+
126+ if final_logrotate_config:
127+ with open(logrotate_config_path, 'w') as f:
128+ f.write(final_logrotate_config)
129
130
131 # #############################################################################

Subscribers

People subscribed via source and target branches

to all changes: