|
| 1 | +from typing import Optional |
| 2 | + |
| 3 | + |
| 4 | +def schedule_name() -> Optional[str]: |
| 5 | + """ |
| 6 | + Retrieve the name of the schedule that invoked this run from the runtime |
| 7 | + environment. |
| 8 | +
|
| 9 | + Returns: |
| 10 | + Optional[str]: The name of the schedule if set, otherwise None. |
| 11 | + """ |
| 12 | + return _get_runtime_env_variable("SCHEDULE_NAME", None) |
| 13 | + |
| 14 | + |
| 15 | +def is_scheduled_run() -> bool: |
| 16 | + """ |
| 17 | + Check if the current run is a scheduled run based on environment variables. |
| 18 | + Returns: |
| 19 | + bool: True if it is a scheduled run, otherwise False. |
| 20 | + """ |
| 21 | + return schedule_name() is not None |
| 22 | + |
| 23 | + |
| 24 | +def schedule_id() -> Optional[str]: |
| 25 | + """ |
| 26 | + Retrieve the ID of the schedule that invoked this run from the runtime |
| 27 | + environment. |
| 28 | +
|
| 29 | + Returns: |
| 30 | + Optional[str]: The ID of the schedule if set, otherwise None. |
| 31 | + """ |
| 32 | + return _get_runtime_env_variable("SCHEDULE_ID", None) |
| 33 | + |
| 34 | + |
| 35 | +def run_id() -> Optional[str]: |
| 36 | + """ |
| 37 | + Retrieve the ID of the current run from the runtime environment. |
| 38 | +
|
| 39 | + Returns: |
| 40 | + Optional[str]: The ID of the run if set, otherwise None. |
| 41 | + """ |
| 42 | + return _get_runtime_env_variable("RUN_ID", None) |
| 43 | + |
| 44 | + |
| 45 | +def run_number() -> Optional[int]: |
| 46 | + """ |
| 47 | + Retrieve the number of the current run from the runtime environment. |
| 48 | +
|
| 49 | + Returns: |
| 50 | + Optional[int]: The run number if set, otherwise None. |
| 51 | + """ |
| 52 | + run_number_str = _get_runtime_env_variable("RUN_NUMBER", None) |
| 53 | + return int(run_number_str) if run_number_str is not None else None |
| 54 | + |
| 55 | + |
| 56 | +def hostname() -> Optional[str]: |
| 57 | + """ |
| 58 | + Retrieve the hostname of the current run assigned by the runtime |
| 59 | + environment. |
| 60 | +
|
| 61 | + Returns: |
| 62 | + Optional[str]: The hostname if set, otherwise None. |
| 63 | + """ |
| 64 | + return _get_env_variable("TOWER__HOST", None) |
| 65 | + |
| 66 | + |
| 67 | +def port() -> Optional[int]: |
| 68 | + """ |
| 69 | + Retrieve the port number assigned to this run by the current runtime |
| 70 | + environment. |
| 71 | +
|
| 72 | + Returns: |
| 73 | + Optional[int]: The port number if set, otherwise None. |
| 74 | + """ |
| 75 | + port_str = _get_env_variable("TOWER__PORT", None) |
| 76 | + return int(port_str) if port_str is not None else None |
| 77 | + |
| 78 | + |
| 79 | +def is_cloud_run() -> bool: |
| 80 | + """ |
| 81 | + Check if the current run is executing in the Tower cloud environment. |
| 82 | +
|
| 83 | + Returns: |
| 84 | + bool: True if running in Tower cloud, otherwise False. |
| 85 | + """ |
| 86 | + val = _get_runtime_env_variable("IS_TOWER_MANAGED", "") |
| 87 | + return _strtobool(val) |
| 88 | + |
| 89 | + |
| 90 | +def runner_name() -> str: |
| 91 | + """Retrieve the name of the runner executing this run from the runtime. If the |
| 92 | + name is unknown, an empty string is returned. |
| 93 | +
|
| 94 | + Returns: |
| 95 | + str: The name of the runner or an empty string if unknown. |
| 96 | + """ |
| 97 | + return _get_runtime_env_variable("RUNNER_NAME", "") |
| 98 | + |
| 99 | + |
| 100 | +def runner_id() -> str: |
| 101 | + """ |
| 102 | + Retrieve the ID of the runner executing this run from the runtime. If the |
| 103 | + ID is unknown, an empty string is returned. |
| 104 | +
|
| 105 | + Returns: |
| 106 | + str: The ID of the runner or an empty string if unknown. |
| 107 | + """ |
| 108 | + return _get_runtime_env_variable("RUNNER_ID", "") |
| 109 | + |
| 110 | + |
| 111 | +def app_name() -> str: |
| 112 | + """ |
| 113 | + Retrieve the name of the app being executed in this run from the runtime. |
| 114 | +
|
| 115 | + Returns: |
| 116 | + str: The name of the app or an empty string if unknown. |
| 117 | + """ |
| 118 | + return _get_runtime_env_variable("APP_NAME") |
| 119 | + |
| 120 | + |
| 121 | +def team_name() -> str: |
| 122 | + """ |
| 123 | + Retrieve the name of the team associated with this run from the runtime. |
| 124 | +
|
| 125 | + Returns: |
| 126 | + str: The name of the team or an empty string if unknown. |
| 127 | + """ |
| 128 | + return _get_runtime_env_variable("TEAM_NAME", "") |
| 129 | + |
| 130 | + |
| 131 | +def environment() -> str: |
| 132 | + """ |
| 133 | + Retrieve the name of the environment this run is running in. |
| 134 | +
|
| 135 | + Returns: |
| 136 | + str: The name of the environment or an empty string if unknown. |
| 137 | + """ |
| 138 | + return _get_runtime_env_variable("ENVIRONMENT_NAME", "") |
| 139 | + |
| 140 | + |
| 141 | +def is_manual_run() -> bool: |
| 142 | + """ |
| 143 | + Check if the current run was manually triggered. |
| 144 | +
|
| 145 | + Returns: |
| 146 | + bool: True if the run was manually triggered, otherwise False. |
| 147 | + """ |
| 148 | + val = _get_runtime_env_variable("IS_MANUAL_RUN", "false") |
| 149 | + return _strtobool(val) |
| 150 | + |
| 151 | + |
| 152 | +def _get_runtime_env_variable(name: str, default: Optional[str] = "") -> Optional[str]: |
| 153 | + """ |
| 154 | + Helper function to retrieve a runtime environment variable. |
| 155 | +
|
| 156 | + Args: |
| 157 | + name (str): The name of the runtime environment variable. |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Optional[str]: The value of the runtime environment variable if set, otherwise None. |
| 161 | + """ |
| 162 | + return _get_env_variable(f"TOWER__RUNTIME__{name}", default) |
| 163 | + |
| 164 | + |
| 165 | +def _get_env_variable(var_name: str, default: Optional[str] = "") -> Optional[str]: |
| 166 | + """ |
| 167 | + Helper function to retrieve an environment variable. |
| 168 | +
|
| 169 | + Args: |
| 170 | + var_name (str): The name of the environment variable. |
| 171 | +
|
| 172 | + Returns: |
| 173 | + Optional[str]: The value of the environment variable if set, otherwise None. |
| 174 | + """ |
| 175 | + import os |
| 176 | + |
| 177 | + return os.getenv(var_name, default) |
| 178 | + |
| 179 | + |
| 180 | +def _strtobool(val: str) -> bool: |
| 181 | + val = val.lower() |
| 182 | + if val in ("y", "yes", "t", "true", "on", "1"): |
| 183 | + return True |
| 184 | + elif val in ("n", "no", "f", "false", "off", "0"): |
| 185 | + return False |
| 186 | + else: |
| 187 | + raise ValueError(f"invalid truth value {val!r}") |
0 commit comments