diff --git a/config.yaml b/config.yaml index 0fca4bc..192c1b4 100644 --- a/config.yaml +++ b/config.yaml @@ -14,3 +14,6 @@ options: this value will become the "mon." key to generate a suitable value, use ceph-authtool /dev/stdout --name=mon. --gen-key + source: + type: string + default: ppa:ceph-ubuntu/dev diff --git a/hooks/ceph.py b/hooks/ceph.py new file mode 100644 index 0000000..2049a07 --- /dev/null +++ b/hooks/ceph.py @@ -0,0 +1,26 @@ + +# +# Copyright 2012 Canonical Ltd. +# +# Authors: +# James Page +# + +import subprocess +import json +import os + +QUORUM = [ 'leader', 'peon' ] + +def is_quorum(): + cmd = [ + "ceph", + "--admin-daemon", + "/var/run/ceph/ceph-mon.%s.asok" % os.uname()[1], + "mon_status" + ] + result = json.loads(subprocess.check_output(cmd)) + if result['state'] in QUORUM: + return True + else: + return False diff --git a/hooks/config-changed b/hooks/config-changed new file mode 120000 index 0000000..9416ca6 --- /dev/null +++ b/hooks/config-changed @@ -0,0 +1 @@ +hooks.py \ No newline at end of file diff --git a/hooks/hooks.py b/hooks/hooks.py index b73bbdb..7fad4db 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -11,11 +11,31 @@ import os import subprocess import sys +import ceph +import utils + def install(): - print "install" + utils.juju_log('INFO', 'Begin install hook.') + utils.configure_source() + utils.install('ceph') + + # TODO: Install the upstart scripts. + utils.juju_log('INFO', 'End install hook.') def config_changed(): - print "config_changed" + utils.juju_log('INFO', 'Begin config-changed hook.') + fsid = utils.config_get('fsid') + if fsid == "": + utils.juju_log('CRITICAL', 'No fsid supplied, cannot proceed.') + sys.exit(1) + + monitor_secret = utils.config_get('monitor-secret') + if monitor_secret == "": + utils.juju_log('CRITICAL', 'No monitor-secret supplied, cannot proceed.') + sys.exit(1) + + osd_devices = utils.config_get('osd-devices') + utils.juju_log('INFO', 'End config-changed hook.') def mon_relation(): print "mon_relation" @@ -32,8 +52,7 @@ hook = os.path.basename(sys.argv[0]) try: hooks[hook]() -except: - subprocess.call(['juju-log', '-l', 'INFO', - "This charm doesn't know how to handle '%s'." % hook]) +except KeyError: + utils.juju_log('INFO', "This charm doesn't know how to handle '%s'." % hook) sys.exit(0) diff --git a/hooks/start b/hooks/start deleted file mode 100644 index f27ac7b..0000000 --- a/hooks/start +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -set -e -set -u - - -for disk in $(config-get osd-devices); do - ceph-disk-prepare $disk -done - -udevadm trigger --subsystem-match=block --action=add - -exit 0 diff --git a/hooks/utils.py b/hooks/utils.py new file mode 100644 index 0000000..9b08a90 --- /dev/null +++ b/hooks/utils.py @@ -0,0 +1,130 @@ + +# +# Copyright 2012 Canonical Ltd. +# +# Authors: +# James Page +# +# Taken from lp:~james-page/charms/precise/ganglia/python-refactor +# + +import subprocess +import os +import sys + +def install (*pkgs): + cmd = [ + "apt-get", + "-y", + "install" + ] + for pkg in pkgs: + cmd.append(pkg) + subprocess.check_call(cmd) + +TEMPLATES_DIR="templates" + +try: + import jinja2 +except ImportError: + install('python-jinja2') + import jinja2 + +def render_template (template_name, context, template_dir=TEMPLATES_DIR): + templates = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir)) + template = templates.get_template(template_name) + return template.render(context) + +def configure_source(): + source = config_get("source") + if (source.startswith("ppa:") or + source.startswith("cloud:") or + source.startswith("http:")): + cmd = [ + "add-apt-repository", + source + ] + subprocess.check_call(cmd) + if source.startswith("http:"): + key = config_get("key") + cmd = [ + "apt-key", + "import", + key + ] + subprocess.check_call(cmd) + cmd = [ + "apt-get", + "update" + ] + subprocess.check_call(cmd) + +# Protocols +TCP="TCP" +UDP="UDP" + +def expose(port, protocol="TCP"): + cmd = [ + "open-port", + "%d/%s" % (port,protocol) + ] + subprocess.check_call(cmd) + +def juju_log(message,severity="INFO"): + cmd = [ + "juju-log", + "--log-level", severity, + message + ] + subprocess.check_call(cmd) + +def relation_ids(relation): + cmd = [ + "relation-ids", + relation + ] + return subprocess.check_output(cmd).split() + +def relation_list(rid): + cmd = [ + "relation-list", + "-r", rid, + ] + return subprocess.check_output(cmd).split() + +def relation_get(attribute,unit=None,rid=None): + cmd = [ + "relation-get", + ] + if rid: + cmd.append("-r") + cmd.append(rid) + cmd.append(attribute) + if unit: + cmd.append(unit) + return subprocess.check_output(cmd).strip() + +def relation_set(*kwargs): + cmd = [ + "relation-set" + ] + for k, v in kwargs.items(): + cmd.append("%s=%s" % (k,v)) + subprocess.check_call(cmd) + +def unit_get(attribute): + cmd = [ + "unit-get", + attribute + ] + return subprocess.check_output(cmd).strip() + +def config_get(attribute): + cmd = [ + "config-get", + attribute + ] + return subprocess.check_output(cmd).strip() + +def juju_log(level, message): + subprocess.call(['juju-log', '-l', level, message]) diff --git a/revision b/revision new file mode 100644 index 0000000..b1bd38b --- /dev/null +++ b/revision @@ -0,0 +1 @@ +13 diff --git a/templates/ceph.conf b/templates/ceph.conf new file mode 100644 index 0000000..d3bf006 --- /dev/null +++ b/templates/ceph.conf @@ -0,0 +1,15 @@ +[global] + auth supported = none + keyring = /etc/ceph/$cluster.$name.keyring + mon host = + +[mon] + keyring = /var/lib/ceph/mon/$cluster-$id/keyring + +[mds] + keyring = /var/lib/ceph/mds/$cluster-$id/keyring + +[osd] + keyring = /var/lib/ceph/osd/$cluster-$id/keyring + osd journal size = 1000 + filestore xattr use omap = true