# Copyright 2015 Canonical Ltd.
#
# This file is part of the PostgreSQL Charm for Juju.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3, as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os.path
from charmhelpers.core import hookenv, host, templating
from charms import reactive
class SyslogProvides(reactive.relations.RelationBase):
# TODO: Should this be global scope?
class states(reactive.relations.StateList):
available = reactive.State('{relation_name}.available')
@hook('{provides:syslog}-relation-changed')
def changed(self):
self.set_state('{relation_name}.available')
self.set_local('path', self._rsyslog_conf_path())
self.set_local('remote_unit', hookenv.remote_unit())
self.set_local('remote_address', self.get_remote('private-address'))
@hook('{provides:syslog}-relation-departed')
def departed(self):
path = self.get_local('path')
if os.path.exists(path):
os.remove(path)
reactive.set_state('syslog.needs_restart')
self.remove_state('{relation_name}.available')
self.depart()
def _rsyslog_conf_path(self):
# Use both the local unit and remote unit in the config file
# path to avoid conflicts with subordinates.
rsyslog_conf_dir = '/etc/rsyslog.d'
local = hookenv.local_unit().replace('/', '_')
remote = hookenv.remote_unit().replace('/', '_')
return os.path.join(rsyslog_conf_dir,
'juju-{}-{}.conf'.format(local, remote))
@not_unless('{provides:syslog}.available')
def configure(self, programname):
templating.render('rsyslog_forward.conf', self.get_local('path'),
dict(local_unit=hookenv.local_unit(),
remote_unit=self.get_local('remote_unit'),
remote_addr=self.get_local('remote_address'),
programname=programname))
reactive.set_state('syslog.needs_restart')
@when('syslog.needs_restart')
def restart_rsyslog():
host.service_restart('rsyslog')
reactive.remove_state('syslog.needs_restart')