def deploy_to_openfaas(self) -> None: """Deploy this job's smart contract to OpenFaaS and update the faas_spec Returns: None, or throws exceptions.InternalServerError """ _log.info("Deploying to OpenFaaS cluster") spec = self.get_openfaas_spec() requests_method = requests.post if self.model.task_type == "create" else requests.put response = requests_method( f"{FAAS_GATEWAY}/system/functions", headers={"Authorization": faas.get_faas_auth()}, json=spec) _log.info(f"Deployment status: {response.status_code}") if response.status_code not in [200, 202]: _log.info(f"OpenFaaS deploy failure: {response.status_code}") self.model.set_state(state=self.end_error_state, msg="Failed message state") raise exceptions.ContractException( "Contract function deployment failure") _log.info("Saving faas_spec.json to storage") if os.environ["STORAGE_TYPE"].lower() == "disk": os.setuid(1000) storage.put_object_as_json( key=f"SMARTCONTRACT/{self.model.id}/faas_spec.json", value=spec)
def create_openfaas_secrets(self) -> None: """Creates secrets for openfaas functions Args: existing_model (obj, optional): The existing model for this contract if action is update Returns: None """ existing_secrets = self.model.existing_secrets or [] if self.update_model: new_secrets = self.update_model.secrets else: new_secrets = self.model.secrets for secret, value in new_secrets.items(): secret_name = f"sc-{self.model.id}-{secret.lower()}" requests_method = requests.post if secret not in existing_secrets else requests.put _log.info(f"Creating secret: {secret_name} at {FAAS_GATEWAY}") response = requests_method( f"{FAAS_GATEWAY}/system/secrets", headers={"Authorization": faas.get_faas_auth()}, json={"name": secret_name, "value": value} ) _log.info(f"Response: {response.status_code}") _log.info(f"Response Body: {response.text}") if response.status_code != 202: self.model.set_state(state=self.end_error_state, msg="Error creating contract secrets") raise exceptions.ContractException("Error creating contract secret") existing_secrets.append(secret.lower()) self.model.existing_secrets = existing_secrets
def delete_openfaas_function(self) -> None: """Delete this job's smart contract in OpenFaaS and remove the faas_spec Returns: None, or throws exceptions.InternalServerError """ _log.info("Deleting OpenFaaS function") response = requests.delete( f"{FAAS_GATEWAY}/system/functions", headers={"Authorization": faas.get_faas_auth()}, json={"functionName": self.function_name} ) _log.info(f"Response Status: {response.status_code}") if response.status_code != 202: self.model.set_state(state=self.end_error_state, msg="Error deleting contract function") _log.info("OpenFaaS delete failure")
def delete_openfaas_secrets(self) -> None: """Deletes secrets for an openfaas function Returns: None """ _log.info(f"Deleting OpenFaaS secrets: {self.model.existing_secrets}") for secret in self.model.existing_secrets: secret_name = f"sc-{self.model.id}-{secret.lower()}" response = requests.delete( f"{FAAS_GATEWAY}/system/secrets", headers={"Authorization": faas.get_faas_auth()}, json={"name": secret_name}) if response.status_code != 202: self.model.set_state(state=self.end_error_state, msg="Error deleting secrets") _log.info(f"Delete secret response: {response.status_code}")
def test_delete_contract(self): my_fake_auth = f"Basic {base64.b64encode('mydata:mydata'.encode('utf-8')).decode('ascii')}" data = faas.get_faas_auth() self.assertEqual(my_fake_auth, data)