commands.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package commands
  2. import (
  3. "os"
  4. "strings"
  5. "github.com/codegangsta/cli"
  6. "github.com/fatih/color"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/cmd/grafana-cli/commands/datamigrations"
  9. "github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
  10. "github.com/grafana/grafana/pkg/cmd/grafana-cli/utils"
  11. "github.com/grafana/grafana/pkg/services/sqlstore"
  12. "github.com/grafana/grafana/pkg/setting"
  13. )
  14. func runDbCommand(command func(commandLine utils.CommandLine, sqlStore *sqlstore.SqlStore) error) func(context *cli.Context) {
  15. return func(context *cli.Context) {
  16. cmd := &utils.ContextCommandLine{Context: context}
  17. debug := cmd.GlobalBool("debug")
  18. cfg := setting.NewCfg()
  19. configOptions := strings.Split(cmd.GlobalString("configOverrides"), " ")
  20. cfg.Load(&setting.CommandLineArgs{
  21. Config: cmd.ConfigFile(),
  22. HomePath: cmd.HomePath(),
  23. Args: append(configOptions, cmd.Args()...), // tailing arguments have precedence over the options string
  24. })
  25. if debug {
  26. cfg.LogConfigSources()
  27. }
  28. engine := &sqlstore.SqlStore{}
  29. engine.Cfg = cfg
  30. engine.Bus = bus.GetBus()
  31. engine.Init()
  32. if err := command(cmd, engine); err != nil {
  33. logger.Errorf("\n%s: ", color.RedString("Error"))
  34. logger.Errorf("%s\n\n", err)
  35. cmd.ShowHelp()
  36. os.Exit(1)
  37. }
  38. logger.Info("\n\n")
  39. }
  40. }
  41. func runPluginCommand(command func(commandLine utils.CommandLine) error) func(context *cli.Context) {
  42. return func(context *cli.Context) {
  43. cmd := &utils.ContextCommandLine{Context: context}
  44. if err := command(cmd); err != nil {
  45. logger.Errorf("\n%s: ", color.RedString("Error"))
  46. logger.Errorf("%s %s\n\n", color.RedString("✗"), err)
  47. cmd.ShowHelp()
  48. os.Exit(1)
  49. }
  50. logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
  51. }
  52. }
  53. var pluginCommands = []cli.Command{
  54. {
  55. Name: "install",
  56. Usage: "install <plugin id> <plugin version (optional)>",
  57. Action: runPluginCommand(installCommand),
  58. }, {
  59. Name: "list-remote",
  60. Usage: "list remote available plugins",
  61. Action: runPluginCommand(listremoteCommand),
  62. }, {
  63. Name: "list-versions",
  64. Usage: "list-versions <plugin id>",
  65. Action: runPluginCommand(listversionsCommand),
  66. }, {
  67. Name: "update",
  68. Usage: "update <plugin id>",
  69. Aliases: []string{"upgrade"},
  70. Action: runPluginCommand(upgradeCommand),
  71. }, {
  72. Name: "update-all",
  73. Aliases: []string{"upgrade-all"},
  74. Usage: "update all your installed plugins",
  75. Action: runPluginCommand(upgradeAllCommand),
  76. }, {
  77. Name: "ls",
  78. Usage: "list all installed plugins",
  79. Action: runPluginCommand(lsCommand),
  80. }, {
  81. Name: "uninstall",
  82. Aliases: []string{"remove"},
  83. Usage: "uninstall <plugin id>",
  84. Action: runPluginCommand(removeCommand),
  85. },
  86. }
  87. var adminCommands = []cli.Command{
  88. {
  89. Name: "reset-admin-password",
  90. Usage: "reset-admin-password <new password>",
  91. Action: runDbCommand(resetPasswordCommand),
  92. },
  93. {
  94. Name: "data-migration",
  95. Usage: "Runs a script that migrates or cleanups data in your db",
  96. Subcommands: []cli.Command{
  97. {
  98. Name: "encrypt-datasource-passwords",
  99. Usage: "Migrates passwords from unsecured fields to secure_json_data field. Return ok unless there is an error. Safe to execute multiple times.",
  100. Action: runDbCommand(datamigrations.EncryptDatasourcePaswords),
  101. },
  102. },
  103. },
  104. }
  105. var Commands = []cli.Command{
  106. {
  107. Name: "plugins",
  108. Usage: "Manage plugins for grafana",
  109. Subcommands: pluginCommands,
  110. },
  111. {
  112. Name: "admin",
  113. Usage: "Grafana admin commands",
  114. Subcommands: adminCommands,
  115. },
  116. }