diff --git a/configuration_flags.go b/configuration_flags.go index 552dc40..7ea5b5b 100644 --- a/configuration_flags.go +++ b/configuration_flags.go @@ -101,8 +101,6 @@ type Configuration struct { StartSubErrorLog flagNodeSlice // Subscriber for hello messages StartSubSayHello flagNodeSlice - // Subscriber for CLI Commands - StartSubCLICommand flagNodeSlice // Subscriber for text logging StartSubTextLogging flagNodeSlice // Subscriber for Echo Request @@ -115,8 +113,6 @@ type Configuration struct { StartSubCLICommandRequestNOSEQ flagNodeSlice // Subscriber for CLICommandReply StartSubCLICommandReply flagNodeSlice - // Subscriber for OpCommand - StartSubOpCommand flagNodeSlice } func NewConfiguration() *Configuration { @@ -138,14 +134,12 @@ func newConfigurationDefaults() Configuration { CentralNodeName: "", StartSubErrorLog: flagNodeSlice{Values: []node{}}, StartSubSayHello: flagNodeSlice{OK: true, Values: []node{"*"}}, - StartSubCLICommand: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubTextLogging: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubEchoRequest: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubEchoReply: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubCLICommandRequest: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubCLICommandRequestNOSEQ: flagNodeSlice{OK: true, Values: []node{"*"}}, StartSubCLICommandReply: flagNodeSlice{OK: true, Values: []node{"*"}}, - StartSubOpCommand: flagNodeSlice{OK: true, Values: []node{"*"}}, } return c } @@ -179,14 +173,12 @@ func (c *Configuration) CheckFlags() error { flag.Var(&c.StartSubErrorLog, "startSubErrorLog", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubSayHello, "startSubSayHello", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") - flag.Var(&c.StartSubCLICommand, "startSubCLICommand", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubTextLogging, "startSubTextLogging", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubEchoRequest, "startSubEchoRequest", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubEchoReply, "startSubEchoReply", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubCLICommandRequest, "startSubCLICommandRequest", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubCLICommandRequestNOSEQ, "startSubCLICommandRequestNOSEQ", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Var(&c.StartSubCLICommandReply, "startSubCLICommandReply", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") - flag.Var(&c.StartSubOpCommand, "startSubOpCommand", "Specify comma separated list for nodes to allow messages from. Use \"*\" for from all. Value RST will turn off subscriber.") flag.Parse() diff --git a/etc/config.toml b/etc/config.toml index 84d4c65..420051d 100644 --- a/etc/config.toml +++ b/etc/config.toml @@ -9,10 +9,6 @@ PromHostAndPort = ":2112" StartPubSayHello = 0 SubscribersDataFolder = "./data" -[StartSubCLICommand] - OK = true - Values = ["*"] - [StartSubCLICommandReply] OK = true Values = ["*"] @@ -37,10 +33,6 @@ SubscribersDataFolder = "./data" OK = true Values = ["*"] -[StartSubOpCommand] - OK = true - Values = ["*"] - [StartSubSayHello] OK = true Values = ["*"] diff --git a/example/OpCommand.json b/example/OpCommand.json deleted file mode 100644 index 7beb3bc..0000000 --- a/example/OpCommand.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - - "toNode": "central", - "data": ["ps"], - "method":"OpCommand", - "timeout":3, - "retries":3, - "methodTimeout": 7 - } -] \ No newline at end of file diff --git a/example/toShip1-CLICommand-CurlMetrics.json b/example/toShip1-CLICommand-CurlMetrics.json deleted file mode 100644 index 0afe5ec..0000000 --- a/example/toShip1-CLICommand-CurlMetrics.json +++ /dev/null @@ -1,10 +0,0 @@ -[ - { - - "toNode": "ship1", - "data": ["bash","-c","curl http://localhost:8888/metrics"], - "method":"CLICommand", - "timeout":3, - "retries":3 - } -] \ No newline at end of file diff --git a/example/toShip1-CLICommand-Two.json b/example/toShip1-CLICommand-Two.json deleted file mode 100644 index b260195..0000000 --- a/example/toShip1-CLICommand-Two.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":5, - "retries":10 - }, - { - - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":5, - "retries":10 - } -] \ No newline at end of file diff --git a/example/toShip1-CLICommand-many.json b/example/toShip1-CLICommand-many.json deleted file mode 100644 index 689ab39..0000000 --- a/example/toShip1-CLICommand-many.json +++ /dev/null @@ -1,100 +0,0 @@ -[ - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship1", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - } -] \ No newline at end of file diff --git a/example/toShip1-CLICommand.json b/example/toShip1-CLICommand.json deleted file mode 100644 index bff6c51..0000000 --- a/example/toShip1-CLICommand.json +++ /dev/null @@ -1,11 +0,0 @@ -[ - { - - "toNode": "ship1", - "data": ["bash","-c","sleep 5 && netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3, - "MethodTimeout": 7 - } -] \ No newline at end of file diff --git a/example/OpCommandRequest.json b/example/toShip1-OpCommandRequest.json similarity index 100% rename from example/OpCommandRequest.json rename to example/toShip1-OpCommandRequest.json diff --git a/example/toShip2-CLICommand-many.json b/example/toShip2-CLICommand-many.json deleted file mode 100644 index f98bd11..0000000 --- a/example/toShip2-CLICommand-many.json +++ /dev/null @@ -1,100 +0,0 @@ -[ - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - }, - { - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - } -] \ No newline at end of file diff --git a/example/toShip2-CLICommand-timeouts.json b/example/toShip2-CLICommand-timeouts.json deleted file mode 100644 index c0b67d6..0000000 --- a/example/toShip2-CLICommand-timeouts.json +++ /dev/null @@ -1,10 +0,0 @@ -[ - { - - "toNode": "ship2", - "data": ["bash","-c","netstat -an|grep -i listen"], - "method":"CLICommand", - "timeout":3, - "retries":3 - } -] \ No newline at end of file diff --git a/example/toShip2-CLICommand.json b/example/toShip2-CLICommand.json deleted file mode 100644 index f5533e6..0000000 --- a/example/toShip2-CLICommand.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - { - "toNode": "ship2", - "data": ["bash","-c","tree ../"], - "method":"CLICommand" - - } -] \ No newline at end of file diff --git a/process.go b/process.go index 45357a4..b4e4e99 100644 --- a/process.go +++ b/process.go @@ -183,7 +183,7 @@ func (p process) messageDeliverNats(natsConn *nats.Conn, message Message) { msg := &nats.Msg{ Subject: string(p.subject.name()), - // Subject: fmt.Sprintf("%s.%s.%s", proc.node, "command", "CLICommand"), + // Subject: fmt.Sprintf("%s.%s.%s", proc.node, "command", "CLICommandRequest"), // Structure of the reply message are: // reply... Reply: fmt.Sprintf("reply.%s", p.subject.name()), diff --git a/startup_processes.go b/startup_processes.go index 5532094..4d01a54 100644 --- a/startup_processes.go +++ b/startup_processes.go @@ -12,16 +12,6 @@ func (s *server) ProcessesStart() { // --- Subscriber services that can be started via flags - // Start a subscriber for OPCommand messages - if s.configuration.StartSubOpCommand.OK { - { - fmt.Printf("Starting OpCommand subscriber: %#v\n", s.nodeName) - sub := newSubject(OpCommand, s.nodeName) - proc := newProcess(s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubOpCommand.Values, nil) - go proc.spawnWorker(s) - } - } - { fmt.Printf("Starting OpCommandRequest subscriber: %#v\n", s.nodeName) sub := newSubject(OpCommandRequest, s.nodeName) @@ -29,17 +19,6 @@ func (s *server) ProcessesStart() { go proc.spawnWorker(s) } - // Start a subscriber for CLICommand messages - if s.configuration.StartSubCLICommand.OK { - { - fmt.Printf("Starting CLICommand subscriber: %#v\n", s.nodeName) - sub := newSubject(CLICommand, s.nodeName) - proc := newProcess(s.processes, s.toRingbufferCh, s.configuration, sub, s.errorKernel.errorCh, processKindSubscriber, s.configuration.StartSubCLICommand.Values, nil) - // fmt.Printf("*** %#v\n", proc) - go proc.spawnWorker(s) - } - } - // Start a subscriber for textLogging messages if s.configuration.StartSubTextLogging.OK { { diff --git a/subscriber_method_types.go b/subscriber_method_types.go index 0ce8db0..a76c4b3 100644 --- a/subscriber_method_types.go +++ b/subscriber_method_types.go @@ -7,15 +7,15 @@ // Overall structure example shown below. // // --- -// type methodCommandCLICommand struct { +// type methodCommandCLICommandRequest struct { // commandOrEvent CommandOrEvent // } // -// func (m methodCommandCLICommand) getKind() CommandOrEvent { +// func (m methodCommandCLICommandRequest) getKind() CommandOrEvent { // return m.commandOrEvent // } // -// func (m methodCommandCLICommand) handler(s *server, message Message, node string) ([]byte, error) { +// func (m methodCommandCLICommandRequest) handler(s *server, message Message, node string) ([]byte, error) { // ... // ... // ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s---", node, message.ID, out)) @@ -50,17 +50,9 @@ type Method string // The constants that will be used throughout the system for // when specifying what kind of Method to send or work with. const ( - // Command for client operation of the system - OpCommand Method = "OpCommand" // Command for client operation request of the system OpCommandRequest Method = "OpCommandRequest" // Execute a CLI command in for example bash or cmd. - // This is a command type, so the output of the command executed - // will directly showed in the ACK message received. - // The data field is a slice of strings where the first string - // value should be the command, and the following the arguments. - CLICommand Method = "CLICommand" - // Execute a CLI command in for example bash or cmd. // This is an event type, where a message will be sent to a // node with the command to execute and an ACK will be replied // if it was delivered succesfully. The output of the command @@ -121,15 +113,9 @@ func (m Method) GetMethodsAvailable() MethodsAvailable { // Event, Used to communicate that an action has been performed. ma := MethodsAvailable{ methodhandlers: map[Method]methodHandler{ - OpCommand: methodOpCommand{ - commandOrEvent: CommandACK, - }, OpCommandRequest: methodOpCommandRequest{ commandOrEvent: CommandACK, }, - CLICommand: methodCLICommand{ - commandOrEvent: CommandACK, - }, CLICommandRequest: methodCLICommandRequest{ commandOrEvent: CommandACK, }, @@ -200,41 +186,6 @@ type methodHandler interface { getKind() CommandOrEvent } -// ----- -type methodOpCommand struct { - commandOrEvent CommandOrEvent -} - -func (m methodOpCommand) getKind() CommandOrEvent { - return m.commandOrEvent -} - -// handler to run a CLI command with timeout context. The handler will -// return the output of the command run back to the calling publisher -// in the ack message. -func (m methodOpCommand) handler(proc process, message Message, node string) ([]byte, error) { - out := []byte{} - - switch { - case message.Data[0] == "ps": - proc.processes.mu.Lock() - for _, v := range proc.processes.active { - s := fmt.Sprintf("* proc - : %v, id: %v, name: %v, allowed from: %s\n", v.processKind, v.processID, v.subject.name(), v.allowedReceivers) - sb := []byte(s) - out = append(out, sb...) - } - proc.processes.mu.Unlock() - - default: - out = []byte("error: no such OpCommand specified: " + message.Data[0]) - ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v, error: %s\n---\n", node, message.ID, out)) - return ackMsg, nil - } - - ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s", node, message.ID, out)) - return ackMsg, nil -} - // ----- type methodOpCommandRequest struct { @@ -309,51 +260,6 @@ func newReplyMessage(proc process, message Message, method Method, outData []byt //-- } -// ----- - -type methodCLICommand struct { - commandOrEvent CommandOrEvent -} - -func (m methodCLICommand) getKind() CommandOrEvent { - return m.commandOrEvent -} - -// handler to run a CLI command with timeout context. The handler will -// return the output of the command run back to the calling publisher -// in the ack message. -func (m methodCLICommand) handler(proc process, message Message, node string) ([]byte, error) { - out := []byte{} - - c := message.Data[0] - a := message.Data[1:] - - ctx, cancel := context.WithTimeout(context.Background(), time.Second*time.Duration(message.MethodTimeout)) - - outCh := make(chan []byte) - - go func() { - cmd := exec.CommandContext(ctx, c, a...) - out, err := cmd.Output() - if err != nil { - log.Printf("error: %v\n", err) - } - outCh <- out - }() - - select { - case <-ctx.Done(): - cancel() - er := fmt.Errorf("error: method timed out %v", message) - sendErrorLogMessage(proc.toRingbufferCh, proc.node, er) - case out = <-outCh: - cancel() - } - - ackMsg := []byte(fmt.Sprintf("confirmed from node: %v: messageID: %v\n---\n%s---", node, message.ID, out)) - return ackMsg, nil -} - type methodTextLogging struct { commandOrEvent CommandOrEvent }