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

esd-spack-installation: added additional spack commands

parent b9f536fa
No related branches found
No related tags found
1 merge request!4feat(spack_operation): implement setup_spack_env functionality
Pipeline #59199 passed with stages
in 23 minutes and 2 seconds
...@@ -29,3 +29,13 @@ class SpackInstallPackagesException(BashCommandException): ...@@ -29,3 +29,13 @@ class SpackInstallPackagesException(BashCommandException):
""" """
To be thrown when the spack fails to install spack packages To be thrown when the spack fails to install spack packages
""" """
class SpackMirrorException(BashCommandException):
"""
To be thrown when the spack add mirror command fails
"""
class SpackGpgException(BashCommandException):
"""
To be thrown when the spack fails to create gpg keys
"""
import os import os
import re import re
import subprocess import subprocess
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, SpackConcertizeException SpackInstallPackagesException, SpackConcertizeException, SpackMirrorException, SpackGpgException
from esd.logger.logger_builder import get_logger from esd.logger.logger_builder import get_logger
from esd.configuration.SpackConfig import SpackConfig from esd.configuration.SpackConfig import SpackConfig
from esd.tests.testing_variables import SPACK_VERSION from esd.tests.testing_variables import SPACK_VERSION
...@@ -12,7 +11,7 @@ from esd.wrapper.spack_wrapper import check_spack_env ...@@ -12,7 +11,7 @@ from esd.wrapper.spack_wrapper import check_spack_env
from esd.utils.utils import run_command, git_clone_repo, log_command, set_bashrc_variable from esd.utils.utils import run_command, git_clone_repo, log_command, set_bashrc_variable
class SpackOperation(ABC): class SpackOperation:
""" """
This class should implement the methods necessary for installing spack, set up an environment, concretize and install packages. This class should implement the methods necessary for installing spack, set up an environment, concretize and install packages.
Factory design pattern is used because there are 2 cases: creating an environment from scratch or creating an environment from the buildcache. Factory design pattern is used because there are 2 cases: creating an environment from scratch or creating an environment from the buildcache.
...@@ -38,10 +37,6 @@ class SpackOperation(ABC): ...@@ -38,10 +37,6 @@ class SpackOperation(ABC):
self.env_path = spack_config.env.path / spack_config.env.env_name self.env_path = spack_config.env.path / spack_config.env.env_name
self.spack_command_on_env = f'source {self.spack_setup_script} && spack env activate -p {self.env_path}' self.spack_command_on_env = f'source {self.spack_setup_script} && spack env activate -p {self.env_path}'
@abstractmethod
def concretize_spack_env(self, force=True):
pass
def create_fetch_spack_environment(self): def create_fetch_spack_environment(self):
if self.spack_config.env.git_path: if self.spack_config.env.git_path:
git_clone_repo(self.spack_config.env.env_name, self.spack_config.env.path / self.spack_config.env.env_name, git_clone_repo(self.spack_config.env.env_name, self.spack_config.env.path / self.spack_config.env.env_name,
...@@ -65,8 +60,10 @@ class SpackOperation(ABC): ...@@ -65,8 +60,10 @@ class SpackOperation(ABC):
set_bashrc_variable('SYSTEMNAME', self.spack_config.system_name, bashrc_path, logger=self.logger) set_bashrc_variable('SYSTEMNAME', self.spack_config.system_name, bashrc_path, logger=self.logger)
os.environ['SYSTEMNAME'] = self.spack_config.system_name os.environ['SYSTEMNAME'] = self.spack_config.system_name
if self.spack_dir.exists() and self.spack_dir.is_dir(): if self.spack_dir.exists() and self.spack_dir.is_dir():
set_bashrc_variable('SPACK_USER_CACHE_PATH', str(self.spack_dir / ".spack"), bashrc_path, logger=self.logger) set_bashrc_variable('SPACK_USER_CACHE_PATH', str(self.spack_dir / ".spack"), bashrc_path,
set_bashrc_variable('SPACK_USER_CONFIG_PATH', str(self.spack_dir / ".spack"), bashrc_path, logger=self.logger) logger=self.logger)
set_bashrc_variable('SPACK_USER_CONFIG_PATH', str(self.spack_dir / ".spack"), bashrc_path,
logger=self.logger)
self.logger.debug('Added env variables SPACK_USER_CACHE_PATH and SPACK_USER_CONFIG_PATH') self.logger.debug('Added env variables SPACK_USER_CACHE_PATH and SPACK_USER_CONFIG_PATH')
else: else:
self.logger.error(f'Invalid installation path: {self.spack_dir}') self.logger.error(f'Invalid installation path: {self.spack_dir}')
...@@ -160,13 +157,52 @@ class SpackOperation(ABC): ...@@ -160,13 +157,52 @@ class SpackOperation(ABC):
def concretize_spack_env(self, force=True): def concretize_spack_env(self, force=True):
force = '--force' if force else '' force = '--force' if force else ''
run_command("bash", "-c", run_command("bash", "-c",
f'source {self.spack_setup_script} && spack env activate -p {self.env_path} && spack concretize {force}', f'{self.spack_command_on_env} && spack concretize {force}',
check=True, check=True,
capture_output=True, text=True, logger=self.logger, logger=self.logger,
info_msg=f'Concertization step for {self.spack_config.env.env_name}', info_msg=f'Concertization step for {self.spack_config.env.env_name}',
exception_msg=f'Failed the concertization step for {self.spack_config.env.env_name}', exception_msg=f'Failed the concertization step for {self.spack_config.env.env_name}',
exception=SpackConcertizeException) exception=SpackConcertizeException)
def create_gpg_keys(self, gpg_name='example', gpg_mail='example@example.com'):
run_command("bash", "-c",
f'source {self.spack_setup_script} && spack gpg init && spack gpg create {gpg_name} {gpg_mail}',
check=True,
logger=self.logger,
info_msg=f'Created pgp keys for {self.spack_config.env.env_name}',
exception_msg=f'Failed to create pgp keys mirror {self.spack_config.env.env_name}',
exception=SpackGpgException)
def add_mirror(self, mirror_name: str, mirror_path: Path, signed=False, autopush=False, global_mirror=False):
autopush = '--autopush' if autopush else ''
signed = '--signed' if signed else ''
if global_mirror:
run_command("bash", "-c",
f'source {self.spack_setup_script} && spack mirror add {autopush} {signed} {mirror_name} {mirror_path}',
check=True,
logger=self.logger,
info_msg=f'Added mirror {mirror_name}',
exception_msg=f'Failed to add mirror {mirror_name}',
exception=SpackMirrorException)
else:
check_spack_env(
run_command("bash", "-c",
f'{self.spack_command_on_env} && spack mirror add {autopush} {signed} {mirror_name} {mirror_path}',
check=True,
logger=self.logger,
info_msg=f'Added mirror {mirror_name}',
exception_msg=f'Failed to add mirror {mirror_name}',
exception=SpackMirrorException))
def remove_mirror(self, mirror_name: str):
run_command("bash", "-c",
f'source {self.spack_setup_script} && spack mirror rm {mirror_name}',
check=True,
logger=self.logger,
info_msg=f'Removing mirror {mirror_name}',
exception_msg=f'Failed to remove mirror {mirror_name}',
exception=SpackMirrorException)
@check_spack_env @check_spack_env
def install_packages(self, jobs: int, signed=True, fresh=False, debug=False): def install_packages(self, jobs: int, signed=True, fresh=False, debug=False):
signed = '' if signed else '--no-check-signature' signed = '' if signed else '--no-check-signature'
......
import os
from esd.build_cache.BuildCacheManager import BuildCacheManager
from esd.logger.logger_builder import get_logger from esd.logger.logger_builder import get_logger
from esd.spack_factory.SpackOperation import SpackOperation from esd.spack_factory.SpackOperation import SpackOperation
from esd.configuration.SpackConfig import SpackConfig from esd.configuration.SpackConfig import SpackConfig
...@@ -8,8 +10,19 @@ class SpackOperationUseCache(SpackOperation): ...@@ -8,8 +10,19 @@ class SpackOperationUseCache(SpackOperation):
This class uses caching for the concretization step and for the installation step. This class uses caching for the concretization step and for the installation step.
""" """
def __init__(self, spack_config: SpackConfig = SpackConfig()): def __init__(self, spack_config: SpackConfig = SpackConfig(), cache_version_concretize='cache',
cache_version_build='cache'):
super().__init__(spack_config, logger=get_logger(__name__)) super().__init__(spack_config, logger=get_logger(__name__))
self.cache_dependency = BuildCacheManager(os.environ.get('CONCRETIZE_OCI_HOST'),
os.environ.get('CONCRETIZE_OCI_PROJECT'),
os.environ.get('CONCRETIZE_OCI_USERNAME'),
os.environ.get('CONCRETIZE_OCI_PASSWORD'),
cache_version=cache_version_concretize)
self.build_cache = BuildCacheManager(os.environ.get('BUILDCACHE_OCI_HOST'),
os.environ.get('BUILDCACHE_OCI_PROJECT'),
os.environ.get('BUILDCACHE_OCI_USERNAME'),
os.environ.get('BUILDCACHE_OCI_PASSWORD'),
cache_version=cache_version_build)
def setup_spack_env(self): def setup_spack_env(self):
super().setup_spack_env() super().setup_spack_env()
......
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