terraform/command/hook_ui.go

202 lines
4.1 KiB
Go
Raw Normal View History

2014-06-27 02:05:21 +02:00
package command
import (
"bytes"
2014-06-27 02:05:21 +02:00
"fmt"
"sort"
"strings"
2014-06-27 07:01:05 +02:00
"sync"
2014-06-27 02:05:21 +02:00
"github.com/hashicorp/terraform/terraform"
"github.com/mitchellh/cli"
"github.com/mitchellh/colorstring"
2014-06-27 02:05:21 +02:00
)
type UiHook struct {
terraform.NilHook
Colorize *colorstring.Colorize
Ui cli.Ui
2014-06-27 07:01:05 +02:00
l sync.Mutex
once sync.Once
resources map[string]uiResourceOp
ui cli.Ui
2014-06-27 02:05:21 +02:00
}
type uiResourceOp byte
const (
uiResourceUnknown uiResourceOp = iota
uiResourceCreate
uiResourceModify
uiResourceDestroy
)
func (h *UiHook) PreApply(
2014-09-25 23:52:06 +02:00
n *terraform.InstanceInfo,
2014-09-17 20:15:07 +02:00
s *terraform.InstanceState,
2014-09-18 01:33:24 +02:00
d *terraform.InstanceDiff) (terraform.HookAction, error) {
h.once.Do(h.init)
2014-09-25 23:52:06 +02:00
id := n.HumanId()
op := uiResourceModify
if d.Destroy {
op = uiResourceDestroy
} else if s.ID == "" {
op = uiResourceCreate
}
h.l.Lock()
h.resources[id] = op
h.l.Unlock()
var operation string
switch op {
case uiResourceModify:
operation = "Modifying..."
case uiResourceDestroy:
operation = "Destroying..."
case uiResourceCreate:
operation = "Creating..."
case uiResourceUnknown:
return terraform.HookActionContinue, nil
}
attrBuf := new(bytes.Buffer)
// Get all the attributes that are changing, and sort them. Also
// determine the longest key so that we can align them all.
keyLen := 0
keys := make([]string, 0, len(d.Attributes))
for key, _ := range d.Attributes {
// Skip the ID since we do that specially
if key == "id" {
continue
}
keys = append(keys, key)
if len(key) > keyLen {
keyLen = len(key)
}
}
sort.Strings(keys)
// Go through and output each attribute
for _, attrK := range keys {
attrDiff := d.Attributes[attrK]
v := attrDiff.New
if attrDiff.NewComputed {
v = "<computed>"
}
attrBuf.WriteString(fmt.Sprintf(
" %s:%s %#v => %#v\n",
attrK,
strings.Repeat(" ", keyLen-len(attrK)),
attrDiff.Old,
v))
}
2014-07-13 18:34:35 +02:00
attrString := strings.TrimSpace(attrBuf.String())
if attrString != "" {
attrString = "\n " + attrString
}
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
2014-07-13 18:34:35 +02:00
"[reset][bold]%s: %s[reset_bold]%s",
id,
operation,
2014-07-13 18:34:35 +02:00
attrString)))
return terraform.HookActionContinue, nil
}
func (h *UiHook) PostApply(
2014-09-25 23:52:06 +02:00
n *terraform.InstanceInfo,
2014-09-17 20:15:07 +02:00
s *terraform.InstanceState,
applyerr error) (terraform.HookAction, error) {
2014-09-25 23:52:06 +02:00
id := n.HumanId()
h.l.Lock()
op := h.resources[id]
delete(h.resources, id)
h.l.Unlock()
var msg string
switch op {
case uiResourceModify:
msg = "Modifications complete"
case uiResourceDestroy:
msg = "Destruction complete"
case uiResourceCreate:
msg = "Creation complete"
case uiResourceUnknown:
return terraform.HookActionContinue, nil
}
if applyerr != nil {
msg = fmt.Sprintf("Error: %s", applyerr)
}
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
"[reset][bold]%s: %s[reset_bold]",
id, msg)))
return terraform.HookActionContinue, nil
}
2014-06-27 02:18:46 +02:00
func (h *UiHook) PreDiff(
2014-09-25 23:52:06 +02:00
n *terraform.InstanceInfo,
s *terraform.InstanceState) (terraform.HookAction, error) {
2014-06-27 02:18:46 +02:00
return terraform.HookActionContinue, nil
}
2014-09-25 23:52:06 +02:00
func (h *UiHook) PreProvision(
n *terraform.InstanceInfo,
provId string) (terraform.HookAction, error) {
id := n.HumanId()
2014-07-27 18:11:53 +02:00
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
"[reset][bold]%s: Provisioning with '%s'...[reset_bold]",
id, provId)))
return terraform.HookActionContinue, nil
}
func (h *UiHook) ProvisionOutput(
n *terraform.InstanceInfo,
provId string,
msg string) {
id := n.HumanId()
var buf bytes.Buffer
buf.WriteString(h.Colorize.Color(fmt.Sprintf(
"[reset]%s (%s): ", id, provId)))
buf.WriteString(msg)
h.ui.Output(buf.String())
}
2014-06-27 02:05:21 +02:00
func (h *UiHook) PreRefresh(
2014-09-25 23:52:06 +02:00
n *terraform.InstanceInfo,
s *terraform.InstanceState) (terraform.HookAction, error) {
2014-06-27 07:01:05 +02:00
h.once.Do(h.init)
2014-09-25 23:52:06 +02:00
id := n.HumanId()
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
"[reset][bold]%s: Refreshing state... (ID: %s)",
id, s.ID)))
2014-06-27 02:05:21 +02:00
return terraform.HookActionContinue, nil
}
2014-06-27 07:01:05 +02:00
func (h *UiHook) init() {
if h.Colorize == nil {
panic("colorize not given")
}
h.resources = make(map[string]uiResourceOp)
2014-06-27 07:01:05 +02:00
// Wrap the ui so that it is safe for concurrency regardless of the
// underlying reader/writer that is in place.
h.ui = &cli.ConcurrentUi{Ui: h.Ui}
}