import.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: "import-json",
  16. Usage: "grafana import",
  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. cli.StringFlag{
  29. Name: "config",
  30. Value: "grafana.ini",
  31. Usage: "path to config file",
  32. },
  33. },
  34. }
  35. func runImport(c *cli.Context) {
  36. dir := c.String("dir")
  37. if len(dir) == 0 {
  38. log.Error(3, "Missing command flag --dir")
  39. return
  40. }
  41. file, err := os.Stat(dir)
  42. if os.IsNotExist(err) {
  43. log.Error(3, "Directory does not exist: %v", dir)
  44. return
  45. }
  46. if !file.IsDir() {
  47. log.Error(3, "%v is not a directory", dir)
  48. return
  49. }
  50. accountName := c.String("account")
  51. if len(accountName) == 0 {
  52. log.Error(3, "Missing command flag --account")
  53. return
  54. }
  55. setting.NewConfigContext()
  56. sqlstore.NewEngine()
  57. sqlstore.EnsureAdminUser()
  58. accountQuery := m.GetAccountByNameQuery{Name: accountName}
  59. if err := bus.Dispatch(&accountQuery); err != nil {
  60. log.Error(3, "Failed to find account", err)
  61. return
  62. }
  63. accountId := accountQuery.Result.Id
  64. visitor := func(path string, f os.FileInfo, err error) error {
  65. if err != nil {
  66. return err
  67. }
  68. if f.IsDir() {
  69. return nil
  70. }
  71. if strings.HasSuffix(f.Name(), ".json") {
  72. if err := importDashboard(path, accountId); err != nil {
  73. log.Error(3, "Failed to import dashboard file: %v, err: %v", path, err)
  74. }
  75. }
  76. return nil
  77. }
  78. if err := filepath.Walk(dir, visitor); err != nil {
  79. log.Error(3, "failed to scan dir for json files: %v", err)
  80. }
  81. }
  82. func importDashboard(path string, accountId int64) error {
  83. log.Info("Importing %v", path)
  84. reader, err := os.Open(path)
  85. if err != nil {
  86. return err
  87. }
  88. dash := m.NewDashboard("temp")
  89. jsonParser := json.NewDecoder(reader)
  90. if err := jsonParser.Decode(&dash.Data); err != nil {
  91. return err
  92. }
  93. dash.Data["id"] = nil
  94. cmd := m.SaveDashboardCommand{
  95. AccountId: accountId,
  96. Dashboard: dash.Data,
  97. }
  98. if err := bus.Dispatch(&cmd); err != nil {
  99. return err
  100. }
  101. return nil
  102. }