def _undeploy(location: Path, inv: ExtendedInvocation): # get blueprint CSAR_db.get_revision(inv.blueprint_id, location, inv.version_id) # get session data (.opera) if not InvocationService.get_dot_opera_from_db(inv.deployment_id, location): raise MissingDeploymentDataError( 'Could not get .opera data from previous job, aborting...') with xopera_util.cwd(location): try: if inv.user_id and Settings.secure_workdir: xopera_util.setup_user([location], inv.user_id, inv.access_token) opera_storage = Storage.create(".opera") if inv.inputs: opera_storage.write_json(inv.inputs, "inputs") opera_undeploy(opera_storage, verbose_mode=False, num_workers=inv.workers) # Outputs in undeployment are not returned return None finally: if inv.user_id and Settings.secure_workdir: xopera_util.cleanup_user()
def _deploy_continue(location: Path, inv: ExtendedInvocation): # get blueprint CSAR_db.get_revision(inv.blueprint_id, location, inv.version_id) # get session data (.opera) InvocationService.get_dot_opera_from_db(inv.deployment_id, location) with xopera_util.cwd(location): try: if inv.user_id and Settings.secure_workdir: xopera_util.setup_user([location], inv.user_id, inv.access_token) opera_storage = Storage.create(".opera") service_template = entry_definitions(location) opera_deploy(service_template, inv.inputs, opera_storage, verbose_mode=False, num_workers=inv.workers, delete_existing_state=inv.clean_state) outputs = opera_outputs(opera_storage) return outputs finally: if inv.user_id and Settings.secure_workdir: xopera_util.cleanup_user()
def outputs(deployment_id: str): with tempfile.TemporaryDirectory() as location: InvocationService.prepare_location(deployment_id, Path(location)) try: with xopera_util.cwd(location): opera_storage = Storage.create(".opera") return opera_outputs(opera_storage), None except Exception as e: return None, (e.__class__.__name__, xopera_util.mask_workdir(location, str(e)))
def diff(deployment_id: str, blueprint_id: str, version_id: str, inputs: dict): storage_old, location_old, storage_new, location_new = InvocationWorkerProcess.prepare_two_workdirs( deployment_id, blueprint_id, version_id, inputs) with xopera_util.cwd(location_new): instance_diff = opera_diff_instances(storage_old, location_old, storage_new, location_new, opera_TemplateComparer(), opera_InstanceComparer(), verbose_mode=False) shutil.rmtree(location_new) shutil.rmtree(location_old) return instance_diff
def validate(blueprint_id: str, version_tag: str, inputs: dict): with tempfile.TemporaryDirectory() as location: CSAR_db.get_revision(blueprint_id, location, version_tag) try: with xopera_util.cwd(location): opera_storage = Storage.create(".opera") service_template = Path(location) / entry_definitions( location) opera_validate(service_template, inputs, opera_storage, verbose=False, executors=False) return None except Exception as e: return "{}: {}".format( e.__class__.__name__, xopera_util.mask_workdir(location, str(e)))
def validate_new(CSAR: FileStorage, inputs: dict): try: with tempfile.TemporaryDirectory() as location: with tempfile.TemporaryDirectory() as csar_workdir: csar_path = Path(csar_workdir) / Path(CSAR.filename) CSAR.save(Path(csar_path).open('wb')) csar_to_blueprint(csar=csar_path, dst=location) with xopera_util.cwd(location): opera_storage = Storage.create(".opera") service_template = Path(location) / entry_definitions( location) opera_validate(service_template, inputs, opera_storage, verbose=False, executors=False) return None except Exception as e: return "{}: {}".format( e.__class__.__name__, xopera_util.mask_workdirs([location, csar_workdir], str(e)), )
def _update(location: Path, inv: ExtendedInvocation): storage_old, location_old, storage_new, location_new = InvocationWorkerProcess.prepare_two_workdirs( inv.deployment_id, inv.blueprint_id, inv.version_id, inv.inputs, location) assert location_new == str(location) with xopera_util.cwd(location_new): try: if inv.user_id and Settings.secure_workdir: xopera_util.setup_user([location_old, location_new], inv.user_id, inv.access_token) instance_diff = opera_diff_instances(storage_old, location_old, storage_new, location_new, opera_TemplateComparer(), opera_InstanceComparer(), verbose_mode=False) opera_update(storage_old, location_old, storage_new, location_new, opera_InstanceComparer(), instance_diff, verbose_mode=False, num_workers=inv.workers, overwrite=False) outputs = opera_outputs(storage_new) return outputs finally: if inv.user_id and Settings.secure_workdir: xopera_util.cleanup_user() shutil.rmtree(location_old)
def test_cwd(self, generic_dir: Path): tree = {f'{i}-new.txt': '' for i in range(4)} with xopera_util.cwd(generic_dir): for key in tree.keys(): assert_that(str(key)).exists()