~canonical-ci-engineering/uci-engine/trunk

« back to all changes in this revision

Viewing changes to charms/precise/webui/hooks/hooks.py

  • Committer: Ubuntu CI Bot
  • Author(s): Celso Providelo
  • Date: 2015-01-07 14:33:28 UTC
  • mfrom: (911.1.8 charms-cleanup)
  • Revision ID: ubuntu_ci_bot-20150107143328-q0d037ojtopm3n9d
Removing the charms from UCI-E since they live in isolated branches now and also adjusting the run-tests to stop loading/running charm tests.

Updating local j-d copy to trunk (r126) for proper support for charm branch pinning. [r=Evan Dandrea]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# Ubuntu CI Engine
3
 
# Copyright 2014 Canonical Ltd.
4
 
 
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.
8
 
 
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.
13
 
 
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/>.
16
 
 
17
 
import contextlib
18
 
import fcntl
19
 
import json
20
 
import os
21
 
import sys
22
 
 
23
 
import charmhelpers.contrib.charmsupport.nrpe
24
 
import charmhelpers.core.hookenv
25
 
import charmhelpers.payload.execd
26
 
 
27
 
from charmhelpers.core import host
28
 
 
29
 
hooks = charmhelpers.core.hookenv.Hooks()
30
 
log = charmhelpers.core.hookenv.log
31
 
 
32
 
 
33
 
###############################################################################
34
 
# Global variables
35
 
###############################################################################
36
 
proxy_confs = '/etc/apache2'
37
 
 
38
 
 
39
 
def health_path():
40
 
    return charmhelpers.core.hookenv.config()['health_path']
41
 
 
42
 
 
43
 
def json_status_file():
44
 
    return os.path.join(health_path(), 'status_urls.json')
45
 
 
46
 
 
47
 
def _add_proxy(url_base, remote):
48
 
    with open(_conf_file(), 'w') as f:
49
 
        f.write(
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')
53
 
 
54
 
 
55
 
def _remove_proxy():
56
 
    cf = _conf_file()
57
 
    if os.path.exists(cf):
58
 
        os.unlink(cf)
59
 
        host.service_restart('apache2')
60
 
 
61
 
 
62
 
@contextlib.contextmanager
63
 
def status_urls():
64
 
    '''enforce exclusive access to the file to support concurrent updates'''
65
 
    with open(json_status_file(), 'r+') as f:
66
 
        try:
67
 
            fcntl.lockf(f, fcntl.LOCK_EX)
68
 
            f.seek(0)
69
 
            data = json.load(f)
70
 
            yield data
71
 
 
72
 
            # clear the file before writing back data as it appears the new
73
 
            # dump doesn't set the end-of-file correctly.
74
 
            f.truncate(0)
75
 
            f.seek(0)
76
 
            json.dump(data, f)
77
 
        finally:
78
 
            fcntl.lockf(f, fcntl.LOCK_UN)
79
 
 
80
 
 
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)
88
 
 
89
 
    with status_urls() as data:
90
 
        data[unit] = {
91
 
            'url': '/' + proxy_base + '/',
92
 
            'unit': unit,
93
 
        }
94
 
    _add_proxy(proxy_base, url)
95
 
 
96
 
 
97
 
@hooks.hook('json_status-relation-departed')
98
 
def json_status_relation_departed():
99
 
    if not os.path.exists(json_status_file()):
100
 
        return
101
 
 
102
 
    unit = charmhelpers.core.hookenv.remote_unit()
103
 
 
104
 
    with status_urls() as data:
105
 
        if unit in data:
106
 
            del data[unit]
107
 
    _remove_proxy()
108
 
 
109
 
 
110
 
@hooks.hook()
111
 
def install():
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:
116
 
            f.write('{}')
117
 
 
118
 
 
119
 
def _conf_file():
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')
123
 
 
124
 
 
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')
129
 
 
130
 
    _add_proxy('logs-' + name, 'http://' + addr + '/')
131
 
 
132
 
 
133
 
@hooks.hook('proxy_logs-relation-departed')
134
 
def proxy_logs_relation_departed():
135
 
    _remove_proxy()
136
 
 
137
 
 
138
 
if __name__ == "__main__":
139
 
    hooks.execute(sys.argv)