| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | package cmd | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"os" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/codegangsta/cli" | 
					
						
							| 
									
										
										
										
											2015-02-05 17:37:13 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/bus" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/log" | 
					
						
							|  |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 05:00:06 +08:00
										 |  |  | var ImportDashboard = cli.Command{ | 
					
						
							| 
									
										
										
										
											2015-02-16 14:40:21 +08:00
										 |  |  | 	Name:        "dashboards:import", | 
					
						
							| 
									
										
										
										
											2015-02-16 04:45:25 +08:00
										 |  |  | 	Usage:       "imports dashboards in JSON from a directory", | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	Description: "Starts Grafana import process", | 
					
						
							|  |  |  | 	Action:      runImport, | 
					
						
							|  |  |  | 	Flags: []cli.Flag{ | 
					
						
							|  |  |  | 		cli.StringFlag{ | 
					
						
							|  |  |  | 			Name:  "dir", | 
					
						
							|  |  |  | 			Usage: "path to folder containing json dashboards", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func runImport(c *cli.Context) { | 
					
						
							|  |  |  | 	dir := c.String("dir") | 
					
						
							|  |  |  | 	if len(dir) == 0 { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 		log.ConsoleFatalf("Missing command flag --dir") | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	file, err := os.Stat(dir) | 
					
						
							|  |  |  | 	if os.IsNotExist(err) { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 		log.ConsoleFatalf("Directory does not exist: %v", dir) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !file.IsDir() { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 		log.ConsoleFatalf("%v is not a directory", dir) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 04:48:36 +08:00
										 |  |  | 	if !c.Args().Present() { | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 		log.ConsoleFatal("Organization name arg is required") | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	orgName := c.Args().First() | 
					
						
							| 
									
										
										
										
											2015-02-16 04:48:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-16 05:57:49 +08:00
										 |  |  | 	initRuntime(c) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	orgQuery := m.GetOrgByNameQuery{Name: orgName} | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&orgQuery); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 		log.ConsoleFatalf("Failed to find account", err) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 	orgId := orgQuery.Result.Id | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	visitor := func(path string, f os.FileInfo, err error) error { | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if f.IsDir() { | 
					
						
							|  |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if strings.HasSuffix(f.Name(), ".json") { | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 			if err := importDashboard(path, orgId); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 				log.ConsoleFatalf("Failed to import dashboard file: %v,  err: %v", path, err) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := filepath.Walk(dir, visitor); err != nil { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 		log.ConsoleFatalf("Failed to scan dir for json files: %v", err) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | func importDashboard(path string, orgId int64) error { | 
					
						
							| 
									
										
										
										
											2015-02-16 04:51:41 +08:00
										 |  |  | 	log.ConsoleInfof("Importing %v", path) | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	reader, err := os.Open(path) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-27 20:45:00 +08:00
										 |  |  | 	defer reader.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 	dash := m.NewDashboard("temp") | 
					
						
							|  |  |  | 	jsonParser := json.NewDecoder(reader) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := jsonParser.Decode(&dash.Data); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-12 06:17:40 +08:00
										 |  |  | 	dash.Data["id"] = nil | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cmd := m.SaveDashboardCommand{ | 
					
						
							| 
									
										
										
										
											2015-02-24 03:07:49 +08:00
										 |  |  | 		OrgId:     orgId, | 
					
						
							| 
									
										
										
										
											2015-01-29 21:33:50 +08:00
										 |  |  | 		Dashboard: dash.Data, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := bus.Dispatch(&cmd); err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |