commands.go 3.6 KB

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