def get_runtime_preinstalls(internal_storage, runtime, memory, config): """ Download runtime information from storage at deserialize """ log_level = os.getenv('PYWREN_LOG_LEVEL') try: logger.debug("Downloading runtime pre-installed modules from COS") region = config['ibm_cf']['endpoint'].split('//')[1].split('.')[0] namespace = config['ibm_cf']['namespace'] runtime_name = create_runtime_name(runtime, memory) runtime_meta = internal_storage.get_runtime_info(region, namespace, runtime_name) preinstalls = runtime_meta['preinstalls'] if not log_level: print() except Exception: logger.debug('Runtime {} is not yet installed'.format(runtime_name)) if not log_level: print('(Installing...)') create_runtime(runtime, memory=memory, config=config) runtime_meta = internal_storage.get_runtime_info(region, namespace, runtime_name) preinstalls = runtime_meta['preinstalls'] if not runtime_valid(runtime_meta): raise Exception(("The indicated runtime: {} " "is not appropriate for this Python version.") .format(runtime)) return preinstalls
def __init__(self, config): self.log_level = os.getenv('PYWREN_LOG_LEVEL') cf_config = extract_cf_config(config) self.namespace = cf_config['namespace'] self.endpoint = cf_config['endpoint'].replace('http:', 'https:') self.runtime = cf_config['runtime'] self.runtime_memory = int(cf_config['runtime_memory']) self.runtime_timeout = int(cf_config['runtime_timeout']) runtime_name = create_runtime_name(self.runtime, self.runtime_memory) self.action_name = create_action_name(runtime_name) self.invocation_retry = config['pywren']['invocation_retry'] self.retry_sleeps = config['pywren']['retry_sleeps'] self.retries = config['pywren']['retries'] self.client = CloudFunctions(cf_config) msg = 'IBM Cloud Functions init for' logger.info('{} namespace: {}'.format(msg, self.namespace)) logger.info('{} host: {}'.format(msg, self.endpoint)) logger.info('{} Runtime: {} - {}MB'.format(msg, self.runtime, self.runtime_memory)) if not self.log_level: print("{} Namespace: {}".format(msg, self.namespace)) print("{} Host: {}".format(msg, self.endpoint)) print('{} Runtime: {} - {}MB'.format(msg, self.runtime, self.runtime_memory), end=' ')
def delete_runtime(image_name, config=None): logger.info('Deleting runtime: {}'.format(image_name)) if config is None: config = wrenconfig.default() else: config = wrenconfig.default(config) storage_config = wrenconfig.extract_storage_config(config) storage_client = storage.InternalStorage(storage_config) cf_config = wrenconfig.extract_cf_config(config) cf_client = CloudFunctions(cf_config) if image_name == 'default': image_name = _get_default_image_name() image_name_formated = create_action_name(image_name) actions = cf_client.list_actions(PACKAGE) region = cf_client.endpoint.split('//')[1].split('.')[0] namespace = cf_client.namespace for action in actions: action_name, memory = action['name'].rsplit('-', 1) if image_name_formated == action_name: memory = int(memory.replace('MB', '')) runtime_name = create_runtime_name(image_name, memory) storage_client.delete_runtime_info(region, namespace, runtime_name) action_name = create_action_name(runtime_name) cf_client.delete_action(action_name)
def _create_blackbox_runtime(image_name, memory, cf_client): # Create runtime_name from image_name memory = cf_client.default_runtime_memory if not memory else memory runtime_name = create_runtime_name(image_name, memory) action_name = create_action_name(runtime_name) # Upload zipped PyWren action with open(ZIP_LOCATION, "rb") as action_zip: action_bin = action_zip.read() logger.debug("Creating blackbox action: {}".format(action_name)) cf_client.create_action(action_name, image_name, code=action_bin, memory=memory)
def _extract_modules(image_name, memory, cf_client, config): # Extract installed Python modules from docker image # And store them into storage # Create storage_handler to upload modules file storage_config = wrenconfig.extract_storage_config(config) internal_storage = storage.InternalStorage(storage_config) pywren_location = _get_pywren_location() action_location = os.path.join(pywren_location, "runtime", "extract_modules.py") with open(action_location, "r") as action_py: action_code = action_py.read() modules_action_name = '{}-modules'.format(create_action_name(image_name)) # old_stdout = sys.stdout # sys.stdout = open(os.devnull, 'w') logger.debug( "Creating action for extracting Python modules list: {}".format( modules_action_name)) cf_client.create_action(modules_action_name, image_name, code=action_code, is_binary=False) # sys.stdout = old_stdout region = cf_client.endpoint.split('//')[1].split('.')[0] namespace = cf_client.namespace memory = cf_client.default_runtime_memory if not memory else memory runtime_name = create_runtime_name(image_name, memory) logger.debug( "Going to extract Python modules list from: {}".format(image_name)) runtime_meta = cf_client.invoke_with_result(modules_action_name) internal_storage.put_runtime_info(region, namespace, runtime_name, runtime_meta) cf_client.delete_action(modules_action_name)