~quobyte/charms/trusty/quobyte-webconsole/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Bruno Ranieri
  • Date: 2016-07-13 14:50:01 UTC
  • Revision ID: bruno@quobyte.com-20160713145001-1h6cddu9sltlvx7w
Initial charm

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
from charmhelpers.core.hookenv import (
 
4
    log,
 
5
    DEBUG,
 
6
    ERROR,
 
7
    status_get,
 
8
    status_set,
 
9
    relation_ids,
 
10
    related_units,
 
11
    relation_get,
 
12
    open_port,
 
13
    Hooks,
 
14
    UnregisteredHookError
 
15
)
 
16
from charmhelpers.fetch import (
 
17
    apt_update,
 
18
    apt_upgrade
 
19
)
 
20
from charmhelpers.core.host import (
 
21
    service
 
22
)
 
23
from quobyte_helpers import (
 
24
    install_quobyte,
 
25
    is_installed,
 
26
    do_update_replica_hosts
 
27
)
 
28
import json
 
29
import sys
 
30
 
 
31
hooks = Hooks()
 
32
 
 
33
 
 
34
def service_restart():
 
35
    # ensure software is installed
 
36
    if not is_installed():
 
37
        result = install_quobyte()
 
38
        if result is False:
 
39
            return 0
 
40
 
 
41
    # ensure registry-hosts are given and  /etc/quobyte/host.cfg is updated
 
42
    result = []
 
43
    for rel_id in relation_ids('quobyte-registry'):
 
44
        for unit in related_units(rel_id):
 
45
            replicas = relation_get('replicas', unit, rel_id)
 
46
            if replicas is not None:
 
47
                log('grep replicas is {} {}'.format(replicas, type(replicas)))
 
48
                result = json.loads(replicas)
 
49
                log('grep list is {} {}'.format(result, type(result)))
 
50
                break
 
51
    if len(result) is 0:
 
52
        msg = 'no quobyte-registry-relation given, waiting for registry'
 
53
        log(msg)
 
54
        status_set('waiting', msg)
 
55
        return 0
 
56
 
 
57
    success, hosts = do_update_replica_hosts(result)
 
58
    if success:
 
59
        log('/etc/quobyte/host.cfg updated: registry={}'.format(hosts))
 
60
    else:
 
61
        log('cannot update /etc/quobyte/host.cfg', ERROR)
 
62
        status_set('blocked', 'cannot update /etc/quobyte/host.cfg')
 
63
 
 
64
    # Web Console: 8080 (TCP), 7866 (TCP, UDP), 7876 (TCP)
 
65
    open_port(8080)
 
66
    open_port(7866)
 
67
    open_port(7866, protocol='UDP')
 
68
    open_port(7876)
 
69
    status_set('active', 'Quobyte-Webconsole Service running')
 
70
    return service('restart', 'quobyte-webconsole')
 
71
 
 
72
 
 
73
def service_stop():
 
74
    if status_get()[0] == 'active':
 
75
        status_set('maintenance', 'Quobyte-Webconsole Service stopped')
 
76
    return service('stop', 'quobyte-webconsole')
 
77
 
 
78
 
 
79
# -------------------------------------------
 
80
 
 
81
 
 
82
@hooks.hook('start')
 
83
def start():
 
84
    return service_restart()
 
85
 
 
86
 
 
87
@hooks.hook('stop')
 
88
def stop():
 
89
    return service_stop()
 
90
 
 
91
 
 
92
@hooks.hook('config-changed')
 
93
def config_changed():
 
94
    return install_quobyte()
 
95
 
 
96
 
 
97
@hooks.hook('upgrade-charm')
 
98
def upgrade_charm():
 
99
    service_stop()
 
100
    apt_update(fatal=True)
 
101
    apt_upgrade(fatal=True)
 
102
    return service_restart()
 
103
 
 
104
 
 
105
@hooks.hook('install')
 
106
def install():
 
107
    return install_quobyte()
 
108
 
 
109
 
 
110
# -------------------------------------------
 
111
 
 
112
 
 
113
@hooks.hook('quobyte-registry-relation-broken')
 
114
def qb_reg_rel_broken():
 
115
    return 0
 
116
 
 
117
 
 
118
@hooks.hook('quobyte-registry-relation-changed')
 
119
def qb_reg_rel_changed():
 
120
    log('quobyte-registry-relation-changed called', DEBUG)
 
121
 
 
122
    return service_restart()
 
123
 
 
124
 
 
125
@hooks.hook('quobyte-registry-relation-departed')
 
126
def qb_reg_rel_joined():
 
127
    return 0
 
128
 
 
129
 
 
130
@hooks.hook('quobyte-registry-relation-joined')
 
131
def qb_reg_rel_departed():
 
132
    return 0
 
133
 
 
134
 
 
135
if __name__ == '__main__':
 
136
    # execute a hook based on the name the program is called by
 
137
    try:
 
138
        hooks.execute(sys.argv)
 
139
    except UnregisteredHookError as e:
 
140
        log('Unknown hook {} - skipping.'.format(e))