Class: Chef::Provider::Etcd

Inherits:
Chef::Provider show all
Defined in:
lib/chef/provider/etcd.rb

Instance Method Summary collapse

Constructor Details

#initialize(new_resource, run_context) ⇒ Etcd

Returns a new instance of Etcd.



10
11
12
# File 'lib/chef/provider/etcd.rb', line 10

def initialize(new_resource, run_context)
  super(new_resource, run_context)
end

Instance Method Details

#action_deleteObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/chef/provider/etcd.rb', line 91

def action_delete
  if key_exist?
    converge_by "will delete etcd key #{new_resource.key}" do
      etcd.delete(new_resource.key)
      new_resource.updated_by_last_action(true)
    end
  else
    Chef::Log.debug("etcd key #{new_resource.key} does not exist")
  end
end

#action_getObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/provider/etcd.rb', line 63

def action_get
  converge_by "will set value of key #{new_resource.key}" do
    if key_exist?
      current_value
    else
      nil
    end
    new_resource.updated_by_last_action(true)
  end
end

#action_setObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/chef/provider/etcd.rb', line 53

def action_set
  if @current_resource.value == new_resource.value
    Chef::Log.debug(" etcd #{new_resource.key} is in sync")
  else
    converge_by "will set value of key #{new_resource.key}" do
      etcd.set(new_resource.key, new_resource.value)
      new_resource.updated_by_last_action(true)
    end
  end
end

#action_test_and_setObject



74
75
76
77
78
79
80
81
82
# File 'lib/chef/provider/etcd.rb', line 74

def action_test_and_set
  begin
    etcd.test_and_set(new_resource.key, new_resource.value, new_resource.prev_value, new_resource.ttl)
  rescue Net::HTTPServerException => e
    converge_by "will not be able test_and_set value of key #{new_resource.key}" do
      new_resource.updated_by_last_action(true)
    end
  end
end

#action_watchObject



84
85
86
87
88
89
# File 'lib/chef/provider/etcd.rb', line 84

def action_watch
  converge_bey "will wait for update from etcd key #{new_resource.key}" do
    etcd.watch(new_resource.key)
    new_resource.updated_by_last_action(true)
  end
end

#configObject



26
27
28
# File 'lib/chef/provider/etcd.rb', line 26

def config
  @config ||= Chef::Config[:etcd]
end

#current_valueObject



45
46
47
48
49
50
51
# File 'lib/chef/provider/etcd.rb', line 45

def current_value
  if key_exist?
    etcd.get(new_resource.key).value
  else
    nil
  end
end

#etcdObject



30
31
32
# File 'lib/chef/provider/etcd.rb', line 30

def etcd
  @etcd ||= ::Etcd.client(config)
end

#key_exist?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
# File 'lib/chef/provider/etcd.rb', line 35

def key_exist?
  exist = true
  begin
    etcd.get(new_resource.key)
  rescue Net::HTTPServerException => e
    exist = false
  end
  exist
end

#load_current_resourceObject



14
15
16
17
18
19
20
# File 'lib/chef/provider/etcd.rb', line 14

def load_current_resource
  @current_resource ||= Chef::Resource::Etcd.new(new_resource.name)
  if key_exist?
    @current_resource.value(current_value)
  end
  @current_resource
end

#whyrun_supported?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/chef/provider/etcd.rb', line 22

def whyrun_supported?
  true
end