~niedbalski/charms/trusty/rsyslog-forwarder-ha/trunk

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Jorge Niedbalski
  • Date: 2014-05-02 19:55:18 UTC
  • Revision ID: jorge.niedbalski@canonical.com-20140502195518-4274miknnbx2lw8h
Initial import of the charm.
        - make test ( runs functional + unit )
        - make lint ( lints the code )
        - make unit ( unit tests )
        - make functional ( unit tests )

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 
13
13
sys.path.insert(0, os.path.join(_HERE, 'charmhelpers'))
14
14
 
15
 
from charmhelpers.core import hookenv
16
 
from charmhelpers.core import host
17
 
from charmhelpers import fetch
 
15
from charmhelpers.core.host import (
 
16
    service_start,
 
17
    service_stop,
 
18
    service_restart,
 
19
)
 
20
 
 
21
from charmhelpers.core.hookenv import (
 
22
    Hooks,
 
23
    relation_id,
 
24
    config as config_get,
 
25
    log as juju_log,
 
26
    remote_unit,
 
27
    relation_get,
 
28
)
 
29
 
 
30
from charmhelpers.fetch import (
 
31
    apt_install
 
32
)
 
33
 
 
34
try:
 
35
    import sqlalchemy
 
36
except ImportError:
 
37
    try:
 
38
        apt_install("python-sqlalchemy")
 
39
    except:
 
40
        pass
18
41
 
19
42
from model import Server, session
20
 
from utils import get_template_dir, get_template, __die
 
43
from utils import get_template_dir, get_template
 
44
from utils import __die as die
21
45
 
22
46
required_packages = [
23
47
    'rsyslog',
31
55
REPLICATION_FILE = '/etc/rsyslog.d/80-rsyslog-replication.conf'
32
56
 
33
57
 
34
 
hooks = hookenv.Hooks()
 
58
hooks = Hooks()
35
59
 
36
60
 
37
61
def update_local_logs(keep=True):
75
99
    servers = session.query(Server).all()
76
100
 
77
101
    if not len(servers):
78
 
        hookenv.log("Ready for add rsyslog relations to this forwarder")
 
102
        juju_log("Ready for add rsyslog relations to this forwarder")
79
103
        sys.exit(0)
80
104
 
81
 
    mode = hookenv.config('replication-mode')
 
105
    mode = config_get('replication-mode')
82
106
 
83
107
    if mode not in ('fanout', 'failover', ):
84
 
        __die("Invalid provided replication mode: %s" % mode)
 
108
        die("Invalid provided replication mode: %s" % mode)
85
109
 
86
110
    if mode == 'failover':
87
111
        if not len(servers) >= 2:
88
 
            hookenv.log(
 
112
            juju_log(
89
113
                "Cannot use failover replication without a secondary server,"
90
114
                " switching to fanout")
91
115
            update_fanout_replication(servers)
94
118
    else:
95
119
        update_fanout_replication(servers)
96
120
 
97
 
    host.service_restart("rsyslog")
 
121
    service_restart("rsyslog")
98
122
 
99
123
 
100
124
@hooks.hook()
101
125
def start():
102
 
    host.service_start("rsyslog")
 
126
    service_start("rsyslog")
103
127
 
104
128
 
105
129
@hooks.hook()
106
130
def stop():
107
 
    host.service_stop("rsyslog")
 
131
    service_stop("rsyslog")
108
132
 
109
133
 
110
134
@hooks.hook()
111
135
def install():
112
136
    for package in required_packages:
113
 
        fetch.apt_install(package, fatal=True)
 
137
        apt_install(package, fatal=True)
114
138
 
115
139
    update_local_logs(
116
 
        keep=hookenv.config('log-locally'))
 
140
        keep=config_get('log-locally'))
117
141
 
118
142
 
119
143
@hooks.hook()
120
144
def syslog_relation_joined():
121
 
    relation_id = hookenv.relation_id()
 
145
    relation = relation_id()
122
146
 
123
147
    if Server.has_relation(relation_id):
124
 
        __die("Relation %s already exists" % relation_id)
 
148
        die("Relation %s already exists" % relation_id)
125
149
 
126
 
    server = Server(relation_id=relation_id,
127
 
                    remote_unit=hookenv.remote_unit(),
128
 
                    private_address=hookenv.relation_get('private-address'))
 
150
    server = Server(relation_id=relation,
 
151
                    remote_unit=remote_unit(),
 
152
                    private_address=relation_get('private-address'))
129
153
    try:
130
154
        session.add(server)
131
155
        session.commit()
132
156
    except Exception:
133
157
        session.rollback()
134
 
        __die("Cannot create server relation")
 
158
        die("Cannot create server relation")
135
159
 
136
160
    update_replication()
137
161
 
138
162
 
139
163
@hooks.hook()
140
164
def syslog_relation_departed():
141
 
    Server.remove(hookenv.relation_id())
 
165
    Server.remove(relation_id())
142
166
    update_replication()
143
167
 
144
168
 
145
169
@hooks.hook()
146
170
def syslog_relation_broken():
147
 
    Server.remove(hookenv.relation_id())
 
171
    Server.remove(relation_id())
148
172
    update_replication()
149
173
 
150
174
 
151
175
@hooks.hook()
 
176
def upgrade_charm():
 
177
    install()
 
178
 
 
179
 
 
180
@hooks.hook()
152
181
def config_changed():
153
 
    update_local_logs(hookenv.config("log-locally"))
 
182
    update_local_logs(config_get("log-locally"))
154
183
    update_replication()
155
184
 
156
185