Merge pull request #1227 from hashicorp/upstream-scada-provider

Use upstream high-level SCADA provider
This commit is contained in:
Alex Dadgar
2016-06-07 09:47:24 -07:00
5 changed files with 117 additions and 190 deletions

View File

@@ -21,7 +21,7 @@ import (
"github.com/hashicorp/logutils"
"github.com/hashicorp/nomad/helper/flag-slice"
"github.com/hashicorp/nomad/helper/gated-writer"
scada "github.com/hashicorp/scada-client"
"github.com/hashicorp/scada-client/scada"
"github.com/mitchellh/cli"
)
@@ -622,7 +622,26 @@ func (c *Command) setupSCADA(config *Config) error {
// Create the new provider and listener
c.Ui.Output("Connecting to Atlas: " + config.Atlas.Infrastructure)
provider, list, err := NewProvider(config, c.logOutput)
scadaConfig := &scada.Config{
Service: "nomad",
Version: fmt.Sprintf("%s%s", config.Version, config.VersionPrerelease),
ResourceType: "nomad-cluster",
Meta: map[string]string{
"auto-join": strconv.FormatBool(config.Atlas.Join),
"region": config.Region,
"datacenter": config.Datacenter,
"client": strconv.FormatBool(config.Client != nil && config.Client.Enabled),
"server": strconv.FormatBool(config.Server != nil && config.Server.Enabled),
},
Atlas: scada.AtlasConfig{
Endpoint: config.Atlas.Endpoint,
Infrastructure: config.Atlas.Infrastructure,
Token: config.Atlas.Token,
},
}
provider, list, err := scada.NewHTTPProvider(scadaConfig, c.logOutput)
if err != nil {
return err
}

View File

@@ -1,112 +0,0 @@
package agent
import (
"net"
"reflect"
"testing"
"github.com/hashicorp/scada-client"
)
func TestProviderService(t *testing.T) {
conf := DefaultConfig()
conf.Version = "0.5.0"
conf.VersionPrerelease = "rc1"
conf.Atlas = &AtlasConfig{}
conf.Atlas.Join = true
conf.Server.Enabled = true
ps := ProviderService(conf)
expect := &client.ProviderService{
Service: "nomad",
ServiceVersion: "0.5.0rc1",
Capabilities: map[string]int{
"http": 1,
},
Meta: map[string]string{
"auto-join": "true",
"region": "global",
"datacenter": "dc1",
"client": "false",
"server": "true",
},
ResourceType: "nomad-cluster",
}
if !reflect.DeepEqual(ps, expect) {
t.Fatalf("bad: %v", ps)
}
}
func TestProviderConfig(t *testing.T) {
conf := DefaultConfig()
conf.Version = "0.5.0"
conf.VersionPrerelease = "rc1"
conf.Atlas = &AtlasConfig{}
conf.Atlas.Join = true
conf.Atlas.Infrastructure = "armon/test"
conf.Atlas.Token = "foobarbaz"
conf.Atlas.Endpoint = "foo.bar:1111"
conf.Server.Enabled = true
pc := ProviderConfig(conf)
expect := &client.ProviderConfig{
Service: &client.ProviderService{
Service: "nomad",
ServiceVersion: "0.5.0rc1",
Capabilities: map[string]int{
"http": 1,
},
Meta: map[string]string{
"auto-join": "true",
"region": "global",
"datacenter": "dc1",
"client": "false",
"server": "true",
},
ResourceType: "nomad-cluster",
},
Handlers: map[string]client.CapabilityProvider{
"http": nil,
},
Endpoint: "foo.bar:1111",
ResourceGroup: "armon/test",
Token: "foobarbaz",
}
if !reflect.DeepEqual(pc, expect) {
t.Fatalf("bad: %v", pc)
}
}
func TestSCADAListener(t *testing.T) {
list := newScadaListener("armon/test")
defer list.Close()
var raw interface{} = list
_, ok := raw.(net.Listener)
if !ok {
t.Fatalf("bad")
}
a, b := net.Pipe()
defer a.Close()
defer b.Close()
go list.Push(a)
out, err := list.Accept()
if err != nil {
t.Fatalf("err: %v", err)
}
if out != a {
t.Fatalf("bad")
}
}
func TestSCADAAddr(t *testing.T) {
var addr interface{} = &scadaAddr{"armon/test"}
_, ok := addr.(net.Addr)
if !ok {
t.Fatalf("bad")
}
}

View File

@@ -1,24 +0,0 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

View File

@@ -1,4 +1,4 @@
package agent
package scada
import (
"crypto/tls"
@@ -7,70 +7,105 @@ import (
"io"
"net"
"os"
"strconv"
"sync"
"time"
"github.com/hashicorp/scada-client"
sc "github.com/hashicorp/scada-client"
)
const (
// providerService is the service name we use
providerService = "nomad"
// resourceType is the type of resource we represent
// when connecting to SCADA
resourceType = "nomad-cluster"
)
// ProviderService returns the service information for the provider
func ProviderService(c *Config) *client.ProviderService {
return &client.ProviderService{
Service: providerService,
ServiceVersion: fmt.Sprintf("%s%s", c.Version, c.VersionPrerelease),
Capabilities: map[string]int{
"http": 1,
},
Meta: map[string]string{
"auto-join": strconv.FormatBool(c.Atlas.Join),
"region": c.Region,
"datacenter": c.Datacenter,
"client": strconv.FormatBool(c.Client != nil && c.Client.Enabled),
"server": strconv.FormatBool(c.Server != nil && c.Server.Enabled),
},
ResourceType: resourceType,
}
// Provider wraps scada-client.Provider to allow most applications to only pull
// in this package
type Provider struct {
*sc.Provider
}
// ProviderConfig returns the configuration for the SCADA provider
func ProviderConfig(c *Config) *client.ProviderConfig {
return &client.ProviderConfig{
Service: ProviderService(c),
Handlers: map[string]client.CapabilityProvider{
"http": nil,
},
type AtlasConfig struct {
// Endpoint is the SCADA endpoint used for Atlas integration. If empty, the
// defaults from the provider are used.
Endpoint string `mapstructure:"endpoint"`
// The name of the infrastructure we belong to, e.g. "hashicorp/prod"
Infrastructure string `mapstructure:"infrastructure"`
// The Atlas authentication token
Token string `mapstructure:"token" json:"-"`
}
// Config holds the high-level information used to instantiate a SCADA provider
// and listener
type Config struct {
// The service name to use
Service string
// The version of the service
Version string
// The type of resource we represent
ResourceType string
// Metadata to send to along with the service information
Meta map[string]string
// If set, TLS certificate verification will be skipped. The value of the
// SCADA_INSECURE environment variable will be considered if this is false.
// If using SCADA_INSECURE, any non-empty value will trigger insecure mode.
Insecure bool
// Holds Atlas configuration
Atlas AtlasConfig
}
// ProviderService returns the service information for the provider
func providerService(c *Config) *sc.ProviderService {
ret := &sc.ProviderService{
Service: c.Service,
ServiceVersion: c.Version,
Capabilities: map[string]int{},
Meta: c.Meta,
ResourceType: c.ResourceType,
}
return ret
}
// providerConfig returns the configuration for the SCADA provider
func providerConfig(c *Config) *sc.ProviderConfig {
ret := &sc.ProviderConfig{
Service: providerService(c),
Handlers: map[string]sc.CapabilityProvider{},
Endpoint: c.Atlas.Endpoint,
ResourceGroup: c.Atlas.Infrastructure,
Token: c.Atlas.Token,
}
}
// NewProvider creates a new SCADA provider using the
// given configuration. Requests for the HTTP capability
// are passed off to the listener that is returned.
func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, net.Listener, error) {
// Get the configuration of the provider
config := ProviderConfig(c)
config.LogOutput = logOutput
// SCADA_INSECURE env variable is used for testing to disable
// TLS certificate verification.
if os.Getenv("SCADA_INSECURE") != "" {
config.TLSConfig = &tls.Config{
// SCADA_INSECURE env variable is used for testing to disable TLS
// certificate verification.
insecure := c.Insecure
if !insecure {
if os.Getenv("SCADA_INSECURE") != "" {
insecure = true
}
}
if insecure {
ret.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
}
return ret
}
// NewProvider creates a new SCADA provider using the given configuration.
// Requests for the HTTP capability are passed off to the listener that is
// returned.
func NewHTTPProvider(c *Config, logOutput io.Writer) (*Provider, net.Listener, error) {
// Get the configuration of the provider
config := providerConfig(c)
config.LogOutput = logOutput
// Set the HTTP capability
config.Service.Capabilities["http"] = 1
// Create an HTTP listener and handler
list := newScadaListener(c.Atlas.Infrastructure)
config.Handlers["http"] = func(capability string, meta map[string]string,
@@ -79,12 +114,13 @@ func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, net.Listener
}
// Create the provider
provider, err := client.NewProvider(config)
provider, err := sc.NewProvider(config)
if err != nil {
list.Close()
return nil, nil, err
}
return provider, list, nil
return &Provider{provider}, list, nil
}
// scadaListener is used to return a net.Listener for

10
vendor/vendor.json vendored
View File

@@ -537,8 +537,16 @@
"revision": "057b893fd996696719e98b6c44649ea14968c811"
},
{
"checksumSHA1": "u9qHbpIgMZ7/fjO0gFfds2m/1ck=",
"path": "github.com/hashicorp/scada-client",
"revision": "84989fd23ad4cc0e7ad44d6a871fd793eb9beb0a"
"revision": "6e896784f66f82cdc6f17e00052db91699dc277d",
"revisionTime": "2016-06-01T22:40:23Z"
},
{
"checksumSHA1": "fv3nX1vDZViW0tA7Aa5Va2lBUtM=",
"path": "github.com/hashicorp/scada-client/scada",
"revision": "6e896784f66f82cdc6f17e00052db91699dc277d",
"revisionTime": "2016-06-01T22:40:23Z"
},
{
"comment": "v0.7.0-18-gc4c55f1",