2015-08-16 20:54:49 +00:00
|
|
|
package agent
|
|
|
|
|
2015-08-16 23:40:04 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
2015-08-16 21:34:38 +00:00
|
|
|
|
2015-08-16 23:40:04 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Agent is a long running daemon that is used to run both
|
|
|
|
// clients and servers. Servers are responsible for managing
|
|
|
|
// state and making scheduling decisions. Clients can be
|
|
|
|
// scheduled to, and are responsible for interfacing with
|
|
|
|
// servers to run allocations.
|
2015-08-16 20:54:49 +00:00
|
|
|
type Agent struct {
|
2015-08-16 23:40:04 +00:00
|
|
|
config *Config
|
|
|
|
logger *log.Logger
|
|
|
|
logOutput io.Writer
|
|
|
|
|
|
|
|
server *nomad.Server
|
|
|
|
client *nomad.Server // TODO
|
|
|
|
|
|
|
|
shutdown bool
|
|
|
|
shutdownCh chan struct{}
|
|
|
|
shutdownLock sync.Mutex
|
2015-08-16 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 23:40:04 +00:00
|
|
|
// NewAgent is used to create a new agent with the given configuration
|
2015-08-16 21:34:38 +00:00
|
|
|
func NewAgent(config *Config, logOutput io.Writer) (*Agent, error) {
|
2015-08-16 23:40:04 +00:00
|
|
|
// Ensure we have a log sink
|
|
|
|
if logOutput == nil {
|
|
|
|
logOutput = os.Stderr
|
|
|
|
}
|
|
|
|
|
|
|
|
a := &Agent{
|
|
|
|
config: config,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
|
|
|
logOutput: logOutput,
|
|
|
|
shutdownCh: make(chan struct{}),
|
|
|
|
}
|
2015-08-16 21:34:38 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:40:04 +00:00
|
|
|
// Leave is used gracefully exit. Clients will inform servers
|
|
|
|
// of their departure so that allocations can be rescheduled.
|
2015-08-16 20:54:49 +00:00
|
|
|
func (a *Agent) Leave() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-16 23:40:04 +00:00
|
|
|
// Shutdown is used to terminate the agent.
|
2015-08-16 20:54:49 +00:00
|
|
|
func (a *Agent) Shutdown() error {
|
2015-08-16 23:40:04 +00:00
|
|
|
a.shutdownLock.Lock()
|
|
|
|
defer a.shutdownLock.Unlock()
|
|
|
|
|
|
|
|
if a.shutdown {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Println("[INFO] agent: requesting shutdown")
|
|
|
|
var err error
|
|
|
|
if a.server != nil {
|
|
|
|
err = a.server.Shutdown()
|
|
|
|
} else {
|
|
|
|
err = a.client.Shutdown()
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Println("[INFO] agent: shutdown complete")
|
|
|
|
a.shutdown = true
|
|
|
|
close(a.shutdownCh)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPC is used to make an RPC call to the Nomad servers
|
|
|
|
func (a *Agent) RPC(method string, args interface{}, reply interface{}) error {
|
|
|
|
if a.server != nil {
|
|
|
|
return a.server.RPC(method, args, reply)
|
|
|
|
}
|
|
|
|
return a.client.RPC(method, args, reply)
|
2015-08-16 20:54:49 +00:00
|
|
|
}
|