diff --git a/commands/management/add_mysql.go b/commands/management/add_mysql.go index 775ddb2a..baa066e6 100644 --- a/commands/management/add_mysql.go +++ b/commands/management/add_mysql.go @@ -93,6 +93,7 @@ type addMySQLCommand struct { ServiceName string Username string Password string + DefaultsFile string AgentPassword string CredentialsSource string Environment string @@ -126,9 +127,17 @@ func (cmd *addMySQLCommand) GetAddress() string { } func (cmd *addMySQLCommand) GetDefaultAddress() string { + if cmd.DefaultsFile != "" { + // address might be specified in defaults file + return "" + } return "127.0.0.1:3306" } +func (cmd *addMySQLCommand) GetDefaultUsername() string { + return "root" +} + func (cmd *addMySQLCommand) GetSocket() string { return cmd.Socket } @@ -193,6 +202,8 @@ func (cmd *addMySQLCommand) Run() (commands.Result, error) { return nil, err } + username := defaultsFileUsernameCheck(cmd) + tablestatsGroupTableLimit := int32(cmd.DisableTablestatsLimit) if cmd.DisableTablestats { if tablestatsGroupTableLimit != 0 { @@ -219,8 +230,9 @@ func (cmd *addMySQLCommand) Run() (commands.Result, error) { Environment: cmd.Environment, Cluster: cmd.Cluster, ReplicationSet: cmd.ReplicationSet, - Username: cmd.Username, + Username: username, Password: cmd.Password, + DefaultsFile: cmd.DefaultsFile, AgentPassword: cmd.AgentPassword, CustomLabels: customLabels, @@ -272,8 +284,9 @@ func init() { AddMySQLC.Flag("node-id", "Node ID (default is autodetected)").StringVar(&AddMySQL.NodeID) AddMySQLC.Flag("pmm-agent-id", "The pmm-agent identifier which runs this instance (default is autodetected)").StringVar(&AddMySQL.PMMAgentID) - AddMySQLC.Flag("username", "MySQL username").Default("root").StringVar(&AddMySQL.Username) + AddMySQLC.Flag("username", "MySQL username").StringVar(&AddMySQL.Username) AddMySQLC.Flag("password", "MySQL password").StringVar(&AddMySQL.Password) + AddMySQLC.Flag("defaults-file", "Path to defaults file").StringVar(&AddMySQL.DefaultsFile) AddMySQLC.Flag("agent-password", "Custom password for /metrics endpoint").StringVar(&AddMySQL.AgentPassword) AddMySQLC.Flag("credentials-source", "Credentials provider").ExistingFileVar(&AddMySQL.CredentialsSource) @@ -306,3 +319,17 @@ func init() { AddMySQLC.Flag("disable-collectors", "Comma-separated list of collector names to exclude from exporter").StringVar(&AddMySQL.DisableCollectors) addGlobalFlags(AddMySQLC) } + +func defaultsFileUsernameCheck(cmd *addMySQLCommand) string { + // defaults file specified, but passed username has higher priority + if cmd.Username != "" && cmd.DefaultsFile != "" { + return cmd.Username + } + + // username not specified, but can be in defaults files + if cmd.Username == "" && cmd.DefaultsFile != "" { + return "" + } + + return cmd.GetDefaultUsername() +} diff --git a/go.mod b/go.mod index ed705566..a5663d5a 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/AlekSi/pointer v1.2.0 github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d github.com/go-openapi/runtime v0.24.0 - github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8 + github.com/percona/pmm v0.0.0-20220526203331-cb61205581dc github.com/pkg/errors v0.9.1 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.1 diff --git a/go.sum b/go.sum index d06ecef8..7a061568 100644 --- a/go.sum +++ b/go.sum @@ -113,8 +113,8 @@ github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= github.com/pelletier/go-toml v1.7.0/go.mod h1:vwGMzjaWMwyfHwgIBhI2YUM4fB6nL6lVAvS1LBMMhTE= -github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8 h1:P5iuV4GRUIviRg/5/FM6ZOKdiBPdwUPbrHld/epM3hk= -github.com/percona/pmm v0.0.0-20220520150831-23069cdf1bb8/go.mod h1:gr+WLd8clEAe2xMFgsGhpw9ziZc2UCWcfy6d3M6Aq00= +github.com/percona/pmm v0.0.0-20220526203331-cb61205581dc h1:J8ZbKkqhlbx4oGeQmUnoZzVJVh/XPcENudq/ovx0dlI= +github.com/percona/pmm v0.0.0-20220526203331-cb61205581dc/go.mod h1:gr+WLd8clEAe2xMFgsGhpw9ziZc2UCWcfy6d3M6Aq00= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=