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.rpcServer = NewAgentRPC(agent, rpcListener, logOutput, logWriter)
if config.Ports.HTTP > 0 || config.Ports.HTTPS > 0 {
servers, err := NewHTTPServers(agent, config, logOutput)
// Enable the SCADA integration
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 {
agent.Shutdown()
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))
}()
}
// 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
}

View File

@ -32,7 +32,7 @@ type HTTPServer struct {
// NewHTTPServers starts new HTTP servers to provide an interface to
// 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 list net.Listener
var httpAddr net.Addr
@ -136,6 +136,26 @@ func NewHTTPServers(agent *Agent, config *Config, logOutput io.Writer) ([]*HTTPS
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
}

View File

@ -5,6 +5,8 @@ import (
"fmt"
"io"
"log"
"net"
"strconv"
"github.com/hashicorp/scada-client"
)
@ -27,8 +29,8 @@ func ProviderService(c *Config) *client.ProviderService {
"http": 1,
},
Meta: map[string]string{
"type": "",
"datacenter": "",
"server": strconv.FormatBool(c.Server),
"datacenter": c.Datacenter,
},
ResourceType: resourceType,
}
@ -48,7 +50,7 @@ func ProviderConfig(c *Config) *client.ProviderConfig {
// 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) {
func NewProvider(c *Config, logOutput io.Writer) (*client.Provider, net.Listener, error) {
// Get the configuration of the provider
config := ProviderConfig(c)
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
// Create the provider
return client.NewProvider(config)
provider, err := client.NewProvider(config)
if err != nil {
return nil, nil, err
}
return provider, nil, nil
}