Ejemplo n.º 1
0
def create_connection():
    """ create mongo mongodb connection """
    try:
        for unit in model.get_units("mongodb"):
            logging.info("Checking if the unit mongodb is active: %s",
                         unit.entity_id)
            logging.info("checking for mongodb connection....")
            mongodb_ip = model.get_status().applications["mongodb"]["units"][
                unit.entity_id]["address"]
            myclient = pymongo.MongoClient("mongodb://" + mongodb_ip +
                                           ":27017/")
            logging.info("Mongodb connected successfully !!!")
    except pymongo.errors.ConnectionFailure:
        logging.info("Could not connect to Mongomongodb")
    return myclient
Ejemplo n.º 2
0
def get_unit_name_from_host_name(host_name, application):
    """Return the juju unit name corresponding to a hostname.

    :param host_name: Host name to map to unit name.
    :type host_name: string
    :param application: Application name
    :type application: string
    :returns: The unit name of the application running on host_name.
    :rtype: str or None
    """
    # Assume that a juju managed hostname always ends in the machine number.
    machine_number = host_name.split('-')[-1]
    unit = None
    app_status = get_application_status(application)
    # If the application is not present there cannot be a matching unit.
    if not app_status:
        return unit
    if is_subordinate_application(application, application_status=app_status):
        # Find the principle services that the subordinate relates to. There
        # may be multiple.
        principle_services = get_principle_applications(
            application, application_status=app_status)
        for principle_service in principle_services:
            # Find the principle unit name that matches the provided
            # hostname.
            principle_unit = get_unit_name_from_host_name(
                host_name, principle_service)
            # If the subordinate has been related to mulitple principles then
            # principle_service may not be running on host_name.
            if principle_unit:
                unit_status = get_application_status(unit=principle_unit)
                unit_names = list(unit_status['subordinates'].keys())
                # The principle may have subordinates related to it other than
                # the 'application' so search through them looking for a match.
                for unit_name in unit_names:
                    if unit_name.split('/')[0] == application:
                        unit = unit_name
    else:
        unit_names = [
            u.entity_id for u in model.get_units(application_name=application)
            if int(u.data['machine-id']) == int(machine_number)
        ]
        if unit_names:
            unit = unit_names[0]
    return unit
Ejemplo n.º 3
0
def encrypt_vip_endpoints():
    """Apply certs and keys to all charms that support them."""
    (cakey, cacert) = create_ca()
    ssl_vip_keys = ['vip', 'ssl_ca', 'ssl_cert', 'ssl_key']
    status = model.get_status()
    for application_name in status.applications.keys():
        app_config = model.get_application_config(application_name)
        if all(k in app_config for k in ssl_vip_keys):
            cn = app_config['vip'].get('value')
            # If there is no vip check if its a non-ha deploy.
            if not cn:
                units = model.get_units(application_name)
                if len(units) == 1:
                    cn = units[0].public_address
            if cn:
                create_certs(application_name, cn, ISSUER_NAME, cakey)
                apply_certs(application_name)
    model.block_until_all_units_idle()
Ejemplo n.º 4
0
def get_application_ip(application):
    """Get the application's IP address.

    :param application: Application name
    :type application: str
    :returns: Application's IP address
    :rtype: str
    """
    try:
        app_config = model.get_application_config(application)
    except KeyError:
        return ''
    vip = app_config.get("vip").get("value")
    if vip:
        ip = vip
    else:
        unit = model.get_units(application)[0]
        ip = unit.public_address
    return ip
Ejemplo n.º 5
0
 def test_kafka_deployment(self):
     first_unit = model.get_units('kafka')[0]
     result = model.run_on_leader('ubuntu', 'lsb_release -cs')
     self.assertEqual(result['Code'], '0')
     self.assertEqual(result['Stdout'].strip(), first_unit.series)
def get_juju_unit_ip(unit):
    application_name = unit.split('/')[0]
    unit_obj = model.get_units(application_name)[0]
    unit_ip = unit_obj.public_address
    return unit_ip