1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-15 17:51:15 +00:00

fixed read messages from file, added helper scripts for ReqCli

This commit is contained in:
postmannen 2023-01-09 03:59:13 +01:00
parent d348ab156f
commit 57e074d210
5 changed files with 180 additions and 39 deletions

View file

@ -233,56 +233,56 @@ func (s *server) readFolder() {
if event.Op == fsnotify.Create || event.Op == fsnotify.Chmod { if event.Op == fsnotify.Create || event.Op == fsnotify.Chmod {
fmt.Printf(" *** got *** : %v, op: %v\n", event.Name, event.Op) fmt.Printf(" *** got *** : %v, op: %v\n", event.Name, event.Op)
}
func() { func() {
fh, err := os.Open(event.Name) fh, err := os.Open(event.Name)
if err != nil { if err != nil {
log.Printf("error: failed to open readFile from readFolder: %v\n", err) log.Printf("error: failed to open readFile from readFolder: %v\n", err)
return return
} }
b, err := io.ReadAll(fh) b, err := io.ReadAll(fh)
if err != nil { if err != nil {
log.Printf("error: failed to readall from readFolder: %v\n", err) log.Printf("error: failed to readall from readFolder: %v\n", err)
fh.Close()
return
}
fh.Close() fh.Close()
return
}
fh.Close()
b = bytes.Trim(b, "\x00") b = bytes.Trim(b, "\x00")
// unmarshal the JSON into a struct // unmarshal the JSON into a struct
sams, err := s.convertBytesToSAMs(b) sams, err := s.convertBytesToSAMs(b)
if err != nil { if err != nil {
er := fmt.Errorf("error: malformed json received on socket: %s\n %v", b, err) er := fmt.Errorf("error: malformed json received on socket: %s\n %v", b, err)
s.errorKernel.errSend(s.processInitial, Message{}, er) s.errorKernel.errSend(s.processInitial, Message{}, er)
return return
} }
for i := range sams { for i := range sams {
// Fill in the value for the FromNode field, so the receiver // Fill in the value for the FromNode field, so the receiver
// can check this field to know where it came from. // can check this field to know where it came from.
sams[i].Message.FromNode = Node(s.nodeName) sams[i].Message.FromNode = Node(s.nodeName)
// Send an info message to the central about the message picked // Send an info message to the central about the message picked
// for auditing. // for auditing.
er := fmt.Errorf("info: message read from socket on %v: %v", s.nodeName, sams[i].Message) er := fmt.Errorf("info: message read from socket on %v: %v", s.nodeName, sams[i].Message)
s.errorKernel.errSend(s.processInitial, Message{}, er) s.errorKernel.errSend(s.processInitial, Message{}, er)
} }
// Send the SAM struct to be picked up by the ring buffer. // Send the SAM struct to be picked up by the ring buffer.
s.toRingBufferCh <- sams s.toRingBufferCh <- sams
// Delete the file. // Delete the file.
err = os.Remove(event.Name) err = os.Remove(event.Name)
if err != nil { if err != nil {
log.Printf("error: failed to remove readFile from readFolder: %v\n", err) log.Printf("error: failed to remove readFile from readFolder: %v\n", err)
return return
} }
}() }()
}
case err, ok := <-watcher.Errors: case err, ok := <-watcher.Errors:
if !ok { if !ok {

47
scripts/cliAshReadfolder.sh Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "No toNode supplied"
exit 1
fi
if [ -z "$2" ]; then
echo "No cmd supplied"
exit 1
fi
command=$2
IFS=',' read -r -a array <<<"$1"
function sendMessage() {
cat >msg.yaml <<EOF
[
{
"toNodes": ["${element}"],
"method": "REQCliCommand",
"methodArgs":
[
"/bin/ash",
"-c",
'echo "--------------------${element}----------------------" && ${command}',
],
"replyMethod": "REQToFileAppend",
"retryWait": 5,
"ACKTimeout": 10,
"retries": 1,
"replyACKTimeout": 10,
"replyRetries": 1,
"methodTimeout": 10,
"replyMethodTimeout": 10,
"directory": "./data/",
"fileName": "debug.log",
},
]
EOF
}
for element in "${array[@]}"; do
sendMessage element command
cp msg.yaml ./readfolder
done

47
scripts/cliAshSocket.sh Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "No toNode supplied"
exit 1
fi
if [ -z "$2" ]; then
echo "No cmd supplied"
exit 1
fi
command=$2
IFS=',' read -r -a array <<<"$1"
function sendMessage() {
cat >msg.yaml <<EOF
[
{
"toNodes": ["${element}"],
"method": "REQCliCommand",
"methodArgs":
[
"/bin/ash",
"-c",
'echo "--------------------${element}----------------------" && ${command}',
],
"replyMethod": "REQToFileAppend",
"retryWait": 5,
"ACKTimeout": 10,
"retries": 1,
"replyACKTimeout": 10,
"replyRetries": 1,
"methodTimeout": 10,
"replyMethodTimeout": 10,
"directory": "./data/",
"fileName": "debug.log",
},
]
EOF
}
for element in "${array[@]}"; do
sendMessage element command
nc -U ./tmp/steward.sock <msg.yaml
done

47
scripts/cliBashReadfolder.sh Executable file
View file

@ -0,0 +1,47 @@
#!/bin/bash
if [ -z "$1" ]; then
echo "No toNode supplied"
exit 1
fi
if [ -z "$2" ]; then
echo "No cmd supplied"
exit 1
fi
command=$2
IFS=',' read -r -a array <<<"$1"
function sendMessage() {
cat >msg.yaml <<EOF
[
{
"toNodes": ["${element}"],
"method": "REQCliCommand",
"methodArgs":
[
"/bin/bash",
"-c",
'echo "--------------------${element}----------------------" && ${command}',
],
"replyMethod": "REQToFileAppend",
"retryWait": 5,
"ACKTimeout": 10,
"retries": 1,
"replyACKTimeout": 10,
"replyRetries": 1,
"methodTimeout": 10,
"replyMethodTimeout": 10,
"directory": "./data/",
"fileName": "debug.log",
},
]
EOF
}
for element in "${array[@]}"; do
sendMessage element command
cp msg.yaml ./readfolder
done