Skip to content

Commit

Permalink
Merge pull request #82 from tongson/ansible_changed
Browse files Browse the repository at this point in the history
Support logging Ansible changed status
  • Loading branch information
tongson authored Oct 2, 2024
2 parents b6f9f06 + 1e45ec4 commit 9017054
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 21 deletions.
1 change: 1 addition & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const cHOSTS1 = "ssh_config"
const cHOSTS2 = "HOSTS"
const cLOG = "LOG"
const cREPAIRED = "__REPAIRED__"
const cCHANGED = "changed=true"
const cRUN = "script"
const cPRE = "script.pre"
const cPOST = "script.post"
Expand Down
53 changes: 32 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,23 @@ func generateHashID() string {
return string([]rune(uid)[:8])
}

func customResult(stdout *bufio.Scanner, stderr *bufio.Scanner) string {
var result string
for stderr.Scan() {
if strings.Contains(stderr.Text(), cREPAIRED) {
result = "repaired"
}
}
// It's unlikely we are gonna look for cCHANGED and cREPAIRED at the same time.
// cCHANGED (Ansible output) has priority.
for stdout.Scan() {
if strings.Contains(stdout.Text(), cCHANGED) {
result = "changed"
}
}
return result
}

func main() {
runtime.MemProfileRate = 0

Expand Down Expand Up @@ -1052,13 +1069,11 @@ func main() {
failed = true
failedLogPrint(scr, opt, out)
} else {
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
if strings.Contains(scanner.Text(), cREPAIRED) {
result = "repaired"
}
}
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
scanner_err.Split(bufio.ScanWords)
scanner_out.Split(bufio.ScanWords)
result = customResult(scanner_out, scanner_err)
okLogPrint(scr, opt, out)
}
} else if _, err := strconv.ParseInt(hostname, 10, 64); err == nil {
Expand Down Expand Up @@ -1116,13 +1131,11 @@ func main() {
failed = true
failedLogPrint(scr, opt, out)
} else {
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
if strings.Contains(scanner.Text(), cREPAIRED) {
result = "repaired"
}
}
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
scanner_err.Split(bufio.ScanWords)
scanner_out.Split(bufio.ScanWords)
result = customResult(scanner_out, scanner_err)
okLogPrint(scr, opt, out)
}
} else {
Expand Down Expand Up @@ -1219,13 +1232,11 @@ func main() {
failed = true
failedLogPrint(scr, opt, out)
} else {
scanner := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
if strings.Contains(scanner.Text(), cREPAIRED) {
result = "repaired"
}
}
scanner_err := bufio.NewScanner(strings.NewReader(out.Stderr))
scanner_out := bufio.NewScanner(strings.NewReader(out.Stdout))
scanner_err.Split(bufio.ScanWords)
scanner_out.Split(bufio.ScanWords)
result = customResult(scanner_out, scanner_err)
okLogPrint(scr, opt, out)
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/changed/linebreak/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
printf "ok\\n"
printf "\\n\\nchanged=true\\n"
1 change: 1 addition & 0 deletions test/changed/linebreak/shell
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bash
2 changes: 2 additions & 0 deletions test/changed/nolinebreak/script
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
printf "ok\\n"
printf "changed=true"
1 change: 1 addition & 0 deletions test/changed/nolinebreak/shell
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bash
22 changes: 22 additions & 0 deletions test/rr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,28 @@ func TestRepaired(T *testing.T) {
})
}

func TestChanged(T *testing.T) {
T.Parallel()
T.Run("changed1", func(t *testing.T) {
rr := RunArg{Exe: cEXE, Args: []string{"changed:nolinebreak"}}
if ret, _ := rr.Run(); !ret {
t.Error("wants `true`")
}
if got := strings.Contains(FileRead("LOG"), "\"msg\":\"changed\""); !got {
t.Error("wants `true`")
}
})
T.Run("changed2", func(t *testing.T) {
rr := RunArg{Exe: cEXE, Args: []string{"changed:linebreak"}}
if ret, _ := rr.Run(); !ret {
t.Error("wants `true`")
}
if got := strings.Contains(FileRead("LOG"), "\"msg\":\"changed\""); !got {
t.Error("wants `true`")
}
})
}

func TestArgs(T *testing.T) {
T.Parallel()
T.Run("args1", func(t *testing.T) {
Expand Down

0 comments on commit 9017054

Please sign in to comment.