3
# Copyright 2014 Canonical Ltd.
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Affero General Public License version 3, as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful, but
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12
# PURPOSE. See the GNU Affero General Public License for more details.
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
23
import charmhelpers.contrib.charmsupport.nrpe
24
import charmhelpers.core.hookenv
25
import charmhelpers.payload.execd
27
from charmhelpers.core import host
29
hooks = charmhelpers.core.hookenv.Hooks()
30
log = charmhelpers.core.hookenv.log
33
###############################################################################
35
###############################################################################
36
proxy_confs = '/etc/apache2'
40
return charmhelpers.core.hookenv.config()['health_path']
43
def json_status_file():
44
return os.path.join(health_path(), 'status_urls.json')
47
def _add_proxy(url_base, remote):
48
with open(_conf_file(), 'w') as f:
50
'RewriteRule ^/%s/(.*)$ %s$1 [P]\n' % (url_base, remote))
51
f.write('ProxyPassReverse /%s %s\n' % (url_base, remote))
52
host.service_restart('apache2')
57
if os.path.exists(cf):
59
host.service_restart('apache2')
62
@contextlib.contextmanager
64
'''enforce exclusive access to the file to support concurrent updates'''
65
with open(json_status_file(), 'r+') as f:
67
fcntl.lockf(f, fcntl.LOCK_EX)
72
# clear the file before writing back data as it appears the new
73
# dump doesn't set the end-of-file correctly.
78
fcntl.lockf(f, fcntl.LOCK_UN)
81
@hooks.hook('json_status-relation-joined')
82
@hooks.hook('json_status-relation-changed')
83
def json_status_relation_joined():
84
url = charmhelpers.core.hookenv.relation_get('status-url')
85
unit = charmhelpers.core.hookenv.remote_unit()
86
proxy_base = 'json_status-' + unit
87
log('status URL is: %s' % url)
89
with status_urls() as data:
91
'url': '/' + proxy_base + '/',
94
_add_proxy(proxy_base, url)
97
@hooks.hook('json_status-relation-departed')
98
def json_status_relation_departed():
99
if not os.path.exists(json_status_file()):
102
unit = charmhelpers.core.hookenv.remote_unit()
104
with status_urls() as data:
112
if not os.path.exists(health_path()):
113
os.mkdir(health_path())
114
if not os.path.exists(json_status_file()):
115
with open(json_status_file(), 'w') as f:
120
unit = charmhelpers.core.hookenv.remote_unit().replace('/', '_')
121
relation = charmhelpers.core.hookenv.relation_type()
122
return os.path.join(proxy_confs, 'juju-' + relation + '-' + unit + '.conf')
125
@hooks.hook('proxy_logs-relation-joined')
126
def proxy_logs_relation_joined():
127
name = charmhelpers.core.hookenv.remote_unit()
128
addr = charmhelpers.core.hookenv.relation_get('private-address')
130
_add_proxy('logs-' + name, 'http://' + addr + '/')
133
@hooks.hook('proxy_logs-relation-departed')
134
def proxy_logs_relation_departed():
138
if __name__ == "__main__":
139
hooks.execute(sys.argv)