-
Notifications
You must be signed in to change notification settings - Fork 447
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into add-kde-neon-6-to-schema
- Loading branch information
Showing
27 changed files
with
432 additions
and
25 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*- | ||
# | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License version 3 as | ||
# published by the Free Software Foundation. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Extension to automatically set environment variables on snaps.""" | ||
|
||
from typing import Any, Dict, Optional, Tuple | ||
|
||
from overrides import overrides | ||
|
||
from .extension import Extension | ||
|
||
|
||
class EnvInjector(Extension): | ||
"""Extension to automatically set environment variables on snaps. | ||
This extension allows you to transform snap options into environment | ||
variables | ||
It configures your application to run a command-chain that transforms the | ||
snap options into environment variables automatically. | ||
- To set global environment variables for all applications **inside** the snap: | ||
.. code-block:: shell | ||
sudo snap set <snap-name> env.<key>=<value> | ||
- To set environment variables for a specific application **inside** the snap: | ||
.. code-block:: shell | ||
sudo snap set <snap-name> apps.<app-name>.env.<key>=<value> | ||
- To set environment file inside the snap: | ||
.. code-block:: shell | ||
sudo snap set <snap-name> env-file=<path-to-env-file> | ||
- To set environment file for a specific app: | ||
.. code-block:: shell | ||
sudo snap set <snap-name> apps.<app-name>.envfile=<path-to-env-file> | ||
""" | ||
|
||
@staticmethod | ||
@overrides | ||
def get_supported_bases() -> Tuple[str, ...]: | ||
return ("core24",) | ||
|
||
@staticmethod | ||
@overrides | ||
def get_supported_confinement() -> Tuple[str, ...]: | ||
return ("strict", "devmode", "classic") | ||
|
||
@staticmethod | ||
@overrides | ||
def is_experimental(base: Optional[str]) -> bool: | ||
return True | ||
|
||
@overrides | ||
def get_root_snippet(self) -> Dict[str, Any]: | ||
return {} | ||
|
||
@overrides | ||
def get_app_snippet(self, *, app_name: str) -> Dict[str, Any]: | ||
"""Return the app snippet to apply.""" | ||
return { | ||
"command-chain": ["bin/command-chain/env-exporter"], | ||
"environment": { | ||
"env_alias": f"{app_name}", | ||
}, | ||
} | ||
|
||
@overrides | ||
def get_part_snippet(self, *, plugin_name: str) -> Dict[str, Any]: | ||
return {} | ||
|
||
@overrides | ||
def get_parts_snippet(self) -> Dict[str, Any]: | ||
toolchain = self.get_toolchain(self.arch) | ||
if toolchain is None: | ||
raise ValueError( | ||
f"Unsupported architecture for env-injector extension: {self.arch}" | ||
) | ||
|
||
return { | ||
"env-injector/env-injector": { | ||
"source": "https://github.com/canonical/snappy-env.git", | ||
"source-tag": "v1.0.0-beta", | ||
"plugin": "nil", | ||
"build-snaps": ["rustup"], | ||
"override-build": f""" | ||
rustup default stable | ||
rustup target add {toolchain} | ||
cargo build --target {toolchain} --release | ||
mkdir -p $SNAPCRAFT_PART_INSTALL/bin/command-chain | ||
cp target/{toolchain}/release/env-exporter $SNAPCRAFT_PART_INSTALL/bin/command-chain | ||
""", | ||
} | ||
} | ||
|
||
def get_toolchain(self, arch: str): | ||
"""Get the Rust toolchain for the current architecture.""" | ||
toolchain = { | ||
"amd64": "x86_64-unknown-linux-gnu", | ||
"arm64": "aarch64-unknown-linux-gnu", | ||
# 'armhf': 'armv8-unknown-linux-gnueabihf', # Tier 2 toolchain | ||
# 'riscv64': 'riscv64gc-unknown-linux-gnu', # Tier 2 toolchain | ||
# 'ppc64el': 'powerpc64-unknown-linux-gnu', # Tier 2 toolchain | ||
# 's390x': 's390x-unknown-linux-gnu', # Tier 2 toolchain | ||
} | ||
return toolchain.get(arch) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
summary: Build and run a basic hello-world snap using extensions | ||
|
||
systems: | ||
- ubuntu-24.04* | ||
|
||
environment: | ||
|
||
SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS: "1" | ||
SNAP_DIR: ../snaps/env-injector-hello | ||
SNAP: env-injector-hello | ||
|
||
prepare: | | ||
#shellcheck source=tests/spread/tools/snapcraft-yaml.sh | ||
. "$TOOLS_DIR/snapcraft-yaml.sh" | ||
set_base "$SNAP_DIR/snap/snapcraft.yaml" | ||
restore: | | ||
cd "$SNAP_DIR" | ||
snapcraft clean | ||
rm -f /var/snap/"${SNAP}"/common/*.env | ||
rm -f ./*.snap | ||
rm -rf ./squashfs-root | ||
#shellcheck source=tests/spread/tools/snapcraft-yaml.sh | ||
. "$TOOLS_DIR/snapcraft-yaml.sh" | ||
restore_yaml "snap/snapcraft.yaml" | ||
execute: | | ||
assert_env() { | ||
local snap_app="$1" | ||
local env_name="$2" | ||
local exp_value="$3" | ||
local actual_value | ||
if ! eval "$snap_app" | grep -q "^${env_name}="; then | ||
echo "Environment variable '$env_name' is not set." | ||
return 1 | ||
fi | ||
if [ -z "$env_name" ]; then | ||
empty=$( "$snap_app" | grep "=${exp_value}") | ||
[ -z "$empty" ] || return 1 | ||
fi | ||
actual_value=$( "$snap_app" | grep "^${env_name}=" | cut -d'=' -f2-) | ||
if [ "$actual_value" != "$exp_value" ]; then | ||
echo "Environment variable '$env_name' does not match the expected value." | ||
echo "Expected: '$env_name=$exp_value', but got: '$env_name=$actual_value'" | ||
return 1 | ||
fi | ||
return 0 | ||
} | ||
cd "$SNAP_DIR" | ||
SNAPCRAFT_ENABLE_EXPERIMENTAL_EXTENSIONS=1 snapcraft | ||
unsquashfs "${SNAP}"_1.0_*.snap | ||
# Check that the env-exporter program is present | ||
[ -f squashfs-root/bin/command-chain/env-exporter ] | ||
# Check that the exec-env script is present | ||
[ -f squashfs-root/usr/bin/exec-env ] | ||
snap install "${SNAP}"_1.0_*.snap --dangerous | ||
echo "[env-injector] Creating global envfile" | ||
echo 'HELLO_WORLD="Hello World"' >> /var/snap/"${SNAP}"/common/global.env | ||
# Load global envfile | ||
snap set env-injector-hello envfile=/var/snap/"${SNAP}"/common/global.env | ||
echo "[TEST] - Check if the global envfile is loaded for all apps" | ||
assert_env "env-injector-hello.hello1" "HELLO_WORLD" "Hello World" || exit 1 | ||
assert_env "env-injector-hello.hello2" "HELLO_WORLD" "Hello World" || exit 1 | ||
assert_env "env-injector-hello.hello-demo" "HELLO_WORLD" "Hello World" || exit 1 | ||
echo "[env-injector] Creating app-specific envfile" | ||
echo 'SCOPED=Scoped' >> /var/snap/"${SNAP}"/common/appenv.env | ||
# Load app-specific envfile | ||
snap set env-injector-hello apps.hello1.envfile=/var/snap/"${SNAP}"/common/appenv.env | ||
echo "[TEST] - Check if the app-specific envfile is loaded for the app" | ||
assert_env "env-injector-hello.hello1" "SCOPED" "Scoped" || exit 1 | ||
echo "[env-injector] Setting global env variable" | ||
# Set env vars: Global | ||
snap set env-injector-hello env.global="World" | ||
echo "[TEST] - Check if the global env var is set for all apps" | ||
assert_env "env-injector-hello.hello1" "GLOBAL" "World" || exit 1 | ||
assert_env "env-injector-hello.hello2" "GLOBAL" "World" || exit 1 | ||
assert_env "env-injector-hello.hello-demo" "GLOBAL" "World" || exit 1 | ||
echo "[env-injector] Setting app-specific env variable" | ||
# Set env vars: specific to each app | ||
snap set env-injector-hello apps.hello1.env.hello="Hello" | ||
snap set env-injector-hello apps.hello2.env.specific="City" | ||
echo "[TEST] - Check if the app-specific env var IS SET for the app hello1" | ||
assert_env "env-injector-hello.hello1" "HELLO" "Hello" || exit 1 | ||
echo "[TEST] - Check if the app-specific env var IS NOT SET for the app hello2" | ||
! assert_env "env-injector-hello.hello2" "HELLO" "Hello" || exit 1 | ||
echo "[TEST] - Check if the app-specific env var IS SET for the app hello2" | ||
assert_env "env-injector-hello.hello2" "SPECIFIC" "City" || exit 1 | ||
echo "[TEST] - Check if the app-specific env var IS NOT SET for the app hello1" | ||
! assert_env "env-injector-hello.hello1" "SPECIFIC" "City" || exit 1 | ||
snap set env-injector-hello env.word.dot="wrong" | ||
echo "[TEST] - Check if the key with dot was ignored" | ||
! assert_env "env-injector-hello.hello1" "" "wrong" || exit 1 | ||
echo "[env-injector] Testing order of env vars" | ||
echo 'ORDER="From envfile"' >> /var/snap/"${SNAP}"/common/local.env | ||
snap set env-injector-hello apps.hello1.env.order="from app-specific" | ||
snap set env-injector-hello apps.hello1.envfile=/var/snap/"${SNAP}"/common/local.env | ||
echo "[TEST] - Check if local overrites global" | ||
assert_env "env-injector-hello.hello1" "ORDER" "from app-specific" || exit 1 | ||
echo "[env-injector] Run hello-demo app" | ||
snap set env-injector-hello apps.myapp.env.specific="City" | ||
echo "[TEST] Make sure that alias is NOT rewritten" | ||
assert_env "env-injector-hello.hello-demo" "SPECIFIC" "City" || exit 1 |
1 change: 1 addition & 0 deletions
1
tests/spread/extensions/snaps/env-injector-hello/snap/hooks/configure
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 @@ | ||
#!/bin/bash |
Oops, something went wrong.