summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorshadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-09 18:59:16 +0000
committershadoi <shadoi@980ebf18-57e1-0310-9a29-db15c13687c0>2006-11-09 18:59:16 +0000
commit51882d98289eea6b362ea99231d480728e937797 (patch)
tree0720b987de3542c9feb33dad9dd0a4e306cb29d8 /lib/puppet
parentcf166c25911f521cdf12178ebbe0b39f81473b35 (diff)
downloadpuppet-51882d98289eea6b362ea99231d480728e937797.tar.gz
puppet-51882d98289eea6b362ea99231d480728e937797.tar.xz
puppet-51882d98289eea6b362ea99231d480728e937797.zip
The new rails files.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1838 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/rails/database/01_puppet_initialize.rb45
-rw-r--r--lib/puppet/rails/database/schema_init.rb64
-rw-r--r--lib/puppet/rails/fact_name.rb3
-rw-r--r--lib/puppet/rails/fact_value.rb3
-rw-r--r--lib/puppet/rails/lib/README4
-rw-r--r--lib/puppet/rails/lib/acts_as_taggable.rb58
-rw-r--r--lib/puppet/rails/lib/init.rb5
-rw-r--r--lib/puppet/rails/lib/tag.rb40
-rw-r--r--lib/puppet/rails/lib/tagging.rb12
-rw-r--r--lib/puppet/rails/param_name.rb13
-rw-r--r--lib/puppet/rails/param_value.rb5
-rw-r--r--lib/puppet/rails/puppet_class.rb7
-rw-r--r--lib/puppet/rails/rails_parameter.rb13
-rw-r--r--lib/puppet/rails/rails_resource.rb34
-rw-r--r--lib/puppet/rails/resource.rb49
-rw-r--r--lib/puppet/rails/source_file.rb3
16 files changed, 266 insertions, 92 deletions
diff --git a/lib/puppet/rails/database/01_puppet_initialize.rb b/lib/puppet/rails/database/01_puppet_initialize.rb
deleted file mode 100644
index 485634004..000000000
--- a/lib/puppet/rails/database/01_puppet_initialize.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-class PuppetInitialize < ActiveRecord::Migration
- require 'sqlite3'
-
- def self.up
- if ActiveRecord::Migration.respond_to?(:verbose)
- ActiveRecord::Migration.verbose = false
- end
-
- # 'type' cannot be a column name, apparently
- create_table :rails_resources do |table|
- table.column :title, :string, :null => false
- table.column :restype, :string, :null => false
- table.column :tags, :string
- table.column :file, :string
- table.column :line, :integer
- table.column :host_id, :integer
- table.column :exported, :boolean
- end
-
- create_table :rails_parameters do |table|
- table.column :name, :string, :null => false
- table.column :value, :string, :null => false
- table.column :file, :string
- table.column :line, :integer
- table.column :rails_resource_id, :integer
- end
-
- create_table :hosts do |table|
- table.column :name, :string, :null => false
- table.column :ip, :string
- table.column :facts, :string
- table.column :connect, :date
- table.column :success, :date
- table.column :classes, :string
- end
- end
-
- def self.down
- drop_table :rails_resources
- drop_table :rails_parameters
- drop_table :hosts
- end
-end
-
-# $Id$
diff --git a/lib/puppet/rails/database/schema_init.rb b/lib/puppet/rails/database/schema_init.rb
new file mode 100644
index 000000000..9151bee46
--- /dev/null
+++ b/lib/puppet/rails/database/schema_init.rb
@@ -0,0 +1,64 @@
+class Puppet::Rails::Schema
+
+def self.init
+ ActiveRecord::Schema.define do
+ create_table :resources do |t|
+ t.column :title, :string, :null => false
+ t.column :type, :string
+ t.column :host_id, :integer
+ t.column :source_file_id, :integer
+ t.column :exported, :boolean
+ end
+
+ create_table :source_files do |t|
+ t.column :filename, :string
+ t.column :path, :string
+ end
+
+ create_table :puppet_classes do |t|
+ t.column :name, :string
+ t.column :host_id, :integer
+ t.column :source_file_id, :integer
+ end
+
+ create_table :hosts do |t|
+ t.column :name, :string, :null => false
+ t.column :ip, :string
+ t.column :connect, :date
+ #Use updated_at to automatically add timestamp on save.
+ t.column :updated_at, :date
+ t.column :source_file_id, :integer
+ end
+
+ create_table :fact_names do |t|
+ t.column :name, :string, :null => false
+ t.column :host_id, :integer, :null => false
+ end
+
+ create_table :fact_values do |t|
+ t.column :value, :string, :null => false
+ t.column :fact_name_id, :integer, :null => false
+ end
+
+ create_table :param_values do |t|
+ t.column :value, :string, :null => false
+ t.column :param_name_id, :integer, :null => false
+ end
+
+ create_table :param_names do |t|
+ t.column :name, :string, :null => false
+ t.column :resource_id, :integer
+ end
+
+ create_table :tags do |t|
+ t.column :name, :string
+ end
+
+ create_table :taggings do |t|
+ t.column :tag_id, :integer
+ t.column :taggable_id, :integer
+ t.column :taggable_type, :string
+ end
+ end
+end
+end
diff --git a/lib/puppet/rails/fact_name.rb b/lib/puppet/rails/fact_name.rb
new file mode 100644
index 000000000..886618ecb
--- /dev/null
+++ b/lib/puppet/rails/fact_name.rb
@@ -0,0 +1,3 @@
+class Puppet::Rails::FactName < ActiveRecord::Base
+ has_many :fact_values
+end
diff --git a/lib/puppet/rails/fact_value.rb b/lib/puppet/rails/fact_value.rb
new file mode 100644
index 000000000..4da74b713
--- /dev/null
+++ b/lib/puppet/rails/fact_value.rb
@@ -0,0 +1,3 @@
+class Puppet::Rails::FactValue < ActiveRecord::Base
+ belongs_to :fact_names
+end
diff --git a/lib/puppet/rails/lib/README b/lib/puppet/rails/lib/README
new file mode 100644
index 000000000..8d2f90822
--- /dev/null
+++ b/lib/puppet/rails/lib/README
@@ -0,0 +1,4 @@
+Acts As Taggable
+=================
+
+Allows for tags to be added to multiple classes. \ No newline at end of file
diff --git a/lib/puppet/rails/lib/acts_as_taggable.rb b/lib/puppet/rails/lib/acts_as_taggable.rb
new file mode 100644
index 000000000..e9bd03696
--- /dev/null
+++ b/lib/puppet/rails/lib/acts_as_taggable.rb
@@ -0,0 +1,58 @@
+module ActiveRecord
+ module Acts #:nodoc:
+ module Taggable #:nodoc:
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ def acts_as_taggable(options = {})
+ write_inheritable_attribute(:acts_as_taggable_options, {
+ :taggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s,
+ :from => options[:from]
+ })
+
+ class_inheritable_reader :acts_as_taggable_options
+
+ has_many :taggings, :as => :taggable, :dependent => true
+ has_many :tags, :through => :taggings
+
+ include ActiveRecord::Acts::Taggable::InstanceMethods
+ extend ActiveRecord::Acts::Taggable::SingletonMethods
+ end
+ end
+
+ module SingletonMethods
+ def find_tagged_with(list)
+ find_by_sql([
+ "SELECT #{table_name}.* FROM #{table_name}, tags, taggings " +
+ "WHERE #{table_name}.#{primary_key} = taggings.taggable_id " +
+ "AND taggings.taggable_type = ? " +
+ "AND taggings.tag_id = tags.id AND tags.name IN (?)",
+ acts_as_taggable_options[:taggable_type], list
+ ])
+ end
+ end
+
+ module InstanceMethods
+ def tag_with(list)
+ Tag.transaction do
+ taggings.destroy_all
+
+ Tag.parse(list).each do |name|
+ if acts_as_taggable_options[:from]
+ send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self)
+ else
+ Tag.find_or_create_by_name(name).on(self)
+ end
+ end
+ end
+ end
+
+ def tag_list
+ tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ")
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/lib/puppet/rails/lib/init.rb b/lib/puppet/rails/lib/init.rb
new file mode 100644
index 000000000..0a07e5c8e
--- /dev/null
+++ b/lib/puppet/rails/lib/init.rb
@@ -0,0 +1,5 @@
+require 'puppet/rails/lib/acts_as_taggable'
+ActiveRecord::Base.send(:include, ActiveRecord::Acts::Taggable)
+
+require 'puppet/rails/lib/tagging'
+require 'puppet/rails/lib/tag'
diff --git a/lib/puppet/rails/lib/tag.rb b/lib/puppet/rails/lib/tag.rb
new file mode 100644
index 000000000..ca31171b2
--- /dev/null
+++ b/lib/puppet/rails/lib/tag.rb
@@ -0,0 +1,40 @@
+class Tag < ActiveRecord::Base
+ has_many :taggings
+
+ def self.parse(list)
+ tag_names = []
+
+ # first, pull out the quoted tags
+ list.gsub!(/\"(.*?)\"\s*/ ) { tag_names << $1; "" }
+
+ # then, replace all commas with a space
+ list.gsub!(/,/, " ")
+
+ # then, get whatever's left
+ tag_names.concat list.split(/\s/)
+
+ # strip whitespace from the names
+ tag_names = tag_names.map { |t| t.strip }
+
+ # delete any blank tag names
+ tag_names = tag_names.delete_if { |t| t.empty? }
+
+ return tag_names
+ end
+
+ def tagged
+ @tagged ||= taggings.collect { |tagging| tagging.taggable }
+ end
+
+ def on(taggable)
+ taggings.create :taggable => taggable
+ end
+
+ def ==(comparison_object)
+ super || name == comparison_object.to_s
+ end
+
+ def to_s
+ name
+ end
+end \ No newline at end of file
diff --git a/lib/puppet/rails/lib/tagging.rb b/lib/puppet/rails/lib/tagging.rb
new file mode 100644
index 000000000..e06e88a14
--- /dev/null
+++ b/lib/puppet/rails/lib/tagging.rb
@@ -0,0 +1,12 @@
+class Tagging < ActiveRecord::Base
+ belongs_to :tag
+ belongs_to :taggable, :polymorphic => true
+
+ def self.tagged_class(taggable)
+ ActiveRecord::Base.send(:class_name_of_active_record_descendant, taggable.class).to_s
+ end
+
+ def self.find_taggable(tagged_class, tagged_id)
+ tagged_class.constantize.find(tagged_id)
+ end
+end \ No newline at end of file
diff --git a/lib/puppet/rails/param_name.rb b/lib/puppet/rails/param_name.rb
new file mode 100644
index 000000000..928838f5c
--- /dev/null
+++ b/lib/puppet/rails/param_name.rb
@@ -0,0 +1,13 @@
+class Puppet::Rails::ParamName < ActiveRecord::Base
+ has_many :param_values
+ belongs_to :resources
+
+ def to_resourceparam(source)
+ hash = {}
+ hash[:name] = self.name.to_sym
+ hash[:source] = source
+ hash[:value] = self.param_values.find(:first).value
+ Puppet::Parser::Resource::Param.new hash
+ end
+end
+
diff --git a/lib/puppet/rails/param_value.rb b/lib/puppet/rails/param_value.rb
new file mode 100644
index 000000000..b01add4a7
--- /dev/null
+++ b/lib/puppet/rails/param_value.rb
@@ -0,0 +1,5 @@
+class Puppet::Rails::ParamValue < ActiveRecord::Base
+ belongs_to :param_names
+
+end
+
diff --git a/lib/puppet/rails/puppet_class.rb b/lib/puppet/rails/puppet_class.rb
new file mode 100644
index 000000000..de54a31e9
--- /dev/null
+++ b/lib/puppet/rails/puppet_class.rb
@@ -0,0 +1,7 @@
+class Puppet::Rails::PuppetClass < ActiveRecord::Base
+ has_many :resources
+ has_many :source_files
+ has_many :hosts
+
+ acts_as_taggable
+end
diff --git a/lib/puppet/rails/rails_parameter.rb b/lib/puppet/rails/rails_parameter.rb
deleted file mode 100644
index 295662146..000000000
--- a/lib/puppet/rails/rails_parameter.rb
+++ /dev/null
@@ -1,13 +0,0 @@
-class Puppet::Rails::RailsParameter < ActiveRecord::Base
- belongs_to :rails_resources
-
- def to_resourceparam(source)
- hash = self.attributes
- hash[:source] = source
- hash.delete("rails_resource_id")
- hash.delete("id")
- Puppet::Parser::Resource::Param.new hash
- end
-end
-
-# $Id$
diff --git a/lib/puppet/rails/rails_resource.rb b/lib/puppet/rails/rails_resource.rb
deleted file mode 100644
index caad1b460..000000000
--- a/lib/puppet/rails/rails_resource.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-require 'puppet'
-require 'puppet/rails/rails_parameter'
-
-#RailsParameter = Puppet::Rails::RailsParameter
-class Puppet::Rails::RailsResource < ActiveRecord::Base
- has_many :rails_parameters, :dependent => :delete_all
- serialize :tags, Array
-
- belongs_to :host
-
- # Convert our object to a resource. Do not retain whether the object
- # is collectable, though, since that would cause it to get stripped
- # from the configuration.
- def to_resource(scope)
- hash = self.attributes
- hash["type"] = hash["restype"]
- hash.delete("restype")
- hash.delete("host_id")
- hash.delete("id")
- hash.each do |p, v|
- hash.delete(p) if v.nil?
- end
- hash[:scope] = scope
- hash[:source] = scope.source
- obj = Puppet::Parser::Resource.new(hash)
- rails_parameters.each do |param|
- obj.set(param.to_resourceparam(scope.source))
- end
-
- return obj
- end
-end
-
-# $Id$
diff --git a/lib/puppet/rails/resource.rb b/lib/puppet/rails/resource.rb
new file mode 100644
index 000000000..bbe71305c
--- /dev/null
+++ b/lib/puppet/rails/resource.rb
@@ -0,0 +1,49 @@
+require 'puppet'
+require 'puppet/rails/lib/init'
+require 'puppet/rails/param_name'
+
+class Puppet::Rails::Resource < ActiveRecord::Base
+ has_many :param_values, :through => :param_names
+ has_many :param_names
+ has_many :source_files
+ belongs_to :hosts
+
+ acts_as_taggable
+
+ Puppet::Type.eachtype do |type|
+ klass = Class.new(Puppet::Rails::Resource)
+ Object.const_set("Puppet%s" % type.name.to_s.capitalize, klass)
+ end
+
+ def parameters
+ hash = {}
+ self.param_values.find(:all).each do |pvalue|
+ pname = self.param_names.find(:first)
+ hash.store(pname.name.to_sym, pvalue.value)
+ end
+ return hash
+ end
+
+ # Convert our object to a resource. Do not retain whether the object
+ # is collectable, though, since that would cause it to get stripped
+ # from the configuration.
+ def to_resource(scope)
+ hash = self.attributes
+ hash.delete("type")
+ hash.delete("host_id")
+ hash.delete("source_file_id")
+ hash.delete("id")
+ hash.each do |p, v|
+ hash.delete(p) if v.nil?
+ end
+ hash[:type] = self.class.to_s.gsub(/Puppet/,'').downcase
+ hash[:scope] = scope
+ hash[:source] = scope.source
+ obj = Puppet::Parser::Resource.new(hash)
+ self.param_names.each do |pname|
+ obj.set(pname.to_resourceparam(scope.source))
+ end
+
+ return obj
+ end
+end
diff --git a/lib/puppet/rails/source_file.rb b/lib/puppet/rails/source_file.rb
new file mode 100644
index 000000000..d300ec74c
--- /dev/null
+++ b/lib/puppet/rails/source_file.rb
@@ -0,0 +1,3 @@
+class Puppet::Rails::SourceFile < ActiveRecord::Base
+ has_many :hosts, :puppet_classes, :resources
+end