From d1e2c14274e010a795b858e39871238c98476a06 Mon Sep 17 00:00:00 2001 From: Brad Marshall Date: Thu, 6 Nov 2014 17:38:43 +1000 Subject: [PATCH] [bradm] Fixed rsync to use /etc/rsync-juju.d, check if host_context is defined before using it --- .../contrib/charmsupport/rsync.py | 32 +++++++++++++++++++ hooks/swift_storage_hooks.py | 25 +++++++-------- hooks/swift_storage_utils.py | 12 ++----- ...0-swift-storage => 050-swift-storage.conf} | 0 4 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 hooks/charmhelpers/contrib/charmsupport/rsync.py rename templates/{050-swift-storage => 050-swift-storage.conf} (100%) diff --git a/hooks/charmhelpers/contrib/charmsupport/rsync.py b/hooks/charmhelpers/contrib/charmsupport/rsync.py new file mode 100644 index 0000000..89bf841 --- /dev/null +++ b/hooks/charmhelpers/contrib/charmsupport/rsync.py @@ -0,0 +1,32 @@ +""" +Support for rsyncd.conf and using fragments dropped inside /etc/rsync-juju.d +""" +import os + +from charmhelpers.core.host import ( + mkdir, +) + +def setup_rsync(): + ''' + Ensure all directories required for rsync exist with correct permissions. + ''' + root_dirs = [ + '/etc/rsync-juju.d', + ] + [mkdir(d, owner='root', group='root') for d in root_dirs + if not os.path.isdir(d)] + + rsyncd_base = """uid = nobody +gid = nogroup +pid file = /var/run/rsyncd.pid +syslog facility = daemon +socket options = SO_KEEPALIVE + +&include /etc/rsync-juju.d +""" + + f = open('/etc/rsyncd.conf','w') + f.write(rsyncd_base) + f.close() + diff --git a/hooks/swift_storage_hooks.py b/hooks/swift_storage_hooks.py index df0d456..8a01f6a 100755 --- a/hooks/swift_storage_hooks.py +++ b/hooks/swift_storage_hooks.py @@ -56,6 +56,10 @@ from swift_storage_utils import ( ) from charmhelpers.contrib.charmsupport.nrpe import NRPE +from charmhelpers.contrib.charmsupport.rsync import setup_rsync + +from distutils.dir_util import mkpath + hooks = Hooks() CONFIGS = register_configs() NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins' @@ -79,23 +83,12 @@ def config_changed(): assert_charm_supports_ipv6() ensure_swift_directories() + setup_rsync() if openstack_upgrade_available('swift'): do_openstack_upgrade(configs=CONFIGS) CONFIGS.write_all() - # If basenode is not installed and managing rsyncd.conf, replicate - # its core functionality. Otherwise concat files - if not os.path.exists('/etc/rsyncd.d/001-baseconfig'): - with open('/etc/rsyncd.d/001-baseconfig') as _in: - rsync_header = _in.read() - with open('/etc/rsyncd.d/050-swift-storage') as _in: - rsync_fragment = _in.read() - with open('/etc/rsyncd.conf', 'w') as out: - out.write(rsync_header + rsync_fragment) - else: - concat_rsync_fragments() - save_script_rc() if relations_of_type('nrpe-external-master'): update_nrpe_config() @@ -140,6 +133,8 @@ def swift_storage_relation_changed(): @hooks.hook('nrpe-external-master-relation-changed') def update_nrpe_config(): log('Refreshing nrpe checks') + if not os.path.exists(NAGIOS_PLUGINS): + mkpath(NAGIOS_PLUGINS) rsync(os.path.join(os.getenv('CHARM_DIR'), 'files', 'nrpe-external-master', 'check_swift_storage.py'), os.path.join(NAGIOS_PLUGINS, 'check_swift_storage.py')) @@ -151,6 +146,7 @@ def update_nrpe_config(): os.path.join(SUDOERS_D, 'swift-storage')) # Find out if nrpe set nagios_hostname hostname = None + host_context = None for rel in relations_of_type('nrpe-external-master'): if 'nagios_hostname' in rel: hostname = rel['nagios_hostname'] @@ -158,7 +154,10 @@ def update_nrpe_config(): break nrpe = NRPE(hostname=hostname) - current_unit = "%s:%s" % (host_context, local_unit()) + if host_context: + current_unit = "%s:%s" % (host_context, local_unit()) + else: + current_unit = local_unit() # check the rings and replication nrpe.add_check( diff --git a/hooks/swift_storage_utils.py b/hooks/swift_storage_utils.py index 23d76d8..d45c904 100644 --- a/hooks/swift_storage_utils.py +++ b/hooks/swift_storage_utils.py @@ -92,8 +92,7 @@ SWIFT_SVCS = [ ] RESTART_MAP = { - '/etc/rsyncd.d/001-baseconfig': ['rsync'], - '/etc/rsyncd.d/050-swift-storage': ['rsync'], + '/etc/rsync-juju.d/050-swift-storage.conf': ['rsync'], '/etc/swift/account-server.conf': ACCOUNT_SVCS, '/etc/swift/container-server.conf': CONTAINER_SVCS, '/etc/swift/object-server.conf': OBJECT_SVCS, @@ -113,11 +112,6 @@ def ensure_swift_directories(): ] [mkdir(d, owner='swift', group='swift') for d in dirs if not os.path.isdir(d)] - root_dirs = [ - '/etc/rsyncd.d', - ] - [mkdir(d, owner='root', group='root') for d in root_dirs - if not os.path.isdir(d)] def register_configs(): @@ -126,9 +120,7 @@ def register_configs(): openstack_release=release) configs.register('/etc/swift/swift.conf', [SwiftStorageContext()]) - configs.register('/etc/rsyncd.d/001-baseconfig', - [RsyncContext(), SwiftStorageServerContext()]) - configs.register('/etc/rsyncd.d/050-swift-storage', + configs.register('/etc/rsync-juju.d/050-swift-storage.conf', [RsyncContext(), SwiftStorageServerContext()]) for server in ['account', 'object', 'container']: configs.register('/etc/swift/%s-server.conf' % server, diff --git a/templates/050-swift-storage b/templates/050-swift-storage.conf similarity index 100% rename from templates/050-swift-storage rename to templates/050-swift-storage.conf