-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
231 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ keywords: | |
- registry | ||
- container | ||
lib_version: 1.0.0 | ||
lib_version_hash: 9c07d26150ab40c0f19ae3991ee02b338aecf50e6c3619414d114276d08e1c1f | ||
lib_version_hash: 04058cefffe4eeadb07035ab157987492a31d5708705d6b7153d262beb75a796 | ||
maintainers: | ||
- email: [email protected] | ||
name: truenas | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 71 additions & 15 deletions
86
ix-dev/community/distribution/templates/library/base_v1_0_0/environment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,90 @@ | ||
from . import utils | ||
from .resources import get_nvidia_gpus_reservations | ||
|
||
|
||
def envs(app: dict | None = None, user: list | None = None): | ||
def envs(app: dict | None = None, user: list | None = None, values: dict | None = None): | ||
app = app or {} | ||
user = user or [] | ||
track_env = {**app} | ||
result = {**app} | ||
|
||
if not user: | ||
user = [] | ||
elif isinstance(user, list): | ||
pass | ||
elif isinstance(user, dict): | ||
user = [{"name": k, "value": v} for k, v in user.items()] | ||
else: | ||
values = values or {} | ||
result = {} | ||
|
||
if not values: | ||
utils.throw_error("Values cannot be empty in environment.py") | ||
|
||
if not isinstance(user, list): | ||
utils.throw_error( | ||
f"Unsupported type for user environment variables [{type(user)}]" | ||
) | ||
|
||
for k in app.keys(): | ||
if not k: | ||
# Always set TZ | ||
result.update({"TZ": values.get("TZ", "Etc/UTC")}) | ||
|
||
# Update envs with nvidia variables | ||
if values.get("resources", {}).get("gpus", {}): | ||
result.update(get_nvidia_env(values.get("resources", {}).get("gpus", {}))) | ||
|
||
# Update envs with run_as variables | ||
if values.get("run_as"): | ||
result.update(get_run_as_envs(values.get("run_as", {}))) | ||
|
||
# Make sure we don't manually set any of the above | ||
for item in app.items(): | ||
if not item[0]: | ||
utils.throw_error("Environment variable name cannot be empty.") | ||
if item[0] in result: | ||
utils.throw_error( | ||
f"Environment variable [{item[0]}] is already defined automatically from the library." | ||
) | ||
result[item[0]] = item[1] | ||
|
||
for item in user: | ||
if not item.get("name"): | ||
utils.throw_error("Environment variable name cannot be empty.") | ||
if item.get("name") in track_env: | ||
if item.get("name") in result: | ||
utils.throw_error( | ||
f"Environment variable [{item['name']}] is already defined from the application developer." | ||
) | ||
track_env[item["name"]] = item.get("value") | ||
result[item["name"]] = item.get("value") | ||
|
||
return result | ||
|
||
|
||
# Sets some common variables that most applications use | ||
def get_run_as_envs(run_as: dict) -> dict: | ||
result = {} | ||
user = run_as.get("user") | ||
group = run_as.get("group") | ||
if user: | ||
result.update( | ||
{ | ||
"PUID": user, | ||
"UID": user, | ||
"USER_ID": user, | ||
} | ||
) | ||
if group: | ||
result.update( | ||
{ | ||
"PGID": group, | ||
"GID": group, | ||
"GROUP_ID": group, | ||
} | ||
) | ||
return result | ||
|
||
|
||
def get_nvidia_env(gpus: dict) -> dict: | ||
reservations = get_nvidia_gpus_reservations(gpus) | ||
if not reservations.get("device_ids"): | ||
return { | ||
"NVIDIA_VISIBLE_DEVICES": "void", | ||
} | ||
|
||
return { | ||
"NVIDIA_VISIBLE_DEVICES": ( | ||
",".join(reservations["device_ids"]) | ||
if reservations.get("device_ids") | ||
else "void" | ||
), | ||
"NVIDIA_DRIVER_CAPABILITIES": "all", | ||
} |
72 changes: 72 additions & 0 deletions
72
ix-dev/community/distribution/templates/library/base_v1_0_0/metadata.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from . import utils | ||
|
||
|
||
def get_header(app_name: str): | ||
return f"""# Welcome to TrueNAS SCALE | ||
Thank you for installing {app_name}! | ||
""" | ||
|
||
|
||
def get_footer(app_name: str): | ||
return f"""## Documentation | ||
Documentation for {app_name} can be found at https://www.truenas.com/docs. | ||
## Bug reports | ||
If you find a bug in this app, please file an issue at | ||
https://ixsystems.atlassian.net or https://github.com/truenas/apps | ||
## Feature requests or improvements | ||
If you find a feature request for this app, please file an issue at | ||
https://ixsystems.atlassian.net or https://github.com/truenas/apps | ||
""" | ||
|
||
|
||
def get_notes(app_name: str, body: str = ""): | ||
if not app_name: | ||
utils.throw_error("Expected [app_name] to be set") | ||
|
||
return f"{get_header(app_name)}\n\n{body}\n\n{get_footer(app_name)}" | ||
|
||
|
||
def get_portals(portals: list): | ||
valid_schemes = ["http", "https"] | ||
result = [] | ||
for portal in portals: | ||
# Most apps have a single portal, lets default to a standard name | ||
name = portal.get("name", "Web UI") | ||
scheme = portal.get("scheme", "http") | ||
path = portal.get("path", "/") | ||
|
||
if not name: | ||
utils.throw_error("Expected [portal.name] to be set") | ||
if name in [p["name"] for p in result]: | ||
utils.throw_error( | ||
f"Expected [portal.name] to be unique, got [{', '.join([p['name'] for p in result]+[name])}]" | ||
) | ||
if scheme not in valid_schemes: | ||
utils.throw_error( | ||
f"Expected [portal.scheme] to be one of [{', '.join(valid_schemes)}], got [{portal['scheme']}]" | ||
) | ||
if not portal.get("port"): | ||
utils.throw_error("Expected [portal.port] to be set") | ||
if not path.startswith("/"): | ||
utils.throw_error( | ||
f"Expected [portal.path] to start with /, got [{portal['path']}]" | ||
) | ||
|
||
result.append( | ||
{ | ||
"name": name, | ||
"scheme": scheme, | ||
# TODO: Default to something else? | ||
"host": portal.get("host", "0.0.0.0"), | ||
"port": portal["port"], | ||
"path": path, | ||
} | ||
) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters