Finish up: find_block_device(), save_script_rc(). Add restart map.

This commit is contained in:
Adam Gandelman 2013-07-18 16:07:22 -07:00
parent 5eebc6c787
commit 85501cbb64
5 changed files with 139 additions and 22 deletions

View File

@ -7,11 +7,13 @@ from collections import OrderedDict
import apt_pkg as apt
import subprocess
import os
import socket
import sys
from charmhelpers.core.hookenv import (
config,
log as juju_log,
unit_get,
)
from charmhelpers.core.host import (
@ -269,3 +271,23 @@ def openstack_upgrade_available(package):
available_vers = get_os_version_install_source(src)
apt.init()
return apt.version_compare(available_vers, cur_vers) == 1
def get_host_ip(hostname=None):
hostname = hostname or unit_get('private-address')
try:
import dns.resolver
except ImportError:
apt_install('python-dnspython')
import dns.resolver
try:
# Test to see if already an IPv4 address
socket.inet_aton(hostname)
return hostname
except socket.error:
# This may throw an NXDOMAIN exception; in which case
# things are badly broken so just let it kill the hook
answers = dns.resolver.query(hostname, 'A')
if answers:
return answers[0].address

View File

@ -35,8 +35,11 @@ def create_loopback(file_path):
:returns: str: Full path to new loopback device (eg, /dev/loop0)
'''
cmd = ['losetup', '--find', file_path]
return check_output(cmd).strip()
file_path = os.path.abspath(file_path)
check_call(['losetup', '--find', file_path])
for d, f in loopback_devices().iteritems():
if f == file_path:
return d
def ensure_loopback_device(path, size):

View File

@ -5,12 +5,13 @@ import sys
from swift_storage_utils import (
PACKAGES,
RESTART_MAP,
determine_block_devices,
do_openstack_upgrade,
ensure_swift_directories,
fetch_swift_rings,
register_configs,
swift_init, # move to openstack utils
save_script_rc,
setup_storage,
)
@ -25,6 +26,7 @@ from charmhelpers.core.hookenv import (
from charmhelpers.core.host import (
apt_install,
apt_update,
restart_on_change,
)
@ -37,25 +39,24 @@ hooks = Hooks()
CONFIGS = register_configs()
@hooks.hook()
@hooks.hook('install')
@restart_on_change(RESTART_MAP)
def install():
conf = config()
src = conf['openstack-origin']
configure_installation_source(src)
configure_installation_source(config('openstack-origin'))
apt_update()
apt_install(PACKAGES)
apt_install(PACKAGES, fatal=True)
CONFIGS.write('/etc/rsyncd.conf')
swift_init('all', 'stop')
setup_storage()
ensure_swift_directories()
@hooks.hook()
@hooks.hook('config-changed')
@restart_on_change(RESTART_MAP)
def config_changed():
if openstack_upgrade_available('swift'):
do_openstack_upgrade(configs=CONFIGS)
CONFIGS.write_all()
# TODO: save landscape scriptrc
save_script_rc()
@hooks.hook()
@ -71,13 +72,16 @@ def swift_storage_relation_joined():
relation_set(**rel_settings)
@hooks.hook()
@hooks.hook('swift-storage-relation-changed')
@restart_on_change(RESTART_MAP)
def swift_storage_relation_changed():
rings_url = relation_get('rings_url')
swift_hash = relation_get('swift_hash')
if None in [rings_url, swift_hash]:
if '' in [rings_url, swift_hash] or None in [rings_url, swift_hash]:
log('swift_storage_relation_changed: Peer not ready?')
sys.exit(0)
CONFIGS.write('/etc/swift/swift.conf')
fetch_swift_rings(rings_url)
swift_init('all', 'start')
if '/usr/bin/nosetests' not in sys.argv:
hooks.execute(sys.argv)

View File

@ -1,12 +1,24 @@
import re
import os
from subprocess import check_call, call
# Stuff copied from cinder py charm, needs to go somewhere
# common.
from misc_utils import (
ensure_block_device,
clean_storage,
)
from swift_storage_context import (
SwiftStorageContext,
SwiftStorageServerContext,
RsyncContext,
)
from charmhelpers.core.host import (
mkdir,
mount,
umount as ensure_block_device,
umount as clean_storage,
)
from charmhelpers.core.hookenv import (
@ -15,11 +27,50 @@ from charmhelpers.core.hookenv import (
ERROR,
)
from charmhelpers.contrib.storage.linux.utils import (
is_block_device,
)
from charmhelpers.contrib.openstack.utils import (
get_host_ip,
get_os_codename_package,
save_script_rc as _save_script_rc,
)
from charmhelpers.contrib.openstack import (
templating,
)
PACKAGES = [
'swift', 'swift-account', 'swift-container',
'swift-object' 'xfsprogs' 'gdisk'
'swift', 'swift-account', 'swift-container', 'swift-object',
'xfsprogs', 'gdisk', 'lvm2', 'python-jinja2',
]
TEMPLATES = 'templates/'
ACCOUNT_SVCS = [
'swift-account', 'swift-account-auditor',
'swift-account-reaper', 'swift-account-replicator'
]
CONTAINER_SVCS = [
'swift-container', 'swift-container-auditor',
'swift-container-updater', 'swift-container-replicator'
]
OBJECT_SVCS = [
'swift-object', 'swift-object-auditor',
'swift-object-updater', 'swift-object-replicator'
]
RESTART_MAP = {
'/etc/rsyncd.conf': ['rsync'],
'/etc/swift/account-server.conf': ACCOUNT_SVCS,
'/etc/swift/container-server.conf': CONTAINER_SVCS,
'/etc/swift/object-server.conf': OBJECT_SVCS,
'/etc/swift/swift.conf': ACCOUNT_SVCS + CONTAINER_SVCS + OBJECT_SVCS
}
def ensure_swift_directories():
'''
@ -36,7 +87,17 @@ def ensure_swift_directories():
def register_configs():
return None
release = get_os_codename_package('python-swift', fatal=False) or 'essex'
configs = templating.OSConfigRenderer(templates_dir=TEMPLATES,
openstack_release=release)
configs.register('/etc/swift/swift.conf',
[SwiftStorageContext()])
configs.register('/etc/rsyncd.conf',
[RsyncContext()])
for server in ['account', 'object', 'container']:
configs.register('/etc/swift/%s-server.conf' % server,
[SwiftStorageServerContext()]),
return configs
def swift_init(target, action, fatal=False):
@ -55,7 +116,18 @@ def do_openstack_upgrade(configs):
def find_block_devices():
pass
found = []
incl = ['sd[a-z]', 'vd[a-z]', 'cciss\/c[0-9]d[0-9]']
blacklist = ['sda', 'vda', 'cciss/c0d0']
with open('/proc/partitions') as proc:
partitions = [p.split() for p in proc.readlines()[2:]]
for partition in [p[3] for p in partitions if p]:
for inc in incl:
_re = re.compile(r'^(%s)$' % inc)
if _re.match(partition) and partition not in blacklist:
found.append(os.path.join('/dev', partition))
return [f for f in found if is_block_device(f)]
def determine_block_devices():
@ -89,7 +161,8 @@ def setup_storage():
_mp = os.path.join('/srv', 'node', _dev)
mkdir(_mp, owner='swift', group='swift')
mount(dev, '/srv/node/%s' % _dev, persist=True)
# TODO: chown again post-mount?
check_call(['chown', '-R', 'swift:swift', '/srv/node/'])
check_call(['chmod', '-R', '0750', '/srv/node/'])
def fetch_swift_rings(rings_url):
@ -100,3 +173,18 @@ def fetch_swift_rings(rings_url):
log('Fetching %s.' % url)
cmd = ['wget', url, '-O', '/etc/swift/%s.ring.gz' % server]
check_call(cmd)
def save_script_rc():
env_vars = {}
ip = get_host_ip()
for server in ['account', 'container', 'object']:
port = config('%s-server-port' % server)
url = 'http://%s:%s/recon/diskusage|"mounted":true' % (ip, port)
svc = server.upper()
env_vars.update({
'OPENSTACK_PORT_%s' % svc: port,
'OPENSTACK_SWIFT_SERVICE_%s' % svc: '%s-server' % server,
'OPENSTACK_URL_%s' % svc: url,
})
_save_script_rc(**env_vars)

View File

@ -1 +1 @@
59
87