2013-07-16 21:27:06 -07:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from swift_storage_utils import (
|
|
|
|
PACKAGES,
|
2013-07-18 16:07:22 -07:00
|
|
|
RESTART_MAP,
|
2013-07-16 21:27:06 -07:00
|
|
|
determine_block_devices,
|
|
|
|
do_openstack_upgrade,
|
|
|
|
ensure_swift_directories,
|
|
|
|
fetch_swift_rings,
|
|
|
|
register_configs,
|
2013-07-18 16:07:22 -07:00
|
|
|
save_script_rc,
|
2013-07-16 21:27:06 -07:00
|
|
|
setup_storage,
|
2014-09-26 19:20:43 +01:00
|
|
|
assert_charm_supports_ipv6
|
2013-07-16 21:27:06 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
from charmhelpers.core.hookenv import (
|
2013-07-19 13:44:37 -07:00
|
|
|
Hooks, UnregisteredHookError,
|
2013-07-16 21:27:06 -07:00
|
|
|
config,
|
|
|
|
log,
|
|
|
|
relation_get,
|
|
|
|
relation_set,
|
2014-10-30 00:52:15 -05:00
|
|
|
relations_of_type,
|
2014-11-04 17:20:52 +10:00
|
|
|
local_unit,
|
2013-07-16 21:27:06 -07:00
|
|
|
)
|
|
|
|
|
2014-10-07 09:37:20 +01:00
|
|
|
from charmhelpers.fetch import (
|
|
|
|
apt_install,
|
|
|
|
apt_update,
|
|
|
|
filter_installed_packages
|
|
|
|
)
|
2014-10-30 00:52:15 -05:00
|
|
|
from charmhelpers.core.host import restart_on_change, rsync
|
2013-09-27 17:33:06 +01:00
|
|
|
from charmhelpers.payload.execd import execd_preinstall
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
from charmhelpers.contrib.openstack.utils import (
|
|
|
|
configure_installation_source,
|
|
|
|
openstack_upgrade_available,
|
|
|
|
)
|
2014-08-13 14:50:51 +08:00
|
|
|
from charmhelpers.contrib.network.ip import (
|
2014-09-18 21:36:03 +08:00
|
|
|
get_ipv6_addr
|
2014-08-13 14:50:51 +08:00
|
|
|
)
|
2014-10-30 00:52:15 -05:00
|
|
|
from swift_storage_utils import (
|
|
|
|
PACKAGES,
|
|
|
|
RESTART_MAP,
|
|
|
|
SWIFT_SVCS,
|
|
|
|
determine_block_devices,
|
|
|
|
do_openstack_upgrade,
|
|
|
|
ensure_swift_directories,
|
|
|
|
fetch_swift_rings,
|
|
|
|
register_configs,
|
|
|
|
save_script_rc,
|
|
|
|
setup_storage,
|
|
|
|
concat_rsync_fragments,
|
|
|
|
)
|
|
|
|
from charmhelpers.contrib.charmsupport.nrpe import NRPE
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
hooks = Hooks()
|
|
|
|
CONFIGS = register_configs()
|
2014-10-30 00:52:15 -05:00
|
|
|
NAGIOS_PLUGINS = '/usr/local/lib/nagios/plugins'
|
|
|
|
SUDOERS_D = '/etc/sudoers.d'
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
|
2013-07-19 12:52:45 -07:00
|
|
|
@hooks.hook()
|
2013-07-16 21:27:06 -07:00
|
|
|
def install():
|
2013-09-27 17:33:06 +01:00
|
|
|
execd_preinstall()
|
2013-07-18 16:07:22 -07:00
|
|
|
configure_installation_source(config('openstack-origin'))
|
2013-07-16 21:27:06 -07:00
|
|
|
apt_update()
|
2013-07-18 16:07:22 -07:00
|
|
|
apt_install(PACKAGES, fatal=True)
|
2013-07-16 21:27:06 -07:00
|
|
|
setup_storage()
|
|
|
|
ensure_swift_directories()
|
|
|
|
|
|
|
|
|
2013-07-18 16:07:22 -07:00
|
|
|
@hooks.hook('config-changed')
|
|
|
|
@restart_on_change(RESTART_MAP)
|
2013-07-16 21:27:06 -07:00
|
|
|
def config_changed():
|
2014-09-18 21:36:03 +08:00
|
|
|
if config('prefer-ipv6'):
|
2014-09-26 19:20:43 +01:00
|
|
|
assert_charm_supports_ipv6()
|
2014-09-18 21:36:03 +08:00
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
ensure_swift_directories()
|
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
if openstack_upgrade_available('swift'):
|
|
|
|
do_openstack_upgrade(configs=CONFIGS)
|
|
|
|
CONFIGS.write_all()
|
2014-10-30 00:52:15 -05:00
|
|
|
|
|
|
|
# If basenode is not installed and managing rsyncd.conf, replicate
|
|
|
|
# its core functionality. Otherwise concat files
|
2014-10-30 17:10:47 +10:00
|
|
|
if not os.path.exists('/etc/rsyncd.d/001-baseconfig'):
|
2014-10-30 00:52:15 -05:00
|
|
|
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()
|
|
|
|
|
2013-07-18 16:07:22 -07:00
|
|
|
save_script_rc()
|
2014-10-30 00:52:15 -05:00
|
|
|
if relations_of_type('nrpe-external-master'):
|
|
|
|
update_nrpe_config()
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
|
2014-10-07 09:37:20 +01:00
|
|
|
@hooks.hook('upgrade-charm')
|
|
|
|
def upgrade_charm():
|
|
|
|
apt_install(filter_installed_packages(PACKAGES), fatal=True)
|
2014-10-30 00:52:15 -05:00
|
|
|
update_nrpe_config()
|
2014-10-07 09:37:20 +01:00
|
|
|
|
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
@hooks.hook()
|
|
|
|
def swift_storage_relation_joined():
|
|
|
|
devs = [os.path.basename(dev) for dev in determine_block_devices()]
|
|
|
|
rel_settings = {
|
|
|
|
'zone': config('zone'),
|
|
|
|
'object_port': config('object-server-port'),
|
|
|
|
'container_port': config('container-server-port'),
|
|
|
|
'account_port': config('account-server-port'),
|
|
|
|
'device': ':'.join(devs),
|
|
|
|
}
|
2014-08-13 14:50:51 +08:00
|
|
|
|
|
|
|
if config('prefer-ipv6'):
|
2014-09-20 00:59:42 +08:00
|
|
|
rel_settings['private-address'] = get_ipv6_addr()[0]
|
2014-08-13 14:50:51 +08:00
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
relation_set(**rel_settings)
|
|
|
|
|
|
|
|
|
2013-07-18 16:07:22 -07:00
|
|
|
@hooks.hook('swift-storage-relation-changed')
|
|
|
|
@restart_on_change(RESTART_MAP)
|
2013-07-16 21:27:06 -07:00
|
|
|
def swift_storage_relation_changed():
|
|
|
|
rings_url = relation_get('rings_url')
|
|
|
|
swift_hash = relation_get('swift_hash')
|
2013-07-18 16:07:22 -07:00
|
|
|
if '' in [rings_url, swift_hash] or None in [rings_url, swift_hash]:
|
2013-07-16 21:27:06 -07:00
|
|
|
log('swift_storage_relation_changed: Peer not ready?')
|
|
|
|
sys.exit(0)
|
|
|
|
CONFIGS.write('/etc/swift/swift.conf')
|
|
|
|
fetch_swift_rings(rings_url)
|
2013-07-18 16:07:22 -07:00
|
|
|
|
2013-07-19 13:44:37 -07:00
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
@hooks.hook('nrpe-external-master-relation-joined')
|
|
|
|
@hooks.hook('nrpe-external-master-relation-changed')
|
|
|
|
def update_nrpe_config():
|
|
|
|
log('Refreshing nrpe checks')
|
|
|
|
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'))
|
|
|
|
rsync(os.path.join(os.getenv('CHARM_DIR'), 'files', 'nrpe-external-master',
|
|
|
|
'check_swift_service'),
|
|
|
|
os.path.join(NAGIOS_PLUGINS, 'check_swift_service'))
|
|
|
|
rsync(os.path.join(os.getenv('CHARM_DIR'), 'files', 'sudo',
|
|
|
|
'swift-storage'),
|
|
|
|
os.path.join(SUDOERS_D, 'swift-storage'))
|
|
|
|
# Find out if nrpe set nagios_hostname
|
|
|
|
hostname = None
|
|
|
|
for rel in relations_of_type('nrpe-external-master'):
|
|
|
|
if 'nagios_hostname' in rel:
|
|
|
|
hostname = rel['nagios_hostname']
|
2014-11-04 17:20:52 +10:00
|
|
|
host_context = rel['nagios_host_context']
|
2014-10-30 00:52:15 -05:00
|
|
|
break
|
|
|
|
nrpe = NRPE(hostname=hostname)
|
2014-11-04 17:20:52 +10:00
|
|
|
|
|
|
|
current_unit = "%s:%s" % (host_context, local_unit())
|
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
# check the rings and replication
|
|
|
|
nrpe.add_check(
|
|
|
|
shortname='swift_storage',
|
2014-11-04 17:20:52 +10:00
|
|
|
description='Check swift storage ring hashes and replication {%s}' % current_unit,
|
2014-10-30 00:52:15 -05:00
|
|
|
check_cmd='check_swift_storage.py {}'.format(
|
|
|
|
config('nagios-check-params'))
|
|
|
|
)
|
|
|
|
# check services are running
|
|
|
|
for service in SWIFT_SVCS:
|
|
|
|
nrpe.add_check(
|
|
|
|
shortname=service,
|
2014-11-04 17:20:52 +10:00
|
|
|
description='service {%s}' % current_unit,
|
2014-10-30 00:52:15 -05:00
|
|
|
check_cmd = 'check_swift_service %s' % service,
|
|
|
|
)
|
|
|
|
nrpe.write()
|
|
|
|
|
|
|
|
|
2013-07-19 13:44:37 -07:00
|
|
|
def main():
|
|
|
|
try:
|
|
|
|
hooks.execute(sys.argv)
|
|
|
|
except UnregisteredHookError as e:
|
|
|
|
log('Unknown hook {} - skipping.'.format(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|