def __init__(self): if os.getenv('VCAP_SERVICES') is None: os.environ['VCAP_SERVICES'] = get_services_from_file( os.getenv('SERVICES_PATH'), 'env/services.yml') env = AppEnv() self.APIALCHEMY_BINDS = {} self.apialchemy_binds = api_helper.parse_services_for_binds( service_prefix['APIALCHEMY'], env.services) self.SQLALCHEMY_BINDS = {} self.sqlalchemy_binds = sqlalchemy_helper.parse_services_for_binds( service_prefix['SQLALCHEMY'], env.services) self.SCHEDULER_JOBS = [] if os.getenv('JOBS_SOURCE_SERVICE') is not None: self.JOBS_SOURCE_REFRESH_INTERVAL = os.getenv( 'JOBS_SOURCE_REFRESH_INTERVAL', '60') self.service_jobs = apscheduler_helper.get_jobs_from_services( job_prefix, template_prefix, env.services) self.source_jobs = {} self.jobs = self.service_jobs
def cloudFoundryfyConfig(config: FlaskConfig): """ Optionale Anpassung der Flask-Konfiguration mit CF-Umgebung. """ cfenv = AppEnv() if len(cfenv.app) > 0: logger.info("app %s %d services: %s", cfenv.name, len(cfenv.services), cfenv.app) for service in cfenv.services: logger.info("bound service '%s': %s", service.name, service.env) {% if cookiecutter.use_reldb.startswith('y') -%} vcapdb = cfenv.get_service(label='p-mysql') if vcapdb: logger.info("%s", vcapdb) config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{username}:{password}@{hostname}:{port}/{name}'.format( **vcapdb.credentials) logger.info("MySQL Service %s konfiguriert", vcapdb.credentials['hostname']) elif 'SQLALCHEMY_DATABASE_URI' not in config: logger.critical("Kein Datenbank-Service gebunden!") {%- endif -%} else: cfenv = None return cfenv
def scp_connect(dest_name, dest_uri, dest_client): ###################################################################### ############### Step 1: Read the environment variables ############### ###################################################################### env = AppEnv() uaa_service = env.get_service(name='uaa_service') dest_service = env.get_service(name='destination_service') sUaaCredentials = dest_service.credentials[ "clientid"] + ':' + dest_service.credentials["clientsecret"] ####################################################################### ##### Step 2: Request a JWT token to access the destination service ### ####################################################################### headers = { 'Authorization': 'Basic ' + base64.b64encode(sUaaCredentials), 'content-type': 'application/x-www-form-urlencoded' } form = [('client_id', dest_service.credentials["clientid"]), ('grant_type', 'client_credentials')] r = requests.post(uaa_service.credentials["url"] + '/oauth/token', data=form, headers=headers) ####################################################################### ###### Step 3: Search your destination in the destination service ##### ####################################################################### token = r.json()["access_token"] headers = {'Authorization': 'Bearer ' + token} r = requests.get(dest_service.credentials["uri"] + '/destination-configuration/v1/destinations/' + dest_name, headers=headers) ####################################################################### ############### Step 4: Access the destination securely ############### ####################################################################### destination = r.json() token = destination["authTokens"][0] headers = { 'Authorization': token["type"] + ' ' + token["value"], 'Accept': 'application/json' } if dest_client: dest_client = '?sap-client=' + dest_client else: #Read sap-client from Destinations configuration dest_client = '?sap-client=' + destination["destinationConfiguration"][ "sap-client"] r = requests.get(destination["destinationConfiguration"]["URL"] + dest_uri + dest_client, headers=headers) return r
def handle(self, *args, **options): env = AppEnv() if env.index is None or env.index == 0: management.call_command('migrate', fake_initial=True) management.call_command('rebuild_index', interactive=False, remove=True)
def get_connection_string(): env = AppEnv() mssql = env.get_service(name='mssql-service') server = 'tcp:' + mssql.credentials['server'] database = mssql.credentials['database'] username = mssql.credentials['username'] password = mssql.credentials['password'] return 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=' + server + ';DATABASE=' + database + ';UID=' + username + ';PWD=' + password
def __init__(self): self.env_parser = Env() self.cf_env_parser = AppEnv() self.ENV = self.env_parser("ENV") self.SEMAPHORE = "cloud-gov-migration-ready" # this is a well-known constant # https://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html self.CLOUDFRONT_HOSTED_ZONE_ID = "Z2FDTNDATAQYW2"
def credhub_secret(key): ''' Read the VCAP_SERVICES env variable & extract the "demo-certificate" value :return: parsed credhub value as a dict ''' cf_env = AppEnv() credhub_env = cf_env.get_service(label="credhub").get_url(key) return credhub_env
def create_app() -> Flask: app = Flask(__name__) app_env = AppEnv() @app.route("/") def index(): return jsonify(app_env.app) @app.route("/services") def services(): return jsonify([s.__dict__ for s in app_env.services]) return app
def down_dir(): dest = request.args.get("destination") path = request.args.get("path") if (dest == None or path == None): return "Please provide paramenters" download_directory(dest, path, AppEnv()) dir_list = [] for root, dirs, files in os.walk('.'): for d in dirs: dir_list.append(os.path.join(root, d)) for f in files: dir_list.append(os.path.join(root, f)) return str(''.join(list(os.popen("ls -lh 124M/run1"))))
def start_job(): service = AppEnv().get_service(name='redeployer-creds') token = service.credentials.get('API_TOKEN') branch = os.environ.get('CIRCLE_BRANCH', 'master') job = os.environ.get('CIRLCE_JOBNAME', 'build') org = os.environ.get('CIRCLE_ORGNAME') repo = os.environ.get('CIRCLE_REPONAME') vcs = os.environ.get('CIRCLE_VCS', 'github') url = f"https://{token}:@circleci.com/api/v1.1/project/{vcs}/{org}/{repo}" url += f"/tree/{branch}" result = requests.post(url, data={'build_parameters[CIRCLE_JOB]': job}) result.raise_for_status()
def hello(): env = AppEnv() app_name = env.name app_uris = env.uris space_name = env.space index = env.index vcap_app_env = json.loads(os.getenv('VCAP_APPLICATION', '{}')) app_mem_limit = str(vcap_app_env["limits"].get('mem')) app_disk_limit = str(vcap_app_env["limits"].get('disk')) #return render_template('index1.html', app_name=app_name, app_uris=app_uris) return render_template('index.html', app_name=app_name, app_uris=app_uris, space_name=space_name, index=index, app_mem_limit=app_mem_limit, app_disk_limit=app_disk_limit)
def __init__(self): self.env = Env() self.cfenv = AppEnv() self.FLASK_ENV = self.env("FLASK_ENV") self.TMPDIR = self.env("TMPDIR", "/app/tmp/") self.DNS_PROPAGATION_SLEEP_TIME = self.env( "DNS_PROPAGATION_SLEEP_TIME", "300") self.CLOUDFRONT_PROPAGATION_SLEEP_TIME = 60 # Seconds self.SQLALCHEMY_TRACK_MODIFICATIONS = False self.TESTING = True self.DEBUG = True self.ACME_POLL_TIMEOUT_IN_SECONDS = self.env( "ACME_POLL_TIMEOUT_IN_SECONDS", 90) self.AWS_POLL_WAIT_TIME_IN_SECONDS = 60 self.AWS_POLL_MAX_ATTEMPTS = 120 # https://docs.aws.amazon.com/Route53/latest/APIReference/API_AliasTarget.html self.CLOUDFRONT_HOSTED_ZONE_ID = "Z2FDTNDATAQYW2"
def download_directory(destination, directory_path, env=AppEnv()): responseText = call_destination(destination, directory_path, env) print(responseText) directory = json.loads(responseText) for item in directory: if (item["type"] == "file"): if item["size"] // 1024 >= 1024: response_stream = call_destination( destination, '/' + '/'.join( item["download_url"].strip("https://").split('/')[1:]), stream=True) print(response_stream) with open(item["name"], 'wb') as fp: for chunk in response_stream.iter_content(chunk_size=1024): fp.write(chunk) else: responseObject = json.loads( call_destination(destination, directory_path + "/" + item["name"])) if "raw_lfs" in responseObject["download_url"]: thread = Thread(target=download_large_file, args=( destination, responseObject, )) thread.start() else: with open(responseObject["name"], 'wb') as fp: fp.write( base64.standard_b64decode( responseObject["content"])) elif (item["type"] == "dir"): os.mkdir(item["name"]) os.chdir(item["name"]) download_directory(destination, directory_path + "/" + item["name"], env) os.chdir('..')
def process_file(): dest = request.args.get("destination") path = request.args.get("path") responseText = call_destination(dest, path, AppEnv()) return responseText
def call_destination( destination: "name of the destination item on the SAP Cloud Platform Cockpit Destinations" = '', path: "endpoint path for accessing the data" = None, env: "CF App Environment Variable" = AppEnv(), requestContentType: "Content type of the request -> Default is always everything" = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', stream: "To stream a large file" = False): """ Function to call the on-prem service via destination, connectivity, and Cloud Connector. Destination to be fetched can be informed on the call. If no destination could be defined, it will return a 400. :param destination: name of the destination item on the SAP Cloud Platform Cockpit Destinations :param path: endpoint path for accessing the data :param request: request object from flask :param env: CF environment object :param requestContentType: Content type of the request -> Default is always everything :return: data from the end point """ if destination == '': return ("Destination not found", 400) elif path is None: return ("Path not found", 400) elif env is None: return ("Environment object is not forwarded", 400) try: vs_uaa_service_credentials = env.get_service( name=xsuaa_service).credentials vs_connectivity_credentials = env.get_service( name=connectivity_service).credentials vs_destination_credentials = env.get_service( name=destination_service).credentials except Exception as err: msg = 'Required services not found! Without services / credentials it will be impossible to succeed!' + \ ' Exception:' + str(err) return (msg, 503) # ------------------------------------------------------------------------------------------------------------------ uaa_url = vs_uaa_service_credentials["url"] + \ '/oauth/token?grant_type=client_credentials' try: response = requests.post( uaa_url, headers={'Accept': 'application/json'}, auth=(vs_destination_credentials['clientid'], vs_destination_credentials['clientsecret'])) jwt_destination = response.json()['access_token'] except Exception as err: msg = "Something wrong getting JWT from xsuaa service for the destination service. Exception: " + \ str(err) return (msg, 500) # ------------------------------------------------------------------------------------------------------------------ destination_url = vs_destination_credentials['uri'] + '/destination-configuration/v1/destinations/' + \ destination try: response = requests.get(destination_url, headers={ 'Accept': 'application/json', 'Authorization': 'Bearer ' + jwt_destination, }) destination = response.json()['destinationConfiguration'] print_version = deepcopy(destination) print_version['Password'] = '******' except Exception as e: msg = "Something wrong reading data from the destination service: " + \ str(e) return (msg, 500) # ------------------------------------------------------------------------------------------------------------------ connectivity_url = vs_uaa_service_credentials["url"] + \ '/oauth/token?grant_type=client_credentials' try: response = requests.post( connectivity_url, headers={'Accept': 'application/json'}, auth=(vs_connectivity_credentials['clientid'], vs_connectivity_credentials['clientsecret'])) jwt_connectivity = response.json()['access_token'] except Exception as e: msg = "Something wrong posting data for the connectivity service. Exception: " + \ str(e) return msg, 500 # ------------------------------------------------------------------------------------------------------------------ proxies = { 'http': "http://" + vs_connectivity_credentials['onpremise_proxy_host'] + ':' + vs_connectivity_credentials['onpremise_proxy_port'], } request_url = destination['URL'] + path # ------------------------------------------------------------------------------------------------------------------ try: if stream: response_stream = requests.get( request_url, proxies=proxies, headers={ 'Accept': requestContentType, # 'SAP-Connectivity-Authentication': 'Bearer ' + jwt_user_auth, 'Proxy-Authorization': 'Bearer ' + jwt_connectivity, "SAP-Connectivity-SCC-Location_ID": destination["CloudConnectorLocationId"] }, auth=(destination['User'], destination['Password']), stream=True) return response_stream else: response = requests.get( request_url, proxies=proxies, headers={ 'Accept': requestContentType, # 'SAP-Connectivity-Authentication': 'Bearer ' + jwt_user_auth, 'Proxy-Authorization': 'Bearer ' + jwt_connectivity, "SAP-Connectivity-SCC-Location_ID": destination["CloudConnectorLocationId"] }, auth=(destination['User'], destination['Password'])) return response.content except Exception as e: msg = "Something wrong when accessing on-premise resource. Exception: " + \ str(e) return (msg, 500)
"django.template.context_processors.request", "django.contrib.auth.context_processors.auth", ], }, }, ] WSGI_APPLICATION = "idemia.wsgi.application" # The VCAP_APPLICATION environment variable is set by cloud.gov and # populated with service information needed to connect to the database. VCAP_ENV_VAR = "VCAP_APPLICATION" if VCAP_ENV_VAR in os.environ: # Deployment to Cloud.gov -- Set DB to RDS ENV = AppEnv() RDS_VARS = ENV.get_service(label="aws-rds") DB_INFO = RDS_VARS.credentials DB_DICT = { "ENGINE": "django.db.backends.postgresql", "NAME": DB_INFO["db_name"], "USER": DB_INFO["username"], "PASSWORD": DB_INFO["password"], "HOST": DB_INFO["host"], "PORT": DB_INFO["port"], } else: # Local development -- use local DB info # See README for setting up postgres container DB_DICT = {
def handle(self, *args, **options): env = AppEnv() if env.index is None or env.index == 0: management.call_command('migrate', '--noinput')
def handle(self, *args, **options): env = AppEnv() if env.index is None or env.index == 0: management.call_command('migrate') management.call_command('rebuild_pgsql_index')
import os import json from cfenv import AppEnv from flask import Flask from flask import send_from_directory from flask import render_template from dbhelper import DBHelper from flask import request app = Flask(__name__) app_env = AppEnv() db = DBHelper(app_env) db.create_table() # Google Maps API Key = "AIzaSyBXYBYNCCPGeRUQU2EmCdCy1PvT8WmO8ck" @app.route("/") def home(): try: crimes = db.get_all_crimes() crimes = json.dumps(crimes) except Exception as e: print(e) crimes = None return render_template("home.html", crimes=crimes) @app.route("/submitcrime", methods=["POST"]) def submitcrime(): category = request.form.get("category")
from __future__ import absolute_import, print_function from cfenv import AppEnv from django.conf import settings cfvEnv = AppEnv() CONF_OBJ = cfvEnv.get_service(label='xsuaa').credentials CLIENT_ID = CONF_OBJ.get('clientid', None) CLIENT_SECRET = CONF_OBJ.get('clientsecret', None) BASE_DOMAIN = CONF_OBJ.get('url', None) APP_NAME = CONF_OBJ.get('xsappname', None) SCOPE = APP_NAME + ".login" ACCESS_TOKEN_URL = '{0}/oauth/token'.format(BASE_DOMAIN) AUTHORIZE_URL = '{0}/oauth/authorize'.format(BASE_DOMAIN)
def __init__(self, storeName = 'TestStoreName'): env = AppEnv() port = int(os.getenv("PORT", 9099)) hana = env.get_service(label='hana')
def env(application, services): return AppEnv()
from flask import request from flask import Response from flask import send_from_directory # import os import json import datetime from cfenv import AppEnv from hdbcli import dbapi import platform app = Flask(__name__) env = AppEnv() # Get port from environment variable or choose 9099 as local default # If you are testing locally (i.e. not with xs or cf deployments, # Be sure to pull all the python modules locally # with pip using XS_PYTHON unzipped to /tmp # mkdir -p local # pip install -t local -r requirements.txt -f /tmp port = int(os.getenv("PORT", 9099)) hana = env.get_service(label='hana') # This module's Flask webserver will respond to these three routes (URL paths) # If there is no path then just return Hello World and this module's instance number # Requests passed through the app-router will never hit this route. @app.route('/')
def hello_world(): app_env = AppEnv() time.sleep(1) return (f'Nome da aplicação: {app_env.name}\n' f'Instância: {app_env.index}\n' f'Endereço da instância: {os.getenv("CF_INSTANCE_ADDR")}\n')