Ejemplo n.º 1
0
    def deploy(self, request_body):
        """
            Deploy an API from a given JSON request by calling pydeployr's API method
        """
        obj_to_store = self.read_json(request_body)
        self.validate_json(obj_to_store, ['api_id', 'api_key', 'entities', 'log_level', 'genapi_version'])

        # retrieve Riak loadbalancer information from deployr.conf
        config = ConfigLoader(config=config_service.load_configuration())

        deploy_result = deploy_api(
            api_id=obj_to_store['api_id'],
            api_key=obj_to_store['api_key'],
            entities=obj_to_store['entities'],
            db_host=config.loadbalancer_host,
            db_port=config.loadbalancer_riak_pb_port,
            genapi_version=obj_to_store['genapi_version'],
            log_level=obj_to_store['log_level']
        ).to_dict()
        logging.info('Received result from deploy job: {}'.format(deploy_result))

        db_result = self.repository.add(
            object_id=uuid.uuid1().hex,
            data={
                u'api_id': obj_to_store['api_id'],
                u'api_key': obj_to_store['api_key'],
                u'entities': obj_to_store['entities'],
                u'db_host': config.loadbalancer_host,
                u'db_port': config.loadbalancer_riak_pb_port,
                u'log_level': obj_to_store['log_level'],
                u'status': deploy_result['status'],
                u'genapi_version': deploy_result['genapi_version'],
                u'app_host': deploy_result['api_host'],
                u'app_port': deploy_result['api_port'],
                u'created_at': deploy_result['created_at']
            }
        )
        logging.info(
            "Received result from DB store: ID = {} -- DATA = {}".format(db_result._key, db_result.get_data()))

        if 'status' in db_result.get_data():
            assert db_result.get_data()['status'] == 0

        loadbalance_deploy_result = self.loadbalance_deploy(
            json.dumps(
                {
                    'api_id': obj_to_store['api_id'],
                    'api_host': deploy_result['api_host'],
                    'api_port': deploy_result['api_port']
                }
            )
        ).to_dict()

        deploy_result.update(loadbalance_deploy_result)
        logging.info('Merged result: {}'.format(deploy_result))
        return deploy_result
Ejemplo n.º 2
0
    
    Copyright (c) 2012 - 2013 apitrary

"""
from __future__ import absolute_import
import logging
from celery import Celery
from kombu.entity import Queue
from pytools.pybalancr.loadbalance_update_repository import loadbalance_update_api, loadbalance_remove_api
from pytools.pydeployr.config_loader import ConfigLoader
from pytools.pydeployr.returncodes import RETURNCODE
from pytools.pydeployr.messages.loadbalance_remove_confirmation_message import LoadbalanceRemoveConfirmationMessage
from pytools.pydeployr.messages.loadbalance_update_confirmation_message import LoadbalanceUpdateConfirmationMessage
from pytools.pydeployr.services import config_service

config = ConfigLoader(config=config_service.load_configuration())
broker_address = 'amqp://{user}:{password}@{host}:{port}'.format(
    user=config.rmq_broker_username,
    password=config.rmq_broker_password,
    host=config.rmq_broker_host,
    port=config.rmq_broker_port
)

celery = Celery(
    'pytools.pybalancr.balancr_tasks',
    broker=broker_address,
    backend=broker_address,
    include=['pytools.pybalancr.balancr_tasks']
)
celery.conf.update(
    CELERY_DEFAULT_QUEUE='balancr.default',