appsv2: add support for a packages_from_raw_bash option in apt where one can add a multiline bash snippet to echo packages

This commit is contained in:
Alexandre Aubin 2023-02-21 14:51:51 +01:00
parent df6a2a2cd2
commit 4dfff20140

View file

@ -20,6 +20,7 @@ import os
import copy import copy
import shutil import shutil
import random import random
import tempfile
from typing import Dict, Any, List from typing import Dict, Any, List
from moulinette import m18n from moulinette import m18n
@ -172,7 +173,30 @@ class AppResource:
app_setting(self.app, key, delete=True) app_setting(self.app, key, delete=True)
def _run_script(self, action, script, env={}, user="root"): def check_output_bash_snippet(self, snippet, env={}):
from yunohost.app import (
_make_environment_for_app_script,
)
env_ = _make_environment_for_app_script(
self.app,
force_include_app_settings=True,
)
env_.update(env)
with tempfile.NamedTemporaryFile(prefix="ynh_") as fp:
fp.write(snippet.encode())
fp.seek(0)
with tempfile.TemporaryFile() as stderr:
out = check_output(f"bash {fp.name}", env=env_, stderr=stderr)
stderr.seek(0)
err = stderr.read().decode()
return out, err
def _run_script(self, action, script, env={}):
from yunohost.app import ( from yunohost.app import (
_make_tmp_workdir_for_app, _make_tmp_workdir_for_app,
_make_environment_for_app_script, _make_environment_for_app_script,
@ -746,6 +770,7 @@ class AptDependenciesAppResource(AppResource):
##### Properties: ##### Properties:
- `packages`: Comma-separated list of packages to be installed via `apt` - `packages`: Comma-separated list of packages to be installed via `apt`
- `packages_from_raw_bash`: A multi-line bash snippet (using triple quotes as open/close) which should echo additional packages to be installed. Meant to be used for packages to be conditionally installed depending on architecture, debian version, install questions, or other logic.
- `extras`: A dict of (repo, key, packages) corresponding to "extra" repositories to fetch dependencies from - `extras`: A dict of (repo, key, packages) corresponding to "extra" repositories to fetch dependencies from
##### Provision/Update: ##### Provision/Update:
@ -767,6 +792,7 @@ class AptDependenciesAppResource(AppResource):
default_properties: Dict[str, Any] = {"packages": [], "extras": {}} default_properties: Dict[str, Any] = {"packages": [], "extras": {}}
packages: List = [] packages: List = []
packages_from_raw_bash: str = ""
extras: Dict[str, Dict[str, str]] = {} extras: Dict[str, Dict[str, str]] = {}
def __init__(self, properties: Dict[str, Any], *args, **kwargs): def __init__(self, properties: Dict[str, Any], *args, **kwargs):
@ -781,6 +807,14 @@ class AptDependenciesAppResource(AppResource):
super().__init__(properties, *args, **kwargs) super().__init__(properties, *args, **kwargs)
if self.packages_from_raw_bash:
out, err = self.check_output_bash_snippet(self.packages_from_raw_bash)
if err:
logger.error("Error while running apt resource packages_from_raw_bash snippet:")
logger.error(err)
self.packages += ", " + out.replace("\n", ", ")
def provision_or_update(self, context: Dict = {}): def provision_or_update(self, context: Dict = {}):
script = [f"ynh_install_app_dependencies {self.packages}"] script = [f"ynh_install_app_dependencies {self.packages}"]
for repo, values in self.extras.items(): for repo, values in self.extras.items():