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 __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 _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 create_runtime(image_name, memory=None, config=None): logger.info( 'Creating new PyWren runtime based on image {}'.format(image_name)) if config is None: config = wrenconfig.default() else: config = wrenconfig.default(config) cf_config = wrenconfig.extract_cf_config(config) cf_client = CloudFunctions(cf_config) cf_client.create_package(PACKAGE) _create_zip_action() if image_name == 'default': image_name = _get_default_image_name() if not memory: # if not memory, this means that the method was called from deploy_runtime script for memory in [ wrenconfig.RUNTIME_MEMORY_DEFAULT, wrenconfig.RUNTIME_RI_MEMORY_DEFAULT ]: _extract_modules(image_name, memory, cf_client, config) _create_blackbox_runtime(image_name, memory, cf_client) else: ri_runtime_deployed = False image_name_formated = create_action_name(image_name) actions = cf_client.list_actions(PACKAGE) for action in actions: action_name, r_memory = action['name'].rsplit('-', 1) if image_name_formated == action_name: r_memory = int(r_memory.replace('MB', '')) if r_memory == wrenconfig.RUNTIME_RI_MEMORY_DEFAULT: ri_runtime_deployed = True break if not ri_runtime_deployed: _extract_modules(image_name, wrenconfig.RUNTIME_RI_MEMORY_DEFAULT, cf_client, config) _create_blackbox_runtime(image_name, wrenconfig.RUNTIME_RI_MEMORY_DEFAULT, cf_client) _extract_modules(image_name, memory, cf_client, config) _create_blackbox_runtime(image_name, memory, cf_client)
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)
def update_runtime(image_name, config=None): logger.info('Updating runtime: {}'.format(image_name)) if config is None: config = wrenconfig.default() else: config = wrenconfig.default(config) cf_config = wrenconfig.extract_cf_config(config) cf_client = CloudFunctions(cf_config) cf_client.create_package(PACKAGE) _create_zip_action() if image_name == 'default': image_name = _get_default_image_name() image_name_formated = create_action_name(image_name) actions = cf_client.list_actions(PACKAGE) for action in actions: action_name, memory = action['name'].rsplit('-', 1) if image_name_formated == action_name: memory = int(memory.replace('MB', '')) _extract_modules(image_name, memory, cf_client, config) _create_blackbox_runtime(image_name, memory, cf_client)