2021-05-07 23:45:15 +02:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-05-22 01:27:19 +02:00
|
|
|
"bytes"
|
2021-05-07 23:45:15 +02:00
|
|
|
"context"
|
|
|
|
"errors"
|
2021-05-18 02:02:00 +02:00
|
|
|
"fmt"
|
2021-05-07 23:45:15 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-07-02 18:28:03 +02:00
|
|
|
"strings"
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-22 01:19:07 +02:00
|
|
|
"github.com/rs/zerolog/log"
|
2021-05-26 01:53:26 +02:00
|
|
|
"go.dagger.io/dagger/keychain"
|
2021-07-01 19:42:52 +02:00
|
|
|
"go.dagger.io/dagger/stdlib"
|
2021-05-07 23:45:15 +02:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-05-19 04:15:17 +02:00
|
|
|
ErrNotInit = errors.New("not initialized")
|
|
|
|
ErrAlreadyInit = errors.New("already initialized")
|
|
|
|
ErrNotExist = errors.New("environment doesn't exist")
|
|
|
|
ErrExist = errors.New("environment already exists")
|
2021-05-07 23:45:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-10-23 00:37:28 +02:00
|
|
|
daggerDir = ".dagger"
|
|
|
|
envDir = "env"
|
|
|
|
stateDir = "state"
|
|
|
|
planDir = "plan"
|
|
|
|
manifestFile = "values.yaml"
|
|
|
|
computedFile = "computed.json"
|
2021-05-07 23:45:15 +02:00
|
|
|
)
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
type Project struct {
|
2021-05-19 04:15:17 +02:00
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func Init(ctx context.Context, dir string) (*Project, error) {
|
2021-05-25 22:03:44 +02:00
|
|
|
root, err := filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
daggerRoot := path.Join(root, daggerDir)
|
|
|
|
if err := os.Mkdir(daggerRoot, 0755); err != nil {
|
2021-05-07 23:45:15 +02:00
|
|
|
if errors.Is(err, os.ErrExist) {
|
|
|
|
return nil, ErrAlreadyInit
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-13 23:32:06 +02:00
|
|
|
|
2021-05-25 22:03:44 +02:00
|
|
|
if err := os.Mkdir(path.Join(daggerRoot, envDir), 0755); err != nil {
|
2021-05-22 01:18:47 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-01 19:42:52 +02:00
|
|
|
|
|
|
|
if err := vendorUniverse(ctx, root); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
return &Project{
|
2021-05-19 04:15:17 +02:00
|
|
|
Path: root,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-18 02:02:00 +02:00
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func Open(ctx context.Context, dir string) (*Project, error) {
|
2021-05-19 04:15:17 +02:00
|
|
|
_, err := os.Stat(path.Join(dir, daggerDir))
|
2021-05-18 02:02:00 +02:00
|
|
|
if err != nil {
|
2021-05-19 04:15:17 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, ErrNotInit
|
|
|
|
}
|
2021-05-18 02:02:00 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
root, err := filepath.Abs(dir)
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
return &Project{
|
2021-05-19 04:15:17 +02:00
|
|
|
Path: root,
|
|
|
|
}, nil
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func Current(ctx context.Context) (*Project, error) {
|
2021-05-07 23:45:15 +02:00
|
|
|
current, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk every parent directory to find .dagger
|
|
|
|
for {
|
2021-05-22 01:18:47 +02:00
|
|
|
_, err := os.Stat(path.Join(current, daggerDir, envDir))
|
2021-05-07 23:45:15 +02:00
|
|
|
if err == nil {
|
|
|
|
return Open(ctx, current)
|
|
|
|
}
|
|
|
|
parent := filepath.Dir(current)
|
|
|
|
if parent == current {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
current = parent
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNotInit
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func (w *Project) envPath(name string) string {
|
2021-05-19 04:15:17 +02:00
|
|
|
return path.Join(w.Path, daggerDir, envDir, name)
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func (w *Project) List(ctx context.Context) ([]*State, error) {
|
2021-05-19 04:15:17 +02:00
|
|
|
var (
|
|
|
|
environments = []*State{}
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
files, err := os.ReadDir(path.Join(w.Path, daggerDir, envDir))
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
for _, f := range files {
|
|
|
|
if !f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
st, err := w.Get(ctx, f.Name())
|
|
|
|
if err != nil {
|
2021-06-15 18:49:57 +02:00
|
|
|
// If the environment doesn't exist (e.g. no values.yaml, skip silently)
|
|
|
|
if !errors.Is(err, ErrNotExist) {
|
|
|
|
log.
|
|
|
|
Ctx(ctx).
|
|
|
|
Err(err).
|
|
|
|
Str("name", f.Name()).
|
|
|
|
Msg("failed to load environment")
|
|
|
|
}
|
2021-05-22 01:19:07 +02:00
|
|
|
continue
|
2021-05-19 04:15:17 +02:00
|
|
|
}
|
|
|
|
environments = append(environments, st)
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
return environments, nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func (w *Project) Get(ctx context.Context, name string) (*State, error) {
|
2021-05-19 04:15:17 +02:00
|
|
|
envPath, err := filepath.Abs(w.envPath(name))
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
if _, err := os.Stat(envPath); err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, ErrNotExist
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
manifest, err := os.ReadFile(path.Join(envPath, manifestFile))
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
2021-06-15 18:49:57 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
|
|
|
return nil, ErrNotExist
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
manifest, err = keychain.Decrypt(ctx, manifest)
|
2021-05-18 02:02:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decrypt state: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-07 23:45:15 +02:00
|
|
|
var st State
|
2021-05-19 04:15:17 +02:00
|
|
|
if err := yaml.Unmarshal(manifest, &st); err != nil {
|
2021-05-07 23:45:15 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
st.Path = envPath
|
2021-07-02 18:28:03 +02:00
|
|
|
// FIXME: Backward compat: Support for old-style `.dagger/env/<name>/plan`
|
2021-06-15 18:49:57 +02:00
|
|
|
if st.Plan.Module == "" {
|
|
|
|
planPath := path.Join(envPath, planDir)
|
2021-07-02 18:28:03 +02:00
|
|
|
if _, err := os.Stat(planPath); err == nil {
|
|
|
|
planRelPath, err := filepath.Rel(w.Path, planPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
st.Plan.Module = planRelPath
|
2021-06-15 18:49:57 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-23 20:02:57 +02:00
|
|
|
st.Project = w.Path
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
computed, err := os.ReadFile(path.Join(envPath, stateDir, computedFile))
|
2021-05-07 23:45:15 +02:00
|
|
|
if err == nil {
|
|
|
|
st.Computed = string(computed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &st, nil
|
|
|
|
}
|
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func (w *Project) Save(ctx context.Context, st *State) error {
|
2021-05-07 23:45:15 +02:00
|
|
|
data, err := yaml.Marshal(st)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
manifestPath := path.Join(st.Path, manifestFile)
|
2021-05-18 02:02:00 +02:00
|
|
|
|
2021-05-22 01:27:19 +02:00
|
|
|
currentEncrypted, err := os.ReadFile(manifestPath)
|
2021-05-18 02:02:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-22 01:27:19 +02:00
|
|
|
currentPlain, err := keychain.Decrypt(ctx, currentEncrypted)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to decrypt state: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only update the encrypted file if there were changes
|
2021-05-25 22:03:44 +02:00
|
|
|
if !bytes.Equal(data, currentPlain) {
|
2021-05-22 01:27:19 +02:00
|
|
|
encrypted, err := keychain.Reencrypt(ctx, manifestPath, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(manifestPath, encrypted, 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if st.Computed != "" {
|
2021-05-19 04:15:17 +02:00
|
|
|
state := path.Join(st.Path, stateDir)
|
2021-05-07 23:45:15 +02:00
|
|
|
if err := os.MkdirAll(state, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := os.WriteFile(
|
|
|
|
path.Join(state, "computed.json"),
|
|
|
|
[]byte(st.Computed),
|
|
|
|
0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-22 21:54:18 +02:00
|
|
|
func (w *Project) Create(ctx context.Context, name string, plan Plan, arch string) (*State, error) {
|
2021-07-02 18:28:03 +02:00
|
|
|
if _, err := w.Get(ctx, name); err == nil {
|
|
|
|
return nil, ErrExist
|
|
|
|
}
|
|
|
|
|
|
|
|
pkg, err := w.cleanPackageName(ctx, plan.Package)
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
2021-05-19 04:15:17 +02:00
|
|
|
return nil, err
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 18:28:03 +02:00
|
|
|
envPath, err := filepath.Abs(w.envPath(name))
|
|
|
|
if err != nil {
|
2021-07-01 19:00:58 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
// Environment directory
|
|
|
|
if err := os.MkdirAll(envPath, 0755); err != nil {
|
|
|
|
return nil, err
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
2021-06-15 18:49:57 +02:00
|
|
|
manifestPath := path.Join(envPath, manifestFile)
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
st := &State{
|
2021-09-23 20:02:57 +02:00
|
|
|
Path: envPath,
|
|
|
|
Project: w.Path,
|
2021-07-02 18:28:03 +02:00
|
|
|
Plan: Plan{
|
|
|
|
Package: pkg,
|
|
|
|
},
|
2021-10-22 20:52:25 +02:00
|
|
|
Name: name,
|
2021-10-22 21:54:18 +02:00
|
|
|
Architecture: arch,
|
2021-05-19 04:15:17 +02:00
|
|
|
}
|
2021-06-15 18:49:57 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
data, err := yaml.Marshal(st)
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
key, err := keychain.Default(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
encrypted, err := keychain.Encrypt(ctx, manifestPath, data, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(manifestPath, encrypted, 0600); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
err = os.WriteFile(
|
|
|
|
path.Join(envPath, ".gitignore"),
|
|
|
|
[]byte("# dagger state\nstate/**\n"),
|
|
|
|
0600,
|
|
|
|
)
|
2021-05-07 23:45:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
return st, nil
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
2021-05-26 16:26:13 +02:00
|
|
|
|
2021-09-23 20:02:57 +02:00
|
|
|
func (w *Project) cleanPackageName(ctx context.Context, pkg string) (string, error) {
|
2021-07-02 18:28:03 +02:00
|
|
|
lg := log.
|
|
|
|
Ctx(ctx).
|
|
|
|
With().
|
|
|
|
Str("package", pkg).
|
|
|
|
Logger()
|
|
|
|
|
|
|
|
if pkg == "" {
|
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the package is not a path, then it must be a domain (e.g. foo.bar/mypackage)
|
|
|
|
if _, err := os.Stat(pkg); err != nil {
|
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the domain is in the correct form
|
|
|
|
if !strings.Contains(pkg, ".") || !strings.Contains(pkg, "/") {
|
|
|
|
return "", fmt.Errorf("invalid package %q", pkg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err := filepath.Abs(pkg)
|
|
|
|
if err != nil {
|
|
|
|
lg.Error().Err(err).Msg("unable to resolve path")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(p, w.Path) {
|
2021-09-23 20:02:57 +02:00
|
|
|
lg.Fatal().Err(err).Msg("package is outside the project")
|
2021-07-02 18:28:03 +02:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
p, err = filepath.Rel(w.Path, p)
|
|
|
|
if err != nil {
|
|
|
|
lg.Fatal().Err(err).Msg("unable to resolve path")
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(p, ".") {
|
|
|
|
p = "./" + p
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
2021-05-26 16:26:13 +02:00
|
|
|
}
|
2021-07-01 19:42:52 +02:00
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
func cueModInit(ctx context.Context, parentDir string) error {
|
2021-07-01 19:42:52 +02:00
|
|
|
lg := log.Ctx(ctx)
|
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
modDir := path.Join(parentDir, "cue.mod")
|
|
|
|
if err := os.Mkdir(modDir, 0755); err != nil {
|
2021-07-01 19:42:52 +02:00
|
|
|
if !errors.Is(err, os.ErrExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
modFile := path.Join(modDir, "module.cue")
|
2021-07-01 19:42:52 +02:00
|
|
|
if _, err := os.Stat(modFile); err != nil {
|
|
|
|
if !errors.Is(err, os.ErrNotExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
lg.Debug().Str("mod", parentDir).Msg("initializing cue.mod")
|
2021-07-01 19:42:52 +02:00
|
|
|
|
|
|
|
if err := os.WriteFile(modFile, []byte("module: \"\"\n"), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:32:06 +02:00
|
|
|
if err := os.Mkdir(path.Join(modDir, "usr"), 0755); err != nil {
|
2021-07-01 19:42:52 +02:00
|
|
|
if !errors.Is(err, os.ErrExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 23:32:06 +02:00
|
|
|
if err := os.Mkdir(path.Join(modDir, "pkg"), 0755); err != nil {
|
2021-07-01 19:42:52 +02:00
|
|
|
if !errors.Is(err, os.ErrExist) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:55:06 +02:00
|
|
|
func vendorUniverse(ctx context.Context, p string) error {
|
2021-07-01 19:42:52 +02:00
|
|
|
// ensure cue module is initialized
|
2021-10-13 23:55:06 +02:00
|
|
|
if err := cueModInit(ctx, p); err != nil {
|
2021-07-01 19:42:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add universe to `.gitignore`
|
|
|
|
if err := os.WriteFile(
|
2021-10-13 23:55:06 +02:00
|
|
|
path.Join(p, "cue.mod", "pkg", ".gitignore"),
|
2021-07-01 19:42:52 +02:00
|
|
|
[]byte(fmt.Sprintf("# dagger universe\n%s\n", stdlib.PackageName)),
|
|
|
|
0600,
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-13 23:55:06 +02:00
|
|
|
log.Ctx(ctx).Debug().Str("mod", p).Msg("vendoring universe")
|
2021-10-23 00:41:14 +02:00
|
|
|
if err := stdlib.Vendor(ctx, p); err != nil {
|
|
|
|
// FIXME(samalba): disabled install remote stdlib temporarily
|
|
|
|
// if _, err := mod.Install(ctx, p, "alpha.dagger.io", ""); err != nil {
|
2021-07-01 19:42:52 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|