Permalink
Browse files

Add razor puppet resource.

* Add image, model, policy, and tag resource.
* Remove ruby 1.8.7 sample manifests.
* Update puppet_x to the correct location.
* Update description and add munging.
* Add example manifests for rz resource management.

Conflicts:
	lib/puppet/provider/rz_image/default.rb
  • Loading branch information...
1 parent deb88d7 commit 6e074caa66cb49646b5220bc3fc3ad571dd27f25 Nan Liu committed Oct 8, 2012
@@ -1,38 +1,33 @@
require 'fileutils'
require 'uri'
+require 'pathname'
+require Pathname.new(__FILE__).dirname.dirname.dirname.dirname.expand_path + 'puppet_x/puppet_labs/razor'
Puppet::Type.type(:rz_image).provide(:default) do
commands :razor => 'razor'
- commands :curl => 'curl'
+ commands :curl => 'curl'
- @image_type = {
- 'MicroKernel Image' => 'mk',
- 'OS Install' => 'os',
- 'VMware Hypervisor Install' => 'esxi',
- }
+ def self.query_razor
+ PuppetX::PuppetLabs::Razor.new(method(:razor))
+ end
+
+ def query_razor
+ self.class.query_razor
+ end
mk_resource_methods
def self.instances
razor_images = Array.new
- begin
- images = razor 'image', 'get'
- images = images.split("\n\n").collect{ |x| Hash[*(x.split(/\n|=>/) - ['Images']).collect{|y| y.strip!}] }
- rescue
- images = {}
- end
- images.each do |i|
- image = {
- :name => i['OS Name'] || i['ISO Filename'],
- :ensure => :present,
- :version => i['Version'] || i['OS Version'],
- :type => @image_type[i['Type']],
- :uuid => i['UUID'],
- }
+ images = query_razor.get_images
- razor_images << new(image)
+ images.each do |i|
+ i[:ensure] = :present
+ # fallback to use the image iso name for mk.
+ i[:name] = i[:name] || i[:isoname]
+ razor_images << new(i)
end
razor_images
end
@@ -50,6 +45,10 @@ def flush
@property_hash.clear
end
+ def download(source, target)
+ curl '-L', source, '-a', '-o', target
+ end
+
def create
@property_hash[:ensure] = :present
@@ -58,7 +57,7 @@ def create
if uri.scheme =~ /^http/
tmpdir = Dir.mktmpdir(nil, '/var/tmp')
source = File.join(tmpdir, File.basename(uri.path))
- curl '-L', resource[:source], '-a', '-o', source
+ download(resource[:source], source)
else
source = resource[:source]
end
@@ -0,0 +1,70 @@
+require 'fileutils'
+
+Puppet::Type.type(:rz_model).provide(:default) do
+
+ commands :razor => 'razor'
+
+ def self.query_razor
+ PuppetX::PuppetLabs::Razor.new(method(:razor))
+ end
+
+ def query_razor
+ self.class.query_razor
+ end
+
+ mk_resource_methods
+
+ def self.instances
+ razor_models = Array.new
+ begin
+ models = query_razor.get_models
+ models.each do |m|
+ m[:ensure] = :present
+ m[:name] = m[:name] || m[:isoname]
+ razor_models << new(m)
+ end
+ rescue
+ razor_models = {}
+ end
+
+ razor_models
+ end
+
+ def self.prefetch(resources)
+ instances.each do |prov|
+ if resource = resources[prov.name]
+ resource.provider = prov
+ end
+ end
+ end
+
+ # Clear out the cached values.
+ def flush
+ @property_hash.clear
+ end
+
+ def create
+ @property_hash[:ensure] = :present
+ uuid = query_razor.get_image_uuid(@resource[:image])
+
+ model = {
+ 'template' => @resource[:template],
+ 'label' => @resource[:name],
+ 'image_uuid' => uuid,
+ 'req_metadata_hash' => @resource[:metadata],
+ }
+
+ Puppet.debug "razor -w model add '#{model.to_pson}'"
+ command = ['razor', '-w', 'model', 'add', "'#{model.to_pson}'"].join(" ")
+ execute(command, :combine => true)
+ end
+
+ def destroy
+ @property_hash[:ensure] = :absent
+ razor '-w', 'model', 'remove', @property_hash[:uuid]
+ end
+
+ def exists?
+ @property_hash[:ensure] == :present
+ end
+end
@@ -0,0 +1,74 @@
+require 'fileutils'
+
+Puppet::Type.type(:rz_policy).provide(:default) do
+
+ commands :razor => 'razor'
+
+ def self.query_razor
+ PuppetX::PuppetLabs::Razor.new(method(:razor))
+ end
+
+ def query_razor
+ self.class.query_razor
+ end
+
+ mk_resource_methods
+
+ def self.instances
+ instances = Array.new
+ begin
+ policies = query_razor.get_policies
+ policies.each do |p|
+ p[:ensure] = :present
+ instances << new(p)
+ end
+ rescue
+ instances = {}
+ end
+
+ instances
+ end
+
+ def self.prefetch(resources)
+ instances.each do |prov|
+ if resource = resources[prov.name]
+ resource.provider = prov
+ end
+ end
+ end
+
+ # Clear out the cached values.
+ def flush
+ @property_hash.clear
+ end
+
+ def create
+ @property_hash[:ensure] = :present
+ uuid = query_razor.get_model_uuid(@resource[:model])
+
+ policy = {
+ 'template' => @resource[:template],
+ 'label' => @resource[:name],
+ 'model_uuid' => uuid,
+ 'broker_uuid' => @resource[:broker],
+ 'enabled' => @resource[:enabled],
+ 'tags' => @resource[:tags],
+ 'maximum' => @resource[:maximum] || 0,
+ }
+
+ policy['broker_uuid'] = @resource[:broker] if @resource[:broker] != 'none'
+
+ Puppet.debug "razor -w policy add '#{policy.to_pson}'"
+ command = ['razor', '-w', 'policy', 'add', "'#{policy.to_pson}'"].join(" ")
+ execute(command, :combine => true)
+ end
+
+ def destroy
+ @property_hash[:ensure] = :absent
+ razor '-w', 'policy', 'remove', @property_hash[:uuid]
+ end
+
+ def exists?
+ @property_hash[:ensure] == :present
+ end
+end
@@ -0,0 +1,105 @@
+require 'fileutils'
+
+Puppet::Type.type(:rz_tag).provide(:default) do
+
+ commands :razor => 'razor'
+
+ def self.query_razor
+ PuppetX::PuppetLabs::Razor.new(method(:razor))
+ end
+
+ def query_razor
+ self.class.query_razor
+ end
+
+ mk_resource_methods
+
+ def self.instances
+ instances = Array.new
+ begin
+ tags = query_razor.get_tags
+ tags.each do |t|
+ t[:ensure] = :present
+ instances << new(t)
+ end
+ rescue
+ instances = {}
+ end
+
+ instances
+ end
+
+ def self.prefetch(resources)
+ instances.each do |prov|
+ if resource = resources[prov.name]
+ resource.provider = prov
+ end
+ end
+ end
+
+ # Clear out the cached values.
+ def flush
+ @property_hash.clear
+ end
+
+ def tag_matcher=(value)
+ uuids = @property_hash[:tag_matcher].collect{|m| m[:uuid] }
+
+ # It's inefficient but safer to add new matchers and remove all existing matchers.
+ value.each do |m|
+ matcher = {
+ :tag_rule_uuid => @property_hash[:uuid],
+ :key => m['key'],
+ :value => m['value'],
+ :compare => m['compare'],
+ :inverse => m['inverse'],
+ }
+ command = ['razor', '-w', 'tag', 'matcher', 'add', "'#{matcher.to_pson}'"].join(" ")
+ results = execute(command, :combine => true)
+ Puppet.debug(results)
+ end
+
+ uuids.each do |i|
+ command = ['razor', '-w', 'tag', 'matcher', 'remove', i].join(" ")
+ results = execute(command, :combine => true)
+ Puppet.debug(results)
+ end
+ end
+
+ def create
+ @property_hash[:ensure] = :present
+
+ tag = {
+ 'name' => @resource[:name],
+ 'tag' => @resource[:tag_label],
+ }
+
+ Puppet.debug "razor -w tag add '#{tag.to_pson}'"
+ tag_uuid = query_razor.add_tag(tag.to_pson)
+
+ @resource[:tag_matcher].each do |m|
+ matcher = {
+ :tag_rule_uuid => tag_uuid,
+ :key => m['key'],
+ :value => m['value'],
+ :compare => m['compare'],
+ :inverse => m['inverse'],
+ }
+ command = ['razor', '-w', 'tag', 'matcher', 'add', "'#{matcher.to_pson}'"].join(" ")
+ results = execute(command, :combine => true)
+ Puppet.debug(results)
+ end
+ end
+
+ def destroy
+ @property_hash[:ensure] = :absent
+ @property_hash[:tag_matcher].each do |m|
+ razor '-w', 'tag', 'matcher', 'remove', m[:uuid]
+ end
+ razor '-w', 'tag', 'remove', @property_hash[:uuid]
+ end
+
+ def exists?
+ @property_hash[:ensure] == :present
+ end
+end
@@ -1,4 +0,0 @@
-module PuppetX
- module Pupppetlabs
- end
-end
@@ -1,6 +1,6 @@
require 'puppet/type'
require 'pathname'
-require Pathname.new(__FILE__).dirname.dirname.expand_path + 'puppet_x/puppet_labs/razor'
+require Pathname.new(__FILE__).dirname.dirname.dirname.expand_path + 'puppet_x/puppet_labs/razor'
Puppet::Type.newtype(:rz_image) do
@doc = <<-EOT
@@ -10,10 +10,8 @@
ensurable
newparam(:name, :namevar => true) do
- desc "The name of the os."
-
- validate do |path|
- end
+ desc "The name of the operating system."
+ newvalues(/\w+/)
end
newparam(:source) do
@@ -23,17 +21,22 @@
end
end
+ newproperty(:version) do
+ desc "The image version. The value is automatically detected for mk and esx."
+ newvalues(/[A-Za-z0-9]/)
+ end
+
newproperty(:type) do
+ desc "The image type, currently support mk, os, esxi"
newvalues('mk', 'os', 'esxi')
defaultto('mk')
- end
-
- newproperty(:version) do
-
+ validate do |value|
+ raise Puppet::Error, "Require image version" if value == 'os' and @resource[:version].nil?
+ end
end
newproperty(:uuid) do
- desc "The image UUID. This property is not expected to be speciified by the user."
+ desc "The image UUID. This property is not expected to be specified by the user."
validate do |value|
raise Puppet::Error, "Do not specify UUID value."
end
Oops, something went wrong.

0 comments on commit 6e074ca

Please sign in to comment.