2017-11-01 14:40:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
2017-11-01 15:17:01 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/blockchain_listener"
|
2017-11-02 22:37:27 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/config"
|
2017-11-01 14:40:04 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/core"
|
2017-11-02 19:37:07 +00:00
|
|
|
"github.com/8thlight/vulcanizedb/geth"
|
|
|
|
"github.com/8thlight/vulcanizedb/observers"
|
2017-11-01 14:40:04 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
do "gopkg.in/godo.v2"
|
|
|
|
)
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
func parseEnvironment(context *do.Context) string {
|
|
|
|
environment := context.Args.MayString("", "environment", "env", "e")
|
|
|
|
if environment == "" {
|
|
|
|
log.Fatalln("--environment required")
|
2017-11-01 15:17:01 +00:00
|
|
|
}
|
2017-11-02 22:37:27 +00:00
|
|
|
return environment
|
2017-11-01 15:17:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
func startBlockchainListener(cfg config.Config) {
|
|
|
|
fmt.Println("Client Path ", cfg.Client.IPCPath)
|
|
|
|
blockchain := geth.NewGethBlockchain(cfg.Client.IPCPath)
|
2017-11-02 19:37:07 +00:00
|
|
|
loggingObserver := observers.BlockchainLoggingObserver{}
|
2017-11-02 22:37:27 +00:00
|
|
|
connectString := config.DbConnectionString(cfg.Database)
|
2017-11-02 11:41:24 +00:00
|
|
|
db, err := sqlx.Connect("postgres", connectString)
|
2017-11-01 15:17:01 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error connecting to DB: %v\n", err)
|
|
|
|
}
|
2017-11-02 19:37:07 +00:00
|
|
|
dbObserver := (observers.BlockchainDBObserver{Db: db})
|
|
|
|
listener := blockchain_listener.NewBlockchainListener(blockchain, []core.BlockchainObserver{
|
2017-11-02 11:41:24 +00:00
|
|
|
loggingObserver,
|
|
|
|
dbObserver,
|
|
|
|
})
|
|
|
|
listener.Start()
|
2017-11-01 15:17:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 14:40:04 +00:00
|
|
|
func tasks(p *do.Project) {
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
p.Task("run", nil, func(context *do.Context) {
|
|
|
|
environment := parseEnvironment(context)
|
|
|
|
cfg := config.NewConfig(environment)
|
|
|
|
startBlockchainListener(cfg)
|
2017-11-01 15:17:01 +00:00
|
|
|
})
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
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)
|
2017-11-01 15:17:01 +00:00
|
|
|
})
|
|
|
|
|
2017-11-02 22:37:27 +00:00
|
|
|
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)
|
2017-11-01 14:40:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
do.Godo(tasks)
|
|
|
|
}
|