Skip to content
Snippets Groups Projects
Commit b18e16ba authored by adrianciu's avatar adrianciu Committed by Adrian Ciu
Browse files

esd-spack-installation: additional methods

parent 94313b94
No related branches found
No related tags found
1 merge request!4feat(spack_operation): implement setup_spack_env functionality
Pipeline #58844 failed with stages
in 9 minutes and 59 seconds
...@@ -5,6 +5,7 @@ stages: ...@@ -5,6 +5,7 @@ stages:
variables: variables:
BUILD_ENV_DOCKER_IMAGE: docker-registry.ebrains.eu/esd/tmp:latest BUILD_ENV_DOCKER_IMAGE: docker-registry.ebrains.eu/esd/tmp:latest
build-wheel: build-wheel:
stage: build stage: build
tags: tags:
......
...@@ -14,13 +14,18 @@ class BashCommandException(SpackException): ...@@ -14,13 +14,18 @@ class BashCommandException(SpackException):
""" """
class NoSpackEnvironmentException(SpackException): class NoSpackEnvironmentException(BashCommandException):
""" """
To be thrown when an operation on a spack environment is executed without the environment being activated or existent To be thrown when an operation on a spack environment is executed without the environment being activated or existent
""" """
class SpackConcertizeException(SpackException): class SpackConcertizeException(BashCommandException):
""" """
To be thrown when the spack concretization step fails To be thrown when the spack concretization step fails
""" """
class SpackInstallPackagesException(BashCommandException):
"""
To be thrown when the spack fails to install spack packages
"""
import os import os
import re
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from pathlib import Path from pathlib import Path
from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException, \
SpackInstallPackagesException
from esd.logger.logger_builder import get_logger from esd.logger.logger_builder import get_logger
from esd.model.SpackModel import SpackModel from esd.model.SpackModel import SpackModel
from esd.spack_manager.wrapper.spack_wrapper import no_spack_env
from esd.utils.utils import run_command, git_clone_repo from esd.utils.utils import run_command, git_clone_repo
...@@ -103,10 +106,10 @@ class SpackManager(ABC): ...@@ -103,10 +106,10 @@ class SpackManager(ABC):
else: else:
if self.spack_env_exists(): if self.spack_env_exists():
result = run_command("bash", "-c", result = run_command("bash", "-c",
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo list', f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo list',
check=True, check=True,
capture_output=True, text=True, logger=self.logger, capture_output=True, text=True, logger=self.logger,
debug_msg=f'Checking if repository {repo_name} was added') debug_msg=f'Checking if repository {repo_name} was added')
else: else:
self.logger.debug('No spack environment defined') self.logger.debug('No spack environment defined')
raise NoSpackEnvironmentException('No spack environment defined') raise NoSpackEnvironmentException('No spack environment defined')
...@@ -124,18 +127,35 @@ class SpackManager(ABC): ...@@ -124,18 +127,35 @@ class SpackManager(ABC):
return False return False
return True return True
@no_spack_env
def add_spack_repo(self, repo_path: Path, repo_name: str): def add_spack_repo(self, repo_path: Path, repo_name: str):
"""Add the Spack repository if it does not exist.""" """Add the Spack repository if it does not exist."""
if self.spack_env_exists(): run_command("bash", "-c",
run_command("bash", "-c", f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo add {repo_path}/{repo_name}',
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack repo add {repo_path}/{repo_name}', check=True, logger=self.logger,
check=True, logger=self.logger, debug_msg=f"Added {repo_name} to spack environment {self.env.env_name}",
debug_msg=f"Added {repo_name} to spack environment {self.env.env_name}", exception_msg=f"Failed to add {repo_name} to spack environment {self.env.env_name}",
exception_msg=f"Failed to add {repo_name} to spack environment {self.env.env_name}", exception=BashCommandException)
exception=BashCommandException)
else: @no_spack_env
self.logger.debug('No spack environment defined') def get_compiler_version(self):
raise NoSpackEnvironmentException('No spack environment defined') result = run_command("bash", "-c",
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack compiler list',
check=True, logger=self.logger,
capture_output=True, text=True,
debug_msg=f"Checking spack environment compiler version for {self.env.env_name}",
exception_msg=f"Failed to checking spack environment compiler version for {self.env.env_name}",
exception=BashCommandException)
# todo add error handling and tests
if result.stdout is None:
self.logger.debug('No gcc found for {self.env.env_name}')
return None
# Find the first occurrence of a GCC compiler using regex
match = re.search(r"gcc@([\d\.]+)", result.stdout)
gcc_version = match.group(1)
self.logger.debug(f'Found gcc for {self.env.env_name}: {gcc_version}')
return gcc_version
def get_spack_installed_version(self): def get_spack_installed_version(self):
spack_version = run_command("bash", "-c", f'source {self.spack_setup_script} && spack --version', spack_version = run_command("bash", "-c", f'source {self.spack_setup_script} && spack --version',
...@@ -147,6 +167,7 @@ class SpackManager(ABC): ...@@ -147,6 +167,7 @@ class SpackManager(ABC):
return spack_version.stdout.strip().split()[0] return spack_version.stdout.strip().split()[0]
return None return None
def install_spack(self, spack_version="v0.21.1", spack_repo='https://github.com/spack/spack'): def install_spack(self, spack_version="v0.21.1", spack_repo='https://github.com/spack/spack'):
try: try:
user = os.getlogin() user = os.getlogin()
......
import functools
from esd.error_handling.exceptions import NoSpackEnvironmentException
def no_spack_env(method):
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
if self.spack_env_exists():
return method(self, *args, **kwargs) # Call the method with 'self'
else:
self.logger.debug('No spack environment defined')
raise NoSpackEnvironmentException('No spack environment defined')
return wrapper
import os
from pathlib import Path from pathlib import Path
import pytest import pytest
from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException from esd.error_handling.exceptions import BashCommandException, NoSpackEnvironmentException
from esd.model.SpackModel import SpackModel from esd.model.SpackModel import SpackModel
from esd.spack_manager.enums.SpackManagerEnum import SpackManagerEnum from esd.spack_manager.enums.SpackManagerEnum import SpackManagerEnum
from esd.spack_manager.factory.SpackManagerCreator import SpackManagerCreator from esd.spack_manager.factory.SpackManagerCreator import SpackManagerCreator
from esd.spack_manager.factory.SpackManagerScratch import SpackManagerScratch
from esd.utils.utils import file_exists_and_not_empty from esd.utils.utils import file_exists_and_not_empty
SPACK_ENV_ACCESS_TOKEN = os.getenv("SPACK_ENV_ACCESS_TOKEN")
def test_spack_repo_exists_1(): def test_spack_repo_exists_1():
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH) spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH)
...@@ -34,9 +33,9 @@ def test_spack_repo_exists_3(tmp_path): ...@@ -34,9 +33,9 @@ def test_spack_repo_exists_3(tmp_path):
def test_spack_from_scratch_setup_1(tmp_path): def test_spack_from_scratch_setup_1(tmp_path):
install_dir = tmp_path install_dir = tmp_path
env = SpackModel('ebrains-spack-builds', install_dir, env = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git', ) 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env,
system_name='ebrainslab') system_name='ebrainslab')
spack_manager.setup_spack_env() spack_manager.setup_spack_env()
assert spack_manager.spack_repo_exists(env.env_name) == False assert spack_manager.spack_repo_exists(env.env_name) == False
...@@ -44,11 +43,11 @@ def test_spack_from_scratch_setup_1(tmp_path): ...@@ -44,11 +43,11 @@ def test_spack_from_scratch_setup_1(tmp_path):
def test_spack_from_scratch_setup_2(tmp_path): def test_spack_from_scratch_setup_2(tmp_path):
install_dir = tmp_path install_dir = tmp_path
env = SpackModel('ebrains-spack-builds', install_dir, env = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git', ) 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env,
repos=[repo, repo], repos=[repo, repo],
system_name='ebrainslab') system_name='ebrainslab')
spack_manager.setup_spack_env() spack_manager.setup_spack_env()
assert spack_manager.spack_repo_exists(env.env_name) == True assert spack_manager.spack_repo_exists(env.env_name) == True
...@@ -58,8 +57,8 @@ def test_spack_from_scratch_setup_3(tmp_path): ...@@ -58,8 +57,8 @@ def test_spack_from_scratch_setup_3(tmp_path):
env = SpackModel('new_env1', install_dir) env = SpackModel('new_env1', install_dir)
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env,
repos=[repo, repo], repos=[repo, repo],
system_name='ebrainslab') system_name='ebrainslab')
with pytest.raises(BashCommandException): with pytest.raises(BashCommandException):
spack_manager.setup_spack_env() spack_manager.setup_spack_env()
...@@ -76,8 +75,8 @@ def test_spack_not_a_valid_repo(): ...@@ -76,8 +75,8 @@ def test_spack_not_a_valid_repo():
env = SpackModel('ebrains-spack-builds', Path(), None) env = SpackModel('ebrains-spack-builds', Path(), None)
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env,
repos=[repo], repos=[repo],
system_name='ebrainslab') system_name='ebrainslab')
with pytest.raises(NoSpackEnvironmentException): with pytest.raises(NoSpackEnvironmentException):
spack_manager.add_spack_repo(repo.path, repo.env_name) spack_manager.add_spack_repo(repo.path, repo.env_name)
...@@ -85,7 +84,7 @@ def test_spack_not_a_valid_repo(): ...@@ -85,7 +84,7 @@ def test_spack_not_a_valid_repo():
def test_spack_from_scratch_concretize_1(tmp_path): def test_spack_from_scratch_concretize_1(tmp_path):
install_dir = tmp_path install_dir = tmp_path
env = SpackModel('ebrains-spack-builds', install_dir, env = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git', ) 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo, repo], spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo, repo],
system_name='ebrainslab') system_name='ebrainslab')
...@@ -98,7 +97,7 @@ def test_spack_from_scratch_concretize_1(tmp_path): ...@@ -98,7 +97,7 @@ def test_spack_from_scratch_concretize_1(tmp_path):
def test_spack_from_scratch_concretize_2(tmp_path): def test_spack_from_scratch_concretize_2(tmp_path):
install_dir = tmp_path install_dir = tmp_path
env = SpackModel('ebrains-spack-builds', install_dir, env = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git', ) 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo, repo], spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo, repo],
system_name='ebrainslab') system_name='ebrainslab')
...@@ -111,11 +110,59 @@ def test_spack_from_scratch_concretize_2(tmp_path): ...@@ -111,11 +110,59 @@ def test_spack_from_scratch_concretize_2(tmp_path):
def test_spack_from_scratch_concretize_3(tmp_path): def test_spack_from_scratch_concretize_3(tmp_path):
install_dir = tmp_path install_dir = tmp_path
env = SpackModel('ebrains-spack-builds', install_dir, env = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git', ) 'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
repo = env repo = env
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env,
repos=[repo, repo], repos=[repo, repo],
system_name='ebrainslab') system_name='ebrainslab')
spack_manager.setup_spack_env() spack_manager.setup_spack_env()
concretization_file_path = spack_manager.env_path / 'spack.lock' concretization_file_path = spack_manager.env_path / 'spack.lock'
assert file_exists_and_not_empty(concretization_file_path) == False assert file_exists_and_not_empty(concretization_file_path) == False
def test_spack_from_scratch_concretize_4(tmp_path):
install_dir = tmp_path
env = SpackModel('test-spack-env', install_dir,
f'https://oauth2:{SPACK_ENV_ACCESS_TOKEN}@gitlab.ebrains.eu/ri/projects-and-initiatives/virtualbraintwin/test-spack-env.git')
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env)
spack_manager.setup_spack_env()
spack_manager.concretize_spack_env(force=False)
concretization_file_path = spack_manager.env_path / 'spack.lock'
assert file_exists_and_not_empty(concretization_file_path) == True
def test_spack_from_scratch_concretize_5(tmp_path):
install_dir = tmp_path
env = SpackModel('test-spack-env', install_dir,
f'https://oauth2:{SPACK_ENV_ACCESS_TOKEN}@gitlab.ebrains.eu/ri/projects-and-initiatives/virtualbraintwin/test-spack-env.git')
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env)
spack_manager.setup_spack_env()
spack_manager.concretize_spack_env(force=True)
concretization_file_path = spack_manager.env_path / 'spack.lock'
assert file_exists_and_not_empty(concretization_file_path) == True
def test_spack_from_scratch_concretize_6(tmp_path):
install_dir = tmp_path
env = SpackModel('test-spack-env', install_dir,
f'https://oauth2:{SPACK_ENV_ACCESS_TOKEN}@gitlab.ebrains.eu/ri/projects-and-initiatives/virtualbraintwin/test-spack-env.git')
repo = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo])
spack_manager.setup_spack_env()
spack_manager.concretize_spack_env(force=False)
concretization_file_path = spack_manager.env_path / 'spack.lock'
assert file_exists_and_not_empty(concretization_file_path) == True
def test_spack_from_scratch_concretize_7(tmp_path):
install_dir = tmp_path
env = SpackModel('test-spack-env', install_dir,
'https://gitlab.ebrains.eu/ri/projects-and-initiatives/virtualbraintwin/test-spack-env.git')
repo = SpackModel('ebrains-spack-builds', install_dir,
'https://gitlab.ebrains.eu/ri/tech-hub/platform/esd/ebrains-spack-builds.git')
spack_manager = SpackManagerCreator.get_spack_manger(SpackManagerEnum.FROM_SCRATCH, env=env, repos=[repo])
spack_manager.setup_spack_env()
spack_manager.concretize_spack_env(force=True)
concretization_file_path = spack_manager.env_path / 'spack.lock'
assert file_exists_and_not_empty(concretization_file_path) == True
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment