Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate opennebula results #206

Merged
merged 8 commits into from
Oct 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 54 additions & 26 deletions lib/puppet/provider/onehost/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ def create
else
onehost('create', resource[:name], '--im', resource[:im_mad], '--vm', resource[:vm_mad], '--net', resource[:vn_mad], '--cluster', resource[:cluster_id])
end
Puppet.debug("Validate Resource State")
post_validate_change
@property_hash[:ensure] = :present
end

#TODO: requires validation as well
def destroy
onehost('delete', resource[:name])
@property_hash.clear
Expand All @@ -41,14 +44,6 @@ def exists?
@property_hash[:ensure] == :present
end

def disable
onehost('disable', resource[:name])
end

def enable
onehost('enable', resource[:name])
end

def add_to_cluster
onecluster("addhost", resource[:cluster_id], resource[:name])
end
Expand Down Expand Up @@ -78,16 +73,15 @@ def validate_cluster
def self.instances
hosts = Nokogiri::XML(onehost('list','-x')).root.xpath('/HOST_POOL/HOST')
hosts.collect do |host|
new(
new(
:name => host.xpath('./NAME').text,
:ensure => :present,
:im_mad => host.xpath('./IM_MAD').text,
:vm_mad => host.xpath('./VM_MAD').text,
:vn_mad => host.xpath('./VN_MAD').text,
:cluster_id => host.xpath('./CLUSTER_ID').text,
:status => {'0' => 'init', '2' => 'enabled','3' => 'error', '4' => 'disabled'}[host.xpath('./STATE').text]
)
end
:cluster_id => host.xpath('./CLUSTER_ID').text
)
end
end

def self.prefetch(resources)
Expand All @@ -99,6 +93,52 @@ def self.prefetch(resources)
end
end

def postfetch()
# In difference to self.instances validation requires the state since, this is necessary to
# judge weather a host was created successfully or not
host = Nokogiri::XML(onehost('show', resource[:name], '-x')).root.xpath('/HOST')
@post_property_hash = Hash.new
@post_property_hash[:name] = host.xpath('./NAME').text.to_s
@post_property_hash[:im_mad] = host.xpath('./IM_MAD').text.to_s
@post_property_hash[:vm_mad] = host.xpath('./VM_MAD').text.to_s
@post_property_hash[:vn_mad] = host.xpath('./VN_MAD').text.to_s
@post_property_hash[:cluster_id] = host.xpath('./CLUSTER_ID').text.to_s
@post_property_hash[:status] = {'0' => 'init', '1' => 'update', '2' => 'enabled','3' => 'error', '4' => 'disabled', '5' => 'enabled', '6' => 'enabled', '7' => 'enabled'}[host.xpath('./STATE').text]
end

def post_validate_change()
unless resource[:self_test]
Puppet.debug("nothing to validate, bye bye")
return
end
Puppet.debug("Validating state")
postfetch
resource_state = Hash.new
resource_state[:name] = resource[:name].to_s
resource_state[:im_mad] = resource[:im_mad].to_s
resource_state[:vm_mad] = resource[:vm_mad].to_s
resource_state[:vn_mad] = resource[:vn_mad].to_s
resource_state[:status] = 'enabled' # <- Hardcoded since enabled is the only reasonable state
resource_state[:cluster_id] = resource[:cluster_id].to_s

max_attempts = 3
attempts = 0
sleep_time = 30

while @post_property_hash != resource_state do
attempts += 1
sleep sleep_time
postfetch
if @post_property_hash[:status].to_s == 'error' and resource_state[:status].to_s != 'error'
raise "Failed to apply resource, final Resource state: #{@post_property_hash[:status]}"
end
if attempts == max_attempts and @post_property_hash != resource_state
raise "Failed to apply resource change"
end
end

end

# setters
def im_mad=(value)
raise "onehosts can not be updated. You have to remove and recreate the host"
Expand All @@ -112,20 +152,8 @@ def vn_mad=(value)
raise "onehosts can not be updated. You have to remove and recreate the host"
end

def status=(value)
if resource[:status] == "enabled" and @property_hash[:status] == "disabled"
enable
elsif @property_hash[:status] != "disabled" and resource[:status] == "disabled"
disable
else
raise "Onehosts cannot be updated. Cannot recover from state: " + @property_hash[:status]
end
end

def cluster_id=(value)
if resource[:status] == "error"
raise "Host in wrong state to perform update on Cluster ID"
elsif value.to_s == "-1" and @property_hash[:cluster_id].to_s != "-1"
if value.to_s == "-1" and @property_hash[:cluster_id].to_s != "-1"
delete_from_cluster
elsif validate_cluster==false
raise "Onehost cannot be updated. Invalid Cluster ID"
Expand Down
10 changes: 5 additions & 5 deletions lib/puppet/type/onehost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
end
end

newparam(:self_test, :boolean => true) do
desc "Control Flag to enable strict checking of applied changes by updating the property_hash
after waiting on the result of an OpenNebula transaction"
end

newproperty(:im_mad) do
desc "Information Driver"
defaultto :dummy
Expand All @@ -48,9 +53,4 @@
defaultto :'-1'
end

newproperty(:status) do
desc "Disable/Enable Host"
defaultto :enabled
end

end
2 changes: 0 additions & 2 deletions spec/acceptance/onecluster_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ class { 'one':
im_mad => 'dummy',
vm_mad => 'dummy',
vn_mad => 'dummy',
status => 'disabled',
}

onehost { 'host02':
im_mad => 'dummy',
vm_mad => 'dummy',
vn_mad => 'dummy',
status => 'disabled',
}

onevnet { 'vnet1':
Expand Down
5 changes: 0 additions & 5 deletions spec/type/onehost_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
@host[:cluster_id].should == '-1'
end

it 'should have property :status' do
@host[:status] = 'enabled'
@host[:status].should == 'enabled'
end

parameter_tests = {
:name => {
:valid => ["test", "foo"],
Expand Down