2014-11-07 14:16:26 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
|
|
|
# Copyright 2014 Canonical Ltd.
|
|
|
|
#
|
2014-11-09 11:54:40 +00:00
|
|
|
import json
|
|
|
|
|
2014-11-07 14:16:26 +01:00
|
|
|
from charmhelpers.core.hookenv import (
|
|
|
|
log,
|
|
|
|
INFO,
|
|
|
|
ERROR
|
|
|
|
)
|
|
|
|
from charmhelpers.contrib.storage.linux.ceph import (
|
|
|
|
create_pool,
|
2014-11-07 15:26:58 +01:00
|
|
|
pool_exists
|
2014-11-07 14:16:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-11-09 11:54:40 +00:00
|
|
|
def decode(f):
|
|
|
|
def decode_inner(req):
|
|
|
|
return json.dumps(f(json.loads(req)))
|
|
|
|
|
|
|
|
return decode_inner
|
|
|
|
|
|
|
|
|
|
|
|
@decode
|
2014-11-07 14:16:26 +01:00
|
|
|
def process_requests(reqs):
|
2014-11-09 11:54:40 +00:00
|
|
|
"""Process a Ceph broker request from a ceph client.
|
|
|
|
|
|
|
|
This is a versioned api. We choose the api version based on provided
|
|
|
|
version from client.
|
|
|
|
"""
|
2014-11-09 12:58:04 +00:00
|
|
|
version = reqs.get('api-version')
|
2014-11-09 11:54:40 +00:00
|
|
|
if version == 1:
|
|
|
|
return process_requests_v1(reqs['ops'])
|
|
|
|
|
|
|
|
msg = ("Missing or invalid api version (%s)" % (version))
|
2014-11-09 12:58:04 +00:00
|
|
|
return {'exit-code': 1, 'stderr': msg}
|
2014-11-09 11:54:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_requests_v1(reqs):
|
|
|
|
"""Process a v1 requests from a ceph client.
|
|
|
|
|
|
|
|
Takes a list of requests (dicts) and processes each one until it hits an
|
|
|
|
error.
|
|
|
|
|
|
|
|
Upon completion of all ops or if an error is found, a response dict is
|
|
|
|
returned containing exit code and any extra info.
|
|
|
|
"""
|
2014-11-07 14:16:26 +01:00
|
|
|
log("Processing %s ceph broker requests" % (len(reqs)), level=INFO)
|
|
|
|
for req in reqs:
|
|
|
|
op = req.get('op')
|
|
|
|
log("Processing op='%s'" % (op), level=INFO)
|
|
|
|
# Use admin client since we do not have other client key locations
|
|
|
|
# setup to use them for these operations.
|
|
|
|
svc = 'admin'
|
2014-11-09 12:58:04 +00:00
|
|
|
if op == "create-pool":
|
2014-11-08 21:13:40 +00:00
|
|
|
params = {'pool': req.get('name'),
|
|
|
|
'replicas': req.get('replicas')}
|
|
|
|
if not all(params.iteritems()):
|
2014-11-09 11:54:40 +00:00
|
|
|
msg = ("Missing parameter(s): %s" %
|
|
|
|
(' '.join([k for k in params.iterkeys()
|
|
|
|
if not params[k]])))
|
|
|
|
log(msg, level=ERROR)
|
2014-11-09 12:58:04 +00:00
|
|
|
return {'exit-code': 1, 'stderr': msg}
|
2014-11-07 14:16:26 +01:00
|
|
|
|
2014-11-08 21:13:40 +00:00
|
|
|
pool = params['pool']
|
|
|
|
replicas = params['replicas']
|
2014-11-07 14:16:26 +01:00
|
|
|
if not pool_exists(service=svc, name=pool):
|
2014-11-08 20:15:17 +00:00
|
|
|
log("Creating pool '%s' (replicas=%s)" % (pool, replicas),
|
|
|
|
level=INFO)
|
2014-11-07 14:16:26 +01:00
|
|
|
create_pool(service=svc, name=pool, replicas=replicas)
|
|
|
|
else:
|
2014-11-08 20:15:17 +00:00
|
|
|
log("Pool '%s' already exists - skipping create" % (pool),
|
|
|
|
level=INFO)
|
|
|
|
else:
|
2014-11-09 11:54:40 +00:00
|
|
|
msg = "Unknown operation '%s'" % (op)
|
|
|
|
log(msg, level=ERROR)
|
2014-11-09 12:58:04 +00:00
|
|
|
return {'exit-code': 1, 'stderr': msg}
|
2014-11-07 14:16:26 +01:00
|
|
|
|
2014-11-09 12:58:04 +00:00
|
|
|
return {'exit-code': 0}
|