commands.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/logger"
  9. "github.com/grafana/grafana/pkg/services/sqlstore"
  10. "github.com/grafana/grafana/pkg/setting"
  11. )
  12. func runDbCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
  13. return func(context *cli.Context) {
  14. cmd := &contextCommandLine{context}
  15. cfg := setting.NewCfg()
  16. cfg.Load(&setting.CommandLineArgs{
  17. Config: cmd.String("config"),
  18. HomePath: cmd.String("homepath"),
  19. Args: flag.Args(),
  20. })
  21. engine := &sqlstore.SqlStore{}
  22. engine.Cfg = cfg
  23. engine.Bus = bus.GetBus()
  24. engine.Init()
  25. if err := command(cmd); err != nil {
  26. logger.Errorf("\n%s: ", color.RedString("Error"))
  27. logger.Errorf("%s\n\n", err)
  28. cmd.ShowHelp()
  29. os.Exit(1)
  30. }
  31. logger.Info("\n\n")
  32. }
  33. }
  34. func runPluginCommand(command func(commandLine CommandLine) error) func(context *cli.Context) {
  35. return func(context *cli.Context) {
  36. cmd := &contextCommandLine{context}
  37. if err := command(cmd); err != nil {
  38. logger.Errorf("\n%s: ", color.RedString("Error"))
  39. logger.Errorf("%s %s\n\n", color.RedString("✗"), err)
  40. cmd.ShowHelp()
  41. os.Exit(1)
  42. }
  43. logger.Info("\nRestart grafana after installing plugins . <service grafana-server restart>\n\n")
  44. }
  45. }
  46. var pluginCommands = []cli.Command{
  47. {
  48. Name: "install",
  49. Usage: "install <plugin id> <plugin version (optional)>",
  50. Action: runPluginCommand(installCommand),
  51. }, {
  52. Name: "list-remote",
  53. Usage: "list remote available plugins",
  54. Action: runPluginCommand(listremoteCommand),
  55. }, {
  56. Name: "list-versions",
  57. Usage: "list-versions <plugin id>",
  58. Action: runPluginCommand(listversionsCommand),
  59. }, {
  60. Name: "update",
  61. Usage: "update <plugin id>",
  62. Aliases: []string{"upgrade"},
  63. Action: runPluginCommand(upgradeCommand),
  64. }, {
  65. Name: "update-all",
  66. Aliases: []string{"upgrade-all"},
  67. Usage: "update all your installed plugins",
  68. Action: runPluginCommand(upgradeAllCommand),
  69. }, {
  70. Name: "ls",
  71. Usage: "list all installed plugins",
  72. Action: runPluginCommand(lsCommand),
  73. }, {
  74. Name: "uninstall",
  75. Aliases: []string{"remove"},
  76. Usage: "uninstall <plugin id>",
  77. Action: runPluginCommand(removeCommand),
  78. },
  79. }
  80. var adminCommands = []cli.Command{
  81. {
  82. Name: "reset-admin-password",
  83. Usage: "reset-admin-password <new password>",
  84. Action: runDbCommand(resetPasswordCommand),
  85. Flags: []cli.Flag{
  86. cli.StringFlag{
  87. Name: "homepath",
  88. Usage: "path to grafana install/home path, defaults to working directory",
  89. },
  90. cli.StringFlag{
  91. Name: "config",
  92. Usage: "path to config file",
  93. },
  94. },
  95. },
  96. }
  97. var Commands = []cli.Command{
  98. {
  99. Name: "plugins",
  100. Usage: "Manage plugins for grafana",
  101. Subcommands: pluginCommands,
  102. },
  103. {
  104. Name: "admin",
  105. Usage: "Grafana admin commands",
  106. Subcommands: adminCommands,
  107. },
  108. }