From: Rein Henrichs <[email protected]>

Puppet Dashboard required access to the Puppet library in Ruby's
$LOAD_PATH. This results in failures when puppet isn't in the load path,
such as when it is installed as a system package.

Adding simple data-only classes for Puppet report classes allows YAML to
reconstitute report objects without the loading puppet-core lib. This
fixes the "no such file to load -- puppet" error and will improve
Dashboard's memory performance.

Signed-off-by: Rein Henrichs <[email protected]>
---
 app/models/report.rb            |    2 +-
 lib/puppet/report.rb            |   35 +++++++++++++
 spec/fixtures/sample_report.yml |  101 +++++++++++++++++++++++++++++++++++++++
 spec/models/report_spec.rb      |   19 +++++++
 4 files changed, 156 insertions(+), 1 deletions(-)
 create mode 100644 lib/puppet/report.rb
 create mode 100644 spec/fixtures/sample_report.yml

diff --git a/app/models/report.rb b/app/models/report.rb
index b3ef256..949b6a7 100644
--- a/app/models/report.rb
+++ b/app/models/report.rb
@@ -1,4 +1,4 @@
-require 'puppet'
+require 'lib/puppet/report'
 class Report < ActiveRecord::Base
   def self.per_page; 20 end # Pagination
 
diff --git a/lib/puppet/report.rb b/lib/puppet/report.rb
new file mode 100644
index 0000000..f7a1ccc
--- /dev/null
+++ b/lib/puppet/report.rb
@@ -0,0 +1,35 @@
+# Simple data objects used to reconstitute reports from YAML. This way we don't
+# have to load the full Puppet library, but it does mean that these must now be
+# updated to handle changes in the Puppet implementation.
+#
+module Puppet #:nodoc:
+  module Transaction
+    class Report
+      attr_reader :logs, :metrics, :host, :time
+    end
+  end
+
+  module Util
+    class Metric
+      attr_reader :type, :name, :values, :label
+
+      # Return a specific value
+      def [](name)
+        value = @values.find { |v| v[0] == name }
+        value && value[2]
+      end
+
+      def inspect
+        "#<#{self.class}:0x#{object_id.to_s(16)} name: #{name.inspect}>"
+      end
+    end
+
+    class Log
+      attr_reader :file, :level, :line, :message, :source, :tags, :time, 
:version
+
+      def inspect
+        "#<#{self.class}:0x#{object_id.to_s(16)} #{"%6s" % level}: 
#{message.inspect}>"
+      end
+    end
+  end
+end
diff --git a/spec/fixtures/sample_report.yml b/spec/fixtures/sample_report.yml
new file mode 100644
index 0000000..a3b9fe5
--- /dev/null
+++ b/spec/fixtures/sample_report.yml
@@ -0,0 +1,101 @@
+--- !ruby/object:Puppet::Transaction::Report 
+host: sample_node
+logs: 
+- !ruby/object:Puppet::Util::Log 
+  level: :info
+  message: Applying configuration version '1258679330'
+  source: Puppet
+  tags: 
+  - info
+  time: 2009-11-19 17:08:50.557829 -08:00
+- !ruby/object:Puppet::Util::Log 
+  level: :info
+  message: Adding /tmp/puppet_test(6d0007e52f7afb7d5a0650b0ffb8a4d1)
+  source: Filebucket[/tmp/puppet/var/clientbucket]
+  tags: 
+  - info
+  time: 2009-11-19 17:08:50.605975 -08:00
+- !ruby/object:Puppet::Util::Log 
+  file: /tmp/puppet/manifests/site.pp
+  level: :info
+  line: 4
+  message: Filebucketed /tmp/puppet_test to puppet with sum 
6d0007e52f7afb7d5a0650b0ffb8a4d1
+  source: //Node[default]/File[/tmp/puppet_test]
+  tags: 
+  - file
+  - node
+  - default
+  - class
+  - main
+  - info
+  time: 2009-11-19 17:08:50.607171 -08:00
+  version: 1258679330
+- !ruby/object:Puppet::Util::Log 
+  file: /tmp/puppet/manifests/site.pp
+  level: :notice
+  line: 4
+  message: content changed '{md5}6d0007e52f7afb7d5a0650b0ffb8a4d1' to 'unknown 
checksum'
+  source: //Node[default]/File[/tmp/puppet_test]/content
+  tags: 
+  - file
+  - node
+  - default
+  - class
+  - main
+  - content
+  - notice
+  time: 2009-11-19 17:08:50.625690 -08:00
+  version: 1258679330
+metrics: 
+  time: !ruby/object:Puppet::Util::Metric 
+    label: Time
+    name: time
+    values: 
+    - - :config_retrieval
+      - Config retrieval
+      - 0.185256958007812
+    - - :total
+      - Total
+      - 0.253255844116211
+    - - :file
+      - File
+      - 0.0679988861083984
+  resources: !ruby/object:Puppet::Util::Metric 
+    label: Resources
+    name: resources
+    values: 
+    - - :out_of_sync
+      - Out of sync
+      - 1
+    - - :total
+      - Total
+      - 3
+    - - :scheduled
+      - Scheduled
+      - 1
+    - - :skipped
+      - Skipped
+      - 0
+    - - :applied
+      - Applied
+      - 1
+    - - :restarted
+      - Restarted
+      - 0
+    - - :failed_restarts
+      - Failed restarts
+      - 0
+    - - :failed
+      - Failed
+      - 0
+  changes: !ruby/object:Puppet::Util::Metric 
+    label: Changes
+    name: changes
+    values: 
+    - - :total
+      - Total
+      - 1
+records: {}
+
+time: 2009-11-19 17:08:50.631428 -08:00
+
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index 6bf80b6..efe1012 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -48,4 +48,23 @@ describe Report do
       }.should change { @node.reported_at }.to(@report_data.time)
     end
   end
+
+  describe "deserializing the report" do
+    before do
+      yaml_file = File.join RAILS_ROOT, "spec", "fixtures", "sample_report.yml"
+      @loading_yaml = proc { YAML.load_file(yaml_file) }
+    end
+
+    it "should be able to parse the report" do
+      @loading_yaml.should_not raise_error
+    end
+
+    it "should return a puppet report object" do
+      @loading_yaml.call.should be_a_kind_of Puppet::Transaction::Report
+    end
+
+    it "should have the correct time" do
+      @loading_yaml.call.time.to_yaml.should == "--- 2009-11-19 
17:08:50.631428 -08:00\ntt"
+    end
+  end
 end
-- 
1.7.0.2

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to