universe vendoring

Rather than injecting universe at runtime, this change will vendor
alpha.dagger.io in `cue.mod` directly.

Fixes #700

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-07-01 19:42:52 +02:00
parent bf6a9f3588
commit f58ee5811b
17 changed files with 170 additions and 59 deletions

View File

@@ -1,5 +1,10 @@
package state
import (
"context"
"path"
)
// Contents of an environment serialized to a file
type State struct {
// State path
@@ -28,11 +33,26 @@ func (s *State) Source() Input {
w := s.Workspace
// FIXME: backward compatibility
if mod := s.Plan.Module; mod != "" {
w = mod
w = path.Join(w, mod)
}
return DirInput(w, []string{}, []string{})
}
// VendorUniverse vendors the latest (built-in) version of the universe into the
// environment's `cue.mod`.
// FIXME: This has nothing to do in `State` and should be tied to a `Workspace`.
// However, since environments could point to different modules before, we have
// to handle vendoring on a per environment basis.
func (s *State) VendorUniverse(ctx context.Context) error {
w := s.Workspace
// FIXME: backward compatibility
if mod := s.Plan.Module; mod != "" {
w = path.Join(w, mod)
}
return vendorUniverse(ctx, w)
}
type Plan struct {
Module string `yaml:"module,omitempty"`
Package string `yaml:"package,omitempty"`

View File

@@ -12,6 +12,7 @@ import (
"github.com/rs/zerolog/log"
"go.dagger.io/dagger/keychain"
"go.dagger.io/dagger/stdlib"
"gopkg.in/yaml.v3"
)
@@ -51,6 +52,11 @@ func Init(ctx context.Context, dir string) (*Workspace, error) {
if err := os.Mkdir(path.Join(daggerRoot, envDir), 0755); err != nil {
return nil, err
}
if err := vendorUniverse(ctx, root); err != nil {
return nil, err
}
return &Workspace{
Path: root,
}, nil
@@ -335,3 +341,63 @@ func (w *Workspace) cleanPackageName(ctx context.Context, pkg string) (string, e
return p, nil
}
func cueModInit(ctx context.Context, p string) error {
lg := log.Ctx(ctx)
mod := path.Join(p, "cue.mod")
if err := os.Mkdir(mod, 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
modFile := path.Join(mod, "module.cue")
if _, err := os.Stat(modFile); err != nil {
if !errors.Is(err, os.ErrNotExist) {
return err
}
lg.Debug().Str("mod", p).Msg("initializing cue.mod")
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
return err
}
}
if err := os.Mkdir(path.Join(mod, "usr"), 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
if err := os.Mkdir(path.Join(mod, "pkg"), 0755); err != nil {
if !errors.Is(err, os.ErrExist) {
return err
}
}
return nil
}
func vendorUniverse(ctx context.Context, p string) error {
// ensure cue module is initialized
if err := cueModInit(ctx, p); err != nil {
return err
}
// add universe to `.gitignore`
if err := os.WriteFile(
path.Join(p, "cue.mod", "pkg", ".gitignore"),
[]byte(fmt.Sprintf("# dagger universe\n%s\n", stdlib.PackageName)),
0600,
); err != nil {
return err
}
log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe")
if err := stdlib.Vendor(ctx, p); err != nil {
return err
}
return nil
}