Skip to content

Commit

Permalink
changed logic to ignore DCs, export works
Browse files Browse the repository at this point in the history
  • Loading branch information
mgaida committed Sep 1, 2017
1 parent abe906c commit 09bce11
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
5 changes: 3 additions & 2 deletions consul/consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ func (c *Consul) GetNodes() []Node {
}

// WriteKVs writes KVs to consul
func (c *Consul) WriteKVs(kvs []KV, keepDC bool) error {
func (c *Consul) WriteKVs(kvs []KV, ignoreDC bool) error {
if c.debug {
for i := range kvs {
log.Println(kvs[i].printKV())
}
}
return c.writeKV(kvs, keepDC)
return c.writeKV(kvs, ignoreDC)
}

// Checks if to kv are equal in tearms of content
func (kv *KV) Equals(ckv KV) bool {
return (kv.Datacenter == ckv.Datacenter) &&
(kv.Key == ckv.Key) &&
Expand Down
4 changes: 2 additions & 2 deletions consul/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
consulapi "github.com/hashicorp/consul/api"
)

func (c *Consul) writeKV(kvs []KV, keepDC bool) error {
func (c *Consul) writeKV(kvs []KV, ignoreDC bool) error {

var kv = consulapi.KVPair{}

Expand All @@ -23,7 +23,7 @@ func (c *Consul) writeKV(kvs []KV, keepDC bool) error {
kv.Value = kvs[i].Value

writeopt := &consulapi.WriteOptions{}
if keepDC {
if !ignoreDC {
writeopt = &consulapi.WriteOptions{Datacenter: kvs[i].Datacenter}
}

Expand Down
52 changes: 36 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func main() {
Name: "verbose",
Usage: "Switch on the verbose mode",
},
cli.StringFlag{
Name: "token",
Usage: "ACL Token to be used to interact with consul",
},
cli.StringFlag{
Name: "dbpassword",
Usage: "Database password to be used to connect",
},
}

app.Commands = []cli.Command{
Expand All @@ -38,7 +46,6 @@ func main() {
consul-mirror.
Returns 0 if the configuration is valid, or 1 if there are problems.`,

Action: func(c *cli.Context) {
if c.Args().Present() {
os.Exit(commandValidate(c.Args().First()))
Expand All @@ -49,8 +56,20 @@ func main() {
cli.Command{
Name: "import",
Usage: "import from consul",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "ignoredc",
Usage: "ignore the dc information in DB",
},
cli.StringFlag{
Name: "prefix",
Usage: "key prefix for keys to be imported",
},
},
Action: func(c *cli.Context) {
commandImport(c.GlobalBool("verbose"), c.BoolT("dc"))
storage, consul := initConsul(c)
defer storage.Close()
commandImport(storage, consul, c.BoolT("ignoredc"), c.String("prefix"))
},
},
cli.Command{
Expand All @@ -71,17 +90,20 @@ func main() {
},
},
Action: func(c *cli.Context) {
commandExport(c.GlobalBool("verbose"), c.BoolT("ignoredc"), c.BoolT("incversion"), c.String("prefix"))
storage, consul := initConsul(c)
defer storage.Close()
commandExport(storage, consul, c.BoolT("ignoredc"), c.BoolT("incversion"), c.String("prefix"))
},
},
}

app.Run(os.Args)
}

func initConsul(verbose bool) (*storage.Mssql, *consul.Consul) {
func initConsul(cli *cli.Context) (*storage.Mssql, *consul.Consul) {
config := configuration.GetConfig("config.json")
config.Debug = verbose

config.OverwriteConfig(cli)

if config.Debug {
log.Printf(config.PrintDebug())
Expand All @@ -95,22 +117,20 @@ func initConsul(verbose bool) (*storage.Mssql, *consul.Consul) {
return conn, consul
}

func commandExport(verbose, ignoreDC, incversion bool, prefix string) {
conn, consul := initConsul(verbose)
defer conn.Close()

func commandExport(storage *storage.Mssql, consul *consul.Consul, ignoreDC, incversion bool, prefix string) {
dcs := consul.GetDCs()
kvs := consul.GetKVs(prefix, dcs)
conn.WriteKVs(kvs, ignoreDC, incversion)
storage.WriteKVs(kvs, ignoreDC, incversion)
}

func commandImport(verbose, keepDC bool) {
conn, consul := initConsul(verbose)

kvs, _ := conn.GetKVs()
err := consul.WriteKVs(kvs, keepDC)
func commandImport(storage *storage.Mssql, consul *consul.Consul, ignoreDC bool, prefix string) {
kvs, err := storage.GetKVs(prefix)
if err != nil {
log.Fatal("Error while fetching the data from the storage: ", err)
}
err = consul.WriteKVs(kvs, ignoreDC)
if err != nil {
log.Fatal(err)
log.Fatal("Error while writing the data to consul", err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions storage/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
)

// GetKVs reads KVs from the storage and returns all with the highest version
func (db *Mssql) GetKVs() ([]consul.KV, error) {
func (db *Mssql) GetKVs(prefix string) ([]consul.KV, error) {
var kv = consul.KV{}
var result = []consul.KV{}

// Get all keys with the highest version
rows, err := db.conn.Query("select DISTINCT a.flags, a.kvkey, a.lockindex, a.modifyindex, a.regex, a.session, a.kvvalue, a.datacenter from [consul].[dbo].[kv] a left outer join [consul].[dbo].[kv] b on a.datacenter = b.datacenter AND a.kvkey = b.kvkey AND a.version < b.version where b.kvkey is NULL")
rows, err := db.conn.Query("select DISTINCT a.flags, a.kvkey, a.lockindex, a.modifyindex, a.regex, a.session, a.kvvalue, a.datacenter from [consul].[dbo].[kv] a left outer join [consul].[dbo].[kv] b on a.datacenter = b.datacenter AND a.kvkey = b.kvkey AND a.key like '?%' AND a.version < b.version where b.kvkey is NULL", prefix)
defer rows.Close()
if err != nil {
log.Fatal(err)
return nil, err
}

for rows.Next() {
err = rows.Scan(&kv.Flags, &kv.Key, &kv.LockIndex, &kv.ModifyIndex, &kv.Regex, &kv.Session, &kv.Value, &kv.Datacenter)
if err != nil {
log.Fatal(err)
return nil, err
}
result = append(result, kv)
}
Expand Down

0 comments on commit 09bce11

Please sign in to comment.