Update to use TOML config

This commit is contained in:
Matt Krump 2017-11-02 17:37:27 -05:00 committed by Eric Meyer
parent 0262a99321
commit e9efc2ff82
10 changed files with 201 additions and 60 deletions

View File

@ -6,7 +6,7 @@ import (
"fmt"
"github.com/8thlight/vulcanizedb/blockchain_listener"
cfg "github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/geth"
"github.com/8thlight/vulcanizedb/observers"
@ -14,18 +14,19 @@ import (
do "gopkg.in/godo.v2"
)
func parseIpcPath(context *do.Context) string {
ipcPath := context.Args.MayString("", "ipc-path", "i")
if ipcPath == "" {
log.Fatalln("--ipc-path required")
func parseEnvironment(context *do.Context) string {
environment := context.Args.MayString("", "environment", "env", "e")
if environment == "" {
log.Fatalln("--environment required")
}
return ipcPath
return environment
}
func startBlockchainListener(config cfg.Config, ipcPath string) {
blockchain := geth.NewGethBlockchain(ipcPath)
func startBlockchainListener(cfg config.Config) {
fmt.Println("Client Path ", cfg.Client.IPCPath)
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
loggingObserver := observers.BlockchainLoggingObserver{}
connectString := cfg.DbConnectionString(cfg.Public().Database)
connectString := config.DbConnectionString(cfg.Database)
db, err := sqlx.Connect("postgres", connectString)
if err != nil {
log.Fatalf("Error connecting to DB: %v\n", err)
@ -40,36 +41,30 @@ func startBlockchainListener(config cfg.Config, ipcPath string) {
func tasks(p *do.Project) {
p.Task("runPublic", nil, func(context *do.Context) {
startBlockchainListener(cfg.Public(), parseIpcPath(context))
p.Task("run", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
startBlockchainListener(cfg)
})
p.Task("runPrivate", nil, func(context *do.Context) {
startBlockchainListener(cfg.Private(), parseIpcPath(context))
p.Task("migrate", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Database.Name)
context.Bash(migrate)
context.Bash(dumpSchema)
})
p.Task("migratePublic", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Public().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString))
context.Bash(fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Public().Database.Name))
})
p.Task("migratePrivate", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Private().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations up", connectString))
context.Bash(fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Private().Database.Name))
})
p.Task("rollbackPublic", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Public().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString))
context.Bash("pg_dump -O -s vulcanize_public > migrations/schema.sql")
})
p.Task("rollbackPrivate", nil, func(context *do.Context) {
connectString := cfg.DbConnectionString(cfg.Private().Database)
context.Bash(fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString))
context.Bash("pg_dump -O -s vulcanize_private > migrations/schema.sql")
p.Task("rollback", nil, func(context *do.Context) {
environment := parseEnvironment(context)
cfg := config.NewConfig(environment)
connectString := config.DbConnectionString(cfg.Database)
migrate := fmt.Sprintf("migrate -database '%s' -path ./migrations down 1", connectString)
dumpSchema := fmt.Sprintf("pg_dump -O -s %s > migrations/schema.sql", cfg.Database.Name)
context.Bash(migrate)
context.Bash(dumpSchema)
})
}

76
Gopkg.lock generated
View File

@ -1,6 +1,18 @@
# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'.
[[projects]]
name = "github.com/BurntSushi/toml"
packages = ["."]
revision = "b26d9c308763d68093482582cea63d69be07a0f0"
version = "v0.3.0"
[[projects]]
branch = "master"
name = "github.com/MichaelTJones/walk"
packages = ["."]
revision = "4748e29d5718c2df4028a6543edf86fd8cc0f881"
[[projects]]
branch = "master"
name = "github.com/btcsuite/btcd"
@ -19,6 +31,12 @@
revision = "817915b46b97fd7bb80e8ab6b69f01a53ac3eebf"
version = "v1.6.0"
[[projects]]
branch = "master"
name = "github.com/howeyc/gopass"
packages = ["."]
revision = "bf9dde6d0d2c004a008c27aaee91170c786f6db8"
[[projects]]
branch = "master"
name = "github.com/jmoiron/sqlx"
@ -31,6 +49,48 @@
packages = [".","oid"]
revision = "b609790bd85edf8e9ab7e0f8912750a786177bcf"
[[projects]]
name = "github.com/mattn/go-colorable"
packages = ["."]
revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072"
version = "v0.0.9"
[[projects]]
name = "github.com/mattn/go-isatty"
packages = ["."]
revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39"
version = "v0.0.3"
[[projects]]
branch = "master"
name = "github.com/mgutz/ansi"
packages = ["."]
revision = "9520e82c474b0a04dd04f8a40959027271bab992"
[[projects]]
branch = "master"
name = "github.com/mgutz/minimist"
packages = ["."]
revision = "39eb8cf573ca29344bd7d7e6ba4d7febdebd37a9"
[[projects]]
name = "github.com/mgutz/str"
packages = ["."]
revision = "968bf66e3da857419e4f6e71b2d5c9ae95682dc4"
version = "v1.2.0"
[[projects]]
name = "github.com/mgutz/to"
packages = ["."]
revision = "00c06406c2dd2e011f153a6502a21473676db33f"
version = "v1.0.0"
[[projects]]
name = "github.com/nozzle/throttler"
packages = ["."]
revision = "d9b45f19996c645d38c9266d1f5cf1990e930119"
version = "v1.0"
[[projects]]
name = "github.com/onsi/ginkgo"
packages = [".","config","internal/codelocation","internal/containernode","internal/failer","internal/leafnodes","internal/remote","internal/spec","internal/spec_iterator","internal/specrunner","internal/suite","internal/testingtproxy","internal/writer","reporters","reporters/stenographer","reporters/stenographer/support/go-colorable","reporters/stenographer/support/go-isatty","types"]
@ -55,6 +115,12 @@
revision = "7af7a1e09ba336d2ea14b1ce73bf693c6837dbf6"
version = "v1.2"
[[projects]]
branch = "master"
name = "golang.org/x/crypto"
packages = ["ssh/terminal"]
revision = "bd6f299fb381e4c3393d1c4b1f0b94f5e77650c8"
[[projects]]
branch = "master"
name = "golang.org/x/net"
@ -64,7 +130,7 @@
[[projects]]
branch = "master"
name = "golang.org/x/sys"
packages = ["unix"]
packages = ["unix","windows"]
revision = "8dbc5d05d6edcc104950cc299a1ce6641235bc86"
[[projects]]
@ -79,6 +145,12 @@
revision = "57907de300222151a123d29255ed17f5ed43fad3"
version = "v0.1.0"
[[projects]]
name = "gopkg.in/godo.v2"
packages = [".","glob","util","watcher","watcher/fswatch"]
revision = "b5fd2f0bef1ebe832e628cfad18ab1cc707f65a1"
version = "v2.0.9"
[[projects]]
branch = "v2"
name = "gopkg.in/karalabe/cookiejar.v2"
@ -100,6 +172,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "3c8b6348d0670c73eb8ad2a42eccd107b996873ac4477e9c82d5be5d95243bfe"
inputs-digest = "f9569275c3b9863e5209029135f13d3d9df9a697b49e75b5cbd37942b23e4f3b"
solver-name = "gps-cdcl"
solver-version = 1

5
config/client.go Normal file
View File

@ -0,0 +1,5 @@
package config
type Client struct {
IPCPath string
}

View File

@ -1,5 +1,46 @@
package config
import (
"log"
"os"
"fmt"
"path/filepath"
"path"
"runtime"
"github.com/BurntSushi/toml"
)
type Config struct {
Database Database
Client Client
}
func NewConfig(environment string) Config {
filenameWithExtension := fmt.Sprintf("%s.toml", environment)
absolutePath := filepath.Join(ProjectRoot(), "config", "environments", filenameWithExtension)
config := parseConfigFile(absolutePath)
config.Client.IPCPath = filepath.Join(ProjectRoot(), config.Client.IPCPath)
return config
}
func ProjectRoot() string {
var _, filename, _, _ = runtime.Caller(0)
return path.Join(path.Dir(filename), "../")
}
func parseConfigFile(configfile string) Config {
var cfg Config
_, err := os.Stat(configfile)
if err != nil {
log.Fatal("Config file is missing: ", configfile)
}
if _, err := toml.DecodeFile(configfile, &cfg); err != nil {
log.Fatal(err)
}
return cfg
}

View File

@ -0,0 +1,13 @@
package config_test
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"testing"
)
func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Config Suite")
}

23
config/config_test.go Normal file
View File

@ -0,0 +1,23 @@
package config_test
import (
"path/filepath"
"github.com/8thlight/vulcanizedb/config"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Loading the config", func() {
It("reads the private config using the environment", func() {
privateConfig := config.NewConfig("private")
Expect(privateConfig.Database.Hostname).To(Equal("localhost"))
Expect(privateConfig.Database.Name).To(Equal("vulcanize_private"))
Expect(privateConfig.Database.Port).To(Equal(5432))
expandedPath := filepath.Join(config.ProjectRoot(), "test_data_dir/geth.ipc")
Expect(privateConfig.Client.IPCPath).To(Equal(expandedPath))
})
})

View File

@ -0,0 +1,7 @@
[database]
name = "vulcanize_private"
hostname = "localhost"
port = 5432
[client]
ipcPath = "test_data_dir/geth.ipc"

View File

@ -1,11 +0,0 @@
package config
func Private() Config {
return Config{
Database: Database{
Name: "vulcanize_private",
Hostname: "localhost",
Port: 5432,
},
}
}

View File

@ -1,11 +0,0 @@
package config
func Public() Config {
return Config{
Database: Database{
Name: "vulcanize_public",
Hostname: "localhost",
Port: 5432,
},
}
}

View File

@ -1,6 +1,8 @@
package observers_test
import (
"runtime"
"github.com/8thlight/vulcanizedb/config"
"github.com/8thlight/vulcanizedb/core"
"github.com/8thlight/vulcanizedb/observers"
@ -10,13 +12,18 @@ import (
. "github.com/onsi/gomega"
)
var (
_, filename, _, _ = runtime.Caller(0)
)
var _ = Describe("Saving blocks to the database", func() {
var db *sqlx.DB
var err error
BeforeEach(func() {
pgConfig := config.DbConnectionString(config.Private().Database)
cfg := config.NewConfig("private")
pgConfig := config.DbConnectionString(cfg.Database)
db, err = sqlx.Connect("postgres", pgConfig)
db.MustExec("DELETE FROM transactions")
db.MustExec("DELETE FROM blocks")