Skip to content

Commit

Permalink
environment variables wip
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelduranfrigola committed Aug 19, 2024
1 parent f07c7b8 commit 2e04167
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions ersilia/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
DEFAULT_API_NAME = "run"
PACKMODE_FILE = "pack_mode.txt"
CARD_FILE = "card.json"
DOTENV_FILE = ".env"
API_SCHEMA_FILE = "api_schema.json"
MODEL_SIZE_FILE = "size.json"
DEFAULT_BATCH_SIZE = 100
Expand Down
Empty file.
84 changes: 84 additions & 0 deletions ersilia/serve/environment/environment_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# TODO This is a placeholder for the environment variables module. This module will be able to manage environment variables, including secrets and API keys, and put them
# in the right place for the model to use them. This module will also be able to get the environment variables from the right place, including the environment, the terminal, etc.
import os
import shutil
import subprocess
#from dotenv import load_dotenv
from ...default import DOTENV_FILE
from ... import ErsiliaBase


class GetEnvironmentVariable(ErsiliaBase):
def __init__(self, config_json=None):
ErsiliaBase.__init__(self, config_json=config_json, credentials_json=None)
self.logger.debug("Environmental variable getter created")

def _get_from_python(self, env):
return os.environ.get(env)

def _get_from_terminal(self, env):
return os.system(f"echo ${env}")

def _get_from_dotenv_in_cwd(self, env):
#load_dotenv()
return os.environ.get(env)

def _get_from_dotenv_in_eos(self, env):

return os.environ.get(env)

def _get_from_github_secrets(self, env):
return

def _get_from_bashrc(self, env):
pass

def get(self, env: str):
value = self._get_from_python(env)
if value is not None:
return value
value = self._get_from_terminal(env)
if value is not None:
return value
value = self._get_from_dotenv(env)
if value is not None:
return value
value = self._get_from_github_secrets(env)
if value is not None:
return value
value = self._get_from_bashrc(env)
if value is not None:
return value


class PutEnvironmentVariable(ErsiliaBase):
def __init__(self, model_id, config_json=None):
ErsiliaBase.__init__(self, config_json=config_json, credentials_json=None)
self.model_id = model_id
self.logger.debug("Environmental variable setter created")

def _get_framework_folder(self):
bundle_path = self._get_bundle_location(model_id=self.model_id)
bentoml_style_path = os.path.join(bundle_path, self.model_id, "artifact", "model", "framework")
if os.path.exists(bentoml_style_path):
return bentoml_style_path
fastapi_style_path = os.path.join(bundle_path, self.model_id, "model", "framework")
if os.path.exists(fastapi_style_path):
return fastapi_style_path
return None

def _send_to_framework_in_docker_as_dotenv(self, dotenv_file):
# TODO: Implement this
subprocess.run("", shell=True).wait()

def _send_to_local_framework_as_dotenv(self, dotenv_file):
# TODO: Test this
framework_dir = self._get_framework_folder()
if framework_dir is None:
self.logger.debug("Framework directory not found. Cannot copy the .env file.")
return None
self.logger.debug("Copying .env file to the framework directory: {0}".format(framework_dir))
shutil.copy(dotenv_file, os.path.join(framework_dir, DOTENV_FILE))

def put(self, env_dict: dict):
pass

0 comments on commit 2e04167

Please sign in to comment.