Allowing environment variables to be set in a spack.yaml (#47587)
This adds a new configuration section called `env_vars:` that can be set in an environment. It looks very similar to the existing `environment:` section that can be added to `modules.yaml`, but it is global for an entire spack environment. It's called `env_vars:` to deconflate it with spack environments (the term was too overloaded). The syntax looks like this: ```yaml spack: specs: - cmake%gcc env_vars: set: ENVAR_SET_IN_ENV_LOAD: "True" ``` Any of our standard environment modifications can be added to the `env_vars` section, e.g. `prepend_path:`, `unset:`, `append_path:`, etc. Operations in `env_vars:` are performed on `spack env activate` and undone on `spack env deactivate`.
This commit is contained in:
parent
4e8a6eec1a
commit
c764400338
@ -53,6 +53,7 @@
|
|||||||
import spack.schema.definitions
|
import spack.schema.definitions
|
||||||
import spack.schema.develop
|
import spack.schema.develop
|
||||||
import spack.schema.env
|
import spack.schema.env
|
||||||
|
import spack.schema.env_vars
|
||||||
import spack.schema.mirrors
|
import spack.schema.mirrors
|
||||||
import spack.schema.modules
|
import spack.schema.modules
|
||||||
import spack.schema.packages
|
import spack.schema.packages
|
||||||
@ -70,6 +71,7 @@
|
|||||||
"compilers": spack.schema.compilers.schema,
|
"compilers": spack.schema.compilers.schema,
|
||||||
"concretizer": spack.schema.concretizer.schema,
|
"concretizer": spack.schema.concretizer.schema,
|
||||||
"definitions": spack.schema.definitions.schema,
|
"definitions": spack.schema.definitions.schema,
|
||||||
|
"env_vars": spack.schema.env_vars.schema,
|
||||||
"view": spack.schema.view.schema,
|
"view": spack.schema.view.schema,
|
||||||
"develop": spack.schema.develop.schema,
|
"develop": spack.schema.develop.schema,
|
||||||
"mirrors": spack.schema.mirrors.schema,
|
"mirrors": spack.schema.mirrors.schema,
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
import spack.environment as ev
|
import spack.environment as ev
|
||||||
import spack.repo
|
import spack.repo
|
||||||
|
import spack.schema.environment
|
||||||
import spack.store
|
import spack.store
|
||||||
from spack.util.environment import EnvironmentModifications
|
from spack.util.environment import EnvironmentModifications
|
||||||
|
|
||||||
@ -156,6 +157,11 @@ def activate(
|
|||||||
# MANPATH, PYTHONPATH, etc. All variables that end in PATH (case-sensitive)
|
# MANPATH, PYTHONPATH, etc. All variables that end in PATH (case-sensitive)
|
||||||
# become PATH variables.
|
# become PATH variables.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
env_vars_yaml = env.manifest.configuration.get("env_vars", None)
|
||||||
|
if env_vars_yaml:
|
||||||
|
env_mods.extend(spack.schema.environment.parse(env_vars_yaml))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if view and env.has_view(view):
|
if view and env.has_view(view):
|
||||||
with spack.store.STORE.db.read_transaction():
|
with spack.store.STORE.db.read_transaction():
|
||||||
@ -189,6 +195,10 @@ def deactivate() -> EnvironmentModifications:
|
|||||||
if active is None:
|
if active is None:
|
||||||
return env_mods
|
return env_mods
|
||||||
|
|
||||||
|
env_vars_yaml = active.manifest.configuration.get("env_vars", None)
|
||||||
|
if env_vars_yaml:
|
||||||
|
env_mods.extend(spack.schema.environment.parse(env_vars_yaml).reversed())
|
||||||
|
|
||||||
active_view = os.getenv(ev.spack_env_view_var)
|
active_view = os.getenv(ev.spack_env_view_var)
|
||||||
|
|
||||||
if active_view and active.has_view(active_view):
|
if active_view and active.has_view(active_view):
|
||||||
|
22
lib/spack/spack/schema/env_vars.py
Normal file
22
lib/spack/spack/schema/env_vars.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
# Copyright Spack Project Developers. See COPYRIGHT file for details.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
|
||||||
|
"""Schema for env_vars.yaml configuration file.
|
||||||
|
|
||||||
|
.. literalinclude:: _spack_root/lib/spack/spack/schema/env_vars.py
|
||||||
|
:lines: 15-
|
||||||
|
"""
|
||||||
|
from typing import Any, Dict
|
||||||
|
|
||||||
|
import spack.schema.environment
|
||||||
|
|
||||||
|
properties: Dict[str, Any] = {"env_vars": spack.schema.environment.definition}
|
||||||
|
|
||||||
|
#: Full schema with metadata
|
||||||
|
schema = {
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "Spack env_vars configuration file schema",
|
||||||
|
"type": "object",
|
||||||
|
"additionalProperties": False,
|
||||||
|
"properties": properties,
|
||||||
|
}
|
@ -20,6 +20,7 @@
|
|||||||
import spack.schema.container
|
import spack.schema.container
|
||||||
import spack.schema.definitions
|
import spack.schema.definitions
|
||||||
import spack.schema.develop
|
import spack.schema.develop
|
||||||
|
import spack.schema.env_vars
|
||||||
import spack.schema.mirrors
|
import spack.schema.mirrors
|
||||||
import spack.schema.modules
|
import spack.schema.modules
|
||||||
import spack.schema.packages
|
import spack.schema.packages
|
||||||
@ -38,6 +39,7 @@
|
|||||||
spack.schema.ci.properties,
|
spack.schema.ci.properties,
|
||||||
spack.schema.definitions.properties,
|
spack.schema.definitions.properties,
|
||||||
spack.schema.develop.properties,
|
spack.schema.develop.properties,
|
||||||
|
spack.schema.env_vars.properties,
|
||||||
spack.schema.mirrors.properties,
|
spack.schema.mirrors.properties,
|
||||||
spack.schema.modules.properties,
|
spack.schema.modules.properties,
|
||||||
spack.schema.packages.properties,
|
spack.schema.packages.properties,
|
||||||
|
@ -3023,6 +3023,35 @@ def test_stack_view_activate_from_default(
|
|||||||
assert "FOOBAR=mpileaks" in shell
|
assert "FOOBAR=mpileaks" in shell
|
||||||
|
|
||||||
|
|
||||||
|
def test_envvar_set_in_activate(tmpdir, mock_fetch, mock_packages, mock_archive, install_mockery):
|
||||||
|
filename = str(tmpdir.join("spack.yaml"))
|
||||||
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
|
f.write(
|
||||||
|
"""\
|
||||||
|
spack:
|
||||||
|
specs:
|
||||||
|
- cmake%gcc
|
||||||
|
env_vars:
|
||||||
|
set:
|
||||||
|
ENVAR_SET_IN_ENV_LOAD: "True"
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
with tmpdir.as_cwd():
|
||||||
|
env("create", "test", "./spack.yaml")
|
||||||
|
with ev.read("test"):
|
||||||
|
install()
|
||||||
|
|
||||||
|
test_env = ev.read("test")
|
||||||
|
output = env("activate", "--sh", "test")
|
||||||
|
|
||||||
|
assert "ENVAR_SET_IN_ENV_LOAD=True" in output
|
||||||
|
|
||||||
|
with test_env:
|
||||||
|
with spack.util.environment.set_env(ENVAR_SET_IN_ENV_LOAD="True"):
|
||||||
|
output = env("deactivate", "--sh")
|
||||||
|
assert "unset ENVAR_SET_IN_ENV_LOAD" in output
|
||||||
|
|
||||||
|
|
||||||
def test_stack_view_no_activate_without_default(
|
def test_stack_view_no_activate_without_default(
|
||||||
installed_environment, template_combinatorial_env, tmp_path
|
installed_environment, template_combinatorial_env, tmp_path
|
||||||
):
|
):
|
||||||
|
@ -1201,19 +1201,19 @@ complete -c spack -n '__fish_spack_using_command config' -l scope -r -d 'configu
|
|||||||
|
|
||||||
# spack config get
|
# spack config get
|
||||||
set -g __fish_spack_optspecs_spack_config_get h/help
|
set -g __fish_spack_optspecs_spack_config_get h/help
|
||||||
complete -c spack -n '__fish_spack_using_command_pos 0 config get' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view'
|
complete -c spack -n '__fish_spack_using_command_pos 0 config get' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view'
|
||||||
complete -c spack -n '__fish_spack_using_command config get' -s h -l help -f -a help
|
complete -c spack -n '__fish_spack_using_command config get' -s h -l help -f -a help
|
||||||
complete -c spack -n '__fish_spack_using_command config get' -s h -l help -d 'show this help message and exit'
|
complete -c spack -n '__fish_spack_using_command config get' -s h -l help -d 'show this help message and exit'
|
||||||
|
|
||||||
# spack config blame
|
# spack config blame
|
||||||
set -g __fish_spack_optspecs_spack_config_blame h/help
|
set -g __fish_spack_optspecs_spack_config_blame h/help
|
||||||
complete -c spack -n '__fish_spack_using_command_pos 0 config blame' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view'
|
complete -c spack -n '__fish_spack_using_command_pos 0 config blame' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view'
|
||||||
complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -f -a help
|
complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -f -a help
|
||||||
complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -d 'show this help message and exit'
|
complete -c spack -n '__fish_spack_using_command config blame' -s h -l help -d 'show this help message and exit'
|
||||||
|
|
||||||
# spack config edit
|
# spack config edit
|
||||||
set -g __fish_spack_optspecs_spack_config_edit h/help print-file
|
set -g __fish_spack_optspecs_spack_config_edit h/help print-file
|
||||||
complete -c spack -n '__fish_spack_using_command_pos 0 config edit' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop mirrors modules packages repos upstreams view'
|
complete -c spack -n '__fish_spack_using_command_pos 0 config edit' -f -a 'bootstrap cdash ci compilers concretizer config definitions develop env_vars mirrors modules packages repos upstreams view'
|
||||||
complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -f -a help
|
complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -f -a help
|
||||||
complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -d 'show this help message and exit'
|
complete -c spack -n '__fish_spack_using_command config edit' -s h -l help -d 'show this help message and exit'
|
||||||
complete -c spack -n '__fish_spack_using_command config edit' -l print-file -f -a print_file
|
complete -c spack -n '__fish_spack_using_command config edit' -l print-file -f -a print_file
|
||||||
|
Loading…
Reference in New Issue
Block a user