commands.go 3.5 KB

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