charm-swift-storage/hooks/swift_storage_hooks.py

103 lines
2.4 KiB
Python
Raw Normal View History

2013-07-16 21:27:06 -07:00
#!/usr/bin/python
import os
import sys
from swift_storage_utils import (
PACKAGES,
RESTART_MAP,
2013-07-16 21:27:06 -07:00
determine_block_devices,
do_openstack_upgrade,
ensure_swift_directories,
fetch_swift_rings,
register_configs,
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,
)
2013-09-23 12:01:06 -07:00
from charmhelpers.fetch import apt_install, apt_update
from charmhelpers.core.host import restart_on_change
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
)
2013-07-16 21:27:06 -07:00
hooks = Hooks()
CONFIGS = register_configs()
@hooks.hook()
2013-07-16 21:27:06 -07:00
def install():
execd_preinstall()
configure_installation_source(config('openstack-origin'))
2013-07-16 21:27:06 -07:00
apt_update()
apt_install(PACKAGES, fatal=True)
2013-07-16 21:27:06 -07:00
setup_storage()
ensure_swift_directories()
@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
2013-07-16 21:27:06 -07:00
if openstack_upgrade_available('swift'):
do_openstack_upgrade(configs=CONFIGS)
CONFIGS.write_all()
save_script_rc()
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)
@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')
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-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()