Etcd: how to batch-load (initialize) from json file?

Created on 3 Jul 2017  路  6Comments  路  Source: etcd-io/etcd

I cannot seem to find a way to initialize etcd using a json file. A two-level file is what I need (directory from each top level, than its children as key-value pairs), but even a flat file would be good at this point. It any other way...

Most helpful comment

am i the first one to ask for this kind of functionality? can we make it into an enhancement?

All 6 comments

not what I meant. this is how how to use a conf file for etcd itself. What I am looking for is a way to input (set/mk/whatever) the values I need served by etcd, after it starts.
It would be nice if, as part of the etcd running command (etcd --name myEtcd...) I could provide a file with the values I want in etcd, preferably (for me) from a json file, that etcd would parse and insert. leaf values should be key:value pairs, anything that is higher level should be a directory.
so, if my init.json file is:

"topVal1" : {
  "key1" : "val1",
  "key2" : "val2"
},
"topVal2" : {
  "key3" : "val3"
}

than
etcd --name "myEtcd" ........ --init-file ./init.json
should yield a directory named topVal1 with 2 kv pairs (key1:val1, key2:val2) and another directory named topVal2 with one kv pair (key3:val3).

no. etcd will not load data from json file. you can write a script to do that after you bootstrap an etcd cluster.

for that matter, this is how i do it now using jq:

for topic in `jq -r 'keys[]' /opt/env `; do 
  echo; 
  for key in `jq .$topic /opt/env | jq -r 'keys[]'`; do 
    echo "$topic/$key:"; 
    myVal=`jq -r [".$topic | .$key"] /opt/env | jq -r .[]`; 
    echo "myVal: $myVal"; 
    ETCDCTL_API=3 etcdctl --endpoints=127.0.0.1:2379 put $topic/$key $myVal ; 
    curl http://127.0.0.1:2379/v2/keys/$topic/$key -XPUT -d value="$myVal"
  done; 
  echo;
done

@ehudkaldor

your script looks good.

am i the first one to ask for this kind of functionality? can we make it into an enhancement?

Was this page helpful?
0 / 5 - 0 ratings