13
13
sys.path.insert(0, os.path.join(_HERE, 'charmhelpers'))
15
from charmhelpers.core import hookenv
16
from charmhelpers.core import host
17
from charmhelpers import fetch
15
from charmhelpers.core.host import (
21
from charmhelpers.core.hookenv import (
30
from charmhelpers.fetch import (
38
apt_install("python-sqlalchemy")
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
22
46
required_packages = [
75
99
servers = session.query(Server).all()
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")
81
mode = hookenv.config('replication-mode')
105
mode = config_get('replication-mode')
83
107
if mode not in ('fanout', 'failover', ):
84
__die("Invalid provided replication mode: %s" % mode)
108
die("Invalid provided replication mode: %s" % mode)
86
110
if mode == 'failover':
87
111
if not len(servers) >= 2:
89
113
"Cannot use failover replication without a secondary server,"
90
114
" switching to fanout")
91
115
update_fanout_replication(servers)
95
119
update_fanout_replication(servers)
97
host.service_restart("rsyslog")
121
service_restart("rsyslog")
102
host.service_start("rsyslog")
126
service_start("rsyslog")
107
host.service_stop("rsyslog")
131
service_stop("rsyslog")
112
136
for package in required_packages:
113
fetch.apt_install(package, fatal=True)
137
apt_install(package, fatal=True)
115
139
update_local_logs(
116
keep=hookenv.config('log-locally'))
140
keep=config_get('log-locally'))
120
144
def syslog_relation_joined():
121
relation_id = hookenv.relation_id()
145
relation = relation_id()
123
147
if Server.has_relation(relation_id):
124
__die("Relation %s already exists" % relation_id)
148
die("Relation %s already exists" % relation_id)
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'))
130
154
session.add(server)
132
156
except Exception:
133
157
session.rollback()
134
__die("Cannot create server relation")
158
die("Cannot create server relation")
136
160
update_replication()
140
164
def syslog_relation_departed():
141
Server.remove(hookenv.relation_id())
165
Server.remove(relation_id())
142
166
update_replication()
146
170
def syslog_relation_broken():
147
Server.remove(hookenv.relation_id())
171
Server.remove(relation_id())
148
172
update_replication()
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()