railsmachine-shadow_facter 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Readme.rdoc ADDED
@@ -0,0 +1,33 @@
1
+ = ShadowFacter
2
+
3
+ ShadowFacter is a Ruby DSL for Facter, extracted out of the work we at Rails
4
+ Machine are doing on Moonshine[http://blog.railsmachine.com/articles/2009/01/16/moonshine-configuration-management-and-deployment/].
5
+
6
+ ShadowFacter provides a DSL for creating facts and processing them using Facter. A binary[http://railsmachine.github.com/shadow_facter/files/bin/shadow_facter.html] is provided to parse facts.
7
+
8
+
9
+ Example:
10
+
11
+ $ cat examples/lib/facts/kernel.rb
12
+ require 'shadow_facter'
13
+
14
+ namespace :kernel do
15
+ fact :name do
16
+ exec "uname -s"
17
+ end
18
+
19
+ fact :release do
20
+ exec "uname -r"
21
+ end
22
+
23
+ fact :version do
24
+ value(:release).to_s.split('.')[0]
25
+ end
26
+ end
27
+
28
+ Executing this fact:
29
+
30
+ $ bin/shadow_facter examples/lib/facts/kernel.rb
31
+ kernel_name => Darwin
32
+ kernel_version => 9
33
+ kernel_release => 9.6.0
data/bin/shadow_facter ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env ruby
2
+ # ShadowFacter allows the simple definition and gathering of facts
3
+ # using Facter[http://reductivelabs.com/projects/facter/].
4
+ #
5
+ #== Sample facts:
6
+ #
7
+ # $ cat examples/lib/facts/kernel.rb
8
+ # require 'shadow_facter'
9
+ #
10
+ # namespace :kernel do
11
+ # fact :name do
12
+ # exec "uname -s"
13
+ # end
14
+ #
15
+ # fact :release do
16
+ # exec "uname -r"
17
+ # end
18
+ #
19
+ # fact :version do
20
+ # facts(:kernel)[:release].to_s.split('.')[0]
21
+ # end
22
+ # end
23
+ #
24
+ #== Executing this fact:
25
+ #
26
+ # $ bin/shadow_facter examples/lib/facts/kernel.rb
27
+ # kernel_name => Darwin
28
+ # kernel_version => 9
29
+ # kernel_release => 9.6.0
30
+ # $
31
+ #
32
+ # The shadow_facter binary parses the given ruby code, which is
33
+ # expected to contain a definition of a namespace and some facts. Facts are
34
+ # gathered and outputed.
35
+
36
+ begin
37
+
38
+ require 'optparse'
39
+ opts = OptionParser.new do |opts|
40
+ opts.banner = <<-EOF
41
+ NAME
42
+ ShadowFacter
43
+
44
+ AUTHOR
45
+ Bradley Taylor
46
+ bradley@railsmachine.com
47
+
48
+ DESCRIPTION
49
+ A Ruby DSL for Facter
50
+
51
+ EXAMPLES
52
+ Sample fact:
53
+
54
+ $ cat examples/lib/facts/kernel.rb
55
+ require 'shadow_facter'
56
+
57
+ namespace :kernel do
58
+ fact :name do
59
+ exec "uname -s"
60
+ end
61
+
62
+ fact :release do
63
+ exec "uname -r"
64
+ end
65
+
66
+ fact :version do
67
+ value(:release).to_s.split('.')[0]
68
+ end
69
+ end
70
+
71
+ Executing this fact:
72
+
73
+ $ bin/shadow_facter examples/lib/facts/kernel.rb
74
+ kernel_name => Darwin
75
+ kernel_version => 9
76
+ kernel_release => 9.6.0
77
+ $
78
+
79
+ The shadow_facter binary parses the given ruby code, which is
80
+ expected to contain a definition of a namespace and some facts. Facts are
81
+ gathered and returned and pretty printed.
82
+
83
+ EOF
84
+ end
85
+
86
+ opts.parse!
87
+
88
+ filename = ARGV[0]
89
+ raise ArgumentError unless filename
90
+
91
+ require 'rubygems'
92
+ require 'shadow_facter'
93
+
94
+ load filename
95
+
96
+ namespaces.each do |namespace|
97
+ facts(namespace).to_hash.each do |k, v|
98
+ puts namespace.to_s + "_" + k.to_s + " => " + v.to_s
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,150 @@
1
+ # ShadowFacter allows the simple definition and gathering of facts
2
+ # using Facter[http://reductivelabs.com/projects/facter/]
3
+ #
4
+
5
+ require 'facter'
6
+
7
+ module ShadowFacter
8
+ class Facts
9
+
10
+ def initialize(namespace, keys)
11
+ @namespace = namespace
12
+ @keys = keys
13
+ end
14
+
15
+ # Return a fact value by key. Returns nil if non-existent or not constrained.
16
+ def [](key)
17
+ value(key)
18
+ end
19
+
20
+ # Return a fact value by key. Returns nil if non-existent or not constrained.
21
+ def value(key)
22
+ f = Facter[Base.facter_key(@namespace, key)]
23
+ f.value unless f.nil?
24
+ end
25
+
26
+ # Return an array of all of the constrained fact keys.
27
+ def keys
28
+ @keys.uniq.select { |k| !value(k).nil? }
29
+ end
30
+
31
+ # Return a boolean on the availability of a fact.
32
+ def has_fact?(key)
33
+ !value(key).nil?
34
+ end
35
+
36
+ # Return a hash of all of the constrained fact keys and values.
37
+ def to_hash
38
+ keys.inject({}) do |h, k|
39
+ h[k] = value(k)
40
+ h
41
+ end
42
+ end
43
+
44
+ # Return yaml of all of the constrained fact keys and values.
45
+ def to_yaml
46
+ require 'yaml'
47
+ to_hash.to_yaml
48
+ end
49
+
50
+ # Return json of all of the constrained fact keys and values.
51
+ def to_json
52
+ require 'json'
53
+ JSON.pretty_generate to_hash
54
+ end
55
+
56
+ # Reload facts in namespace.
57
+ def reload!
58
+ keys.each { |key| Facter[Base.facter_key(@namespace, key)].flush }
59
+ end
60
+
61
+ end
62
+
63
+
64
+ class Base
65
+ class << self
66
+
67
+ # Define a namespace.
68
+ def namespace(name)
69
+ @namespaces ||= Hash.new
70
+ raise "Nested namespaces not supported yet!" unless @current_namespace.nil?
71
+ @current_namespace = name
72
+ yield
73
+ @current_namespace = nil
74
+ end
75
+
76
+ # Return an array of defined namespaces names.
77
+ def namespaces()
78
+ @namespaces.keys
79
+ end
80
+
81
+ # Define a fact in Facter using a value or block. Can be confined with a hash
82
+ # of Facter keys (namespace_key) and value.
83
+ #
84
+ # Examples:
85
+ # fact :tea, "oolong"
86
+ # fact :tea, "puerh", {:drinks_season => "winter"}
87
+ # fact(:rand) { rand }
88
+ def fact(key, value=nil, confine_args={}, &block)
89
+ raise "Namespace required!" unless current_namespace
90
+ @namespaces[current_namespace] ||= []
91
+ @namespaces[current_namespace] << key
92
+ block = lambda { value.to_s } unless block_given?
93
+ Facter.add(current_key(key)) do
94
+ confine confine_args unless confine_args.empty?
95
+ setcode block
96
+ end
97
+ end
98
+
99
+ # Return an instance of the Facts class for the specified namespace.
100
+ def facts(namespace)
101
+ ShadowFacter::Facts.new(namespace, @namespaces[namespace])
102
+ end
103
+
104
+ # Construct a namespaced key for using with Facter.
105
+ def facter_key(namespace, key)
106
+ (namespace.to_s + "_" + key.to_s).to_sym
107
+ end
108
+
109
+ def current_namespace
110
+ @current_namespace
111
+ end
112
+
113
+ def current_key(key)
114
+ facter_key(current_namespace, key)
115
+ end
116
+
117
+ private :current_namespace, :current_key
118
+ end
119
+ end
120
+ end
121
+
122
+ # Return an array of defined namespaces names.
123
+ def namespaces()
124
+ ShadowFacter::Base.namespaces
125
+ end
126
+
127
+ # Define a namespace.
128
+ def namespace(name, &b)
129
+ ShadowFacter::Base.namespace(name, &b)
130
+ end
131
+
132
+ # Define a fact in Facter using a value or block. Can be confined with a hash
133
+ # of Facter keys (namespace_key) and value.
134
+ # Examples:
135
+ # fact :tea, "oolong"
136
+ # fact :tea, "puerh", {:drinks_season => "winter"}
137
+ # fact(:rand) { rand }
138
+ def fact(key, value=nil, confine_args={}, &b)
139
+ ShadowFacter::Base.fact(key, value, confine_args, &b)
140
+ end
141
+
142
+ # Return an instance of the Facts class for the specified namespace.
143
+ def facts(namespace)
144
+ ShadowFacter::Base.facts(namespace)
145
+ end
146
+
147
+ # Execute a system command via Facter.
148
+ def exec(command)
149
+ Facter::Util::Resolution.exec(command)
150
+ end
@@ -0,0 +1,4 @@
1
+ require File.join(File.dirname(__FILE__) + '/shadow_facter', 'base.rb')
2
+
3
+
4
+
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: railsmachine-shadow_facter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bradley Taylor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-19 00:00:00 -08:00
13
+ default_executable: shadow_facter
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: facter
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.5.2
23
+ version:
24
+ description: Ruby simple DSL wrapper library for Facter
25
+ email:
26
+ - bradley@railsmachine.com
27
+ executables:
28
+ - shadow_facter
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - Readme.rdoc
35
+ - lib/shadow_facter.rb
36
+ - lib/shadow_facter/base.rb
37
+ - bin/shadow_facter
38
+ has_rdoc: true
39
+ homepage: http://railsmachine.com
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project: moonshine
60
+ rubygems_version: 1.2.0
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Facter wrapper library
64
+ test_files: []
65
+