示例#1
0
文件: hooks.py 项目: 0atman/wsgi-app
def link_database(
    scheme,
    database_host,
    port='',
    username='',
    password='',
    database_name='',
    variable_name='DATABASE_URL'
):
    """
    Create a link with a database by setting an environment variable
    The variable will be "DATABASE_URL" by default
    """

    database_url = build_url(
        scheme=scheme,
        domain=database_host,
        port=port,
        username=username,
        password=password,
        path=database_name
    )

    update_property_in_json_file(
        env_file_path, variable_name, database_url
    )

    # Relation changed - re-run update target
    update_target()

    # Reset wsgi relation settings
    wsgi_relation()
示例#2
0
文件: hooks.py 项目: 0atman/wsgi-app
def webservice_relation():
    """
    Create "WEBSERVICE_URL" environment variable from relation
    """

    log('Function: webservice_relation')
    http_protocol = relation_get('http_protocol') or 'http'
    address = relation_get('private-address')
    hostname = relation_get('hostname') or address

    # If hostname is IP address or FQDN, use it
    # otherwise use private_address
    ip_regex = re.compile(
        (
            r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)"
            r"{3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
        )
    )
    hostname_regex = re.compile(
        (
            r"^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+"
            r"([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$"
        )
    )

    if hostname_regex.match(hostname) or ip_regex.match(hostname):
        domain = hostname
    else:
        domain = address

    webservice_url = build_url(
        scheme=http_protocol,
        domain=domain,
        port=relation_get("port")
    )

    update_property_in_json_file(
        env_file_path, 'WEBSERVICE_URL', webservice_url
    )

    # Relation changed - re-run update target
    update_target()

    # Reset wsgi relation settings
    wsgi_relation()