2014-06-27 02:05:21 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2014-10-06 08:29:13 +02:00
|
|
|
"bufio"
|
2014-07-13 02:03:55 +02:00
|
|
|
"bytes"
|
2014-06-27 02:05:21 +02:00
|
|
|
"fmt"
|
2014-07-13 02:03:55 +02:00
|
|
|
"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"
|
2014-07-13 02:03:55 +02:00
|
|
|
"github.com/mitchellh/colorstring"
|
2014-06-27 02:05:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UiHook struct {
|
|
|
|
terraform.NilHook
|
|
|
|
|
2014-07-13 02:03:55 +02:00
|
|
|
Colorize *colorstring.Colorize
|
|
|
|
Ui cli.Ui
|
2014-06-27 07:01:05 +02:00
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
l sync.Mutex
|
|
|
|
once sync.Once
|
|
|
|
resources map[string]uiResourceOp
|
|
|
|
ui cli.Ui
|
2014-06-27 02:05:21 +02:00
|
|
|
}
|
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
type uiResourceOp byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
uiResourceUnknown uiResourceOp = iota
|
|
|
|
uiResourceCreate
|
|
|
|
uiResourceModify
|
|
|
|
uiResourceDestroy
|
|
|
|
)
|
|
|
|
|
2014-06-27 07:11:04 +02:00
|
|
|
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) {
|
2014-06-27 07:11:04 +02:00
|
|
|
h.once.Do(h.init)
|
|
|
|
|
2014-09-25 23:52:06 +02:00
|
|
|
id := n.HumanId()
|
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
op := uiResourceModify
|
2014-07-13 02:03:55 +02:00
|
|
|
if d.Destroy {
|
2014-07-27 06:20:31 +02:00
|
|
|
op = uiResourceDestroy
|
2014-07-13 02:03:55 +02:00
|
|
|
} else if s.ID == "" {
|
2014-07-27 06:20:31 +02:00
|
|
|
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:
|
2014-07-13 02:03:55 +02:00
|
|
|
operation = "Creating..."
|
2014-07-27 06:20:31 +02:00
|
|
|
case uiResourceUnknown:
|
|
|
|
return terraform.HookActionContinue, nil
|
2014-07-13 02:03:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:03:55 +02:00
|
|
|
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
|
2014-07-13 18:34:35 +02:00
|
|
|
"[reset][bold]%s: %s[reset_bold]%s",
|
2014-07-13 02:03:55 +02:00
|
|
|
id,
|
|
|
|
operation,
|
2014-07-13 18:34:35 +02:00
|
|
|
attrString)))
|
2014-07-13 02:03:55 +02:00
|
|
|
|
2014-06-27 07:11:04 +02:00
|
|
|
return terraform.HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
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,
|
2014-07-27 06:20:31 +02:00
|
|
|
applyerr error) (terraform.HookAction, error) {
|
2014-09-25 23:52:06 +02:00
|
|
|
id := n.HumanId()
|
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-10-05 01:33:47 +02:00
|
|
|
func (h *UiHook) ProvisionOutput(
|
|
|
|
n *terraform.InstanceInfo,
|
|
|
|
provId string,
|
|
|
|
msg string) {
|
|
|
|
id := n.HumanId()
|
|
|
|
var buf bytes.Buffer
|
2014-10-06 08:29:13 +02:00
|
|
|
buf.WriteString(h.Colorize.Color("[reset]"))
|
|
|
|
|
|
|
|
prefix := fmt.Sprintf("%s (%s): ", id, provId)
|
|
|
|
s := bufio.NewScanner(strings.NewReader(msg))
|
|
|
|
for s.Scan() {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s%s\n", prefix, s.Text()))
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ui.Output(strings.TrimSpace(buf.String()))
|
2014-10-05 01:33:47 +02:00
|
|
|
}
|
|
|
|
|
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()
|
2014-07-13 02:17:03 +02:00
|
|
|
h.ui.Output(h.Colorize.Color(fmt.Sprintf(
|
2014-07-27 06:20:31 +02:00
|
|
|
"[reset][bold]%s: Refreshing state... (ID: %s)",
|
2014-07-13 02:17:03 +02:00
|
|
|
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() {
|
2014-07-13 02:03:55 +02:00
|
|
|
if h.Colorize == nil {
|
2014-07-13 05:37:30 +02:00
|
|
|
panic("colorize not given")
|
2014-07-13 02:03:55 +02:00
|
|
|
}
|
|
|
|
|
2014-07-27 06:20:31 +02:00
|
|
|
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}
|
|
|
|
}
|