2017-10-03 14:46:19 +02:00
|
|
|
package habitat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-05 18:44:35 +02:00
|
|
|
"crypto/sha256"
|
2017-10-03 14:46:19 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-10-06 18:20:52 +02:00
|
|
|
"net/url"
|
2017-10-03 14:46:19 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-09-09 20:03:39 +02:00
|
|
|
version "github.com/hashicorp/go-version"
|
2017-10-03 14:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/communicator"
|
|
|
|
"github.com/hashicorp/terraform/communicator/remote"
|
2019-09-05 18:44:35 +02:00
|
|
|
"github.com/hashicorp/terraform/configs/hcl2shim"
|
2017-10-03 14:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2019-09-05 18:44:35 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/validation"
|
2017-10-03 14:46:19 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2019-09-05 18:44:35 +02:00
|
|
|
"github.com/mitchellh/go-linereader"
|
2017-10-03 14:46:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type provisioner struct {
|
2017-12-12 00:15:10 +01:00
|
|
|
Version string
|
2019-09-05 18:44:35 +02:00
|
|
|
AutoUpdate bool
|
|
|
|
HttpDisable bool
|
2017-12-12 00:15:10 +01:00
|
|
|
Services []Service
|
|
|
|
PermanentPeer bool
|
2019-09-05 18:44:35 +02:00
|
|
|
ListenCtl string
|
2017-12-12 00:15:10 +01:00
|
|
|
ListenGossip string
|
|
|
|
ListenHTTP string
|
|
|
|
Peer string
|
2019-09-05 18:44:35 +02:00
|
|
|
Peers []string
|
2017-12-12 00:15:10 +01:00
|
|
|
RingKey string
|
|
|
|
RingKeyContent string
|
2019-09-05 18:44:35 +02:00
|
|
|
CtlSecret string
|
2017-12-12 00:15:10 +01:00
|
|
|
SkipInstall bool
|
|
|
|
UseSudo bool
|
|
|
|
ServiceType string
|
2018-02-13 21:11:59 +01:00
|
|
|
ServiceName string
|
2017-12-12 00:15:10 +01:00
|
|
|
URL string
|
|
|
|
Channel string
|
|
|
|
Events string
|
|
|
|
Organization string
|
2019-09-05 18:44:35 +02:00
|
|
|
GatewayAuthToken string
|
2017-12-12 00:15:10 +01:00
|
|
|
BuilderAuthToken string
|
|
|
|
SupOptions string
|
2019-09-09 20:03:39 +02:00
|
|
|
AcceptLicense bool
|
2019-09-05 18:44:35 +02:00
|
|
|
|
2019-09-29 20:16:25 +02:00
|
|
|
installHabitat provisionFn
|
|
|
|
startHabitat provisionFn
|
|
|
|
uploadRingKey provisionFn
|
|
|
|
uploadCtlSecret provisionFn
|
|
|
|
startHabitatService provisionServiceFn
|
2019-09-05 18:44:35 +02:00
|
|
|
|
2019-09-29 20:16:25 +02:00
|
|
|
osType string
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
type provisionFn func(terraform.UIOutput, communicator.Communicator) error
|
|
|
|
type provisionServiceFn func(terraform.UIOutput, communicator.Communicator, Service) error
|
|
|
|
|
2017-10-03 14:46:19 +02:00
|
|
|
func Provisioner() terraform.ResourceProvisioner {
|
|
|
|
return &schema.Provisioner{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"version": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2019-09-05 18:44:35 +02:00
|
|
|
"auto_update": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
"http_disable": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
"peer": &schema.Schema{
|
2017-10-03 14:46:19 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2019-09-05 18:44:35 +02:00
|
|
|
},
|
|
|
|
"peers": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"service_type": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "systemd",
|
|
|
|
ValidateFunc: validation.StringInSlice([]string{"systemd", "unmanaged"}, false),
|
2017-10-03 14:46:19 +02:00
|
|
|
},
|
2018-02-13 21:11:59 +01:00
|
|
|
"service_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "hab-supervisor",
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
"use_sudo": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2019-09-09 20:03:39 +02:00
|
|
|
"accept_license": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
"permanent_peer": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2019-09-05 18:44:35 +02:00
|
|
|
"listen_ctl": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
"listen_gossip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"listen_http": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"ring_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-10-06 18:20:52 +02:00
|
|
|
"ring_key_content": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2019-09-05 18:44:35 +02:00
|
|
|
"ctl_secret": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-10-06 18:20:52 +02:00
|
|
|
"url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2019-09-05 18:44:35 +02:00
|
|
|
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
|
|
|
|
u, err := url.Parse(val.(string))
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("invalid URL specified for %q: %v", key, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("invalid URL specified for %q (scheme must be specified)", key))
|
|
|
|
}
|
|
|
|
|
|
|
|
return warns, errs
|
|
|
|
},
|
2017-10-06 18:20:52 +02:00
|
|
|
},
|
|
|
|
"channel": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"events": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2019-09-05 18:44:35 +02:00
|
|
|
"organization": &schema.Schema{
|
2017-10-06 18:20:52 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2019-09-05 18:44:35 +02:00
|
|
|
"gateway_auth_token": &schema.Schema{
|
2017-10-06 18:20:52 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-12-12 00:15:10 +01:00
|
|
|
"builder_auth_token": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
"service": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"binds": &schema.Schema{
|
|
|
|
Type: schema.TypeList,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"bind": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"alias": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"service": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"topology": &schema.Schema{
|
2019-09-05 18:44:35 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validation.StringInSlice([]string{"leader", "standalone"}, false),
|
2017-10-03 14:46:19 +02:00
|
|
|
},
|
|
|
|
"user_toml": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"strategy": &schema.Schema{
|
2019-09-05 18:44:35 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ValidateFunc: validation.StringInSlice([]string{"none", "rolling", "at-once"}, false),
|
2017-10-03 14:46:19 +02:00
|
|
|
},
|
|
|
|
"channel": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"url": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2019-09-05 18:44:35 +02:00
|
|
|
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
|
|
|
|
u, err := url.Parse(val.(string))
|
|
|
|
if err != nil {
|
|
|
|
errs = append(errs, fmt.Errorf("invalid URL specified for %q: %v", key, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.Scheme == "" {
|
|
|
|
errs = append(errs, fmt.Errorf("invalid URL specified for %q (scheme must be specified)", key))
|
|
|
|
}
|
|
|
|
|
|
|
|
return warns, errs
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
},
|
2017-10-06 18:20:52 +02:00
|
|
|
"application": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"environment": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
"service_key": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2017-10-03 14:46:19 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ApplyFunc: applyFn,
|
|
|
|
ValidateFunc: validateFn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyFn(ctx context.Context) error {
|
|
|
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
|
|
|
s := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
|
|
|
|
d := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
p, err := decodeConfig(d)
|
2017-10-03 14:46:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
// Automatically determine the OS type
|
|
|
|
switch t := s.Ephemeral.ConnInfo["type"]; t {
|
|
|
|
case "ssh", "":
|
|
|
|
p.osType = "linux"
|
|
|
|
case "winrm":
|
|
|
|
p.osType = "windows"
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported connection type: %s", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch p.osType {
|
|
|
|
case "linux":
|
|
|
|
p.installHabitat = p.linuxInstallHabitat
|
|
|
|
p.uploadRingKey = p.linuxUploadRingKey
|
|
|
|
p.uploadCtlSecret = p.linuxUploadCtlSecret
|
|
|
|
p.startHabitat = p.linuxStartHabitat
|
|
|
|
p.startHabitatService = p.linuxStartHabitatService
|
|
|
|
case "windows":
|
|
|
|
return fmt.Errorf("windows is not supported yet for the habitat provisioner")
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported os type: %s", p.osType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a new communicator
|
2017-10-03 14:46:19 +02:00
|
|
|
comm, err := communicator.New(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:06:28 +01:00
|
|
|
retryCtx, cancel := context.WithTimeout(ctx, comm.Timeout())
|
2018-02-15 00:32:29 +01:00
|
|
|
defer cancel()
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
// Wait and retry until we establish the connection
|
2018-03-20 18:06:28 +01:00
|
|
|
err = communicator.Retry(retryCtx, func() error {
|
2018-02-15 00:32:29 +01:00
|
|
|
return comm.Connect(o)
|
2017-10-03 14:46:19 +02:00
|
|
|
})
|
2018-02-15 00:32:29 +01:00
|
|
|
|
2017-10-03 14:46:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer comm.Disconnect()
|
|
|
|
|
|
|
|
if !p.SkipInstall {
|
2017-10-06 18:20:52 +02:00
|
|
|
o.Output("Installing habitat...")
|
2019-09-05 18:44:35 +02:00
|
|
|
if err := p.installHabitat(o, comm); err != nil {
|
2017-10-03 14:46:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
if p.RingKeyContent != "" {
|
|
|
|
o.Output("Uploading supervisor ring key...")
|
|
|
|
if err := p.uploadRingKey(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
if p.CtlSecret != "" {
|
|
|
|
o.Output("Uploading ctl secret...")
|
|
|
|
if err := p.uploadCtlSecret(o, comm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
o.Output("Starting the habitat supervisor...")
|
2019-09-05 18:44:35 +02:00
|
|
|
if err := p.startHabitat(o, comm); err != nil {
|
2017-10-03 14:46:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.Services != nil {
|
|
|
|
for _, service := range p.Services {
|
2017-10-06 18:20:52 +02:00
|
|
|
o.Output("Starting service: " + service.Name)
|
2019-09-05 18:44:35 +02:00
|
|
|
if err := p.startHabitatService(o, comm, service); err != nil {
|
2017-10-03 14:46:19 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateFn(c *terraform.ResourceConfig) (ws []string, es []error) {
|
2019-09-05 18:44:35 +02:00
|
|
|
ringKeyContent, ok := c.Get("ring_key_content")
|
|
|
|
if ok && ringKeyContent != "" && ringKeyContent != hcl2shim.UnknownVariableValue {
|
|
|
|
ringKey, ringOk := c.Get("ring_key")
|
|
|
|
if ringOk && ringKey == "" {
|
|
|
|
es = append(es, errors.New("if ring_key_content is specified, ring_key must be specified as well"))
|
2017-10-06 18:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 20:03:39 +02:00
|
|
|
v, ok := c.Get("version")
|
|
|
|
if ok && v != nil && strings.TrimSpace(v.(string)) != "" {
|
|
|
|
if _, err := version.NewVersion(v.(string)); err != nil {
|
|
|
|
es = append(es, errors.New(v.(string)+" is not a valid version."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acceptLicense, ok := c.Get("accept_license")
|
|
|
|
if ok && !acceptLicense.(bool) {
|
|
|
|
if v != nil && strings.TrimSpace(v.(string)) != "" {
|
|
|
|
versionOld, _ := version.NewVersion("0.79.0")
|
|
|
|
versionRequired, _ := version.NewVersion(v.(string))
|
|
|
|
if versionRequired.GreaterThan(versionOld) {
|
|
|
|
es = append(es, errors.New("Habitat end user license agreement needs to be accepted, set the accept_license argument to true to accept"))
|
|
|
|
}
|
|
|
|
} else { // blank means latest version
|
|
|
|
es = append(es, errors.New("Habitat end user license agreement needs to be accepted, set the accept_license argument to true to accept"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
// Validate service level configs
|
|
|
|
services, ok := c.Get("service")
|
|
|
|
if ok {
|
2019-09-05 18:44:35 +02:00
|
|
|
data, dataOk := services.(string)
|
|
|
|
if dataOk {
|
|
|
|
es = append(es, fmt.Errorf("service '%v': must be a block", data))
|
2017-10-06 18:20:52 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 18:44:35 +02:00
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
return ws, es
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Service struct {
|
2017-10-06 18:20:52 +02:00
|
|
|
Name string
|
|
|
|
Strategy string
|
|
|
|
Topology string
|
|
|
|
Channel string
|
|
|
|
Group string
|
|
|
|
URL string
|
|
|
|
Binds []Bind
|
|
|
|
BindStrings []string
|
|
|
|
UserTOML string
|
|
|
|
AppName string
|
|
|
|
Environment string
|
|
|
|
ServiceGroupKey string
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
func (s *Service) getPackageName(fullName string) string {
|
|
|
|
return strings.Split(fullName, "/")[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) getServiceNameChecksum() string {
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(s.Name)))
|
|
|
|
}
|
|
|
|
|
2017-10-03 14:46:19 +02:00
|
|
|
type Bind struct {
|
2017-10-06 18:20:52 +02:00
|
|
|
Alias string
|
|
|
|
Service string
|
|
|
|
Group string
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bind) toBindString() string {
|
|
|
|
return fmt.Sprintf("%s:%s.%s", b.Alias, b.Service, b.Group)
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
func decodeConfig(d *schema.ResourceData) (*provisioner, error) {
|
2017-10-03 14:46:19 +02:00
|
|
|
p := &provisioner{
|
2017-12-12 00:15:10 +01:00
|
|
|
Version: d.Get("version").(string),
|
2019-09-05 18:44:35 +02:00
|
|
|
AutoUpdate: d.Get("auto_update").(bool),
|
|
|
|
HttpDisable: d.Get("http_disable").(bool),
|
2017-12-12 00:15:10 +01:00
|
|
|
Peer: d.Get("peer").(string),
|
2019-09-05 18:44:35 +02:00
|
|
|
Peers: getPeers(d.Get("peers").([]interface{})),
|
2017-12-12 00:15:10 +01:00
|
|
|
Services: getServices(d.Get("service").(*schema.Set).List()),
|
|
|
|
UseSudo: d.Get("use_sudo").(bool),
|
2019-09-09 20:03:39 +02:00
|
|
|
AcceptLicense: d.Get("accept_license").(bool),
|
2017-12-12 00:15:10 +01:00
|
|
|
ServiceType: d.Get("service_type").(string),
|
2018-02-13 21:11:59 +01:00
|
|
|
ServiceName: d.Get("service_name").(string),
|
2017-12-12 00:15:10 +01:00
|
|
|
RingKey: d.Get("ring_key").(string),
|
|
|
|
RingKeyContent: d.Get("ring_key_content").(string),
|
2019-09-05 18:44:35 +02:00
|
|
|
CtlSecret: d.Get("ctl_secret").(string),
|
2017-12-12 00:15:10 +01:00
|
|
|
PermanentPeer: d.Get("permanent_peer").(bool),
|
2019-09-05 18:44:35 +02:00
|
|
|
ListenCtl: d.Get("listen_ctl").(string),
|
2017-12-12 00:15:10 +01:00
|
|
|
ListenGossip: d.Get("listen_gossip").(string),
|
|
|
|
ListenHTTP: d.Get("listen_http").(string),
|
|
|
|
URL: d.Get("url").(string),
|
|
|
|
Channel: d.Get("channel").(string),
|
|
|
|
Events: d.Get("events").(string),
|
|
|
|
Organization: d.Get("organization").(string),
|
|
|
|
BuilderAuthToken: d.Get("builder_auth_token").(string),
|
2019-09-05 18:44:35 +02:00
|
|
|
GatewayAuthToken: d.Get("gateway_auth_token").(string),
|
2017-10-06 18:20:52 +02:00
|
|
|
}
|
2017-10-03 14:46:19 +02:00
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:44:35 +02:00
|
|
|
func getPeers(v []interface{}) []string {
|
|
|
|
peers := make([]string, 0, len(v))
|
|
|
|
for _, rawPeerData := range v {
|
|
|
|
peers = append(peers, rawPeerData.(string))
|
|
|
|
}
|
|
|
|
return peers
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
func getServices(v []interface{}) []Service {
|
2017-10-03 14:46:19 +02:00
|
|
|
services := make([]Service, 0, len(v))
|
|
|
|
for _, rawServiceData := range v {
|
|
|
|
serviceData := rawServiceData.(map[string]interface{})
|
|
|
|
name := (serviceData["name"].(string))
|
|
|
|
strategy := (serviceData["strategy"].(string))
|
|
|
|
topology := (serviceData["topology"].(string))
|
|
|
|
channel := (serviceData["channel"].(string))
|
|
|
|
group := (serviceData["group"].(string))
|
|
|
|
url := (serviceData["url"].(string))
|
2017-10-06 18:20:52 +02:00
|
|
|
app := (serviceData["application"].(string))
|
|
|
|
env := (serviceData["environment"].(string))
|
2017-10-03 14:46:19 +02:00
|
|
|
userToml := (serviceData["user_toml"].(string))
|
2017-10-06 18:20:52 +02:00
|
|
|
serviceGroupKey := (serviceData["service_key"].(string))
|
2017-10-03 14:46:19 +02:00
|
|
|
var bindStrings []string
|
2017-10-06 18:20:52 +02:00
|
|
|
binds := getBinds(serviceData["bind"].(*schema.Set).List())
|
2017-10-03 14:46:19 +02:00
|
|
|
for _, b := range serviceData["binds"].([]interface{}) {
|
|
|
|
bind, err := getBindFromString(b.(string))
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
binds = append(binds, bind)
|
|
|
|
}
|
|
|
|
|
|
|
|
service := Service{
|
2017-10-06 18:20:52 +02:00
|
|
|
Name: name,
|
|
|
|
Strategy: strategy,
|
|
|
|
Topology: topology,
|
|
|
|
Channel: channel,
|
|
|
|
Group: group,
|
|
|
|
URL: url,
|
|
|
|
UserTOML: userToml,
|
|
|
|
BindStrings: bindStrings,
|
|
|
|
Binds: binds,
|
|
|
|
AppName: app,
|
|
|
|
Environment: env,
|
|
|
|
ServiceGroupKey: serviceGroupKey,
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:20:52 +02:00
|
|
|
func getBinds(v []interface{}) []Bind {
|
2017-10-03 14:46:19 +02:00
|
|
|
binds := make([]Bind, 0, len(v))
|
|
|
|
for _, rawBindData := range v {
|
|
|
|
bindData := rawBindData.(map[string]interface{})
|
|
|
|
alias := bindData["alias"].(string)
|
|
|
|
service := bindData["service"].(string)
|
|
|
|
group := bindData["group"].(string)
|
|
|
|
bind := Bind{
|
|
|
|
Alias: alias,
|
|
|
|
Service: service,
|
|
|
|
Group: group,
|
|
|
|
}
|
|
|
|
binds = append(binds, bind)
|
|
|
|
}
|
|
|
|
return binds
|
|
|
|
}
|
|
|
|
|
2018-03-16 15:54:53 +01:00
|
|
|
func (p *provisioner) copyOutput(o terraform.UIOutput, r io.Reader) {
|
2017-10-03 14:46:19 +02:00
|
|
|
lr := linereader.New(r)
|
|
|
|
for line := range lr.Ch {
|
|
|
|
o.Output(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *provisioner) runCommand(o terraform.UIOutput, comm communicator.Communicator, command string) error {
|
|
|
|
outR, outW := io.Pipe()
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
|
2018-03-16 15:54:53 +01:00
|
|
|
go p.copyOutput(o, outR)
|
|
|
|
go p.copyOutput(o, errR)
|
|
|
|
defer outW.Close()
|
|
|
|
defer errW.Close()
|
2017-10-03 14:46:19 +02:00
|
|
|
|
|
|
|
cmd := &remote.Cmd{
|
|
|
|
Command: command,
|
|
|
|
Stdout: outW,
|
|
|
|
Stderr: errW,
|
|
|
|
}
|
|
|
|
|
2018-03-15 17:55:57 +01:00
|
|
|
if err := comm.Start(cmd); err != nil {
|
2019-09-05 18:44:35 +02:00
|
|
|
return fmt.Errorf("error executing command %q: %v", cmd.Command, err)
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
2018-03-16 15:54:53 +01:00
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
return err
|
2018-03-15 17:55:57 +01:00
|
|
|
}
|
2017-10-03 14:46:19 +02:00
|
|
|
|
2018-03-15 17:55:57 +01:00
|
|
|
return nil
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBindFromString(bind string) (Bind, error) {
|
|
|
|
t := strings.FieldsFunc(bind, func(d rune) bool {
|
|
|
|
switch d {
|
|
|
|
case ':', '.':
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if len(t) != 3 {
|
2019-09-05 18:44:35 +02:00
|
|
|
return Bind{}, errors.New("invalid bind specification: " + bind)
|
2017-10-03 14:46:19 +02:00
|
|
|
}
|
|
|
|
return Bind{Alias: t[0], Service: t[1], Group: t[2]}, nil
|
|
|
|
}
|