agent: SCADA HTTP listener

This commit is contained in:
Armon Dadgar 2015-02-04 18:17:45 -08:00
parent 55597dc38f
commit 456645f2fb
3 changed files with 46 additions and 19 deletions

View File

@ -332,8 +332,21 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
c.Ui.Output("Starting Consul agent RPC...") c.Ui.Output("Starting Consul agent RPC...")
c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter) c.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter)
if config.Ports.HTTP > 0 || config.Ports.HTTPS > 0 { // Enable the SCADA integration
servers, err := NewHTTPServers(agent, config, logOutput) var scadaList net.Listener
if config.AtlasCluster != "" {
provider, list, 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
scadaList = list
}
if config.Ports.HTTP > 0 || config.Ports.HTTPS > 0 || scadaList != nil {
servers, err := NewHTTPServers(agent, config, scadaList, logOutput)
if err != nil { if err != nil {
agent.Shutdown() agent.Shutdown()
c.Ui.Error(fmt.Sprintf("Error starting http servers: %s", err)) c.Ui.Error(fmt.Sprintf("Error starting http servers: %s", err))
@ -383,18 +396,6 @@ func (c *Command) setupAgent(config *Config, logOutput io.Writer, logWriter *log
c.checkpointResults(checkpoint.Check(updateParams)) c.checkpointResults(checkpoint.Check(updateParams))
}() }()
} }
// 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 return nil
} }

View File

@ -32,7 +32,7 @@ type HTTPServer struct {
// NewHTTPServers starts new HTTP servers to provide an interface to // NewHTTPServers starts new HTTP servers to provide an interface to
// the agent. // the agent.
func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPServer, error) { func NewHTTPServers(agent *Agent, config *Config, scada net.Listener, logOutput io.Writer) ([]*HTTPServer, error) {
var tlsConfig *tls.Config var tlsConfig *tls.Config
var list net.Listener var list net.Listener
var httpAddr net.Addr var httpAddr net.Addr
@ -136,6 +136,26 @@ func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPS
servers = append(servers, srv) servers = append(servers, srv)
} }
if scada != nil {
// Create the mux
mux := http.NewServeMux()
// Create the server
srv := &HTTPServer{
agent: agent,
mux: mux,
listener: scada,
logger: log.New(logOutput, "", log.LstdFlags),
uiDir: config.UiDir,
addr: "SCADA",
}
srv.registerHandlers(false) // Never allow debug for SCADA
// Start the server
go http.Serve(list, mux)
servers = append(servers, srv)
}
return servers, nil return servers, nil
} }

View File

@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"io" "io"
"log" "log"
"net"
"strconv"
"github.com/hashicorp/scada-client" "github.com/hashicorp/scada-client"
) )
@ -27,8 +29,8 @@ func ProviderService(c *Config) *client.ProviderService {
"http": 1, "http": 1,
}, },
Meta: map[string]string{ Meta: map[string]string{
"type": "", "server": strconv.FormatBool(c.Server),
"datacenter": "", "datacenter": c.Datacenter,
}, },
ResourceType: resourceType, ResourceType: resourceType,
} }
@ -48,7 +50,7 @@ func ProviderConfig(c *Config) *client.ProviderConfig {
// NewProvider creates a new SCADA provider using the // NewProvider creates a new SCADA provider using the
// given configuration. Requests are routed to the // given configuration. Requests are routed to the
func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, error) { func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, net.Listener, error) {
// Get the configuration of the provider // Get the configuration of the provider
config := ProviderConfig(c) config := ProviderConfig(c)
config.Logger = log.New(logOutput, "", log.LstdFlags) config.Logger = log.New(logOutput, "", log.LstdFlags)
@ -62,5 +64,9 @@ func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, error) {
config.Handlers["http"] = nil config.Handlers["http"] = nil
// Create the provider // Create the provider
return client.NewProvider(config) provider, err := client.NewProvider(config)
if err != nil {
return nil, nil, err
}
return provider, nil, nil
} }