示例#1
0
def ostack_discover_task(cluster_id):
    db = get_db()
    cluster_doc = db['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        logger.error('Cluster with ID=%s was not found' % cluster_id)
        return

    cluster = Cluster.from_doc(cluster_doc)

    logger.info('Starting OpenStack discovery for cluster "%s" (id=%s)' %
                (cluster.name, cluster.id))

    discovery = OpenstackDiscovery()

    openstack = None
    try:
        openstack = discovery.discover(cluster.nodes,
                                       cluster.private_key)
    except Exception:
        message = traceback.format_exc()
        logger.error(message)

    logger.info('Finished OpenStack discovery for cluster "%s" (id=%s)' %
                (cluster.name, cluster.id))

    cluster.data = openstack_for_json(openstack)

    db['clusters'].save(cluster.as_doc())
示例#2
0
def add_cluster():
    data = json.loads(request.data)
    errors = {}
    if 'nodes' in data and (isinstance(data['nodes'], str) or
                            isinstance(data['nodes'], unicode)):
        data['nodes'] = data['nodes'].split()

    if not 'name' in data or data['name'] == '':
        errors['name'] = ['Cluster name is required']
    if not 'nodes' in data or data['nodes'] == []:
        errors['nodes'] = ['At least one cluster node is required']
    if 'private_key' not in data:
        errors['private_key'] = ['Private key for accessing nodes is required']
    elif not is_key_valid(data['private_key']):
        errors['private_key'] = ['Private key format is unknown']

    if len(errors) > 0:
        return json.dumps(dict(errors=errors)), 422

    cluster = Cluster(**data)

    cluster_id = get_db()['clusters'].save(cluster.as_doc())

    ostack_discover_task.delay(str(cluster_id))

    return json.dumps(dict(id=str(cluster_id))), 201
示例#3
0
def add_cluster():
    data = json.loads(request.data)
    errors = {}
    if 'nodes' in data and (isinstance(data['nodes'], str)
                            or isinstance(data['nodes'], unicode)):
        data['nodes'] = data['nodes'].split()

    if not 'name' in data or data['name'] == '':
        errors['name'] = ['Cluster name is required']
    if not 'nodes' in data or data['nodes'] == []:
        errors['nodes'] = ['At least one cluster node is required']
    if 'private_key' not in data:
        errors['private_key'] = ['Private key for accessing nodes is required']
    elif not is_key_valid(data['private_key']):
        errors['private_key'] = ['Private key format is unknown']

    if len(errors) > 0:
        return json.dumps(dict(errors=errors)), 422

    cluster = Cluster(**data)

    cluster_id = get_db()['clusters'].save(cluster.as_doc())

    ostack_discover_task.delay(str(cluster_id))

    return json.dumps(dict(id=str(cluster_id))), 201
示例#4
0
文件: celery.py 项目: plomakin/rubick
def ostack_discover_task(cluster_id):
    db = get_db()
    cluster_doc = db['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        logger.error('Cluster with ID=%s was not found' % cluster_id)
        return

    cluster = Cluster.from_doc(cluster_doc)

    logger.info('Starting OpenStack discovery for cluster "%s" (id=%s)' %
                (cluster.name, cluster.id))

    discovery = OpenstackDiscovery()

    openstack = None
    try:
        openstack = discovery.discover(cluster.nodes, cluster.private_key)
    except:
        message = traceback.format_exc()
        logger.error(message)

    logger.info('Finished OpenStack discovery for cluster "%s" (id=%s)' %
                (cluster.name, cluster.id))

    cluster.data = openstack_for_json(openstack)

    db['clusters'].save(cluster.as_doc())
示例#5
0
def get_cluster(cluster_id):
    cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404

    cluster = Cluster.from_doc(cluster_doc)

    return json.dumps(cluster.for_json()), 200
示例#6
0
def discover_cluster(cluster_id):
    cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404

    ostack_discover_task.delay(cluster_id)

    return '', 200
示例#7
0
def get_cluster(cluster_id):
    cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404

    cluster = Cluster.from_doc(cluster_doc)

    return json.dumps(cluster.for_json()), 200
示例#8
0
def discover_cluster(cluster_id):
    cluster_doc = get_db()['clusters'].find_one({'_id': ObjectId(cluster_id)})
    if not cluster_doc:
        return json.dumps({'errors': {'cluster_id': 'Cluster not found'}}), 404

    ostack_discover_task.delay(cluster_id)

    return '', 200
示例#9
0
def launch_validation():
    form = ValidateClusterForm()
    if form.validate_on_submit():
        db = get_db()
        cluster_doc = db['clusters'].find_one({
            '_id': ObjectId(form.cluster_id.data)})
        if not cluster_doc:
            return json.dumps({'errors': {
                'cluster_id': 'Cluster not found'}}), 404

        cluster = Cluster.from_doc(cluster_doc)
        request = InspectionRequest(
            cluster.nodes,
            username='******',
            private_key=cluster.private_key)

        job = ostack_inspect_task.delay(request)

        return json.dumps({'id': job.id}), 202
    else:
        return json.dumps(dict(errors=form.errors)), 422
示例#10
0
def launch_validation():
    form = ValidateClusterForm()
    if form.validate_on_submit():
        db = get_db()
        cluster_doc = db['clusters'].find_one(
            {'_id': ObjectId(form.cluster_id.data)})
        if not cluster_doc:
            return json.dumps({'errors': {
                'cluster_id': 'Cluster not found'
            }}), 404

        cluster = Cluster.from_doc(cluster_doc)
        request = InspectionRequest(cluster.nodes,
                                    username='******',
                                    private_key=cluster.private_key)

        job = ostack_inspect_task.delay(request)

        return json.dumps({'id': job.id}), 202
    else:
        return json.dumps(dict(errors=form.errors)), 422
示例#11
0
def get_clusters():
    db = get_db()
    return json.dumps([Cluster.from_doc(doc) for doc in db['clusters'].find()])
示例#12
0
def del_cluster(cluster_id):
    get_db()['clusters'].remove({'_id': ObjectId(cluster_id)})
    return '', 200
示例#13
0
def get_clusters():
    db = get_db()
    return json.dumps([Cluster.from_doc(doc) for doc in db['clusters'].find()])
示例#14
0
def del_cluster(cluster_id):
    get_db()['clusters'].remove({'_id': ObjectId(cluster_id)})
    return '', 200