diff --git a/coscine_watchdog/coscine_client.py b/coscine_watchdog/coscine_client.py index ef949fb6e0ac4371c9e64ca50c7f9230b637e38a..881430492614e6c2552a6470a0dad339189b9d0f 100644 --- a/coscine_watchdog/coscine_client.py +++ b/coscine_watchdog/coscine_client.py @@ -11,9 +11,9 @@ import coscine import os import signal import sys +import subprocess # TODO create new coscine resource with new application profile once available -# TODO add git commit to data app = Flask(__name__) # Create a new Flask application @@ -39,8 +39,30 @@ logger.addHandler(console_handler) # Add console handler to logger logger.addHandler(file_handler) # Add file handler to logger # Retrieve environment variables for API token and robot UUID -api_token = os.environ.get("COSCINE_API_TOKEN") # Get COSCINE_API_TOKEN from environment -robot_uuid = os.environ.get("ROBOT_UUID") # Get ROBOT_UUID from environment +def get_env_variable(var_name): + value = os.environ.get(var_name) + if value is None: + raise EnvironmentError(f"The environment variable {var_name} is not set.") + return value + +try: + api_token = get_env_variable("COSCINE_API_TOKEN") # Get COSCINE_API_TOKEN from environment + robot_uuid = get_env_variable("ROBOT_UUID") # Get ROBOT_UUID from environment +except EnvironmentError as e: + print(e) + # Handle the error appropriately, e.g., exit the program or set default values + api_token = None + robot_uuid = None + +try: + # Run the git command to get the latest commit hash + result = subprocess.run(['git', 'rev-parse', 'HEAD'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) + # The commit hash will be in result.stdout + commit_hash = result.stdout.strip() +except subprocess.CalledProcessError as e: + print(f"Error occurred while getting the latest git commit: {e.stderr}") + + def upload_coscine(filepath: Path, token: str) -> None: """