Skip to content

Commit

Permalink
Merge pull request #21 from chuckleheads/elliott/cleanupening
Browse files Browse the repository at this point in the history
Cleanup config parsing and database migration errors
  • Loading branch information
elliott-davis authored May 22, 2018
2 parents ad48acd + d4d1efc commit 904a2fc
Show file tree
Hide file tree
Showing 45 changed files with 395 additions and 294 deletions.
8 changes: 8 additions & 0 deletions .studiorc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ start-hurtlocker() {
start-sessionsrv
}

stop-hurtlocker() {
stop-rabbitmq
stop-datastore
stop-originsrv
stop-agent
stop-sessionsrv
}

start-rabbitmq() {
hab svc load core/rabbitmq
}
Expand Down
26 changes: 13 additions & 13 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions components/datastore/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ pkg_upstream_url="https://github.com/chuckleheads/hurtlocker"
pkg_license=('Apache-2.0')
pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_deps=(core/cockroach)
pkg_exports=(
[port]=port
)
pkg_exposes=(port)

do_build() {
return 0
Expand Down
6 changes: 3 additions & 3 deletions components/originsrv/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ var migrateCmd = &cobra.Command{
Short: "Run the database migrations for originsrv",
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Running Migrations...")
dbConfig, err := DBConfigFromViper()
config, err := ConfigFromViper()
if err != nil {
panic(err.Error())
}
db := data_store.New(dbConfig)
db := data_store.New(&config.Datastore)

migrations.Migrate(db, dbConfig.Migrations)
migrations.Migrate(db, config.Datastore.Migrations)
log.Printf("Migrations complete")
},
}
Expand Down
18 changes: 9 additions & 9 deletions components/originsrv/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /hab/svc/originsrv/config/config.toml)")
viper.SetDefault("host", "localhost")
viper.SetDefault("port", 26257)
viper.SetDefault("database", "originsrv")
viper.SetDefault("username", "root")
viper.SetDefault("password", "")
viper.SetDefault("ssl-mode", "disable")
viper.SetDefault("datastore.host", "localhost")
viper.SetDefault("datastore.port", 26257)
viper.SetDefault("datastore.database", "originsrv")
viper.SetDefault("datastore.username", "root")
viper.SetDefault("datastore.password", "")
viper.SetDefault("datastore.ssl-mode", "disable")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -69,9 +69,9 @@ func initConfig() {
}
}

// DBConfigFromViper fetches database config from viper
func DBConfigFromViper() (*config.DBConfig, error) {
cfg := &config.DBConfig{}
// ConfigFromViper fetches database config from viper
func ConfigFromViper() (*config.Config, error) {
cfg := &config.Config{}
if err := viper.Unmarshal(cfg); err != nil {
panic(err.Error())
}
Expand Down
4 changes: 2 additions & 2 deletions components/originsrv/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func setup() {
func runGRPC(lis net.Listener) {
server := grpc.NewServer()

dbConfig, err := DBConfigFromViper()
config, err := ConfigFromViper()
if err != nil {
panic(err.Error())
}
db := data_store.New(dbConfig)
db := data_store.New(&config.Datastore)
pbs.RegisterOriginsServer(server, srv.NewServer(db))
log.Printf("gRPC Listening on %s\n", lis.Addr().String())
server.Serve(lis)
Expand Down
2 changes: 1 addition & 1 deletion components/originsrv/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package config

type Config struct {
Database DBConfig
Datastore DBConfig
}
6 changes: 3 additions & 3 deletions components/originsrv/data_store/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"database/sql"
"fmt"

"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/source/file"
_ "github.com/lib/pq"
"github.com/mattes/migrate"
"github.com/mattes/migrate/database/postgres"
_ "github.com/mattes/migrate/source/file"
)

func Migrate(db *sql.DB, migrations_dir string) {
Expand Down
8 changes: 4 additions & 4 deletions components/originsrv/habitat/config/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[datastore]
database = "{{cfg.datastore.database}}"
{{#eachAlive bind.database.members as |member| ~}}
{{#eachAlive bind.datastore.members as |member| ~}}
{{#if @first ~}}
host = "member.sys.ip"
port = "member.cfg.port"
host = "{{member.sys.ip}}"
port = {{member.cfg.port}}
{{/if ~}}
{{/eachAlive ~}}
migrations = {{pkg.path}}/migrations
migrations = "{{pkg.path}}/migrations"
2 changes: 1 addition & 1 deletion components/originsrv/habitat/hooks/init
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cockroach sql --execute="CREATE DATABASE IF NOT EXISTS {{cfg.datastore.database}};" --insecure

originsrv migrate
originsrv migrate --config "{{pkg.svc_config_path}}/config.toml"
5 changes: 5 additions & 0 deletions components/originsrv/habitat/hooks/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

exec 2>&1

exec {{pkg.name}} start --config "{{pkg.svc_config_path}}/config.toml"
1 change: 0 additions & 1 deletion components/originsrv/habitat/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_bin_dirs=(bin)
pkg_build_deps=(core/go core/git core/dep)
pkg_deps=(core/cockroach)
pkg_svc_run="${pkg_name} start"
pkg_binds=(
[datastore]="port"
)
7 changes: 3 additions & 4 deletions components/sessionsrv/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ var migrateCmd = &cobra.Command{
Short: "Run the database migrations for sessionsrv",
Run: func(cmd *cobra.Command, args []string) {
log.Printf("Running Migrations...")
dbConfig, err := DBConfigFromViper()
config, err := ConfigFromViper()
if err != nil {
panic(err.Error())
}
db := data_store.New(dbConfig)

migrations.Migrate(db, dbConfig.Migrations)
db := data_store.New(&config.Datastore)
migrations.Migrate(db, config.Datastore.Migrations)
log.Printf("Migrations complete")
},
}
Expand Down
25 changes: 15 additions & 10 deletions components/sessionsrv/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -35,12 +36,12 @@ func init() {
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is /hab/svc/sessionsrv/config/config.toml)")
viper.SetDefault("host", "localhost")
viper.SetDefault("port", 26257)
viper.SetDefault("database", "sessionsrv")
viper.SetDefault("username", "root")
viper.SetDefault("password", "")
viper.SetDefault("ssl-mode", "disable")
viper.SetDefault("datastore.host", "localhost")
viper.SetDefault("datastore.port", 26257)
viper.SetDefault("datastore.database", "sessionsrv")
viper.SetDefault("datastore.username", "root")
viper.SetDefault("datastore.password", "")
viper.SetDefault("datastore.ssl-mode", "disable")
}

// initConfig reads in config file and ENV variables if set.
Expand All @@ -51,7 +52,7 @@ func initConfig() {
} else {
home := filepath.Dir("/hab/svc/sessionsrv/config")

// Search config in home directory with name ".config" (without extension).
// Search config in home directory with name "config" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName("config")
}
Expand All @@ -61,14 +62,18 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
} else {
fmt.Printf("Err: %v", err)
}

}

// DBConfigFromViper fetches database config from viper
func DBConfigFromViper() (*config.DBConfig, error) {
cfg := &config.DBConfig{}
// ConfigFromViper fetches database config from viper
func ConfigFromViper() (*config.Config, error) {
cfg := &config.Config{}
if err := viper.Unmarshal(cfg); err != nil {
panic(err.Error())
}
log.Printf("CONFIG: %v", cfg)
return cfg, nil
}
4 changes: 2 additions & 2 deletions components/sessionsrv/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func setup() {
func runGRPC(lis net.Listener) {
server := grpc.NewServer()

dbConfig, err := DBConfigFromViper()
config, err := ConfigFromViper()
if err != nil {
panic(err.Error())
}
db := data_store.New(dbConfig)
db := data_store.New(&config.Datastore)
pb.RegisterAccountsServer(server, srv.NewServer(db))

log.Printf("gRPC Listening on %s\n", lis.Addr().String())
Expand Down
2 changes: 1 addition & 1 deletion components/sessionsrv/config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package config

type Config struct {
Database DBConfig
Datastore DBConfig
}
16 changes: 12 additions & 4 deletions components/sessionsrv/data_store/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ package migrations
import (
"database/sql"
"fmt"
"log"

"github.com/golang-migrate/migrate"
"github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/source/file"
_ "github.com/lib/pq"
"github.com/mattes/migrate"
"github.com/mattes/migrate/database/postgres"
_ "github.com/mattes/migrate/source/file"
)

func Migrate(db *sql.DB, migrations_dir string) {
driver, err := postgres.WithInstance(db, &postgres.Config{})
if err != nil {
panic(err.Error())
}
log.Printf("file://%s", migrations_dir)
m, err := migrate.NewWithDatabaseInstance(
fmt.Sprintf("file://%s", migrations_dir),
"postgres", driver)
m.Up()
if err != nil {
panic(err.Error())
}
err = m.Up()
if err != nil {
panic(err.Error())
}
}
8 changes: 4 additions & 4 deletions components/sessionsrv/habitat/config/config.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[datastore]
database = "{{cfg.datastore.database}}"
{{#eachAlive bind.database.members as |member| ~}}
{{#eachAlive bind.datastore.members as |member| ~}}
{{#if @first ~}}
host = "member.sys.ip"
port = "member.cfg.port"
host = "{{member.sys.ip}}"
port = {{member.cfg.port}}
{{/if ~}}
{{/eachAlive ~}}
migrations = {{pkg.path}}/migrations
migrations = "{{pkg.path}}/migrations"
2 changes: 1 addition & 1 deletion components/sessionsrv/habitat/hooks/init
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

cockroach sql --execute="CREATE DATABASE IF NOT EXISTS {{cfg.datastore.database}};" --insecure

sessionsrv migrate
sessionsrv migrate --config "{{pkg.svc_config_path}}/config.toml"
5 changes: 5 additions & 0 deletions components/sessionsrv/habitat/hooks/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh

exec 2>&1

exec {{pkg.name}} start --config "{{pkg.svc_config_path}}/config.toml"
1 change: 0 additions & 1 deletion components/sessionsrv/habitat/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pkg_maintainer="The Habitat Maintainers <[email protected]>"
pkg_bin_dirs=(bin)
pkg_build_deps=(core/go core/git core/dep)
pkg_deps=(core/cockroach)
pkg_svc_run="${pkg_name} start"
pkg_binds=(
[datastore]="port"
)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 904a2fc

Please sign in to comment.