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-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-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 (
|
|
|
|
daggerDir = ".dagger"
|
2021-05-19 04:15:17 +02:00
|
|
|
envDir = "env"
|
2021-05-07 23:45:15 +02:00
|
|
|
stateDir = "state"
|
2021-05-19 04:15:17 +02:00
|
|
|
planDir = "plan"
|
2021-05-07 23:45:15 +02:00
|
|
|
manifestFile = "values.yaml"
|
|
|
|
computedFile = "computed.json"
|
|
|
|
)
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
type Workspace struct {
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
|
|
|
func Init(ctx context.Context, dir string) (*Workspace, 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-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-05-19 04:15:17 +02:00
|
|
|
return &Workspace{
|
|
|
|
Path: root,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-05-18 02:02:00 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
func Open(ctx context.Context, dir string) (*Workspace, error) {
|
|
|
|
_, 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-05-19 04:15:17 +02:00
|
|
|
return &Workspace{
|
|
|
|
Path: root,
|
|
|
|
}, nil
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
func Current(ctx context.Context) (*Workspace, 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-05-19 04:15:17 +02:00
|
|
|
func (w *Workspace) envPath(name string) string {
|
|
|
|
return path.Join(w.Path, daggerDir, envDir, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Workspace) List(ctx context.Context) ([]*State, error) {
|
|
|
|
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-05-22 01:19:07 +02:00
|
|
|
log.
|
|
|
|
Ctx(ctx).
|
|
|
|
Err(err).
|
|
|
|
Str("name", f.Name()).
|
|
|
|
Msg("failed to load environment")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Workspace) Get(ctx context.Context, name string) (*State, error) {
|
|
|
|
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 {
|
|
|
|
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-05-26 00:14:39 +02:00
|
|
|
st.Plan = path.Join(envPath, planDir)
|
2021-05-19 04:15:17 +02:00
|
|
|
st.Workspace = 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-05-19 04:15:17 +02:00
|
|
|
func (w *Workspace) 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-05-19 04:15:17 +02:00
|
|
|
func (w *Workspace) Create(ctx context.Context, name string) (*State, error) {
|
|
|
|
envPath, err := filepath.Abs(w.envPath(name))
|
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-05-19 04:15:17 +02:00
|
|
|
// Environment directory
|
|
|
|
if err := os.MkdirAll(envPath, 0755); err != nil {
|
|
|
|
if errors.Is(err, os.ErrExist) {
|
|
|
|
return nil, ErrExist
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
2021-05-19 04:15:17 +02:00
|
|
|
return nil, err
|
2021-05-07 23:45:15 +02:00
|
|
|
}
|
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
// Plan directory
|
|
|
|
if err := os.Mkdir(path.Join(envPath, planDir), 0755); err != nil {
|
|
|
|
if errors.Is(err, os.ErrExist) {
|
|
|
|
return nil, ErrExist
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
manifestPath := path.Join(envPath, manifestFile)
|
2021-05-07 23:45:15 +02:00
|
|
|
|
2021-05-19 04:15:17 +02:00
|
|
|
st := &State{
|
|
|
|
Path: envPath,
|
|
|
|
Workspace: w.Path,
|
2021-05-26 00:14:39 +02:00
|
|
|
Plan: path.Join(envPath, planDir),
|
2021-05-19 04:15:17 +02:00
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|