diff --git a/main.go b/main.go index c48707fa6e5..287ba9aa348 100644 --- a/main.go +++ b/main.go @@ -39,8 +39,7 @@ func main() { app.Flags = append(app.Flags, []cli.Flag{ cli.StringFlag{ Name: "config", - Value: "grafana.ini", - Usage: "path to config file", + Usage: "path to grafana.ini config file", }, }...) app.Run(os.Args) diff --git a/pkg/cmd/accounts.go b/pkg/cmd/accounts.go index f2f8d27cacd..8758cb06e11 100644 --- a/pkg/cmd/accounts.go +++ b/pkg/cmd/accounts.go @@ -6,7 +6,6 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/services/sqlstore" "github.com/grafana/grafana/pkg/setting" "os" "text/tabwriter" @@ -34,9 +33,7 @@ var DeleteAccount = cli.Command{ } func listAccounts(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) accountsQuery := m.GetAccountsQuery{} if err := bus.Dispatch(&accountsQuery); err != nil { @@ -53,9 +50,7 @@ func listAccounts(c *cli.Context) { } func createAccount(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if !c.Args().Present() { log.ConsoleFatal("Account name arg is required") @@ -80,9 +75,7 @@ func createAccount(c *cli.Context) { } func deleteAccount(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if !c.Args().Present() { log.ConsoleFatal("Account name arg is required") diff --git a/pkg/cmd/common.go b/pkg/cmd/common.go new file mode 100644 index 00000000000..338baa36aac --- /dev/null +++ b/pkg/cmd/common.go @@ -0,0 +1,13 @@ +package cmd + +import ( + "github.com/codegangsta/cli" + "github.com/grafana/grafana/pkg/services/sqlstore" + "github.com/grafana/grafana/pkg/setting" +) + +func initRuntime(c *cli.Context) { + setting.NewConfigContext(c.GlobalString("config")) + sqlstore.NewEngine() + sqlstore.EnsureAdminUser() +} diff --git a/pkg/cmd/dashboard.go b/pkg/cmd/dashboard.go index a805e16eccb..eaae9700164 100644 --- a/pkg/cmd/dashboard.go +++ b/pkg/cmd/dashboard.go @@ -10,8 +10,6 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/services/sqlstore" - "github.com/grafana/grafana/pkg/setting" ) var ImportDashboard = cli.Command{ @@ -48,9 +46,7 @@ func runImport(c *cli.Context) { accountName := c.Args().First() - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) accountQuery := m.GetAccountByNameQuery{Name: accountName} if err := bus.Dispatch(&accountQuery); err != nil { diff --git a/pkg/cmd/datasource.go b/pkg/cmd/datasource.go index 7e292579227..bdd458e2b5d 100644 --- a/pkg/cmd/datasource.go +++ b/pkg/cmd/datasource.go @@ -6,8 +6,6 @@ import ( "github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/log" m "github.com/grafana/grafana/pkg/models" - "github.com/grafana/grafana/pkg/services/sqlstore" - "github.com/grafana/grafana/pkg/setting" "os" "text/tabwriter" ) @@ -69,9 +67,7 @@ var ( ) func createDataSource(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if len(c.Args()) != 3 { log.ConsoleFatal("Missing required arguments") @@ -131,9 +127,7 @@ func createDataSource(c *cli.Context) { } func listDatasources(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if !c.Args().Present() { log.ConsoleFatal("Account name arg is required") @@ -163,9 +157,7 @@ func listDatasources(c *cli.Context) { } func describeDataSource(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if len(c.Args()) != 2 { log.ConsoleFatal("Account and datasource name args are required") @@ -206,9 +198,7 @@ func describeDataSource(c *cli.Context) { } func deleteDataSource(c *cli.Context) { - setting.NewConfigContext() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() + initRuntime(c) if len(c.Args()) != 2 { log.ConsoleFatal("Account and datasource name args are required") diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go index 1207a3b20ef..b858072398c 100644 --- a/pkg/cmd/web.go +++ b/pkg/cmd/web.go @@ -17,7 +17,6 @@ import ( "github.com/grafana/grafana/pkg/log" "github.com/grafana/grafana/pkg/middleware" "github.com/grafana/grafana/pkg/services/eventpublisher" - "github.com/grafana/grafana/pkg/services/sqlstore" "github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/social" ) @@ -71,10 +70,9 @@ func runWeb(c *cli.Context) { log.Info("Starting Grafana") log.Info("Version: %v, Commit: %v, Build date: %v", setting.BuildVersion, setting.BuildCommit, time.Unix(setting.BuildStamp, 0)) - setting.NewConfigContext() + initRuntime(c) + social.NewOAuthService() - sqlstore.NewEngine() - sqlstore.EnsureAdminUser() eventpublisher.Init() var err error diff --git a/pkg/setting/setting.go b/pkg/setting/setting.go index 4d3ef9161c3..01e368abc6f 100644 --- a/pkg/setting/setting.go +++ b/pkg/setting/setting.go @@ -164,9 +164,13 @@ func loadEnvVariableOverrides() { } } -func NewConfigContext() { +func NewConfigContext(config string) { configFiles := findConfigFiles() + if config != "" { + configFiles = append(configFiles, config) + } + //log.Info("Loading config files: %v", configFiles) var err error diff --git a/pkg/setting/setting_test.go b/pkg/setting/setting_test.go index be031e3f4cc..253eb94f123 100644 --- a/pkg/setting/setting_test.go +++ b/pkg/setting/setting_test.go @@ -15,7 +15,7 @@ func TestLoadingSettings(t *testing.T) { Convey("Testing loading settings from ini file", t, func() { Convey("Given the default ini files", func() { - NewConfigContext() + NewConfigContext("") So(AppName, ShouldEqual, "Grafana") So(AdminUser, ShouldEqual, "admin") @@ -23,7 +23,7 @@ func TestLoadingSettings(t *testing.T) { Convey("Should be able to override via environment variables", func() { os.Setenv("GF_SECURITY_ADMIN_USER", "superduper") - NewConfigContext() + NewConfigContext("") So(AdminUser, ShouldEqual, "superduper") })