Skip to content

fix: allow setting MagicDir in Options #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Sep 9, 2024
Merged

fix: allow setting MagicDir in Options #337

merged 14 commits into from
Sep 9, 2024

Conversation

johnstcn
Copy link
Member

@johnstcn johnstcn commented Sep 6, 2024

Relates to coder/terraform-provider-envbuilder#43

This PR unexports constants.MagicDir and replaces it with a different implementation that supports dynamically changing MagicDir at runtime.
There is also some more "strategic" hard-coding of certain values to ensure they do not change when MagicDir changes.

See also: #336

A subsequent PR will update the Terraform provider.

@johnstcn johnstcn self-assigned this Sep 6, 2024
envbuilder.go Outdated
Comment on lines 1410 to 1427
// We always expect the magic directory to be set to the default, signifying that
// the user is running envbuilder in a container.
// If this is set to anything else we should bail out to prevent accidental data loss.
defaultMagicDir := constants.MagicDir("")
kanikoDir, ok := os.LookupEnv("KANIKO_DIR")
if !ok || strings.TrimSpace(kanikoDir) != constants.MagicDir {
if force {
bailoutSecs := 10
logger(log.LevelWarn, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!")
logger(log.LevelWarn, "You have %d seconds to bail out!", bailoutSecs)
for i := bailoutSecs; i > 0; i-- {
logger(log.LevelWarn, "%d...", i)
<-time.After(time.Second)
}
} else {
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", constants.MagicDir)
if !ok || strings.TrimSpace(kanikoDir) != defaultMagicDir.String() {
if !force {
logger(log.LevelError, "KANIKO_DIR is not set to %s. Bailing!\n", defaultMagicDir.String())
logger(log.LevelError, "To bypass this check, set FORCE_SAFE=true.")
return errors.New("safety check failed")
}
bailoutSecs := 10
logger(log.LevelWarn, "WARNING! BYPASSING SAFETY CHECK! THIS WILL DELETE YOUR ROOT FILESYSTEM!")
logger(log.LevelWarn, "You have %d seconds to bail out!", bailoutSecs)
for i := bailoutSecs; i > 0; i-- {
logger(log.LevelWarn, "%d...", i)
<-time.After(time.Second)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: inverted the logic here for readability.

Comment on lines +1485 to +1487
for k := range configFile.AuthConfigs {
logf(log.LevelInfo, "Docker config contains auth for registry %q", k)
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: I found this useful when troubleshooting!

Comment on lines 171 to 174
// MagicDir is the path to the directory where all envbuilder files should be
// stored. By default, this is set to `/.envbuilder`. This is intentionally
// excluded from the CLI options.
MagicDir constants.MagicDir
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review: you can now set MagicDir in Options. You can't set it through ENVBUILDER_MAGIC_DIR though; this is intentional.

Copy link
Contributor

@dannykopping dannykopping left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, only nits.

Copy link
Member

@mtojek mtojek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel the constants package is becoming pretty useless with the dynamic type. What do you think about moving more of the logic to options instead?

return defaultMagicDir
}
return filepath.Join("/", string(m))
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking this abstraction could be a source of bugs. One might not always think to call String and instead do string(MagicDir) which would result in an empty string or potentially lack of /.

I'd rather see this as a validation step in options so that the input is verified there, and an error can be returned if it's not absolute. The options struct could have the MagicImage and MagicBuilt functions if that's preferable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a simpler option here it to simply change the type to a struct with a single unexported field. I'm less worried about leaking the implementation details; we can simply move MagicDir to an internal package.

@johnstcn
Copy link
Member Author

johnstcn commented Sep 9, 2024

I feel the constants package is becoming pretty useless with the dynamic type. What do you think about moving more of the logic to options instead?

I moved some of the logic to options, and the rest to internal/magicdir.

@johnstcn johnstcn merged commit e6c8c66 into main Sep 9, 2024
4 checks passed
@johnstcn johnstcn deleted the cj/less-magic branch September 9, 2024 22:03
johnstcn added a commit that referenced this pull request Sep 26, 2024
- Unexports constants.MagicDir and moves it to an internal package.
- Moves other const/var declarations in `constants` package to where they are used
- Removes constants package
- Adds MagicDirBase to options (intentionally not configurable via CLI)
- Removes usages of default MagicDir to instead use path provided by options

Co-authored-by: Danny Kopping <[email protected]>
Co-authored-by: Mathias Fredriksson <[email protected]>
(cherry picked from commit e6c8c66)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants