build

package
v1.1.2-alpha Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 18, 2026 License: BSD-3-Clause Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

build

Build system for creating overlay images from Conda packages, shell scripts, or Apptainer definition files with dependency resolution and scheduler integration.

Build Types

Type Source Description
Conda Package spec or YAML Micromamba environment
Shell Build script (#DEP, #SBATCH, #ENV, #INTERACTIVE) Custom installation
Def Apptainer .def file Apptainer build
Ref Shell script (large datasets) Reference data (genomes, indices)

Architecture

object.go       BuildObject interface, base implementation
conda.go        Conda package builds via micromamba
script.go       Shell script builds (apps and ref data)
def.go          Apptainer definition file builds
graph.go        Dependency graph, topological sort, parallel execution
fetch.go        Remote build script downloading
env.go          Environment variable extraction from overlays
whitelist.go    Package name validation

Key Types

BuildObject - Interface for all build types:

  • Type(), NameVersion(), Build(ctx, buildDeps), CreateTmpOverlay()
  • GetMissingDependencies(), RequiresScheduler(), IsInstalled()

BuildType - IsConda, IsDef, IsShell, IsRef

BuildGraph - Manages dependency resolution:

  • Expands transitive dependencies
  • Detects cycles
  • Topological sort (dependencies → dependents)
  • Separates local vs scheduler builds
  • Parallel execution with job dependencies

ScriptSpecs - Build resource requirements (alias to scheduler.ScriptSpecs)

Usage

Creating BuildObjects
// Auto-resolve from name/version
obj, err := build.NewBuildObject("cellranger/9.0.1", external=false, imagesDir, tmpDir)

// Conda with custom source (YAML or package list)
obj, err := build.NewCondaObjectWithSource("myenv/1.0", "/path/env.yml", imagesDir, tmpDir)

// From external file (script or def)
obj, err := build.FromExternalSource("myapp/1.0", "/path/script.sh", isApptainer=false, imagesDir, tmpDir)
Building
// Single build
if err := obj.Build(ctx, buildDeps=true); err != nil {
    // Handle build errors
}

// Build with dependency graph
graph, err := build.NewBuildGraph(objects, imagesDir, tmpDir, submitJobs=true)
if err := graph.Run(ctx); err != nil {
    // Handle errors
}
Build Scripts

Shell scripts support metadata headers:

  • #DEP:name/version - Dependencies
  • #SBATCH / #PBS / #BSUB - Scheduler directives (HTCondor uses native .sub files)
  • #ENV:VAR=$app_root - Environment variables to export
  • #INTERACTIVE:prompt - User input prompts

Available variables in scripts: $NCPUS, $target_dir, $tmp_dir, $app_name, $version

Required function: install()

Dependency Resolution
// Check missing dependencies
missing, err := obj.GetMissingDependencies()

// Find and download remote build script
info, found := build.FindBuildScript("cellranger/9.0.1")
if found && info.IsRemote {
    localPath, err := build.DownloadRemoteScript(info, tmpDir)
}

Build Workflow

Conda:

  1. Check if overlay exists
  2. Create temporary ext3 overlay
  3. micromamba create inside container
  4. Set permissions, pack to SquashFS
  5. Cleanup

Shell:

  1. Check if overlay exists
  2. Create temporary overlay
  3. Build missing dependencies (if enabled)
  4. Run script inside container with overlays
  5. For ref: verify files, create SquashFS from $cnt_dir
  6. For apps: create SquashFS from /cnt
  7. Extract and save ENV variables
  8. Cleanup

Def:

  1. Check if overlay exists
  2. Build SIF with Apptainer
  3. Extract SquashFS partition from SIF
  4. Cleanup

BuildGraph Execution

  1. Expand dependencies - Resolve all transitive deps
  2. Detect cycles - Fail on circular dependencies
  3. Topological sort - Order by dependencies
  4. Separate local/scheduler - Based on resource requirements
  5. Parallel execution:
    • Local builds: Run concurrently with worker pool
    • Scheduler builds: Submit with dependency chains

Environment Variables

Overlays can export environment variables via #ENV: directives:

#ENV:CELLRANGER_ROOT=$app_root
#ENV:PATH=$app_root/bin:$PATH

Extracted to .env file alongside overlay, loaded at runtime.

Default Resource Allocation

Build CPUs are allocated based on priority:

  1. Scheduler-allocated CPUs (inside job)
  2. config.Global.Build.DefaultCPUs
  3. Fallback: 2

Searches in order:

  1. Local absolute path
  2. CONDATAINER_EXTRA_BASE_DIRS
  3. Portable dir (<install>/build-scripts/)
  4. Scratch dir ($SCRATCH/condatainer/build-scripts/)
  5. User dir (~/.local/share/condatainer/build-scripts/)
  6. Remote fetch from GitHub (if PreferRemote or not found locally)

Error Handling

  • ErrTmpOverlayExists - Temporary overlay already exists (build in progress)
  • ErrBuildCancelled - User interrupted build (Ctrl+C)
  • Uses isCancelledByUser() to detect interrupts gracefully

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BuildTypeConda = BuildType{IsConda: true}
	BuildTypeDef   = BuildType{IsDef: true}
	BuildTypeShell = BuildType{IsShell: true}
	BuildTypeRef   = BuildType{IsShell: true, IsRef: true}
)

Predefined build types

View Source
var ErrBuildCancelled = errors.New("build cancelled by user")
View Source
var ErrTmpOverlayExists = errors.New("temporary overlay already exists")
View Source
var PreferRemote bool

PreferRemote controls the precedence of build script resolution. When true, remote scripts take precedence over local scripts. When false (default), local scripts take precedence over remote scripts. Set by CLI commands (--remote flag) or config (prefer_remote: true).

Functions

func DownloadRemoteScript

func DownloadRemoteScript(info ScriptInfo, tmpDir string) (string, error)

DownloadRemoteScript downloads a remote build script to the local tmp directory Returns the local path to the downloaded script

func GetAllBuildScripts

func GetAllBuildScripts(includeRemote bool) (map[string]ScriptInfo, error)

GetAllBuildScripts returns both local and remote build scripts Local scripts take precedence over remote scripts with the same name

func GetDefBuiltList added in v1.1.1

func GetDefBuiltList() map[string]bool

GetDefBuiltList returns the def list, loading from all directories if needed Reads .def_list from ALL image directories (extra → portable → scratch → user) and merges them. Writes only happen to the first writable directory when UpdateDefBuiltList() or RemoveFromDefBuiltList() is called.

func GetEnvDictFromBuildScript

func GetEnvDictFromBuildScript(scriptPath string) (map[string]EnvEntry, error)

GetEnvDictFromBuildScript parses #ENV:KEY=VALUE and #ENVNOTE:KEY description lines from build script. Returns a map of environment variables with their values and notes.

Example lines in script:

#ENV:STAR_INDEX_DIR=$app_root/star
#ENVNOTE:STAR_INDEX_DIR STAR index dir

func GetLocalBuildScripts

func GetLocalBuildScripts() (map[string]ScriptInfo, error)

GetLocalBuildScripts scans all build-scripts directories and returns a map of name -> ScriptInfo. Searches user → scratch → legacy → system directories. First match wins (user scripts shadow system ones).

func GetOverlayArgsFromDependencies

func GetOverlayArgsFromDependencies(dependencies []string) ([]string, error)

GetOverlayArgsFromDependencies generates overlay mount arguments for Apptainer from dependencies. Each dependency is mounted as read-only.

func GetOverlayEnvConfigsFromDependencies

func GetOverlayEnvConfigsFromDependencies(dependencies []string) ([]string, error)

GetOverlayEnvConfigsFromDependencies reads .env files from dependencies and returns environment settings. Returns a list of ["--env", "KEY=VALUE", ...] arguments.

func GetPathEnvFromDependencies

func GetPathEnvFromDependencies(dependencies []string) (string, error)

GetPathEnvFromDependencies generates the PATH environment variable from dependencies. It prepends /cnt/<dep>/bin paths for each dependency overlay.

func GetRemoteBuildScripts

func GetRemoteBuildScripts() (map[string]ScriptInfo, error)

GetRemoteBuildScripts fetches the build scripts metadata from GitHub Returns a map of name -> ScriptInfo

func GetRemoteMetadataURL added in v1.1.1

func GetRemoteMetadataURL() string

GetRemoteMetadataURL returns the URL for the remote build scripts metadata using the configured branch

func RemoveFromDefBuiltList added in v1.1.1

func RemoveFromDefBuiltList(nameVersion, overlayPath string)

RemoveFromDefBuiltList removes an overlay name from the def list in the same directory as the overlay This should be called after removing a .def overlay

func SaveEnvFile

func SaveEnvFile(overlayPath string, envDict map[string]EnvEntry, relativePath string) error

SaveEnvFile saves environment variables to a .env file next to the overlay. The $app_root placeholder is replaced with the actual overlay path.

func UpdateDefBuiltList added in v1.1.1

func UpdateDefBuiltList(nameVersion, overlayPath string)

UpdateDefBuiltList adds an overlay name to the def list in the same directory as the overlay This should be called after building a .def overlay

Types

type BaseBuildObject

type BaseBuildObject struct {
	// contains filtered or unexported fields
}

BaseBuildObject provides common functionality for all build types

func (*BaseBuildObject) BuildSource

func (b *BaseBuildObject) BuildSource() string

func (*BaseBuildObject) Cleanup

func (b *BaseBuildObject) Cleanup(failed bool) error

func (*BaseBuildObject) CntDirPath

func (b *BaseBuildObject) CntDirPath() string

func (*BaseBuildObject) CreateTmpOverlay

func (b *BaseBuildObject) CreateTmpOverlay(ctx context.Context, force bool) error

func (*BaseBuildObject) Dependencies

func (b *BaseBuildObject) Dependencies() []string

func (*BaseBuildObject) GetMissingDependencies

func (b *BaseBuildObject) GetMissingDependencies() ([]string, error)

func (*BaseBuildObject) IsInstalled

func (b *BaseBuildObject) IsInstalled() bool

func (*BaseBuildObject) NameVersion

func (b *BaseBuildObject) NameVersion() string

func (*BaseBuildObject) RequiresScheduler

func (b *BaseBuildObject) RequiresScheduler() bool

func (*BaseBuildObject) ScriptSpecs

func (b *BaseBuildObject) ScriptSpecs() *ScriptSpecs

func (*BaseBuildObject) TargetOverlayPath

func (b *BaseBuildObject) TargetOverlayPath() string

func (*BaseBuildObject) TmpOverlayPath

func (b *BaseBuildObject) TmpOverlayPath() string

type BuildGraph

type BuildGraph struct {
	// contains filtered or unexported fields
}

BuildGraph manages dependency resolution and build ordering It expands BuildObject items to include all transitive dependencies, detects cycles, produces a topologically-sorted order (dependencies before dependents), and separates items into two ordered lists:

  • localBuilds: BuildObjects without scheduler requirements (build locally)
  • schedulerBuilds: BuildObjects that require scheduler submission

func NewBuildGraph

func NewBuildGraph(buildObjects []BuildObject, imagesDir, tmpDir string, submitJobs bool) (*BuildGraph, error)

NewBuildGraph creates a BuildGraph from a list of BuildObjects All overlays are stored in imagesDir regardless of type

func (*BuildGraph) GetAllBuilds

func (bg *BuildGraph) GetAllBuilds() []BuildObject

GetAllBuilds returns all builds in topologically sorted order

func (*BuildGraph) GetJobIDs

func (bg *BuildGraph) GetJobIDs() map[string]string

GetJobIDs returns the map of job IDs for scheduler builds

func (*BuildGraph) GetLocalBuilds

func (bg *BuildGraph) GetLocalBuilds() []BuildObject

GetLocalBuilds returns the list of builds to run locally

func (*BuildGraph) GetSchedulerBuilds

func (bg *BuildGraph) GetSchedulerBuilds() []BuildObject

GetSchedulerBuilds returns the list of builds to submit via scheduler

func (*BuildGraph) Run

func (bg *BuildGraph) Run(ctx context.Context) error

Run executes the build graph First runs local builds, then submits scheduler jobs

type BuildObject

type BuildObject interface {
	// Core properties
	NameVersion() string
	BuildSource() string
	Dependencies() []string
	IsInstalled() bool

	// Type information
	Type() BuildType

	// Path management
	TmpOverlayPath() string
	TargetOverlayPath() string
	CntDirPath() string

	// Scheduler metadata
	ScriptSpecs() *ScriptSpecs
	RequiresScheduler() bool

	// Build operations
	Build(ctx context.Context, buildDeps bool) error
	GetMissingDependencies() ([]string, error)
	CreateTmpOverlay(ctx context.Context, force bool) error
	Cleanup(failed bool) error

	// String representation
	String() string
}

BuildObject represents a build target with its metadata and dependencies

func FromExternalSource

func FromExternalSource(targetPrefix, source string, isApptainer bool, imagesDir, tmpDir string) (BuildObject, error)

FromExternalSource creates a BuildObject from an external build script or def file All overlays are stored in imagesDir regardless of type

func NewBuildObject

func NewBuildObject(nameVersion string, external bool, imagesDir, tmpDir string) (BuildObject, error)

NewBuildObject creates a BuildObject from a name/version string Format: "name/version" for conda/shell, "name" for def, "prefix/name/version" for ref All overlays are stored in imagesDir regardless of type

func NewCondaObjectWithSource

func NewCondaObjectWithSource(nameVersion, buildSource string, imagesDir, tmpDir string) (BuildObject, error)

NewCondaObjectWithSource creates a CondaBuildObject with custom buildSource This is used for the -n flag to create a single sqf with multiple packages or YAML buildSource can be:

  • Path to YAML file (e.g., "/path/to/environment.yml")
  • Comma-separated package list (e.g., "nvim,nodejs,samtools/1.16")

type BuildType

type BuildType struct {
	IsConda bool
	IsDef   bool
	IsShell bool
	IsRef   bool
}

BuildType represents the type of build target

func (BuildType) String

func (bt BuildType) String() string

String implements fmt.Stringer for BuildType. IsRef is checked before IsShell so ref overlays are reported as "ref".

type CondaBuildObject

type CondaBuildObject struct {
	*BaseBuildObject
	// contains filtered or unexported fields
}

CondaBuildObject implements BuildObject for conda packages Supports three modes: 1. Single package (name/version): packageName and packageVersion are set 2. Multiple packages (-n flag): buildSource contains comma-separated package specs 3. YAML file (-p flag): buildSource contains path to .yml/.yaml file

func (*CondaBuildObject) Build

func (c *CondaBuildObject) Build(ctx context.Context, buildDeps bool) error

Build implements the conda package build workflow Workflow:

  1. Check if overlay already exists (skip if yes)
  2. Create temporary ext3 overlay
  3. Run micromamba create inside container with overlay
  4. Set permissions
  5. Pack to SquashFS
  6. Cleanup

func (*CondaBuildObject) String

func (c *CondaBuildObject) String() string

String returns a string representation

func (*CondaBuildObject) Type

func (c *CondaBuildObject) Type() BuildType

Type returns the build type

type DefBuildObject

type DefBuildObject struct {
	*BaseBuildObject
}

DefBuildObject implements BuildObject for Apptainer definition files

func (*DefBuildObject) Build

func (d *DefBuildObject) Build(ctx context.Context, buildDeps bool) error

Build implements the Apptainer .def build workflow Workflow:

  1. Check if overlay already exists (skip if yes)
  2. Try downloading prebuilt overlay if available
  3. Build SIF from .def file using apptainer build --fakeroot
  4. Extract SquashFS partition from SIF
  5. Set permissions
  6. Cleanup

func (*DefBuildObject) String

func (d *DefBuildObject) String() string

String returns a string representation

func (*DefBuildObject) Type

func (d *DefBuildObject) Type() BuildType

Type returns the build type

type EnvEntry

type EnvEntry struct {
	Value string
	Note  string
}

EnvEntry holds an environment variable value and its note

type RemoteScriptEntry

type RemoteScriptEntry struct {
	RelativePath string   `json:"relative_path"`
	Deps         []string `json:"deps"`
	Sbatch       bool     `json:"sbatch"`
	Whatis       string   `json:"whatis"`
}

RemoteScriptEntry represents a single script entry in the metadata

type ScriptBuildObject

type ScriptBuildObject struct {
	*BaseBuildObject
	// contains filtered or unexported fields
}

ScriptBuildObject implements BuildObject for shell scripts

func (*ScriptBuildObject) Build

func (s *ScriptBuildObject) Build(ctx context.Context, buildDeps bool) error

Build implements the shell script build workflow Workflow:

  1. Check if overlay already exists (skip if yes)
  2. Create temporary ext3 overlay
  3. Check and build missing dependencies if buildDeps=true
  4. Run shell script inside container with overlays
  5. For ref overlays: verify files exist, create SquashFS from cnt_dir
  6. For app overlays: set permissions, create SquashFS from /cnt
  7. Extract and save ENV variables if present
  8. Set permissions and cleanup

func (*ScriptBuildObject) String

func (s *ScriptBuildObject) String() string

String returns a string representation

func (*ScriptBuildObject) Type

func (s *ScriptBuildObject) Type() BuildType

Type returns the build type

type ScriptInfo

type ScriptInfo struct {
	Name        string // name/version format (e.g., "samtools/1.21")
	Path        string // full path to build script (local) or relative path (remote)
	IsContainer bool   // true if .def file
	IsRemote    bool   // true if from remote repository
}

ScriptInfo holds information about a build script

func FindBuildScript

func FindBuildScript(nameVersion string) (ScriptInfo, bool)

FindBuildScript looks for a build script by name/version. By default, local scripts take precedence over remote. When PreferRemote is true, remote scripts take precedence over local. Returns the ScriptInfo and a boolean indicating if it was found.

type ScriptSpecs

type ScriptSpecs = scheduler.ScriptSpecs

ScriptSpecs mirrors the scheduler module's job spec metadata.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL