2013-07-16 21:27:06 -07:00
|
|
|
#!/usr/bin/python
|
2016-07-01 18:15:00 +01:00
|
|
|
#
|
|
|
|
# Copyright 2016 Canonical Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2015-07-17 10:57:19 +01:00
|
|
|
from lib.swift_storage_utils import (
|
2013-07-16 21:27:06 -07:00
|
|
|
PACKAGES,
|
2013-07-18 16:07:22 -07:00
|
|
|
RESTART_MAP,
|
2015-01-09 10:07:27 +00:00
|
|
|
SWIFT_SVCS,
|
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,
|
2015-01-12 13:05:44 +00:00
|
|
|
assert_charm_supports_ipv6,
|
|
|
|
setup_rsync,
|
2016-03-02 12:39:50 +00:00
|
|
|
remember_devices,
|
2015-10-09 03:47:26 +00:00
|
|
|
REQUIRED_INTERFACES,
|
2015-10-13 09:18:52 +00:00
|
|
|
assess_status,
|
2016-03-02 12:39:50 +00:00
|
|
|
ensure_devs_tracked,
|
2016-09-20 13:32:28 +01:00
|
|
|
VERSION_PACKAGE,
|
2013-07-16 21:27:06 -07:00
|
|
|
)
|
|
|
|
|
2015-08-28 11:21:17 +01:00
|
|
|
from lib.misc_utils import pause_aware_restart_on_change
|
|
|
|
|
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,
|
2016-03-02 12:39:50 +00:00
|
|
|
relation_ids,
|
2013-07-16 21:27:06 -07:00
|
|
|
relation_set,
|
2014-10-30 00:52:15 -05:00
|
|
|
relations_of_type,
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set,
|
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
|
|
|
|
)
|
2017-01-16 14:00:07 +01:00
|
|
|
from charmhelpers.core.host import (
|
|
|
|
add_to_updatedb_prunepath,
|
|
|
|
rsync,
|
|
|
|
)
|
2017-02-02 07:56:53 -05:00
|
|
|
|
|
|
|
from charmhelpers.core.sysctl import create as create_sysctl
|
|
|
|
|
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,
|
2015-10-09 03:47:26 +00:00
|
|
|
set_os_workload_status,
|
2016-09-20 13:32:28 +01:00
|
|
|
os_application_version_set,
|
2013-07-16 21:27:06 -07:00
|
|
|
)
|
2014-08-13 14:50:51 +08:00
|
|
|
from charmhelpers.contrib.network.ip import (
|
2017-09-29 11:33:48 +01:00
|
|
|
get_relation_ip,
|
2014-08-13 14:50:51 +08:00
|
|
|
)
|
2015-01-12 12:04:01 +00:00
|
|
|
from charmhelpers.contrib.charmsupport import nrpe
|
2016-03-22 21:00:53 +00:00
|
|
|
from charmhelpers.contrib.hardening.harden import harden
|
2013-07-16 21:27:06 -07:00
|
|
|
|
2014-11-06 17:38:43 +10:00
|
|
|
from distutils.dir_util import mkpath
|
|
|
|
|
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'
|
2017-01-16 14:00:07 +01:00
|
|
|
STORAGE_MOUNT_PATH = '/srv/node'
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
|
2015-09-22 14:57:37 +01:00
|
|
|
@hooks.hook('install.real')
|
2016-03-22 21:00:53 +00:00
|
|
|
@harden()
|
2013-07-16 21:27:06 -07:00
|
|
|
def install():
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set('maintenance', 'Executing pre-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'))
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set('maintenance', 'Installing apt packages')
|
2013-07-16 21:27:06 -07:00
|
|
|
apt_update()
|
2013-07-18 16:07:22 -07:00
|
|
|
apt_install(PACKAGES, fatal=True)
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set('maintenance', 'Setting up storage')
|
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')
|
2015-08-27 09:22:33 +01:00
|
|
|
@pause_aware_restart_on_change(RESTART_MAP)
|
2016-03-22 21:00:53 +00:00
|
|
|
@harden()
|
2013-07-16 21:27:06 -07:00
|
|
|
def config_changed():
|
2014-09-18 21:36:03 +08:00
|
|
|
if config('prefer-ipv6'):
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set('maintenance', 'Configuring 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()
|
2014-11-06 17:38:43 +10:00
|
|
|
setup_rsync()
|
2014-10-30 00:52:15 -05:00
|
|
|
|
2015-09-22 15:58:18 -07:00
|
|
|
if not config('action-managed-upgrade') and \
|
|
|
|
openstack_upgrade_available('swift'):
|
2015-10-08 02:15:44 +00:00
|
|
|
status_set('maintenance', 'Running openstack upgrade')
|
2013-07-16 21:27:06 -07:00
|
|
|
do_openstack_upgrade(configs=CONFIGS)
|
2016-03-02 12:39:50 +00:00
|
|
|
|
|
|
|
setup_storage()
|
|
|
|
|
|
|
|
for rid in relation_ids('swift-storage'):
|
|
|
|
swift_storage_relation_joined(rid=rid)
|
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
CONFIGS.write_all()
|
2014-10-30 00:52:15 -05:00
|
|
|
|
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'):
|
2015-01-09 10:07:27 +00:00
|
|
|
update_nrpe_config()
|
2017-02-02 07:56:53 -05:00
|
|
|
|
|
|
|
sysctl_dict = config('sysctl')
|
|
|
|
if sysctl_dict:
|
|
|
|
create_sysctl(sysctl_dict, '/etc/sysctl.d/50-swift-storage-charm.conf')
|
|
|
|
|
2017-01-16 14:00:07 +01:00
|
|
|
add_to_updatedb_prunepath(STORAGE_MOUNT_PATH)
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
|
2014-10-07 09:37:20 +01:00
|
|
|
@hooks.hook('upgrade-charm')
|
2016-03-22 21:00:53 +00:00
|
|
|
@harden()
|
2014-10-07 09:37:20 +01:00
|
|
|
def upgrade_charm():
|
|
|
|
apt_install(filter_installed_packages(PACKAGES), fatal=True)
|
2014-10-30 00:52:15 -05:00
|
|
|
update_nrpe_config()
|
2016-03-02 12:39:50 +00:00
|
|
|
ensure_devs_tracked()
|
2014-10-07 09:37:20 +01:00
|
|
|
|
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
@hooks.hook()
|
2016-03-02 12:39:50 +00:00
|
|
|
def swift_storage_relation_joined(rid=None):
|
2013-07-16 21:27:06 -07:00
|
|
|
rel_settings = {
|
|
|
|
'zone': config('zone'),
|
|
|
|
'object_port': config('object-server-port'),
|
|
|
|
'container_port': config('container-server-port'),
|
|
|
|
'account_port': config('account-server-port'),
|
|
|
|
}
|
2014-08-13 14:50:51 +08:00
|
|
|
|
2016-03-02 12:39:50 +00:00
|
|
|
devs = determine_block_devices() or []
|
|
|
|
devs = [os.path.basename(d) for d in devs]
|
|
|
|
rel_settings['device'] = ':'.join(devs)
|
|
|
|
# Keep a reference of devices we are adding to the ring
|
|
|
|
remember_devices(devs)
|
|
|
|
|
2017-09-29 11:33:48 +01:00
|
|
|
rel_settings['private-address'] = get_relation_ip('swift-storage')
|
2014-08-13 14:50:51 +08:00
|
|
|
|
2017-09-29 11:33:48 +01:00
|
|
|
relation_set(relation_id=rid, relation_settings=rel_settings)
|
2013-07-16 21:27:06 -07:00
|
|
|
|
|
|
|
|
2013-07-18 16:07:22 -07:00
|
|
|
@hooks.hook('swift-storage-relation-changed')
|
2015-08-27 09:22:33 +01:00
|
|
|
@pause_aware_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)
|
2015-11-24 13:51:05 +00:00
|
|
|
|
|
|
|
CONFIGS.write('/etc/rsync-juju.d/050-swift-storage.conf')
|
2013-07-16 21:27:06 -07:00
|
|
|
CONFIGS.write('/etc/swift/swift.conf')
|
2015-11-24 13:51:05 +00:00
|
|
|
|
2013-07-16 21:27:06 -07:00
|
|
|
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():
|
2015-01-12 12:04:01 +00:00
|
|
|
# python-dbus is used by check_upstart_job
|
|
|
|
apt_install('python-dbus')
|
2014-10-30 00:52:15 -05:00
|
|
|
log('Refreshing nrpe checks')
|
2014-11-06 17:38:43 +10:00
|
|
|
if not os.path.exists(NAGIOS_PLUGINS):
|
|
|
|
mkpath(NAGIOS_PLUGINS)
|
2015-01-12 13:24:00 +00:00
|
|
|
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'))
|
2015-01-12 12:04:01 +00:00
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
# Find out if nrpe set nagios_hostname
|
2015-01-12 12:04:01 +00:00
|
|
|
hostname = nrpe.get_nagios_hostname()
|
|
|
|
current_unit = nrpe.get_nagios_unit_name()
|
|
|
|
nrpe_setup = nrpe.NRPE(hostname=hostname)
|
2014-11-04 17:20:52 +10:00
|
|
|
|
2014-10-30 00:52:15 -05:00
|
|
|
# check the rings and replication
|
2015-01-12 12:04:01 +00:00
|
|
|
nrpe_setup.add_check(
|
2014-10-30 00:52:15 -05:00
|
|
|
shortname='swift_storage',
|
2015-01-09 10:07:27 +00: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'))
|
|
|
|
)
|
2015-01-12 12:04:01 +00:00
|
|
|
nrpe.add_init_service_checks(nrpe_setup, SWIFT_SVCS, current_unit)
|
|
|
|
nrpe_setup.write()
|
2014-10-30 00:52:15 -05:00
|
|
|
|
|
|
|
|
2016-03-22 21:00:53 +00:00
|
|
|
@hooks.hook('update-status')
|
|
|
|
@harden()
|
|
|
|
def update_status():
|
|
|
|
log('Updating status.')
|
|
|
|
|
|
|
|
|
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))
|
2015-10-13 09:18:52 +00:00
|
|
|
set_os_workload_status(CONFIGS, REQUIRED_INTERFACES,
|
|
|
|
charm_func=assess_status)
|
2016-09-20 13:32:28 +01:00
|
|
|
os_application_version_set(VERSION_PACKAGE)
|
2013-07-19 13:44:37 -07:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|