From be781bdb98310455600872d2aa14f89c4dcee599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Mon, 24 Nov 2014 10:17:13 +0100 Subject: [PATCH] Tried postgres --- Dockerfile | 45 ++++++++++++++++++++++++ conf/grafana.ini | 13 ++++++- install_dependencies.sh | 2 -- pkg/middleware/middleware.go | 2 +- pkg/routes/api/api_account.go | 6 ++-- pkg/server/server.go | 29 --------------- pkg/stores/sqlstore/sqlstore.go | 2 +- pkg/stores/sqlstore/sqlstore_accounts.go | 12 +++---- start_dependencies.sh | 14 ++++---- 9 files changed, 76 insertions(+), 49 deletions(-) create mode 100644 Dockerfile delete mode 100644 install_dependencies.sh delete mode 100644 pkg/server/server.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000000..d2fdc855c24 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +FROM ubuntu + +# Add the PostgreSQL PGP key to verify their Debian packages. +# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc +RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 + +# Add PostgreSQL's repository. It contains the most recent stable release +# of PostgreSQL, ``9.3``. +RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list + +# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3 +# There are some warnings (in red) that show up during the build. You can hide +# them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive +RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 + +# Note: The official Debian and Ubuntu images automatically ``apt-get clean`` +# after each ``apt-get`` + +# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed`` +USER postgres + +# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and +# then create a database `docker` owned by the ``docker`` role. +# Note: here we use ``&&\`` to run commands one after the other - the ``\`` +# allows the RUN command to span multiple lines. +RUN /etc/init.d/postgresql start &&\ + psql --command "CREATE USER grafana WITH SUPERUSER PASSWORD 'grafana';" &&\ + createdb -O grafana grafana + +# Adjust PostgreSQL configuration so that remote connections to the +# database are possible. +RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf + +# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf`` +RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf + +# Expose the PostgreSQL port +EXPOSE 5432 + +# Add VOLUMEs to allow backup of config, logs and databases +VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] + +# Set the default command to run when starting the container +CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"] + diff --git a/conf/grafana.ini b/conf/grafana.ini index e38f094d39a..857a3d248f5 100644 --- a/conf/grafana.ini +++ b/conf/grafana.ini @@ -65,7 +65,18 @@ ssl_mode = disable ; For "sqlite3" only path = data/grafana.db - +; [database] +; ; Either "mysql", "postgres" or "sqlite3", it's your choice +; type = postgres +; host = 127.0.0.1:5432 +; name = grafana +; user = grafana +; password = grafana +; ; For "postgres" only, either "disable", "require" or "verify-full" +; ssl_mode = disable +; ; For "sqlite3" only +; path = data/grafana.db +; [log] root_path = ; Either "console", "file", "conn", "smtp" or "database", default is "console" diff --git a/install_dependencies.sh b/install_dependencies.sh deleted file mode 100644 index eafcee62380..00000000000 --- a/install_dependencies.sh +++ /dev/null @@ -1,2 +0,0 @@ -go get code.google.com/p/goprotobuf/{proto,protoc-gen-go} - diff --git a/pkg/middleware/middleware.go b/pkg/middleware/middleware.go index c519a4a1b68..a2a7d6ba01d 100644 --- a/pkg/middleware/middleware.go +++ b/pkg/middleware/middleware.go @@ -76,7 +76,7 @@ func (ctx *Context) JsonApiErr(status int, message string, err error) { resp["message"] = message } - ctx.HTML(status, "index") + ctx.JSON(status, resp) } func (ctx *Context) JsonBody(model interface{}) bool { diff --git a/pkg/routes/api/api_account.go b/pkg/routes/api/api_account.go index 8579fc0a4ce..a2300713dc0 100644 --- a/pkg/routes/api/api_account.go +++ b/pkg/routes/api/api_account.go @@ -40,12 +40,12 @@ func AddCollaborator(c *middleware.Context) { accountToAdd, err := models.GetAccountByLogin(model.Email) if err != nil { - c.JSON(404, utils.DynMap{"message": "Collaborator not found"}) + c.JsonApiErr(404, "Collaborator not found", nil) return } if accountToAdd.Id == c.UserAccount.Id { - c.JSON(400, utils.DynMap{"message": "Cannot add yourself as collaborator"}) + c.JsonApiErr(400, "Cannot add yourself as collaborator", nil) return } @@ -53,7 +53,7 @@ func AddCollaborator(c *middleware.Context) { err = models.AddCollaborator(collaborator) if err != nil { - c.JSON(400, utils.DynMap{"message": err.Error()}) + c.JsonApiErr(500, "Could not add collaborator", err) return } diff --git a/pkg/server/server.go b/pkg/server/server.go deleted file mode 100644 index 0a71ed44d41..00000000000 --- a/pkg/server/server.go +++ /dev/null @@ -1,29 +0,0 @@ -package server - -import ( - "github.com/torkelo/grafana-pro/pkg/api" - "github.com/torkelo/grafana-pro/pkg/configuration" - "github.com/torkelo/grafana-pro/pkg/stores" -) - -type Server struct { - HttpServer *api.HttpServer - Store stores.Store -} - -func NewServer(cfg *configuration.Cfg) (*Server, error) { - store := stores.New() - - httpServer := api.NewHttpServer(cfg, store) - - return &Server{ - HttpServer: httpServer, - Store: store, - }, nil -} - -func (self *Server) ListenAndServe() error { - self.HttpServer.ListenAndServe() - - return nil -} diff --git a/pkg/stores/sqlstore/sqlstore.go b/pkg/stores/sqlstore/sqlstore.go index e18db0f88c5..0e39aa5fb80 100644 --- a/pkg/stores/sqlstore/sqlstore.go +++ b/pkg/stores/sqlstore/sqlstore.go @@ -50,7 +50,7 @@ func LoadModelsConfig() { DbCfg.Name = setting.Cfg.MustValue("database", "name") DbCfg.User = setting.Cfg.MustValue("database", "user") if len(DbCfg.Pwd) == 0 { - DbCfg.Pwd = setting.Cfg.MustValue("database", "passwd") + DbCfg.Pwd = setting.Cfg.MustValue("database", "password") } DbCfg.SslMode = setting.Cfg.MustValue("database", "ssl_mode") DbCfg.Path = setting.Cfg.MustValue("database", "path", "data/grafana.db") diff --git a/pkg/stores/sqlstore/sqlstore_accounts.go b/pkg/stores/sqlstore/sqlstore_accounts.go index d7320455ce2..27f2b1d720e 100644 --- a/pkg/stores/sqlstore/sqlstore_accounts.go +++ b/pkg/stores/sqlstore/sqlstore_accounts.go @@ -61,9 +61,9 @@ func GetAccountByLogin(emailOrLogin string) (*models.Account, error) { func GetCollaboratorsForAccount(accountId int64) ([]*models.CollaboratorInfo, error) { collaborators := make([]*models.CollaboratorInfo, 0) - sess := x.Table("Collaborator") - sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id") - sess.Where("Collaborator.for_account_id=?", accountId) + sess := x.Table("collaborator") + sess.Join("INNER", "account", "account.id=collaborator.account_Id") + sess.Where("collaborator.for_account_id=?", accountId) err := sess.Find(&collaborators) return collaborators, err @@ -91,9 +91,9 @@ func AddCollaborator(collaborator *models.Collaborator) error { func GetOtherAccountsFor(accountId int64) ([]*models.OtherAccount, error) { collaborators := make([]*models.OtherAccount, 0) - sess := x.Table("Collaborator") - sess.Join("INNER", "Account", "Account.id=Collaborator.account_Id") - sess.Where("Collaborator.account_id=?", accountId) + sess := x.Table("collaborator") + sess.Join("INNER", "account", "account.id=collaborator.account_Id") + sess.Where("account_id=?", accountId) err := sess.Find(&collaborators) return collaborators, err } diff --git a/start_dependencies.sh b/start_dependencies.sh index e1117ab6977..8c7a428845c 100755 --- a/start_dependencies.sh +++ b/start_dependencies.sh @@ -1,7 +1,9 @@ -docker kill rethinkdb -docker rm rethinkdb +docker build -t gf-pro-image . -docker run -d -p 8180:8080 -p 28015:28015 -p 29015:29015 \ - --name rethinkdb \ - -v /var/docker/grafana-pro-rethinkdb:/data \ - dockerfile/rethinkdb +docker kill gf-pro +docker rm gf-pro + +docker run -d --name="gf-pro" \ + -p 127.0.0.1:5432:5432 \ + -v /var/docker/grafana-pro/postgresql:/var/lib/postgres \ + gf-pro-image