Module: EtcdTools::Etcd

Included in:
Cli::Etcd2Yaml, Cli::EtcdERB, Cli::Yaml2Etcd, Erb
Defined in:
lib/etcd-tools/etcd.rb

Instance Method Summary collapse

Instance Method Details

#etcd2hash(etcd, path = "") ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/etcd-tools/etcd.rb', line 32

def etcd2hash (etcd, path="")
  begin
    h = {}
    etcd.get(path).children.each do |child|
      if etcd.get(child.key).directory?
        h[child.key.split('/').last.to_s] = etcd2hash etcd, child.key
      else
        h[child.key.split('/').last.to_s] = child.value
      end
    end
    return Hash[h.sort]
  rescue Exception => e
    return nil
  end
end

#etcd_connect(url) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/etcd-tools/etcd.rb', line 6

def etcd_connect (url)
  (host, port) = url.gsub(/^https?:\/\//, '').gsub(/\/$/, '').split(':')
  etcd = ::Etcd.client(host: host, port: port)
  begin
    etcd.version
    return etcd
  rescue Exception => e
    raise e #fixme
  end
end

#hash2etcd(etcd, hash, path = "") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/etcd-tools/etcd.rb', line 17

def hash2etcd (etcd, hash, path="")
  begin
    hash.each do |key, value|
      etcd_key = path + "/" + key.to_s
      if value === Hash
        hash2etcd(value, etcd_key)
      else
        etcd.set(etcd_key, value: value)
      end
    end
  rescue Exception => e
    raise e #fixme
  end
end