def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    if not vcap_services or 'schnapps' not in vcap_services:
        logger.debug("No backup service detected")
        return

    backup_service = {}
    if 'amazon-s3' in vcap_services:
        s3_credentials = vcap_services['amazon-s3'][0]['credentials']
        backup_service['filesCredentials'] = {
            'accessKey': s3_credentials['access_key_id'],
            'secretKey': s3_credentials['secret_access_key'],
            'bucketName': s3_credentials['bucket'],
        }
        if 'key_suffix' in s3_credentials:  # Not all s3 plans have this field
            backup_service['filesCredentials']['keySuffix'] = s3_credentials['key_suffix']

    try:
        db_config = buildpackutil.get_database_config()
        if db_config['DatabaseType'] != 'PostgreSQL':
            raise Exception(
                'Schnapps only supports postgresql, not %s'
                % db_config['DatabaseType']
            )
        host_and_port = db_config['DatabaseHost'].split(':')
        backup_service['databaseCredentials'] = {
            'host': host_and_port[0],
            'username': db_config['DatabaseUserName'],
            'password': db_config['DatabasePassword'],
            'dbname': db_config['DatabaseName'],
            'port': int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    except Exception as e:
        logger.exception(
            'Schnapps will not be activated because error occurred with '
            'parsing the database credentials'
        )
        return
    schnapps_url = vcap_services['schnapps'][0]['credentials']['url']
    schnapps_api_key = vcap_services['schnapps'][0]['credentials']['apiKey']

    try:
        result = requests.put(
            schnapps_url,
            headers={
                'Content-Type': 'application/json',
                'apiKey': schnapps_api_key
            },
            data=json.dumps(backup_service),
        )
    except Exception as e:
        logger.warning('Failed to contact backup service: ' + e)
        return

    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)
def get_license_subscription():
    try:
        vcap_services = buildpackutil.get_vcap_services_data()
        if "mendix-platform" in vcap_services:
            subscription = vcap_services["mendix-platform"][0]
            logger.debug("Configuring license subscription for %s" %
                         subscription["name"])
            credentials = subscription["credentials"]
            return {
                "License.EnvironmentName": credentials["environment_id"],
                "License.LicenseServerURL": credentials["license_server_url"],
                "License.SubscriptionSecret": credentials["secret"],
                "License.UseLicenseServer": True,
            }
    except Exception as e:
        logger.warning("Failed to configure license subscription: " + str(e))
    return {}
Exemple #3
0
def get_filestore_config(m2ee):
    vcap_services = buildpackutil.get_vcap_services_data()

    config = _get_s3_specific_config(vcap_services, m2ee)

    if config is None:
        config = _get_swift_specific_config(vcap_services, m2ee)

    if config is None:
        config = _get_azure_storage_specific_config(vcap_services, m2ee)

    if config is None:
        logger.warning(
            'External file store not configured, uploaded files in the app '
            'will not persist across restarts. See https://github.com/mendix/'
            'cf-mendix-buildpack for file store configuration details.')
        return {}
    else:
        return config
Exemple #4
0
def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    if not vcap_services or 'schnapps' not in vcap_services:
        logger.info("No backup service detected")
        return

    backup_service = {}
    if 'amazon-s3' in vcap_services:
        s3_credentials = vcap_services['amazon-s3'][0]['credentials']
        backup_service['filesCredentials'] = {
            'accessKey': s3_credentials['access_key_id'],
            'secretKey': s3_credentials['secret_access_key'],
            'bucketName': s3_credentials['bucket'],
        }
        if 'key_suffix' in s3_credentials:  # Not all s3 plans have this field
            backup_service['filesCredentials']['keySuffix'] = s3_credentials['key_suffix']

    if 'PostgreSQL' in vcap_services:
        db_config = buildpackutil.get_database_config()
        host_and_port = db_config['DatabaseHost'].split(':')
        backup_service['databaseCredentials'] = {
            'host': host_and_port[0],
            'username': db_config['DatabaseUserName'],
            'password': db_config['DatabasePassword'],
            'dbname': db_config['DatabaseName'],
            'port': int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    schnapps_url = vcap_services['schnapps'][0]['credentials']['url']
    schnapps_api_key = vcap_services['schnapps'][0]['credentials']['apiKey']

    result = requests.put(
        schnapps_url,
        headers={
            'Content-Type': 'application/json',
            'apiKey': schnapps_api_key
        },
        data=json.dumps(backup_service),
    )
    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)
 def __init__(self):
     self.vcap_services = buildpackutil.get_vcap_services_data()
def service_backups():
    vcap_services = buildpackutil.get_vcap_services_data()
    schnapps = None
    amazon_s3 = None
    for key in vcap_services:
        if key.startswith("amazon-s3"):
            amazon_s3 = key
        if key.startswith("schnapps"):
            schnapps = key

    if not vcap_services or schnapps not in vcap_services:
        logger.debug("No backup service detected")
        return

    backup_service = {}
    if amazon_s3 in vcap_services:
        s3_credentials = vcap_services[amazon_s3][0]["credentials"]
        backup_service["filesCredentials"] = {
            "accessKey": s3_credentials["access_key_id"],
            "secretKey": s3_credentials["secret_access_key"],
            "bucketName": s3_credentials["bucket"],
        }
        if "key_suffix" in s3_credentials:  # Not all s3 plans have this field
            backup_service["filesCredentials"]["keySuffix"] = s3_credentials[
                "key_suffix"
            ]

    try:
        db_config = buildpackutil.get_database_config()
        if db_config["DatabaseType"] != "PostgreSQL":
            raise Exception(
                "Schnapps only supports postgresql, not %s"
                % db_config["DatabaseType"]
            )
        host_and_port = db_config["DatabaseHost"].split(":")
        backup_service["databaseCredentials"] = {
            "host": host_and_port[0],
            "username": db_config["DatabaseUserName"],
            "password": db_config["DatabasePassword"],
            "dbname": db_config["DatabaseName"],
            "port": int(host_and_port[1]) if len(host_and_port) > 1 else 5432,
        }
    except Exception as e:
        logger.exception(
            "Schnapps will not be activated because error occurred with "
            "parsing the database credentials"
        )
        return
    schnapps_url = vcap_services[schnapps][0]["credentials"]["url"]
    schnapps_api_key = vcap_services[schnapps][0]["credentials"]["apiKey"]

    try:
        result = requests.put(
            schnapps_url,
            headers={
                "Content-Type": "application/json",
                "apiKey": schnapps_api_key,
            },
            data=json.dumps(backup_service),
        )
    except requests.exceptions.SSLError as e:
        logger.warning("Failed to contact backup service. SSLError: " + str(e))
        return
    except Exception as e:
        logger.warning("Failed to contact backup service: ", exc_info=True)
        return

    if result.status_code == 200:
        logger.info("Successfully updated backup service")
    else:
        logger.warning("Failed to update backup service: " + result.text)
Exemple #7
0
def determine_cluster_redis_credentials():
    vcap_services = buildpackutil.get_vcap_services_data()
    if vcap_services and 'rediscloud' in vcap_services:
        return vcap_services['rediscloud'][0]['credentials']
    logger.error("Redis Cloud Service should be configured for this app")
    sys.exit(1)
Exemple #8
0
def get_filestore_config(m2ee):
    access_key = secret = bucket = encryption_keys = key_suffix = None

    vcap_services = buildpackutil.get_vcap_services_data()
    endpoint = None
    v2_auth = ''
    if vcap_services and 'amazon-s3' in vcap_services:
        _conf = vcap_services['amazon-s3'][0]['credentials']
        access_key = _conf['access_key_id']
        secret = _conf['secret_access_key']
        bucket = _conf['bucket']
        if 'encryption_keys' in _conf:
            encryption_keys = _conf['encryption_keys']
        if 'key_suffix' in _conf:
            key_suffix = _conf['key_suffix']
    elif vcap_services and 'p-riakcs' in vcap_services:
        _conf = vcap_services['p-riakcs'][0]['credentials']
        access_key = _conf['access_key_id']
        secret = _conf['secret_access_key']
        pattern = r'https://(([^:]+):([^@]+)@)?([^/]+)/(.*)'
        match = re.search(pattern, _conf['uri'])
        endpoint = 'https://' + match.group(4)
        bucket = match.group(5)
        v2_auth = 'true'

    access_key = os.getenv('S3_ACCESS_KEY_ID', access_key)
    secret = os.getenv('S3_SECRET_ACCESS_KEY', secret)
    bucket = os.getenv('S3_BUCKET_NAME', bucket)
    if 'S3_ENCRYPTION_KEYS' in os.environ:
        encryption_keys = json.loads(os.getenv('S3_ENCRYPTION_KEYS'))

    perform_deletes = os.getenv('S3_PERFORM_DELETES', '').lower() == 'false'
    key_suffix = os.getenv('S3_KEY_SUFFIX', key_suffix)
    endpoint = os.getenv('S3_ENDPOINT', endpoint)
    v2_auth = os.getenv('S3_USE_V2_AUTH', v2_auth).lower() == 'true'
    sse = os.getenv('S3_USE_SSE', '').lower() == 'true'

    if not (access_key and secret and bucket):
        logger.warning(
            'External file store not configured, uploaded files in the app '
            'will not persist across restarts. See https://github.com/mendix/'
            'cf-mendix-buildpack for file store configuration details.'
        )
        return {}

    logger.info(
        'S3 config detected, activating external file store'
    )
    config = {
        'com.mendix.core.StorageService': 'com.mendix.storage.s3',
        'com.mendix.storage.s3.AccessKeyId': access_key,
        'com.mendix.storage.s3.SecretAccessKey': secret,
        'com.mendix.storage.s3.BucketName': bucket,
    }
    if not perform_deletes:
        config['com.mendix.storage.s3.PerformDeleteFromStorage'] = False
    if key_suffix:
        config['com.mendix.storage.s3.ResourceNameSuffix'] = key_suffix
    if v2_auth:
        config['com.mendix.storage.s3.UseV2Auth'] = v2_auth
    if endpoint:
        config['com.mendix.storage.s3.EndPoint'] = endpoint
    if m2ee.config.get_runtime_version() >= 5.17 and encryption_keys:
        config['com.mendix.storage.s3.EncryptionKeys'] = encryption_keys
    if m2ee.config.get_runtime_version() >= 6 and sse:
        config['com.mendix.storage.s3.UseSSE'] = sse
    return config