Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dbuild/appjail_choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def option_forms(cfg: Any) -> dict[str, dict[str, dict[str, Any]]]:
base = _director_dict(base_text)

for c in cfg.metadata.choices:
if not c.applies("appjail"):
continue
for o in c.options:
files: dict[str, str] = {}
hostnames: dict[str, str] = {}
Expand Down
20 changes: 18 additions & 2 deletions dbuild/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@

# The database engines dbuild knows how to run. One place: an app that
# offers postgres gets this service, and a fix here fixes every app.
RUNTIMES = ("podman", "appjail")

ENGINES: dict[str, dict[str, Any]] = {
"sqlite": {"label": "SQLite", "type": "sqlite",
"doc": "A file in the app's config folder. Right for one person, nothing extra to run."},
Expand Down Expand Up @@ -142,11 +144,17 @@ class Choice:
service: str = "" # the app service the choice wires (database); "" = first
env_map: dict[str, str] = field(default_factory=dict) # database: role -> the app's variable
options: list[Option] = field(default_factory=list)
# The runtimes the choice means something on; empty = every one. A choice
# about podman's socket has nothing to offer an AppJail install.
runtimes: list[str] = field(default_factory=list)

@property
def default_option(self) -> Option | None:
return next((o for o in self.options if o.id == self.default), None)

def applies(self, runtime: str) -> bool:
return not self.runtimes or runtime in self.runtimes


def _database_options(cid: str, c: dict[str, Any], services: dict[str, Any]) -> tuple[list[Option], dict[str, str]]:
env_map = {str(k): str(v) for k, v in (c.get("env") or {}).items()}
Expand Down Expand Up @@ -257,8 +265,10 @@ def parse(meta: dict[str, Any], compose_data: dict[str, Any] | None = None) -> l
drop=[str(x) for x in (o.get("drop") or [])],
))
default = str(c.get("default", "")) or (opts[0].id if opts else "")
runtimes = c.get("runtimes") or []
out.append(Choice(id=str(cid), label=label, kind=kind, doc=doc, default=default,
service=str(c.get("service", "")), env_map=env_map, options=opts))
service=str(c.get("service", "")), env_map=env_map, options=opts,
runtimes=[str(r) for r in runtimes] if isinstance(runtimes, list) else [str(runtimes)]))
return out


Expand All @@ -276,6 +286,9 @@ def validate(choices: list[Choice], compose_data: dict[str, Any]) -> list[str]:
if not c.options:
errors.append(f"{where}: no options")
continue
for r in c.runtimes:
if r not in RUNTIMES:
errors.append(f"{where}.runtimes: '{r}' is not a runtime (one of {', '.join(RUNTIMES)})")
if c.default_option is None:
errors.append(f"{where}: default '{c.default}' is not one of its options")
elif c.default_option.profile or c.default_option.service_yaml:
Expand Down Expand Up @@ -565,5 +578,8 @@ def to_fjord(choices: list[Choice], compose_text: str, compose_data: dict[str, A
"hostnames": dict(o.hostnames),
"ask": [{"name": a.name, "label": a.label, "default": a.default, "type": a.type, "values": dict(a.values)} for a in o.ask],
})
out.append({"id": c.id, "kind": c.kind, "label": c.label, "doc": c.doc, "default": c.default, "options": opts})
entry = {"id": c.id, "kind": c.kind, "label": c.label, "doc": c.doc, "default": c.default, "options": opts}
if c.runtimes:
entry["runtimes"] = list(c.runtimes)
out.append(entry)
return {"choices": out}
35 changes: 24 additions & 11 deletions dbuild/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,13 @@ def admonition(m: re.Match) -> str:
return _GITHUB_ALERT_RE.sub(admonition, text)


def _combination_count(cfg: Config) -> int:
def _combination_count(cfg: Config, runtime: str = "") -> int:
"""How many ways the choices can be answered; on one runtime, counting
only the choices it shows."""
import math

return math.prod(len(c.options) for c in cfg.metadata.choices) if cfg.metadata.choices else 0
cs = [c for c in cfg.metadata.choices if not runtime or c.applies(runtime)]
return math.prod(len(c.options) for c in cs) if cs else 0


def _choices_mode(cfg: Config) -> str:
Expand Down Expand Up @@ -383,10 +386,10 @@ def _capped_note(cfg: Config, runtime: str) -> str:
"""What the docs say in place of the choices once they are past
MAX_COMBOS (the README; the site when the merge fails), or "" when every
combination is listed."""
count = _combination_count(cfg)
count = _combination_count(cfg, runtime)
if count <= MAX_COMBOS:
return ""
cs = cfg.metadata.choices
cs = [c for c in cfg.metadata.choices if c.applies(runtime)]
where = "its jail from `appjail-director.yml`" if runtime == "appjail" else "its service from `compose.yaml`"
# Every option only keeps or drops services (the default drops none).
if all(_pure(o) for c in cs for o in c.options):
Expand Down Expand Up @@ -600,10 +603,16 @@ def build(pair_list: list) -> list[dict]:
tpl = next((o for o in opts if o.jail_template_file), None)
combos.append({
"id": suffix or "default", "default": is_default,
"selections": [{"choice": c.id, "choice_label": c.label, "option": o.id, "label": o.label, "default": o.id == c.default} for c, o in pairs],
"selections": [{"choice": c.id, "choice_label": c.label, "option": o.id, "label": o.label, "default": o.id == c.default,
"runtimes": [r for r in choices_mod.RUNTIMES if c.applies(r)]} for c, o in pairs],
# A runtime lists a combination only when every choice it does
# not show is at its default (AppJail never sees podman's socket).
"runtimes": [r for r in choices_mod.RUNTIMES if all(c.applies(r) or o.id == c.default for c, o in pairs)],
"labels": {r: ", ".join(o.label for c, o in pairs if c.applies(r)) for r in choices_mod.RUNTIMES},
"label": ", ".join(o.label for _, o in pairs),
"doc": " ".join(o.doc for _, o in pairs if o.doc),
"docs": [o.doc for _, o in pairs if o.doc],
"docs_for": {r: [o.doc for c, o in pairs if o.doc and c.applies(r)] for r in choices_mod.RUNTIMES},
"parts": parts, "jails": jails, "folders": folders,
"appjail": aj, "appjail_env": aj_env,
"director_text": director_text, "director_env_text": director_env_text,
Expand Down Expand Up @@ -645,14 +654,18 @@ def tree(level: int, picked: tuple) -> dict | None:
return {"id": c.id, "label": c.label, "doc": c.doc, "options": nodes}

declared = [{"id": c.id, "label": c.label, "doc": c.doc, "default": c.default, "toggle": _toggle(c),
"runtimes": [r for r in choices_mod.RUNTIMES if c.applies(r)],
"options": [{"id": o.id, "label": o.label, "doc": o.doc, "default": o.id == c.default} for o in c.options]}
for c in cfg.metadata.choices]
choice_ids = [c.id for c in cfg.metadata.choices]
options = [[o.id for o in sorted(c.options, key=lambda o: o.id != c.default)] for c in cfg.metadata.choices]
def merge(rendered: list[str]) -> dict | None:
"""The site's one block for the combinations' renders (in
all_combos' order), or None when they do not merge."""
keys = [tuple(sel["option"] for sel in cb["selections"]) for cb in all_combos()]
def merge(rendered: list[str], runtime: str = "podman") -> dict | None:
"""The site's one block for the combinations' renders (all_combos'
order, the runtime's combinations only), or None when they do not
merge."""
shown = [c for c in cfg.metadata.choices if c.applies(runtime)]
choice_ids = [c.id for c in shown]
options = [[o.id for o in sorted(c.options, key=lambda o: o.id != c.default)] for c in shown]
keys = [tuple(sel["option"] for sel in cb["selections"] if runtime in sel["runtimes"])
for cb in all_combos() if runtime in cb["runtimes"]]
got = choice_merge.merge(dict(zip(keys, (r.split("\n") for r in rendered), strict=True)), keys[0], options)
if got is None:
return None
Expand Down
22 changes: 13 additions & 9 deletions dbuild/templates/README.j2
Original file line number Diff line number Diff line change
Expand Up @@ -448,22 +448,24 @@ Before deploying, ensure your host environment is ready. See the [Quick Start Gu
### Podman Compose

{%- if compose_text and choices %}
{%- for c in choices %}
{%- for c in choices if "podman" in c.runtimes %}

**{{ c.label }}.** {{ c.doc }}
{%- endfor %}
{%- if choices_capped.podman %}

{{ choices_capped.podman }}
{%- endif %}
{%- for cb in (combos[:1] if choices_capped.podman else combos) %}
{%- if not choices_capped.podman %}
{#- Only the combinations Podman can reach: an AppJail-only choice at its default. #}
{%- set pm_combos = [] %}{% for cb in combos if "podman" in cb.runtimes %}{% set _ = pm_combos.append(cb) %}{% endfor %}
{%- for cb in (pm_combos[:1] if choices_capped.podman or pm_combos | length == 1 else pm_combos) %}
{%- if not choices_capped.podman and pm_combos | length > 1 %}

#### {{ cb.label }}{% if cb.default %} (default){% endif %}
#### {{ cb.labels.podman }}{% if cb.default %} (default){% endif %}
{%- endif %}
{%- if cb.doc %}
{%- if cb.docs_for.podman %}

{{ cb.doc }}
{{ cb.docs_for.podman | join(" ") }}
{%- endif %}

{%- if cb.example_env | trim %}
Expand Down Expand Up @@ -535,10 +537,12 @@ Save as `compose.yaml`, then run `podman-compose up -d`.

{{ choices_capped.appjail }}
{%- endif %}
{%- for cb in (combos[:1] if choices_capped.appjail else combos) %}
{%- if not choices_capped.appjail %}
{#- Only the combinations AppJail can reach: a podman-only choice at its default. #}
{%- set aj_combos = [] %}{% for cb in combos if "appjail" in cb.runtimes %}{% set _ = aj_combos.append(cb) %}{% endfor %}
{%- for cb in (aj_combos[:1] if choices_capped.appjail or aj_combos | length == 1 else aj_combos) %}
{%- if not choices_capped.appjail and aj_combos | length > 1 %}

#### {{ cb.label }}{% if cb.default %} (default){% endif %}
#### {{ cb.labels.appjail }}{% if cb.default %} (default){% endif %}
{%- endif %}

{{ appjail_director_snippet(option=cb) }}
Expand Down
36 changes: 21 additions & 15 deletions dbuild/templates/README.mkdocs.j2
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,33 @@
choice_merge folds them into one block whose varying lines carry the
picks that keep them; the script shows those. Combinations the merge
cannot express follow it whole. #}
{#- A choice that is not for this runtime (runtimes: [podman]) gets no row
here, and only the combinations with it at its default are listed, keyed
without it: the script builds a form's key from its own rows. #}
{%- macro choice_form(runtime) -%}
{%- set shown = [] %}{% for c in choices if runtime in c.runtimes %}{% set _ = shown.append(c) %}{% endfor %}
{%- set merged = none %}
{%- if choices_mode == "merged" %}
{%- if shown and choices_mode == "merged" %}
{%- set rendered = [] %}
{%- for cb in all_combos() %}{% set _ = rendered.append(combo_files(cb, runtime, true)) %}{% endfor %}
{%- set merged = choice_merge(rendered) %}
{%- for cb in all_combos() if runtime in cb.runtimes %}{% set _ = rendered.append(combo_files(cb, runtime, true)) %}{% endfor %}
{%- set merged = choice_merge(rendered, runtime) %}
{%- endif %}
{%- if choices_capped[runtime] and not merged %}
{%- if not shown %}
{{ combo_files(combos[0], runtime) }}
{% elif choices_capped[runtime] and not merged %}
{{ choices_capped[runtime] }}

{{ combo_files(combos[0], runtime) }}
{% else -%}
<div class="deploy-choices" data-app="{{ name }}" data-runtime="{{ runtime }}"{% if choices | selectattr("toggle") | list %} data-order="{{ choices | map(attribute='id') | join(' ') }}"{% endif %} markdown>
{% for c in choices if not c.toggle %}
<div class="deploy-choices" data-app="{{ name }}" data-runtime="{{ runtime }}"{% if shown | selectattr("toggle") | list %} data-order="{{ shown | map(attribute='id') | join(' ') }}"{% endif %} markdown>
{% for c in shown if not c.toggle %}

**{{ c.label }}** {{ c.doc }}
{: .choice data-choice="{{ c.id }}" }

<div class="opts" data-choice="{{ c.id }}">{% for o in c.options %}<button type="button" data-option="{{ o.id }}"{% if o.default %} data-default aria-pressed="true"{% else %} aria-pressed="false"{% endif %}{% if o.doc %} title="{{ o.doc | replace('"', "'") }}"{% endif %}>{{ o.label }}</button>{% endfor %}</div>
{% endfor %}
{%- set toggles = choices | selectattr("toggle") | list %}
{%- set toggles = shown | selectattr("toggle") | list %}
{%- if toggles %}

<div class="toggles">{% for c in toggles %}<button type="button" class="toggle" data-choice="{{ c.id }}" data-on="{{ c.toggle.on }}" data-off="{{ c.toggle.off }}" aria-pressed="true" title="{{ c.doc | replace('"', "'") }}"><span class="t-name">{{ c.label }}</span><span class="t-doc">{{ c.doc }}</span></button>{% endfor %}</div>
Expand All @@ -61,9 +67,9 @@
</div>
{% endfor %}
{%- else %}
{% for cb in combos %}
{% for cb in combos if runtime in cb.runtimes %}

<div class="deploy-combo" data-key="{% for sel in cb.selections %}{{ sel.choice }}={{ sel.option }}{{ ";" if not loop.last }}{% endfor %}"{% if not cb.default %} hidden{% endif %} markdown>
<div class="deploy-combo" data-key="{% for sel in cb.selections if runtime in sel.runtimes %}{{ sel.choice }}={{ sel.option }}{{ ";" if not loop.last }}{% endfor %}"{% if not cb.default %} hidden{% endif %} markdown>

{{ combo_files(cb, runtime) }}

Expand All @@ -79,11 +85,11 @@
{% if runtime == "podman" %}
{{ you_get(cb, cb.parts, "container", merged) }}
{% if merged %}
{% for d in cb.docs %}
{% for d in cb.docs_for.podman %}
{{ d }}
{% endfor %}
{% elif cb.doc %}
{{ cb.doc }}
{% elif cb.docs_for.podman %}
{{ cb.docs_for.podman | join(" ") }}
{% endif %}
{#- No .env at all (SQLite: nothing to set): no step asking to fill one in. #}
{% if cb.example_env_site | trim -%}
Expand All @@ -106,11 +112,11 @@ Then run `podman-compose up -d`.
{% else %}
{{ you_get(cb, cb.jails, "jail", merged) }}
{% if merged %}
{% for d in cb.docs %}
{% for d in cb.docs_for.appjail %}
{{ d }}
{% endfor %}
{% elif cb.doc %}
{{ cb.doc }}
{% elif cb.docs_for.appjail %}
{{ cb.docs_for.appjail | join(" ") }}
{% endif %}
{{ appjail_director_snippet(port_var, cb, merged) }}

Expand Down
Loading
Loading