Skip to content

Commit 70b7f9a

Browse files
sgaistarjun024
authored andcommitted
feat: allow use of mamba solver in place of conda's default solver
This can be enabled through the use of the BP_CONDA_SOLVER environment variable. The use of the mamba solver offers a welcome speedup for the resolution of conda environments.
1 parent 1853d59 commit 70b7f9a

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

build.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package miniconda
22

33
import (
4+
"os"
45
"path/filepath"
56
"time"
67

78
"github.com/paketo-buildpacks/packit/v2"
89
"github.com/paketo-buildpacks/packit/v2/cargo"
910
"github.com/paketo-buildpacks/packit/v2/chronos"
1011
"github.com/paketo-buildpacks/packit/v2/draft"
12+
"github.com/paketo-buildpacks/packit/v2/pexec"
1113
"github.com/paketo-buildpacks/packit/v2/postal"
1214
"github.com/paketo-buildpacks/packit/v2/sbom"
1315
"github.com/paketo-buildpacks/packit/v2/scribe"
@@ -34,6 +36,14 @@ type SBOMGenerator interface {
3436
GenerateFromDependency(dependency postal.Dependency, dir string) (sbom.SBOM, error)
3537
}
3638

39+
func GetEnvOrDefault(key, defaultValue string) string {
40+
value, exists := os.LookupEnv(key)
41+
if !exists {
42+
return defaultValue
43+
}
44+
return value
45+
}
46+
3747
// Build will return a packit.BuildFunc that will be invoked during the build
3848
// phase of the buildpack lifecycle.
3949
//
@@ -137,6 +147,39 @@ func Build(
137147
logger.Action("Completed in %s", duration.Round(time.Millisecond))
138148
logger.Break()
139149

150+
solver := GetEnvOrDefault("BP_CONDA_SOLVER", "conda")
151+
152+
if solver == "mamba" {
153+
logger.Subprocess("Installing mamba solver")
154+
155+
conda := pexec.NewExecutable(condaLayer.Path + "/bin/conda")
156+
duration, err = clock.Measure(func() error {
157+
return conda.Execute(pexec.Execution{
158+
Args: []string{"install", "-n", "base", "conda-libmamba-solver", "-y"},
159+
})
160+
})
161+
if err != nil {
162+
return packit.BuildResult{}, err
163+
}
164+
165+
logger.Action("Solver completed in %s", duration.Round(time.Millisecond))
166+
logger.Break()
167+
168+
logger.Subprocess("Configuring mamba solver")
169+
duration, err = clock.Measure(func() error {
170+
return conda.Execute(pexec.Execution{
171+
Args: []string{"config", "--set", "solver", "libmamba"},
172+
})
173+
})
174+
if err != nil {
175+
return packit.BuildResult{}, err
176+
}
177+
178+
logger.Action("Configuration completed in %s", duration.Round(time.Millisecond))
179+
logger.Break()
180+
181+
}
182+
140183
condaLayer.Metadata = map[string]interface{}{
141184
DepKey: dependencyChecksum,
142185
}

integration/logging_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,35 @@ func testLogging(t *testing.T, context spec.G, it spec.S) {
7272
MatchRegexp(` Completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
7373
))
7474
})
75+
76+
it("logs helpful information to the user with mamba installation", func() {
77+
var (
78+
logs fmt.Stringer
79+
err error
80+
)
81+
82+
image, logs, err = pack.WithNoColor().Build.
83+
WithEnv(map[string]string{"BP_CONDA_SOLVER": "mamba"}).
84+
WithPullPolicy("never").
85+
WithBuildpacks(
86+
settings.Buildpacks.Miniconda.Online,
87+
settings.Buildpacks.BuildPlan.Online,
88+
).
89+
Execute(name, source)
90+
Expect(err).ToNot(HaveOccurred(), logs.String)
91+
92+
Expect(logs).To(ContainLines(
93+
MatchRegexp(fmt.Sprintf(`%s \d+\.\d+\.\d+`, settings.Buildpack.Name)),
94+
" Executing build process",
95+
MatchRegexp(` Installing Miniconda \d+\.\d+\.\d+`),
96+
MatchRegexp(` Completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
97+
"",
98+
" Installing mamba solver",
99+
MatchRegexp(` Solver completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
100+
"",
101+
" Configuring mamba solver",
102+
MatchRegexp(` Configuration completed in ([0-9]*(\.[0-9]*)?[a-z]+)+`),
103+
))
104+
})
75105
})
76106
}

0 commit comments

Comments
 (0)