Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: add streaming output #36

Merged
merged 6 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 26 additions & 2 deletions cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"os"

"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
Expand All @@ -42,6 +44,11 @@ var applyCmd = &cobra.Command{
logrus.WithError(err).Fatal("could not load params")
}

// set up execution context
ctx, cancel := context.WithCancel(context.Background())
GracefulExit(cancel)

// iterate over modules
for _, fname := range args {
logger := logrus.WithField("filename", fname)

Expand All @@ -50,18 +57,35 @@ var applyCmd = &cobra.Command{
logger.WithError(err).Fatal("could not parse file")
}

plan, err := exec.Plan(graph)
planStatus := make(chan *exec.StatusMessage, 1)
go func() {
for msg := range planStatus {
logger.WithField("path", msg.Path).Info(msg.Status)
}
close(planStatus)
}()

plan, err := exec.PlanWithStatus(ctx, graph, planStatus)
if err != nil {
logger.WithError(err).Fatal("planning failed")
}

results, err := exec.Apply(graph, plan)
status := make(chan *exec.StatusMessage, 1)
go func() {
for msg := range status {
logger.WithField("path", msg.Path).Info(msg.Status)
}
close(status)
}()

results, err := exec.ApplyWithStatus(ctx, graph, plan, status)
if err != nil {
logger.WithError(err).Fatal("applying failed")
}

var failed bool

fmt.Print("\n")
for _, result := range results {
if !result.Success {
failed = true
Expand Down
46 changes: 46 additions & 0 deletions cmd/graceful_exit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 cmd

import (
"os"
"os/signal"

"github.com/Sirupsen/logrus"

"golang.org/x/net/context"
)

// GracefulExit traps interrupt signals for a graceful exit
func GracefulExit(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
interruptCount := 0

for range c {
interruptCount++

switch interruptCount {
case 1:
logrus.Info("gracefully shutting down (interrupt again to halt)")
cancel()
case 2:
logrus.Warn("hard stop! System may be in an incomplete state")
os.Exit(2)
}
}
}()
}
17 changes: 16 additions & 1 deletion cmd/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"errors"
"fmt"

"golang.org/x/net/context"

"github.com/Sirupsen/logrus"
"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
Expand All @@ -41,6 +43,10 @@ var planCmd = &cobra.Command{
logrus.WithError(err).Fatal("could not load params")
}

// set up execution context
ctx, cancel := context.WithCancel(context.Background())
GracefulExit(cancel)

for _, fname := range args {
logger := logrus.WithField("filename", fname)

Expand All @@ -49,11 +55,20 @@ var planCmd = &cobra.Command{
logger.WithError(err).Fatal("could not parse file")
}

results, err := exec.Plan(graph)
status := make(chan *exec.StatusMessage, 1)
go func() {
for msg := range status {
logger.WithField("path", msg.Path).Info(msg.Status)
}
close(status)
}()

results, err := exec.PlanWithStatus(ctx, graph, status)
if err != nil {
logger.WithError(err).Fatal("planning failed")
}

fmt.Print("\n")
for _, result := range results {
fmt.Println(result)
}
Expand Down
31 changes: 29 additions & 2 deletions exec/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"sync"

"golang.org/x/net/context"

"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
)
Expand All @@ -40,8 +42,8 @@ func (p *ApplyResult) String() string {
)
}

// Apply the operations to be performed
func Apply(graph *load.Graph, plan []*PlanResult) (results []*ApplyResult, err error) {
// ApplyWithStatus applies the operations checked in plan
func ApplyWithStatus(ctx context.Context, graph *load.Graph, plan []*PlanResult, status chan<- *StatusMessage) (results []*ApplyResult, err error) {
// transform checks into something we can look up easily
planResults := map[string]*PlanResult{}
for _, result := range plan {
Expand All @@ -52,6 +54,12 @@ func Apply(graph *load.Graph, plan []*PlanResult) (results []*ApplyResult, err e
lock := new(sync.Mutex)

err = graph.Walk(func(path string, res resource.Resource) error {
select {
case <-ctx.Done():
return nil
default:
}

planResult, ok := planResults[path]
if !ok || !planResult.WillChange {
return nil
Expand All @@ -70,6 +78,12 @@ func Apply(graph *load.Graph, plan []*PlanResult) (results []*ApplyResult, err e
}
)

select {
case <-ctx.Done():
return nil
case status <- &StatusMessage{Path: path, Status: "applying"}:
}

result.NewStatus, result.Success, err = task.Apply()
if err != nil {
return err
Expand All @@ -84,3 +98,16 @@ func Apply(graph *load.Graph, plan []*PlanResult) (results []*ApplyResult, err e

return results, err
}

// Apply does the same thing as ApplyWithStatus, but drops all status messages
func Apply(ctx context.Context, graph *load.Graph, plan []*PlanResult) (results []*ApplyResult, err error) {
status := make(chan *StatusMessage, 1)
defer close(status)
go func() {
for range status {
continue
}
}()

return ApplyWithStatus(ctx, graph, plan, status)
}
6 changes: 4 additions & 2 deletions exec/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package exec_test
import (
"testing"

"golang.org/x/net/context"

"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/helpers"
"github.com/asteris-llc/converge/load"
Expand All @@ -31,11 +33,11 @@ func TestApply(t *testing.T) {
graph, err := load.Load("../samples/basic.hcl", resource.Values{})
require.NoError(t, err)

plan, err := exec.Plan(graph)
plan, err := exec.Plan(context.Background(), graph)
assert.NoError(t, err)

helpers.InTempDir(t, func() {
results, err := exec.Apply(graph, plan)
results, err := exec.Apply(context.Background(), graph, plan)
assert.NoError(t, err)
assert.Equal(
t,
Expand Down
31 changes: 29 additions & 2 deletions exec/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"fmt"
"sync"

"golang.org/x/net/context"

"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
)
Expand All @@ -38,11 +40,17 @@ func (p *PlanResult) String() string {
)
}

// Plan the operations to be performed
func Plan(graph *load.Graph) (results []*PlanResult, err error) {
// PlanWithStatus plans the operations to be performed and outputs status
func PlanWithStatus(ctx context.Context, graph *load.Graph, status chan<- *StatusMessage) (results []*PlanResult, err error) {
lock := new(sync.Mutex)

err = graph.Walk(func(path string, res resource.Resource) error {
select {
case <-ctx.Done():
return nil
default:
}

monitor, ok := res.(resource.Monitor)
if !ok {
return nil
Expand All @@ -53,6 +61,12 @@ func Plan(graph *load.Graph) (results []*PlanResult, err error) {
result = &PlanResult{Path: path}
)

select {
case <-ctx.Done():
return nil
case status <- &StatusMessage{Path: path, Status: "checking status"}:
}

result.CurrentStatus, result.WillChange, err = monitor.Check()
if err != nil {
return err
Expand All @@ -67,3 +81,16 @@ func Plan(graph *load.Graph) (results []*PlanResult, err error) {

return results, err
}

// Plan does the same thing as PlanWithStatus, but drops all status messages
func Plan(ctx context.Context, graph *load.Graph) (results []*PlanResult, err error) {
status := make(chan *StatusMessage, 1)
defer close(status)
go func() {
for range status {
continue
}
}()

return PlanWithStatus(ctx, graph, status)
}
4 changes: 3 additions & 1 deletion exec/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package exec_test
import (
"testing"

"golang.org/x/net/context"

"github.com/asteris-llc/converge/exec"
"github.com/asteris-llc/converge/load"
"github.com/asteris-llc/converge/resource"
Expand All @@ -30,7 +32,7 @@ func TestPlan(t *testing.T) {
graph, err := load.Load("../samples/basic.hcl", resource.Values{})
require.NoError(t, err)

results, err := exec.Plan(graph)
results, err := exec.Plan(context.Background(), graph)
assert.NoError(t, err)
assert.Equal(
t,
Expand Down
27 changes: 27 additions & 0 deletions exec/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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 exec

import "fmt"

// StatusMessage is returned as status for Plan and Apply
type StatusMessage struct {
Path string
Status string
}

func (s *StatusMessage) String() string {
return fmt.Sprintf("%s: %s", s.Path, s.Status)
}
27 changes: 27 additions & 0 deletions vendor/golang.org/x/net/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading