Skip to content

Commit

Permalink
Merge pull request #220 from asteris-llc/feature/value-passing
Browse files Browse the repository at this point in the history
Feature/value passing
  • Loading branch information
rebeccaskinner authored Sep 8, 2016
2 parents 0b516d3 + 87e904c commit 41e45a5
Show file tree
Hide file tree
Showing 48 changed files with 2,230 additions and 310 deletions.
33 changes: 21 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NAME = $(shell awk -F\" '/^const Name/ { print $$2 }' cmd/root.go)
VERSION = $(shell awk -F\" '/^const Version/ { print $$2 }' cmd/version.go)
TOLINT = $(shell find . -type f \( -not -ipath './vendor*' -not -iname 'main.go' -iname '*.go' \) -exec dirname {} \; | sort -u)
RPCLINT=$(shell find ./rpc -type f \( -not -iname 'root.*.go' -iname '*.go' \) )
TOLINT = $(shell find . -type f \( -not -ipath './vendor*' -not -ipath './docs_source*' -not -ipath './rpc*' -not -iname 'main.go' -iname '*.go' \) -exec dirname {} \; | sort -u)
TESTDIRS = $(shell find . -name '*_test.go' -exec dirname \{\} \; | grep -v vendor | uniq)
NONVENDOR = ${shell find . -name '*.go' | grep -v vendor}
BENCHDIRS= $(shell find . -name '*_test.go' | grep -v vendor | xargs grep '*testing.B' | cut -d: -f1 | xargs dirname | uniq)
Expand Down Expand Up @@ -45,27 +46,35 @@ samples/%.png: samples/% converge
lint:
@echo '# golint'
@for dir in ${TOLINT}; do golint $${dir}/...; done # github.com/golang/lint/golint
@for file in ${RPCLINT}; do golint $${file}; done # github.com/golang/lint/golint

@echo '# go tool vet'
@go tool vet -all -shadow ${TOLINT} # built in
@go tool vet -all -shadow ${TOLINT}
@go tool vet -all -shadow ${RPCLINT} # built in

@echo '# gosimple'
@gosimple ${TOLINT} # honnef.co/go/simple/cmd/gosimple
@gosimple ${RPCLINT} # honnef.co/go/simple/cmd/gosimple

@echo '# unconvert'
@unconvert ${TOLINT} # github.com/mdempsky/unconvert
@unconvert ${RPCLINT} # github.com/mdempsky/unconvert

@echo '# structcheck'
@structcheck ${TOLINT} # github.com/opennota/check/cmd/structcheck
@structcheck ${RPCLINT} # github.com/opennota/check/cmd/structcheck

@echo '# varcheck'
@varcheck ${TOLINT} # github.com/opennota/check/cmd/varcheck
@varcheck ${RPCLINT} # github.com/opennota/check/cmd/varcheck

@echo '# aligncheck'
@aligncheck ${TOLINT} # github.com/opennota/check/cmd/aligncheck
@aligncheck ${RPCLINT} # github.com/opennota/check/cmd/aligncheck

@echo '# gas'
@gas ${TOLINT} # github.com/HewlettPackard/gas
@gas ${RPCLINT} # github.com/HewlettPackard/gas

vendor: ${NONVENDOR}
glide install --strip-vcs --strip-vendor --update-vendored
Expand Down Expand Up @@ -99,25 +108,25 @@ package: xcompile

rpc/pb/root.pb.go: rpc/pb/root.proto
protoc -I rpc/pb \
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:rpc/pb \
rpc/pb/root.proto
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--go_out=Mgoogle/api/annotations.proto=github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis/google/api,plugins=grpc:rpc/pb \
rpc/pb/root.proto

rpc/pb/root.pb.gw.go: rpc/pb/root.proto
protoc -I rpc/pb \
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--grpc-gateway_out=logtostderr=true:rpc/pb \
rpc/pb/root.proto
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--grpc-gateway_out=logtostderr=true:rpc/pb \
rpc/pb/root.proto

rpc/pb/root.swagger.json: rpc/pb/root.proto
protoc -I rpc/pb \
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--swagger_out=logtostderr=true:rpc/pb \
rpc/pb/root.proto
-I vendor/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
--swagger_out=logtostderr=true:rpc/pb \
rpc/pb/root.proto

docs: docs_source/**/*
rm -rf docs || true
cd docs_source; make
$(MAKE) -C docs_source
mv docs_source/public docs

.PHONY: test gotest vendor-update vendor-clean xcompile package samples/errors/*.hcl blackbox/*.sh lint bench license-check
119 changes: 47 additions & 72 deletions apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,100 +18,75 @@ import (
"context"
"fmt"

"github.com/asteris-llc/converge/executor"
"github.com/asteris-llc/converge/executor/either"
"github.com/asteris-llc/converge/graph"
"github.com/asteris-llc/converge/helpers/logging"
"github.com/asteris-llc/converge/plan"
"github.com/asteris-llc/converge/resource"
"github.com/asteris-llc/converge/render"
"github.com/pkg/errors"
)

// MkPipelineF is a function to generate a pipeline given an id
type MkPipelineF func(*graph.Graph, string) executor.Pipeline

// ErrTreeContainsErrors is a signal value to indicate errors in the graph
var ErrTreeContainsErrors = errors.New("apply had errors, check graph")

// Apply the actions in a Graph of resource.Tasks
func Apply(ctx context.Context, in *graph.Graph) (*graph.Graph, error) {
renderingPlant, err := render.NewFactory(ctx, in)
if err != nil {
return nil, err
}
pipeline := func(g *graph.Graph, id string) executor.Pipeline {
return Pipeline(g, id, renderingPlant)
}
return execPipeline(ctx, in, pipeline, renderingPlant, nil)
}

// PlanAndApply plans and applies each node
func PlanAndApply(ctx context.Context, in *graph.Graph) (*graph.Graph, error) {
return WithNotify(ctx, in, nil)
}

// WithNotify is Apply, but with notification functions
// WithNotify calls PlanAndApply with a notifier
func WithNotify(ctx context.Context, in *graph.Graph, notify *graph.Notifier) (*graph.Graph, error) {
var hasErrors error
renderingPlant, err := render.NewFactory(ctx, in)
if err != nil {
return nil, err
}
pipeline := func(g *graph.Graph, id string) executor.Pipeline {
return plan.Pipeline(g, id, renderingPlant).Connect(Pipeline(g, id, renderingPlant))
}
return execPipeline(ctx, in, pipeline, renderingPlant, notify)
}

logger := logging.GetLogger(ctx).WithField("function", "Apply")
// Apply the actions in a Graph of resource.Tasks
func execPipeline(ctx context.Context, in *graph.Graph, pipelineF MkPipelineF, renderingPlant *render.Factory, notify *graph.Notifier) (*graph.Graph, error) {
var hasErrors error

out, err := in.Transform(
ctx,
out, err := in.Transform(ctx,
notify.Transform(func(id string, out *graph.Graph) error {
val := out.Get(id)
result, ok := val.(*plan.Result)
if !ok {
return fmt.Errorf("%s: could not get *plan.Result, was %T", id, val)
}

for _, depID := range graph.Targets(out.DownEdges(id)) {
dep, ok := out.Get(depID).(*Result)
if !ok {
return fmt.Errorf("graph walked out of order: %q before dependency %q", id, depID)
}

if err := dep.Error(); err != nil {
out.Add(
id,
&Result{
Ran: false,
Status: &resource.Status{},
Plan: result,
Err: fmt.Errorf("error in dependency %q", depID),
},
)
// early return here after we set the signal error
hasErrors = ErrTreeContainsErrors
return nil
renderingPlant.Graph = out
pipeline := pipelineF(out, id)
result := pipeline.Exec(either.ReturnM(out.Get(id)))
val, isRight := result.FromEither()
if !isRight {
hasErrors = ErrTreeContainsErrors
if e, ok := val.(error); ok {
return e
}
}

var newResult *Result

if result.Status.HasChanges() {
logger.WithField("id", id).Debug("applying")

err := result.Task.Apply()
if err != nil {
err = errors.Wrapf(err, "error applying %s", id)
}

var status resource.TaskStatus

if err == nil {
status, err = result.Task.Check()
if err != nil {
err = errors.Wrapf(err, "error checking %s", id)
} else if status.HasChanges() {
err = fmt.Errorf("%s still needs to be changed after application", id)
}
}

if err != nil {
hasErrors = ErrTreeContainsErrors
}

newResult = &Result{
Ran: true,
Status: status,
Plan: result,
Err: err,
}
} else {
newResult = &Result{
Ran: false,
Status: result.Status,
Plan: result,
Err: nil,
}
asResult, ok := val.(*Result)
if !ok {
return fmt.Errorf("expected asResult but got %T", val)
}

out.Add(id, newResult)
if nil != asResult.Error() {
hasErrors = ErrTreeContainsErrors
}

out.Add(id, asResult)
return nil
}),
)
Expand Down
169 changes: 169 additions & 0 deletions apply/pipeline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright © 2016 Asteris, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package apply

import (
"errors"
"fmt"

"github.com/asteris-llc/converge/executor"
"github.com/asteris-llc/converge/executor/either"
"github.com/asteris-llc/converge/executor/monad"
"github.com/asteris-llc/converge/graph"
"github.com/asteris-llc/converge/plan"
"github.com/asteris-llc/converge/render"
"github.com/asteris-llc/converge/resource"
)

type pipelineGen struct {
Graph *graph.Graph
ID string
RenderingPlant *render.Factory
}

type resultWrapper struct {
Plan *plan.Result
}

// Pipeline generates a pipeline to evaluate a single graph node
func Pipeline(g *graph.Graph, id string, factory *render.Factory) executor.Pipeline {
gen := &pipelineGen{Graph: g, RenderingPlant: factory, ID: id}
return executor.NewPipeline().
AndThen(gen.GetTask).
AndThen(gen.DependencyCheck).
AndThen(gen.maybeSkipApplication).
AndThen(gen.applyNode).
AndThen(gen.maybeRunFinalCheck)
}

// GetResult returns Right resultWrapper if the value is a *plan.Result, or Left
// Error if not
func (g *pipelineGen) GetTask(idi interface{}) monad.Monad {
if plan, ok := idi.(*plan.Result); ok {
return either.RightM(resultWrapper{Plan: plan})
}
return either.LeftM(fmt.Errorf("expected plan.Result but got %T", idi))
}

// DependencyCheck looks for failing dependency nodes. If an error is
// encountered it returns `Left error`, if failing dependencies are encountered
// it returns `Right (Left apply.Result)` and otherwise returns `Right (Right
// plan.Result)`. The return values are structured to short-circuit `PlanNode`
// if we have failures.
func (g *pipelineGen) DependencyCheck(taskI interface{}) monad.Monad {
result, ok := taskI.(resultWrapper)
if !ok {
return either.LeftM(errors.New("input node is not a task wrapper"))
}
for _, depID := range graph.Targets(g.Graph.DownEdges(g.ID)) {
elem := g.Graph.Get(depID)
dep, ok := elem.(executor.Status)
if !ok {
return either.LeftM(fmt.Errorf("apply.DependencyCheck: expected %s to have type executor.Status but got type %T", depID, elem))
}
if err := dep.Error(); err != nil {
errResult := &Result{
Ran: false,
Status: &resource.Status{WillChange: true},
Err: fmt.Errorf("error in dependency %q", depID),
}
return either.RightM(either.LeftM(errResult))
}
}
return either.RightM(either.RightM(result))
}

// maybeSkipApplication :: Either *apply.Result *plan.Result -> Either *apply.Result resultWrapper
func (g *pipelineGen) maybeSkipApplication(resultI interface{}) monad.Monad {
// checkResult :: Either apply.Result Plan.Result
checkResult := func(plannerI interface{}) interface{} {
plan := plannerI.(resultWrapper)
if !plan.Plan.Status.HasChanges() {
return either.LeftM(&Result{
Ran: false,
Task: plan.Plan.Task,
Plan: plan.Plan,
Err: nil,
})
}
return either.RightM(plan)
}
return monad.FMap(checkResult, resultI.(either.EitherM))
}

// applyNode runs apply on the node, it takes an Either *apply.Result
// *plan.Result and, if the input value is Left, returns it as a Right value,
// otherwise it attempts to run apply on the *plan.Result.Task and returns an
// appropriate Left or Right value.
func (g *pipelineGen) applyNode(taski interface{}) monad.Monad {
taskE, ok := taski.(either.EitherM)
if !ok {
return either.LeftM(fmt.Errorf("expected either.EitherM but got %T", taski))
}
val, isRight := taskE.FromEither()
if !isRight {
return either.RightM(val)
}
twrapper, ok := val.(resultWrapper)
if !ok {
return either.LeftM(fmt.Errorf("apply expected a resultWrappert but got %T", val))
}
renderer, err := g.Renderer(g.ID)
if err != nil {
return either.LeftM(fmt.Errorf("unable to get renderer for %s", g.ID))
}
applyStatus, err := twrapper.Plan.Task.Apply(renderer)
if err != nil {
err = fmt.Errorf("error applying %s: %s", g.ID, err)
}
return either.RightM(&Result{
Ran: true,
Status: applyStatus,
Task: twrapper.Plan.Task,
Plan: twrapper.Plan,
Err: err,
})
}

// maybeRunFinalCheck :: *Result -> Either error *Result; looks to see if the
// current result ran, and if so it re-runs plan and sets PostCheck to the
// resulting status.
func (g *pipelineGen) maybeRunFinalCheck(resultI interface{}) monad.Monad {
result, ok := resultI.(*Result)
if !ok {
return either.LeftM(fmt.Errorf("expected *Result but got %T", resultI))
}
if !result.Ran {
return either.RightM(result)
}
task := result.Plan.Task
return plan.Pipeline(g.Graph, g.ID, g.RenderingPlant).
Exec(either.ReturnM(task)).
AndThen(func(planI interface{}) monad.Monad {
plan, ok := planI.(*plan.Result)
if !ok {
return either.LeftM(fmt.Errorf("expected *plan.Result but got %T", planI))
}
result.PostCheck = plan.Status
if plan.HasChanges() {
result.Err = fmt.Errorf("%s still has changes after apply", g.ID)
}
return either.RightM(result)
})
}

func (g *pipelineGen) Renderer(id string) (*render.Renderer, error) {
return g.RenderingPlant.GetRenderer(id)
}
Loading

0 comments on commit 41e45a5

Please sign in to comment.