From 6ec1d16327c9514e62b0c9c2ca93f6fa3b2aade0 Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 20 Apr 2017 13:26:36 +0200 Subject: [PATCH] fix: cli admin reset-password fixes cmd args Fixes the homepath and config command line args. This allows the command to be used even when the homepath is different from the default. Fixes #7730 --- docs/sources/administration/cli.md | 20 +++++++++++++++++++- pkg/cmd/grafana-cli/commands/commands.go | 20 +++++++++++++------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/docs/sources/administration/cli.md b/docs/sources/administration/cli.md index 8c7755506e8..4578f70ffd6 100644 --- a/docs/sources/administration/cli.md +++ b/docs/sources/administration/cli.md @@ -27,6 +27,24 @@ To show all admin commands: ### Reset admin password -You can reset the password for the admin user using the CLI. +You can reset the password for the admin user using the CLI. The use case for this command is when you have lost the admin password. `grafana-cli admin reset-admin-password ...` + +If running the command returns this error: + +> Could not find config defaults, make sure homepath command line parameter is set or working directory is homepath + +then there are two flags that can be used to set homepath and the config file path. + +`grafana-cli admin reset-admin-password --homepath "/usr/share/grafana" newpass` + +If you have not lost the admin password then it is better to set in the Grafana UI. If you need to set the password in a script then the [Grafana API]({{< relref "http_api/user/#change-password" >}}) can be used. Here is an example with curl using basic auth: + +``` +curl -X PUT -H "Content-Type: application/json" -d '{ + "oldPassword": "admin", + "newPassword": "newpass", + "confirmNew": "newpass" +}' http://admin:admin@:3000/api/user/password +``` diff --git a/pkg/cmd/grafana-cli/commands/commands.go b/pkg/cmd/grafana-cli/commands/commands.go index 8b2ecfcf7f5..d8f01bbdcab 100644 --- a/pkg/cmd/grafana-cli/commands/commands.go +++ b/pkg/cmd/grafana-cli/commands/commands.go @@ -11,22 +11,18 @@ import ( "github.com/grafana/grafana/pkg/setting" ) -var configFile = flag.String("config", "", "path to config file") -var homePath = flag.String("homepath", "", "path to grafana install/home path, defaults to working directory") - func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) { return func(context *cli.Context) { + cmd := &contextCommandLine{context} - flag.Parse() setting.NewConfigContext(&setting.CommandLineArgs{ - Config: *configFile, - HomePath: *homePath, + Config: cmd.String("config"), + HomePath: cmd.String("homepath"), Args: flag.Args(), }) sqlstore.NewEngine() - cmd := &contextCommandLine{context} if err := command(cmd); err != nil { logger.Errorf("\n%s: ", color.RedString("Error")) logger.Errorf("%s\n\n", err) @@ -95,6 +91,16 @@ var adminCommands = []cli.Command{ Name: "reset-admin-password", Usage: "reset-admin-password ", Action: runDbCommand(resetPasswordCommand), + Flags: []cli.Flag{ + cli.StringFlag{ + Name: "homepath", + Usage: "path to grafana install/home path, defaults to working directory", + }, + cli.StringFlag{ + Name: "config", + Usage: "path to config file", + }, + }, }, }