Minor cleanup and and got the client sending agents again.

This commit is contained in:
James Wells 2022-07-14 13:27:46 -07:00
parent 42623f63c6
commit 134f33cd6a
Signed by: jwells
GPG key ID: 73196D10B8E65666
7 changed files with 50 additions and 64 deletions

View file

@ -1,4 +1,4 @@
FROM dragonheim/golang:1.17 as builder FROM dragonheim/golang:1.18 as builder
ARG SEMVER ARG SEMVER
WORKDIR /gagent WORKDIR /gagent
@ -12,7 +12,7 @@ RUN apk add --no-cache zeromq-dev build-base git
RUN go build -o /gagent/bin/gagent -ldflags "-X main.semVER=${SEMVER}" cmd/gagent/main.go RUN go build -o /gagent/bin/gagent -ldflags "-X main.semVER=${SEMVER}" cmd/gagent/main.go
RUN strip /gagent/bin/gagent RUN strip /gagent/bin/gagent
FROM alpine:3.15 FROM alpine:3.16
ARG SEMVER ARG SEMVER
LABEL Name="G'Agent" LABEL Name="G'Agent"
LABEL Maintainer="jwells@dragonheim.net" LABEL Maintainer="jwells@dragonheim.net"
@ -23,8 +23,8 @@ RUN apk add --no-cache zeromq && mkdir -p -m 0700 /etc/gagent
COPY --from=builder /gagent/assets/examples/gagent.hcl /etc/gagent/gagent.hcl COPY --from=builder /gagent/assets/examples/gagent.hcl /etc/gagent/gagent.hcl
COPY --from=builder /gagent/bin/gagent /usr/bin/ COPY --from=builder /gagent/bin/gagent /usr/bin/
# Router Client Worker # Router Client Worker Prometheus
EXPOSE 35570/tcp 35571/tcp 35572/tcp EXPOSE 35570/tcp 35572/tcp 35571/tcp 9101/tcp
VOLUME /etc/gagent VOLUME /etc/gagent
CMD ["/usr/bin/gagent"] CMD ["/usr/bin/gagent"]

View file

@ -9,4 +9,5 @@
"clients": { "clients": {
"client": "7e9d13fe-5151-5876-66c0-20ca03e8fca4" "client": "7e9d13fe-5151-5876-66c0-20ca03e8fca4"
} }
} }

View file

@ -143,35 +143,28 @@ func init() {
config.UUID = uuid.NewV4UUID().String() config.UUID = uuid.NewV4UUID().String()
/* /*
* By default, we want to listen on all IP addresses. It can be overridden * By default, we want to listen on all IP addresses.
* in the configuration file by setting listenaddr
*/ */
config.ListenAddr = "0.0.0.0" config.ListenAddr = "0.0.0.0"
/* /*
* By default, G'Agent will use port 9101 or monitoring via prometheus. * G'Agent will use this port for monitoring via prometheus., If set
* It can be overridden in the configuration file by setting clientport * is set to 0, G'Agent will not listen for prometheus metrics.
*/ */
config.MonitorPort = 9101 config.MonitorPort = 9101
/* /*
* By default, G'Agent client will use port 35572 to communicate with the * G'Agent client will use this port to communicate with the routers.
* routers, but you can override it by setting the clientport in the
* configuration file
*/ */
config.ClientPort = 35572 config.ClientPort = 35572
/* /*
* By default, G'Agent router will use port 35570 to communicate with * G'Agent router will use this port to communicate with other routers.
* other routers, but you can override it by setting the routerport in
* the configuration file
*/ */
config.RouterPort = 35570 config.RouterPort = 35570
/* /*
* By default, G'Agent worker will use port 35571 to communicate with the * G'Agent worker will use this port to communicate with the routers.
* routers, but you can override it by setting the workerport in the
* configuration file
*/ */
config.WorkerPort = 35571 config.WorkerPort = 35571

View file

@ -2,6 +2,7 @@ package client
import ( import (
sha "crypto/sha256" sha "crypto/sha256"
hex "encoding/hex"
fmt "fmt" fmt "fmt"
ioutil "io/ioutil" ioutil "io/ioutil"
log "log" log "log"
@ -40,9 +41,10 @@ func Main(wg *sync.WaitGroup, config gstructs.GagentConfig) {
log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.ScriptCode) log.Printf("[DEBUG] Agent file contents: \n----- -----\n%s\n----- -----\n", agent.ScriptCode)
} }
agent.Client = config.UUID agent.Client = config.UUID
agent.Shasum = fmt.Sprintf("%s", sha.Sum256(agent.ScriptCode)) tmpsum := sha.Sum256([]byte(agent.ScriptCode))
agent.Shasum = fmt.Sprintf("%v", hex.EncodeToString(tmpsum[:]))
log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum) log.Printf("[INFO] SHA256 of Agent file: %s", agent.Shasum)
agent.Status = "loaded" agent.Status = 1
agent.Hints = getTagsFromHints(agent) agent.Hints = getTagsFromHints(agent)
agent.Answer = nil agent.Answer = nil

View file

@ -28,7 +28,8 @@ func NewConsulClient(addr string) (Client, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &client{consul: c}, nil return &client{c}, nil
//return &client{consul: c}, nil
} }
// Register a service with consul local agent // Register a service with consul local agent
@ -51,10 +52,10 @@ func (c *client) Service(service, tag string) ([]*consul.ServiceEntry, *consul.Q
passingOnly := true passingOnly := true
addrs, meta, err := c.consul.Health().Service(service, tag, passingOnly, nil) addrs, meta, err := c.consul.Health().Service(service, tag, passingOnly, nil)
if len(addrs) == 0 && err == nil { if len(addrs) == 0 && err == nil {
return nil, fmt.Errorf("service ( %s ) was not found", service) return nil, nil, fmt.Errorf("service ( %s ) was not found", service)
} }
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
return addrs, meta, nil return addrs, meta, nil
} }

View file

@ -24,16 +24,14 @@ type GagentConfig struct {
*/ */
type ClientDetails struct { type ClientDetails struct {
/* /*
* Client name for display purposes in logs and * Client name for display purposes in logs and diagnostics.
* diagnostics.
*/ */
ClientName string `hcl:",label"` ClientName string `hcl:",label"`
/* /*
* UUID String for the client node. This is used by * UUID String for the client node. This is used by the router to
* the router to determine which MQ client to send * determine which MQ client to send the agent's results to. This
* the agent's results to. This attempts to keep the * attempts to keep the clients unique globally.
* clients unique globally.
*/ */
ClientID string `hcl:"clientid,optional"` ClientID string `hcl:"clientid,optional"`
} }
@ -43,52 +41,44 @@ type ClientDetails struct {
*/ */
type RouterDetails struct { type RouterDetails struct {
/* /*
* Router name for display purposes in logs and * Router name for display purposes in logs and diagnostics.
* diagnostics
*/ */
RouterName string `hcl:",label"` RouterName string `hcl:",label"`
/* /*
* UUID String for the router node. This is used by * UUID String for the router node. This is used by the clients,
* the clients, routers, and workers to determine * routers, and workers to determine which MQ router to send the
* which MQ router to send the agent's requests to. * agent's requests to. This attempts to keep the routers unique
* This attempts to keep the routers unique globally. * globally.
*/ */
RouterID string `hcl:"routerid,attr"` RouterID string `hcl:"routerid,attr"`
/* /*
* This is the IP address or hostname the router * This is the IP address or hostname the router will listen on. The
* will listen on. The router will start up a 0MQ * router will start up a 0MQ service that clients and workers will
* service that clients and workers will connect to. * connect to.
*/ */
RouterAddr string `hcl:"address,attr"` RouterAddr string `hcl:"address,attr"`
/* /*
* This is the is the port that the router listens * G'Agent client will use this port to communicate with the routers.
* on for clients. If not defined, it will default
* to 35571.
*/ */
ClientPort int64 `hcl:"clientport,optional"` ClientPort int64 `hcl:"clientport,optional"`
/* /*
* This is the is the port that the router listens * G'Agent router will use this port to communicate with other routers.
* on for routers. If not defined, it will default
* to 35570.
*/ */
RouterPort int64 `hcl:"routerport,optional"` RouterPort int64 `hcl:"routerport,optional"`
/* /*
* This is the is the port that the router listens * G'Agent worker will use this port to communicate with the routers.
* on for clients. If not defined, it will default
* to 35572.
*/ */
WorkerPort int64 `hcl:"workerport,optional"` WorkerPort int64 `hcl:"workerport,optional"`
/* /*
* These tags will be passed to the router upon * These tags will be passed to the router upon connection. The router
* connection. The router will then use these * will then use these tags to help determine which worker / client to
* tags to help determine which worker / client * send the client's requests and results to.
* to send the client's requests and results to.
*/ */
RouterTags []string `hcl:"tags,optional"` RouterTags []string `hcl:"tags,optional"`
} }
@ -98,33 +88,30 @@ type RouterDetails struct {
*/ */
type WorkerDetails struct { type WorkerDetails struct {
/* /*
* Router name for display purposes in logs and * Router name for display purposes in logs and diagnostics.
* diagnostics
*/ */
WorkerName string `hcl:",label"` WorkerName string `hcl:",label"`
/* /*
* UUID String for the worker node. This is used * UUID String for the worker node. This is used by the router to
* by the router to determine which MQ client to * determine which MQ client to send agents to. This attempts to keep
* send agents to. This attempts to keep the * the workers unique globally.
* workers unique globally.
*/ */
WorkerID string `hcl:"workerid,attr"` WorkerID string `hcl:"workerid,attr"`
/* /*
* These tags will be passed to the router upon * These tags will be passed to the router upon connection. The router
* connection. The router will then use these * will then use these tags to help determine which worker / client to
* tags to help determine which worker / client * send the agent and it's results to.
* to send the agent and it's results to.
*/ */
WorkerTags []string `hcl:"tags,optional"` WorkerTags []string `hcl:"tags,optional"`
} }
type AgentDetails struct { type AgentDetails struct {
Client string `hcl:"client"` Client string `hcl:"client"`
Status int64 `hcl:"status"`
Shasum string `hcl:"shasum"` Shasum string `hcl:"shasum"`
Status string `hcl:"status"`
ScriptCode []byte
Hints []string Hints []string
ScriptCode []byte
Answer []byte Answer []byte
} }

View file

@ -57,7 +57,9 @@ func getAgent(wg *sync.WaitGroup, uuid string, connectString string) {
subscriber, _ := zmq.NewSocket(zmq.REP) subscriber, _ := zmq.NewSocket(zmq.REP)
defer subscriber.Close() defer subscriber.Close()
_ = subscriber.Connect(connectString) // _ = subscriber.Connect(connectString)
foo := subscriber.Connect(connectString)
log.Printf("[DEBUG] Connected to %s\n", foo)
msg, err := subscriber.Recv(0) msg, err := subscriber.Recv(0)
if err != nil { if err != nil {