Skip to content

Commit 267af67

Browse files
Merge pull request #206 from arnetogo/master
validate opennebula results
2 parents 2dd0c7d + a82184a commit 267af67

4 files changed

Lines changed: 59 additions & 38 deletions

File tree

lib/puppet/provider/onehost/cli.rb

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ def create
2929
else
3030
onehost('create', resource[:name], '--im', resource[:im_mad], '--vm', resource[:vm_mad], '--net', resource[:vn_mad], '--cluster', resource[:cluster_id])
3131
end
32+
Puppet.debug("Validate Resource State")
33+
post_validate_change
3234
@property_hash[:ensure] = :present
3335
end
3436

37+
#TODO: requires validation as well
3538
def destroy
3639
onehost('delete', resource[:name])
3740
@property_hash.clear
@@ -41,14 +44,6 @@ def exists?
4144
@property_hash[:ensure] == :present
4245
end
4346

44-
def disable
45-
onehost('disable', resource[:name])
46-
end
47-
48-
def enable
49-
onehost('enable', resource[:name])
50-
end
51-
5247
def add_to_cluster
5348
onecluster("addhost", resource[:cluster_id], resource[:name])
5449
end
@@ -78,16 +73,15 @@ def validate_cluster
7873
def self.instances
7974
hosts = Nokogiri::XML(onehost('list','-x')).root.xpath('/HOST_POOL/HOST')
8075
hosts.collect do |host|
81-
new(
76+
new(
8277
:name => host.xpath('./NAME').text,
8378
:ensure => :present,
8479
:im_mad => host.xpath('./IM_MAD').text,
8580
:vm_mad => host.xpath('./VM_MAD').text,
8681
:vn_mad => host.xpath('./VN_MAD').text,
87-
:cluster_id => host.xpath('./CLUSTER_ID').text,
88-
:status => {'0' => 'init', '2' => 'enabled','3' => 'error', '4' => 'disabled'}[host.xpath('./STATE').text]
89-
)
90-
end
82+
:cluster_id => host.xpath('./CLUSTER_ID').text
83+
)
84+
end
9185
end
9286

9387
def self.prefetch(resources)
@@ -99,6 +93,52 @@ def self.prefetch(resources)
9993
end
10094
end
10195

96+
def postfetch()
97+
# In difference to self.instances validation requires the state since, this is necessary to
98+
# judge weather a host was created successfully or not
99+
host = Nokogiri::XML(onehost('show', resource[:name], '-x')).root.xpath('/HOST')
100+
@post_property_hash = Hash.new
101+
@post_property_hash[:name] = host.xpath('./NAME').text.to_s
102+
@post_property_hash[:im_mad] = host.xpath('./IM_MAD').text.to_s
103+
@post_property_hash[:vm_mad] = host.xpath('./VM_MAD').text.to_s
104+
@post_property_hash[:vn_mad] = host.xpath('./VN_MAD').text.to_s
105+
@post_property_hash[:cluster_id] = host.xpath('./CLUSTER_ID').text.to_s
106+
@post_property_hash[:status] = {'0' => 'init', '1' => 'update', '2' => 'enabled','3' => 'error', '4' => 'disabled', '5' => 'enabled', '6' => 'enabled', '7' => 'enabled'}[host.xpath('./STATE').text]
107+
end
108+
109+
def post_validate_change()
110+
unless resource[:self_test]
111+
Puppet.debug("nothing to validate, bye bye")
112+
return
113+
end
114+
Puppet.debug("Validating state")
115+
postfetch
116+
resource_state = Hash.new
117+
resource_state[:name] = resource[:name].to_s
118+
resource_state[:im_mad] = resource[:im_mad].to_s
119+
resource_state[:vm_mad] = resource[:vm_mad].to_s
120+
resource_state[:vn_mad] = resource[:vn_mad].to_s
121+
resource_state[:status] = 'enabled' # <- Hardcoded since enabled is the only reasonable state
122+
resource_state[:cluster_id] = resource[:cluster_id].to_s
123+
124+
max_attempts = 3
125+
attempts = 0
126+
sleep_time = 30
127+
128+
while @post_property_hash != resource_state do
129+
attempts += 1
130+
sleep sleep_time
131+
postfetch
132+
if @post_property_hash[:status].to_s == 'error' and resource_state[:status].to_s != 'error'
133+
raise "Failed to apply resource, final Resource state: #{@post_property_hash[:status]}"
134+
end
135+
if attempts == max_attempts and @post_property_hash != resource_state
136+
raise "Failed to apply resource change"
137+
end
138+
end
139+
140+
end
141+
102142
# setters
103143
def im_mad=(value)
104144
raise "onehosts can not be updated. You have to remove and recreate the host"
@@ -112,20 +152,8 @@ def vn_mad=(value)
112152
raise "onehosts can not be updated. You have to remove and recreate the host"
113153
end
114154

115-
def status=(value)
116-
if resource[:status] == "enabled" and @property_hash[:status] == "disabled"
117-
enable
118-
elsif @property_hash[:status] != "disabled" and resource[:status] == "disabled"
119-
disable
120-
else
121-
raise "Onehosts cannot be updated. Cannot recover from state: " + @property_hash[:status]
122-
end
123-
end
124-
125155
def cluster_id=(value)
126-
if resource[:status] == "error"
127-
raise "Host in wrong state to perform update on Cluster ID"
128-
elsif value.to_s == "-1" and @property_hash[:cluster_id].to_s != "-1"
156+
if value.to_s == "-1" and @property_hash[:cluster_id].to_s != "-1"
129157
delete_from_cluster
130158
elsif validate_cluster==false
131159
raise "Onehost cannot be updated. Invalid Cluster ID"

lib/puppet/type/onehost.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
end
2626
end
2727

28+
newparam(:self_test, :boolean => true) do
29+
desc "Control Flag to enable strict checking of applied changes by updating the property_hash
30+
after waiting on the result of an OpenNebula transaction"
31+
end
32+
2833
newproperty(:im_mad) do
2934
desc "Information Driver"
3035
defaultto :dummy
@@ -48,9 +53,4 @@
4853
defaultto :'-1'
4954
end
5055

51-
newproperty(:status) do
52-
desc "Disable/Enable Host"
53-
defaultto :enabled
54-
end
55-
5656
end

spec/acceptance/onecluster_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ class { 'one':
1010
im_mad => 'dummy',
1111
vm_mad => 'dummy',
1212
vn_mad => 'dummy',
13-
status => 'disabled',
1413
}
1514
1615
onehost { 'host02':
1716
im_mad => 'dummy',
1817
vm_mad => 'dummy',
1918
vn_mad => 'dummy',
20-
status => 'disabled',
2119
}
2220
2321
onevnet { 'vnet1':

spec/type/onehost_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@
4444
@host[:cluster_id].should == '-1'
4545
end
4646

47-
it 'should have property :status' do
48-
@host[:status] = 'enabled'
49-
@host[:status].should == 'enabled'
50-
end
51-
5247
parameter_tests = {
5348
:name => {
5449
:valid => ["test", "foo"],

0 commit comments

Comments
 (0)