env -> route cleanup
Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
parent
3bfa056b9b
commit
a45f3447b7
@ -60,8 +60,8 @@ func NewClient(ctx context.Context, host string) (*Client, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: return completed *Env, instead of *compiler.Value
|
// FIXME: return completed *Route, instead of *compiler.Value
|
||||||
func (c *Client) Up(ctx context.Context, env *Route) (*compiler.Value, error) {
|
func (c *Client) Up(ctx context.Context, route *Route) (*compiler.Value, error) {
|
||||||
lg := log.Ctx(ctx)
|
lg := log.Ctx(ctx)
|
||||||
eg, gctx := errgroup.WithContext(ctx)
|
eg, gctx := errgroup.WithContext(ctx)
|
||||||
|
|
||||||
@ -78,7 +78,7 @@ func (c *Client) Up(ctx context.Context, env *Route) (*compiler.Value, error) {
|
|||||||
outr, outw := io.Pipe()
|
outr, outw := io.Pipe()
|
||||||
eg.Go(func() error {
|
eg.Go(func() error {
|
||||||
defer outw.Close()
|
defer outw.Close()
|
||||||
return c.buildfn(gctx, env, events, outw)
|
return c.buildfn(gctx, route, events, outw)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Spawn output retriever
|
// Spawn output retriever
|
||||||
@ -95,11 +95,11 @@ func (c *Client) Up(ctx context.Context, env *Route) (*compiler.Value, error) {
|
|||||||
return out, compiler.Err(eg.Wait())
|
return out, compiler.Err(eg.Wait())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) buildfn(ctx context.Context, env *Route, ch chan *bk.SolveStatus, w io.WriteCloser) error {
|
func (c *Client) buildfn(ctx context.Context, route *Route, ch chan *bk.SolveStatus, w io.WriteCloser) error {
|
||||||
lg := log.Ctx(ctx)
|
lg := log.Ctx(ctx)
|
||||||
|
|
||||||
// Scan local dirs to grant access
|
// Scan local dirs to grant access
|
||||||
localdirs := env.LocalDirs()
|
localdirs := route.LocalDirs()
|
||||||
for label, dir := range localdirs {
|
for label, dir := range localdirs {
|
||||||
abs, err := filepath.Abs(dir)
|
abs, err := filepath.Abs(dir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -132,24 +132,24 @@ func (c *Client) buildfn(ctx context.Context, env *Route, ch chan *bk.SolveStatu
|
|||||||
s := NewSolver(c.c, gw, ch)
|
s := NewSolver(c.c, gw, ch)
|
||||||
|
|
||||||
lg.Debug().Msg("loading configuration")
|
lg.Debug().Msg("loading configuration")
|
||||||
if err := env.Update(ctx, s); err != nil {
|
if err := route.LoadLayout(ctx, s); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute output overlay
|
// Compute output overlay
|
||||||
lg.Debug().Msg("computing env")
|
lg.Debug().Msg("computing route")
|
||||||
if err := env.Up(ctx, s, nil); err != nil {
|
if err := route.Up(ctx, s, nil); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export env to a cue directory
|
// Export route to a cue directory
|
||||||
// FIXME: this should be elsewhere
|
// FIXME: this should be elsewhere
|
||||||
lg.Debug().Msg("exporting env")
|
lg.Debug().Msg("exporting route")
|
||||||
span, _ := opentracing.StartSpanFromContext(ctx, "Env.Export")
|
span, _ := opentracing.StartSpanFromContext(ctx, "Route.Export")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
st := llb.Scratch().File(
|
st := llb.Scratch().File(
|
||||||
llb.Mkfile("state.cue", 0600, env.State().JSON()),
|
llb.Mkfile("state.cue", 0600, route.State().JSON()),
|
||||||
llb.WithCustomName("[internal] serializing state to JSON"),
|
llb.WithCustomName("[internal] serializing state to JSON"),
|
||||||
)
|
)
|
||||||
ref, err := s.Solve(ctx, st)
|
ref, err := s.Solve(ctx, st)
|
||||||
@ -178,7 +178,7 @@ func (c *Client) buildfn(ctx context.Context, env *Route, ch chan *bk.SolveStatu
|
|||||||
func (c *Client) outputfn(ctx context.Context, r io.Reader) (*compiler.Value, error) {
|
func (c *Client) outputfn(ctx context.Context, r io.Reader) (*compiler.Value, error) {
|
||||||
lg := log.Ctx(ctx)
|
lg := log.Ctx(ctx)
|
||||||
|
|
||||||
// FIXME: merge this into env output.
|
// FIXME: merge this into route output.
|
||||||
out := compiler.EmptyStruct()
|
out := compiler.EmptyStruct()
|
||||||
|
|
||||||
tr := tar.NewReader(r)
|
tr := tar.NewReader(r)
|
||||||
|
@ -123,11 +123,6 @@ func (v *Value) Int64() (int64, error) {
|
|||||||
return v.val.Int64()
|
return v.val.Int64()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Value) SourceUnsafe() string {
|
|
||||||
s, _ := v.SourceString()
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// Proxy function to the underlying cue.Value
|
// Proxy function to the underlying cue.Value
|
||||||
func (v *Value) Path() cue.Path {
|
func (v *Value) Path() cue.Path {
|
||||||
return v.val.Path()
|
return v.val.Path()
|
||||||
@ -236,12 +231,6 @@ func (v *Value) Source() ([]byte, error) {
|
|||||||
return cueformat.Node(v.val.Eval().Syntax())
|
return cueformat.Node(v.val.Eval().Syntax())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return cue source for this value, as a Go string
|
|
||||||
func (v *Value) SourceString() (string, error) {
|
|
||||||
b, err := v.Source()
|
|
||||||
return string(b), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (v *Value) IsEmptyStruct() bool {
|
func (v *Value) IsEmptyStruct() bool {
|
||||||
if st, err := v.Struct(); err == nil {
|
if st, err := v.Struct(); err == nil {
|
||||||
if st.Len() == 0 {
|
if st.Len() == 0 {
|
||||||
|
@ -84,7 +84,11 @@ func ops(code ...*compiler.Value) ([]*compiler.Value, error) {
|
|||||||
ops = append(ops, xops...)
|
ops = append(ops, xops...)
|
||||||
} else {
|
} else {
|
||||||
// 4. error
|
// 4. error
|
||||||
return nil, fmt.Errorf("not executable: %s", x.SourceUnsafe())
|
source, err := x.Source()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("not executable: %s", source)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return ops, nil
|
return ops, nil
|
||||||
|
@ -82,18 +82,6 @@ type Route struct {
|
|||||||
state *compiler.Value
|
state *compiler.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) ID() string {
|
|
||||||
return r.st.ID
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Route) Name() string {
|
|
||||||
return r.st.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Route) LayoutSource() Input {
|
|
||||||
return r.st.LayoutSource
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRoute(st *RouteState) (*Route, error) {
|
func NewRoute(st *RouteState) (*Route, error) {
|
||||||
empty := compiler.EmptyStruct()
|
empty := compiler.EmptyStruct()
|
||||||
r := &Route{
|
r := &Route{
|
||||||
@ -125,23 +113,47 @@ func NewRoute(st *RouteState) (*Route, error) {
|
|||||||
return r, nil
|
return r, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *Route) ID() string {
|
||||||
|
return r.st.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) Name() string {
|
||||||
|
return r.st.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) LayoutSource() Input {
|
||||||
|
return r.st.LayoutSource
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) Layout() *compiler.Value {
|
||||||
|
return r.layout
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) Input() *compiler.Value {
|
||||||
|
return r.input
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Route) Output() *compiler.Value {
|
||||||
|
return r.output
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Route) State() *compiler.Value {
|
func (r *Route) State() *compiler.Value {
|
||||||
return r.state
|
return r.state
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the base configuration
|
// LoadLayout loads the layout
|
||||||
func (r *Route) Update(ctx context.Context, s Solver) error {
|
func (r *Route) LoadLayout(ctx context.Context, s Solver) error {
|
||||||
span, ctx := opentracing.StartSpanFromContext(ctx, "r.Update")
|
span, ctx := opentracing.StartSpanFromContext(ctx, "r.Update")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
layout, err := r.st.LayoutSource.Compile()
|
layoutSource, err := r.st.LayoutSource.Compile()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := NewPipeline("[internal] source", s, nil)
|
p := NewPipeline("[internal] source", s, nil)
|
||||||
// execute updater script
|
// execute updater script
|
||||||
if err := p.Do(ctx, layout); err != nil {
|
if err := p.Do(ctx, layoutSource); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,24 +162,16 @@ func (r *Route) Update(ctx context.Context, s Solver) error {
|
|||||||
stdlib.Path: stdlib.FS,
|
stdlib.Path: stdlib.FS,
|
||||||
"/": p.FS(),
|
"/": p.FS(),
|
||||||
}
|
}
|
||||||
base, err := compiler.Build(sources)
|
layout, err := compiler.Build(sources)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("base config: %w", err)
|
return fmt.Errorf("layout config: %w", err)
|
||||||
}
|
}
|
||||||
r.layout = base
|
r.layout = layout
|
||||||
|
|
||||||
// Commit
|
// Commit
|
||||||
return r.mergeState()
|
return r.mergeState()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) Base() *compiler.Value {
|
|
||||||
return r.layout
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Route) Output() *compiler.Value {
|
|
||||||
return r.output
|
|
||||||
}
|
|
||||||
|
|
||||||
// Scan all scripts in the environment for references to local directories (do:"local"),
|
// Scan all scripts in the environment for references to local directories (do:"local"),
|
||||||
// and return all referenced directory names.
|
// and return all referenced directory names.
|
||||||
// This is used by clients to grant access to local directories when they are referenced
|
// This is used by clients to grant access to local directories when they are referenced
|
||||||
@ -299,7 +303,7 @@ func (r *Route) Up(ctx context.Context, s Solver, _ *UpOpts) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
span, _ := opentracing.StartSpanFromContext(ctx, "r.Compute: merge state")
|
span, _ := opentracing.StartSpanFromContext(ctx, "merge state")
|
||||||
defer span.Finish()
|
defer span.Finish()
|
||||||
|
|
||||||
return r.mergeState()
|
return r.mergeState()
|
||||||
|
Reference in New Issue
Block a user