Exemplo n.º 1
0
func inputFile(kv *consulapi.KV, path string) {
	f, err := os.Open(path)
	if err != nil {
		log.Fatal(err.Error())
	}

	r := csv.NewReader(f)

	for {
		record, err := r.Read()
		if err == io.EOF {
			break
		}
		if err != nil {
			log.Fatal(err)
		}

		if len(record) != 2 {
			fmt.Printf("Incorrectly formatted line: %v\n", record)
		}

		d := &consulapi.KVPair{Key: record[0], Value: []byte(record[1])}
		_, err = kv.Put(d, nil)
		if err != nil {
			if consulapi.IsServerError(err) && inRetries < 10 {
				log.Println("consul server err: retry after 1s")
				<-time.After(time.Second)
				inRetries++
				inputFile(kv, path)
				return
			}
			log.Fatal(err.Error())
		}
	}
}
Exemplo n.º 2
0
func storeData(kv *api.KV, key string, value []byte) {
	pair, _, err := kv.Get(key, nil)
	if err != nil {
		Error.Fatal("Failed reading from Consul for key:", key, "Error:", err)
	}
	if pair == nil || string(pair.Value) != string(value) {
		var p *api.KVPair
		if pair == nil {
			p = &api.KVPair{Key: key, Value: value}
		} else {
			p = &api.KVPair{Key: key, Value: value, ModifyIndex: pair.ModifyIndex}
		}
		success, meta, err := kv.CAS(p, nil)
		if err != nil {
			Error.Fatal(err)
		}
		if !success {
			storeData(kv, key, value)
		} else {
			Trace.Print("Request took: ", meta.RequestTime.Seconds(), " seconds")
			Info.Print("Updated key '", key, "' to '", string(value), "'")
		}
	} else {
		Trace.Print("not updating key", key)
	}
}
Exemplo n.º 3
0
func monitorConsulGoroutine(kv *consulapi.KV, service string, lastIndex uint64) {
	for {
		pair, qm, err := kv.Get(
			consulPrefix+service,
			&consulapi.QueryOptions{
				WaitIndex:         lastIndex,
				RequireConsistent: true,
			})
		if err != nil {
			if consulapi.IsServerError(err) {
				// Consul unavailable. Try again.
				time.Sleep(1 * time.Second)
				continue
			}
			if strings.Contains(err.Error(), "read: connection timed out") {
				// Try again.
				time.Sleep(1 * time.Second)
				continue
			}

			log.Fatalf("Error monitoring config in Consul: %v\n", err)
		}
		if pair == nil {
			log.Fatalf("Config in consul has been deleted\n")
		}

		updateConsulFlags(pair.Value)
		lastIndex = qm.LastIndex
	}
}
Exemplo n.º 4
0
func keyExists(key string, kv *consul.KV) bool {
	kp, _, err := kv.Get(key, nil)
	if err != nil {
		log.Warnf("Could not get key path %s: %v", key, err)
		return false
	}

	return kp != nil
}
Exemplo n.º 5
0
func fetchConsul(kv *consul.KV, env string, key string) (value string, conf_err error) {
	pair, _, _ := kv.Get("go-sms/"+env+"/"+key, nil)
	if pair == nil {
		conf_err = errors.New("Failed to fetch \"" + key + "\" key for you environment. Leave default: " + defaults[key])
		value = defaults[key]
		return
	}

	value = string(pair.Value)
	return
}
// getSequencer gets the lockindex, session id and the key which constitutes the sequencer for the current lock.
func getSequencer(kv *api.KV, key string) (*sequencer, error) {
	kvPair, _, err := kv.Get(key, nil)
	if err != nil {
		log.Println("Can't get the sequencer")
		return nil, err
	}
	if kvPair == nil {
		return nil, nil
	}
	seq := sequencer{}
	seq.lockIndex = kvPair.LockIndex
	seq.session = kvPair.Session
	seq.key = kvPair.Key
	return &seq, nil
}
Exemplo n.º 7
0
func NewPackageCatalog(kv *consul.KV, repositoryRoot string) (*packageCatalog, error) {
	catalog := &packageCatalog{kv: kv}
	pkgIndex := make(map[string]map[string]map[string]string)

	keys, _, err := kv.Keys(repositoryRoot, "", nil)
	if err != nil {
		return nil, err
	}
	sort.Strings(keys)

	// package key example: mantl-install/repository/0/repo/packages/S/spark/3/config.json
	for _, key := range keys {
		parts := strings.Split(key, "/")
		if len(parts) == 9 {
			repoIdx := parts[2]
			name := parts[6]
			verIdx := parts[7]
			_, ok := pkgIndex[name]
			if !ok {
				pkgIndex[name] = make(map[string]map[string]string)
			}

			_, ok = pkgIndex[name][repoIdx]
			if !ok {
				pkgIndex[name][repoIdx] = make(map[string]string)
			}

			_, ok = pkgIndex[name][repoIdx][verIdx]
			if !ok {
				pkgKey := key[0 : strings.LastIndex(key, "/")+1]
				pkgIndex[name][repoIdx][verIdx] = pkgKey
			}
		}
	}

	catalog.catalog = pkgIndex
	return catalog, nil
}
Exemplo n.º 8
0
func outputFile(kv *consulapi.KV, path, prefix string) {

	vs, _, err := kv.List(prefix, nil)
	if err != nil {
		if consulapi.IsServerError(err) && outRetries < 10 {
			log.Println("consul server err: retry after 1s")
			<-time.After(time.Second)
			outRetries++
			outputFile(kv, path, prefix)
			return
		}
		log.Fatal(err.Error())
	}

	f, err := os.Create(path)
	if err != nil {
		log.Fatal(err.Error())
	}
	defer f.Close()
	for _, val := range vs {
		fmt.Fprintf(f, "%s,%s\n", val.Key, val.Value)
	}
	f.Sync()
}
Exemplo n.º 9
0
	"github.com/hashicorp/consul/api"
)

type Key struct {
	Value string
}

type ConsulProvider struct {
	Watch        bool
	Endpoint     string
	Prefix       string
	Filename     string
	consulClient *api.Client
}

var kvClient *api.KV

var ConsulFuncMap = template.FuncMap{
	"List": func(keys ...string) []string {
		joinedKeys := strings.Join(keys, "")
		keysPairs, _, err := kvClient.Keys(joinedKeys, "/", nil)
		if err != nil {
			log.Error("Error getting keys ", joinedKeys, err)
			return nil
		}
		keysPairs = fun.Filter(func(key string) bool {
			if key == joinedKeys {
				return false
			}
			return true
		}, keysPairs).([]string)
Exemplo n.º 10
0
func consulKeys(kv *api.KV, prefix string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
	return kv.Keys(prefix, "", &api.QueryOptions{WaitIndex: lastIndex})
}
Exemplo n.º 11
0
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
	return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex})
}