~vila/uci-engine/enable-nova-and-swift

« back to all changes in this revision

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

  • Committer: Evan Dandrea
  • Date: 2014-06-30 12:07:54 UTC
  • mfrom: (630 uci-engine)
  • mto: This revision was merged to the branch mainline in revision 655.
  • Revision ID: evan.dandrea@canonical.com-20140630120754-z6x5eqdahpp2qfrt
Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# You should have received a copy of the GNU Affero General Public License
15
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
 
17
import contextlib
 
18
import fcntl
17
19
import json
18
20
import os
19
21
import sys
32
34
# Global variables
33
35
###############################################################################
34
36
proxy_confs = '/etc/apache2'
35
 
health_path = charmhelpers.core.hookenv.config()['health_path']
36
 
json_status_file = os.path.join(health_path, 'status_urls.json')
 
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')
37
45
 
38
46
 
39
47
def _add_proxy(url_base, remote):
44
52
    host.service_restart('apache2')
45
53
 
46
54
 
47
 
@hooks.hook('proxy_logs-relation-broken')
48
55
def _remove_proxy():
49
56
    cf = _conf_file()
50
57
    if os.path.exists(cf):
52
59
        host.service_restart('apache2')
53
60
 
54
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
            f.seek(0)
 
73
            json.dump(data, f)
 
74
        finally:
 
75
            fcntl.lockf(f, fcntl.LOCK_UN)
 
76
 
 
77
 
55
78
@hooks.hook('json_status-relation-joined')
56
79
@hooks.hook('json_status-relation-changed')
57
80
def json_status_relation_joined():
58
81
    url = charmhelpers.core.hookenv.relation_get('status-url')
59
82
    unit = charmhelpers.core.hookenv.remote_unit()
 
83
    proxy_base = 'json_status-' + unit
60
84
    log('status URL is: %s' % url)
61
85
 
62
 
    rid = charmhelpers.core.hookenv.relation_id().replace(':', '_')
63
 
 
64
 
    data = {}
65
 
    if os.path.exists(json_status_file):
66
 
        data = json.load(open(json_status_file))
67
 
    data[rid] = {
68
 
        'url': '/' + rid + '/',
69
 
        'unit': unit,
70
 
    }
71
 
 
72
 
    with open(json_status_file, 'w') as f:
73
 
        json.dump(data, f)
74
 
 
75
 
    _add_proxy(rid, url)
76
 
 
77
 
 
78
 
@hooks.hook('json_status-relation-broken')
79
 
def json_status_relation_broken():
80
 
    if not os.path.exists(json_status_file):
 
86
    with status_urls() as data:
 
87
        data[unit] = {
 
88
            'url': '/' + proxy_base + '/',
 
89
            'unit': unit,
 
90
        }
 
91
    _add_proxy(proxy_base, url)
 
92
 
 
93
 
 
94
@hooks.hook('json_status-relation-departed')
 
95
def json_status_relation_departed():
 
96
    if not os.path.exists(json_status_file()):
81
97
        return
82
98
 
83
 
    data = json.load(open(json_status_file))
84
 
    rid = charmhelpers.core.hookenv.relation_id()
85
 
    if rid in data:
86
 
        del data[rid]
87
 
    with open(json_status_file, 'w') as f:
88
 
        json.dump(data, f)
 
99
    unit = charmhelpers.core.hookenv.remote_unit()
89
100
 
 
101
    with status_urls() as data:
 
102
        if unit in data:
 
103
            del data[unit]
90
104
    _remove_proxy()
91
105
 
92
106
 
93
107
@hooks.hook()
94
108
def install():
95
 
    if not os.path.exists(health_path):
96
 
        os.mkdir(health_path)
97
 
    if not os.path.exists(json_status_file):
98
 
        with open(json_status_file, 'w') as f:
 
109
    if not os.path.exists(health_path()):
 
110
        os.mkdir(health_path())
 
111
    if not os.path.exists(json_status_file()):
 
112
        with open(json_status_file(), 'w') as f:
99
113
            f.write('{}')
100
114
 
101
115
 
102
116
def _conf_file():
103
 
    # apache can't use a ":" in the Include directive so change it to a dash
104
 
    relation = 'juju-' + charmhelpers.core.hookenv.relation_id() + '.conf'
105
 
    return os.path.join(proxy_confs, relation.replace(':', '-'))
 
117
    unit = charmhelpers.core.hookenv.remote_unit().replace('/', '_')
 
118
    relation = charmhelpers.core.hookenv.relation_type()
 
119
    return os.path.join(proxy_confs, 'juju-' + relation + '-' + unit + '.conf')
106
120
 
107
121
 
108
122
@hooks.hook('proxy_logs-relation-joined')
109
123
def proxy_logs_relation_joined():
110
 
    name, _ = charmhelpers.core.hookenv.remote_unit().split('/')
 
124
    name = charmhelpers.core.hookenv.remote_unit()
111
125
    addr = charmhelpers.core.hookenv.relation_get('private-address')
112
126
 
113
127
    _add_proxy('logs-' + name, 'http://' + addr + '/')
114
128
 
115
129
 
116
 
@hooks.hook('proxy_logs-relation-broken')
117
 
def proxy_logs_relation_broken():
 
130
@hooks.hook('proxy_logs-relation-departed')
 
131
def proxy_logs_relation_departed():
118
132
    _remove_proxy()
119
133
 
120
134
 
121
135
if __name__ == "__main__":
122
 
    hooks.execute(sys.argv)
 
 
b'\\ No newline at end of file'
 
136
    hooks.execute(sys.argv)