diff --git a/command/agent/command.go b/command/agent/command.go index d1f8205857..4b75d7896f 100644 --- a/command/agent/command.go +++ b/command/agent/command.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/go-checkpoint" "github.com/hashicorp/go-syslog" "github.com/hashicorp/logutils" + scada "github.com/hashicorp/scada-client" "github.com/mitchellh/cli" ) @@ -45,6 +46,7 @@ type Command struct { rpcServer *AgentRPC httpServers []*HTTPServer dnsServer *DNSServer + scadaProvider *scada.Provider } // readConfig is responsible for setup of our configuration using @@ -382,6 +384,17 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log }() } + // Enable the SCADA integration + if config.AtlasCluster != "" { + provider, err := NewProvider(config, logOutput) + if err != nil { + agent.Shutdown() + c.Ui.Error(fmt.Sprintf("Error starting SCADA connection: %s", err)) + return err + } + c.scadaProvider = provider + } + return nil } @@ -589,10 +602,12 @@ func (c *Command) Run(args []string) int { if c.dnsServer != nil { defer c.dnsServer.Shutdown() } - for _, server := range c.httpServers { defer server.Shutdown() } + if c.scadaProvider != nil { + defer c.scadaProvider.Shutdown() + } // Join startup nodes if specified if err := c.startupJoin(config); err != nil { @@ -631,6 +646,12 @@ func (c *Command) Run(args []string) int { gossipEncrypted = c.agent.client.Encrypted() } + // Determine the Atlas cluster + cluster := config.AtlasCluster + if cluster == "" { + cluster = "" + } + // Let the agent know we've finished registration c.agent.StartSync() @@ -644,6 +665,7 @@ func (c *Command) Run(args []string) int { config.Ports.SerfLan, config.Ports.SerfWan)) c.Ui.Info(fmt.Sprintf("Gossip encrypt: %v, RPC-TLS: %v, TLS-Incoming: %v", gossipEncrypted, config.VerifyOutgoing, config.VerifyIncoming)) + c.Ui.Info(fmt.Sprintf(" Atlas Cluster: %v", cluster)) // Enable log streaming c.Ui.Info("") diff --git a/command/agent/scada.go b/command/agent/scada.go new file mode 100644 index 0000000000..75ebfdd1b0 --- /dev/null +++ b/command/agent/scada.go @@ -0,0 +1,66 @@ +package agent + +import ( + "crypto/tls" + "fmt" + "io" + "log" + + "github.com/hashicorp/scada-client" +) + +const ( + // providerService is the service name we use + providerService = "consul" + + // resourceType is the type of resource we represent + // when connecting to SCADA + resourceType = "infrastructures" +) + +// 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{ + "type": "", + "datacenter": "", + }, + ResourceType: resourceType, + } +} + +// 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, + }, + ResourceGroup: c.AtlasCluster, + Token: c.AtlasToken, + } +} + +// NewProvider creates a new SCADA provider using the +// given configuration. Requests are routed to the +func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, error) { + // Get the configuration of the provider + config := ProviderConfig(c) + config.Logger = log.New(logOutput, "", log.LstdFlags) + + // TODO: REMOVE + config.TLSConfig = &tls.Config{ + InsecureSkipVerify: true, + } + + // TODO: Setup the handlers + config.Handlers["http"] = nil + + // Create the provider + return client.NewProvider(config) +}