Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions internal/app/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (app *App) CliSwitch(switchFrom, switchTo string, waitTimeout time.Duration

switchover.From = fromHost
switchover.To = toHost
switchover.InitiatedBy = app.config.Hostname
switchover.InitiatedBy = util.GuessWhoRunning() + "@" + app.config.Hostname
switchover.InitiatedAt = time.Now()
switchover.Cause = CauseManual

Expand Down Expand Up @@ -360,7 +360,7 @@ func (app *App) CliEnableMaintenance(waitTimeout time.Duration, reason string) i
app.dcs.Initialize()

maintenance := &Maintenance{
InitiatedBy: app.config.Hostname,
InitiatedBy: util.GuessWhoRunning() + "@" + app.config.Hostname,
InitiatedAt: time.Now(),
Reason: reason,
}
Expand Down
41 changes: 41 additions & 0 deletions internal/util/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package util

import (
"os"
"slices"

"github.com/shirou/gopsutil/v3/process"
)

var notInformativeUsernames = []string{"root", "mysql"}

func GuessWhoRunning() string {
pid := os.Getppid()

p, err := process.NewProcess(int32(pid))
if err != nil {
return ""
}

for i := 0; i < 50; i++ {
if p == nil {
return "unknown_dolphin"
}

p, err = p.Parent()
if err != nil {
return "unknown_sakila"
}

// Known issue: cross-compiled builds by default uses CGO_ENABLED="0" (aka static builds)
// this may break user.LookupId() for LDAP/NIS users (user.UnknownUserError returned)
username, err := p.Username()
if err != nil {
return ""
}
if !slices.Contains(notInformativeUsernames, username) {
return username
}
}
return "unknown"
}
2 changes: 1 addition & 1 deletion tests/features/async_setting.feature
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ Feature: mysync async mode tests
"from": "mysql1",
"to": "",
"cause": "manual",
"initiated_by": "mysql1",
"initiated_by": "REGEXP:.*@mysql1",
"result": {
"ok": true
}
Expand Down
2 changes: 1 addition & 1 deletion tests/features/maintenance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Feature: maintenance mode
And zookeeper node "/test/maintenance" should match json
"""
{
"initiated_by": "mysql1"
"initiated_by": "REGEXP:.*@mysql1"
}
"""
When I run command on host "mysql1"
Expand Down
2 changes: 1 addition & 1 deletion tests/features/switchover_from.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Feature: manual switchover from old master
"from": "mysql1",
"to": "",
"cause": "manual",
"initiated_by": "mysql1",
"initiated_by": "REGEXP:.*@mysql1",
"result": {
"ok": false,
"error": "no quorum, have 0 replicas while 2 is required"
Expand Down
2 changes: 1 addition & 1 deletion tests/features/zk_maintenance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Feature: maintenance during dead zookeeper
Then zookeeper node "/test/maintenance" should match json within "90" seconds
"""
{
"initiated_by": "mysql1"
"initiated_by": "REGEXP:.*@mysql1"
}
"""
When I run command on host "mysql1" with timeout "30" seconds
Expand Down
10 changes: 9 additions & 1 deletion tests/testutil/matchers/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ func jsonContains(a, e interface{}, path []string) []string {
return path
}
case reflect.String:
if a.(string) != e.(string) {
_a := a.(string)
_e := e.(string)
prefix := "REGEXP:"
if strings.HasPrefix(_e, prefix) {
eReg := regexp.MustCompile(_e[len(prefix):])
if !eReg.MatchString(_a) {
return path
}
} else if _a != _e {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively - add jq as matcher... however it seems to be overkill for this task (And I am not sure that go-implementations supports match filter)

https://developer.zendesk.com/documentation/integration-services/developer-guide/jq-cheat-sheet/#extracting-a-pattern-from-a-string

return path
}
default:
Expand Down
Loading