diff --git a/command/agent/command.go b/command/agent/command.go index b9f5d1eeb..5c8371b79 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -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 } diff --git a/command/agent/scada_test.go b/command/agent/scada_test.go deleted file mode 100644 index 686db0682..000000000 --- a/command/agent/scada_test.go +++ /dev/null @@ -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") - } -} diff --git a/command/agent/scada.go b/vendor/github.com/hashicorp/scada-client/scada/scada.go similarity index 54% rename from command/agent/scada.go rename to vendor/github.com/hashicorp/scada-client/scada/scada.go index 815671243..8d3dc3ad3 100644 --- a/command/agent/scada.go +++ b/vendor/github.com/hashicorp/scada-client/scada/scada.go @@ -1,4 +1,4 @@ -package agent +package scada import ( "crypto/tls" @@ -7,65 +7,107 @@ 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" + InfrastructureResource = "infrastructures" + BoxesResource = "boxes" + SharesResource = "shares" + NomadClusterResource = "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, } + + 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 NewProvider(c *Config, logOutput io.Writer) (*client.Provider, net.Listener, error) { +// 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 := providerConfig(c) config.LogOutput = logOutput - // SCADA_INSECURE env variable is used for testing to disable - // TLS certificate verification. - if os.Getenv("SCADA_INSECURE") != "" { + // Set the HTTP capability + config.Service.Capabilities["http"] = 1 + + // 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 { config.TLSConfig = &tls.Config{ InsecureSkipVerify: true, } @@ -79,12 +121,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 diff --git a/vendor/vendor.json b/vendor/vendor.json index 1f03dedbb..c68d82c5b 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -540,6 +540,12 @@ "path": "github.com/hashicorp/scada-client", "revision": "84989fd23ad4cc0e7ad44d6a871fd793eb9beb0a" }, + { + "checksumSHA1": "YQamL+KX0yqqM+aiwWaFy2FZEyE=", + "path": "github.com/hashicorp/scada-client/scada", + "revision": "225f7ae4780b59d1bd990de93cae7496bade95a0", + "revisionTime": "2016-06-01T18:41:54Z" + }, { "comment": "v0.7.0-18-gc4c55f1", "path": "github.com/hashicorp/serf/coordinate",