dashboard.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package cmd
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/codegangsta/cli"
  8. "github.com/grafana/grafana/pkg/bus"
  9. "github.com/grafana/grafana/pkg/log"
  10. m "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/services/sqlstore"
  12. "github.com/grafana/grafana/pkg/setting"
  13. )
  14. var ImportJson = cli.Command{
  15. Name: "dashboard:import",
  16. Usage: "imports dashboards in JSON from a directory",
  17. Description: "Starts Grafana import process",
  18. Action: runImport,
  19. Flags: []cli.Flag{
  20. cli.StringFlag{
  21. Name: "dir",
  22. Usage: "path to folder containing json dashboards",
  23. },
  24. cli.StringFlag{
  25. Name: "account",
  26. Usage: "Account name to save dashboards under",
  27. },
  28. },
  29. }
  30. func runImport(c *cli.Context) {
  31. dir := c.String("dir")
  32. if len(dir) == 0 {
  33. log.Error(3, "Missing command flag --dir")
  34. return
  35. }
  36. file, err := os.Stat(dir)
  37. if os.IsNotExist(err) {
  38. log.Error(3, "Directory does not exist: %v", dir)
  39. return
  40. }
  41. if !file.IsDir() {
  42. log.Error(3, "%v is not a directory", dir)
  43. return
  44. }
  45. accountName := c.String("account")
  46. if len(accountName) == 0 {
  47. log.Error(3, "Missing command flag --account")
  48. return
  49. }
  50. setting.NewConfigContext()
  51. sqlstore.NewEngine()
  52. sqlstore.EnsureAdminUser()
  53. accountQuery := m.GetAccountByNameQuery{Name: accountName}
  54. if err := bus.Dispatch(&accountQuery); err != nil {
  55. log.Error(3, "Failed to find account", err)
  56. return
  57. }
  58. accountId := accountQuery.Result.Id
  59. visitor := func(path string, f os.FileInfo, err error) error {
  60. if err != nil {
  61. return err
  62. }
  63. if f.IsDir() {
  64. return nil
  65. }
  66. if strings.HasSuffix(f.Name(), ".json") {
  67. if err := importDashboard(path, accountId); err != nil {
  68. log.Error(3, "Failed to import dashboard file: %v, err: %v", path, err)
  69. }
  70. }
  71. return nil
  72. }
  73. if err := filepath.Walk(dir, visitor); err != nil {
  74. log.Error(3, "failed to scan dir for json files: %v", err)
  75. }
  76. }
  77. func importDashboard(path string, accountId int64) error {
  78. log.Info("Importing %v", path)
  79. reader, err := os.Open(path)
  80. if err != nil {
  81. return err
  82. }
  83. dash := m.NewDashboard("temp")
  84. jsonParser := json.NewDecoder(reader)
  85. if err := jsonParser.Decode(&dash.Data); err != nil {
  86. return err
  87. }
  88. dash.Data["id"] = nil
  89. cmd := m.SaveDashboardCommand{
  90. AccountId: accountId,
  91. Dashboard: dash.Data,
  92. }
  93. if err := bus.Dispatch(&cmd); err != nil {
  94. return err
  95. }
  96. return nil
  97. }