summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-07-03 20:06:32 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-07-03 20:06:32 +0000
commite57c5131aee017bd015e9419b5a1ceeb306677a7 (patch)
tree597984120e13b03bd96017afb84a050a728e17ea /lib
parent25cf31b00057d552fcffde568049e37655c31fbc (diff)
downloadpuppet-e57c5131aee017bd015e9419b5a1ceeb306677a7.tar.gz
puppet-e57c5131aee017bd015e9419b5a1ceeb306677a7.tar.xz
puppet-e57c5131aee017bd015e9419b5a1ceeb306677a7.zip
Adding Ruby Gem support to packaging
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1354 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/type/package.rb48
-rwxr-xr-xlib/puppet/type/package/gem.rb119
2 files changed, 123 insertions, 44 deletions
diff --git a/lib/puppet/type/package.rb b/lib/puppet/type/package.rb
index ba0b307d1..58afe2c1c 100644
--- a/lib/puppet/type/package.rb
+++ b/lib/puppet/type/package.rb
@@ -60,6 +60,10 @@ module Puppet
mod.module_eval(&block)
+ class << mod
+ include Puppet::Util
+ end
+
# It's at least conceivable that a module would not define this method
# "module_function" makes the :list method private, so if the parent
# method also called module_function, then it's already private
@@ -575,50 +579,6 @@ module Puppet
end
end
end # Puppet.type(:package)
-
- # this is how we retrieve packages
- class PackageSource
- attr_accessor :uri
- attr_writer :retrieve
-
- @@sources = Hash.new(false)
-
- def PackageSource.get(file)
- type = file.sub(%r{:.+},'')
- source = nil
- if source = @@sources[type]
- return source.retrieve(file)
- else
- raise Puppet::Error, "Unknown package source: %s" % type
- end
- end
-
- def initialize(name)
- if block_given?
- yield self
- end
-
- @@sources[name] = self
- end
-
- def retrieve(path)
- @retrieve.call(path)
- end
-
- end
-
- PackageSource.new("file") { |obj|
- obj.retrieve = proc { |path|
- # this might not work for windows...
- file = path.sub(%r{^file://},'')
-
- if FileTest.exists?(file)
- return file
- else
- raise Puppet::Error, "File %s does not exist" % file
- end
- }
- }
end
# $Id$
diff --git a/lib/puppet/type/package/gem.rb b/lib/puppet/type/package/gem.rb
new file mode 100755
index 000000000..3763cf992
--- /dev/null
+++ b/lib/puppet/type/package/gem.rb
@@ -0,0 +1,119 @@
+module Puppet
+ Puppet.type(:package).newpkgtype(:gem) do
+ if gem = %x{which gem 2>/dev/null}.chomp and gem != ""
+ @@gem = gem
+ else
+ @@gem = nil
+ end
+
+ def self.extended(mod)
+ unless @@gem
+ raise Puppet::Error, "The gem command is missing; gems unavailable"
+ end
+ end
+
+ def gemlist(hash)
+ command = "#{@@gem} list "
+
+ if hash[:local]
+ command += "--local "
+ else
+ command += "--remote "
+ end
+
+ if hash[:justme]
+ command += self[:name]
+ end
+ begin
+ list = execute(command).split("\n\n").collect do |set|
+ if gemhash = gemsplit(set)
+ gemhash[:type] = :gem
+ gemhash[:ensure] = gemhash[:version][0]
+ gemhash
+ else
+ nil
+ end
+ end.reject { |p| p.nil? }
+ rescue ExecutionFailure => detail
+ raise Puppet::Error, "Could not list gems: %s" % detail
+ end
+
+ if hash[:justme]
+ return list.shift
+ else
+ return list
+ end
+ end
+
+ module_function :gemlist
+
+ def gemsplit(desc)
+ case desc
+ when /^\*\*\*/: return nil
+ when /^(\S+)\s+\((.+)\)\n/
+ name = $1
+ version = $2.split(/,\s*/)
+ return {
+ :name => name,
+ :version => version
+ }
+ else
+ Puppet.warning "Could not match %s" % desc
+ nil
+ end
+ end
+
+ module_function :gemsplit
+
+ def install(useversion = true)
+ command = "#{@@gem} install "
+ if self[:version] and useversion
+ command += "-v %s " % self[:version]
+ end
+ if source = self[:source]
+ command += source
+ else
+ command += self[:name]
+ end
+ begin
+ execute(command)
+ rescue ExecutionFailure => detail
+ raise Puppet::Error, "Could not install %s: %s" %
+ [self[:name], detail]
+ end
+ end
+
+ def latest
+ # This always gets the latest version available.
+ hash = gemlist(:justme => true)
+
+ return hash[:version][0]
+ end
+
+ def list(justme = false)
+ gemlist(:local => true).each do |hash|
+ Puppet::Type.type(:package).installedpkg(hash)
+ end
+ end
+
+ def query
+ gemlist(:justme => true, :local => true)
+ end
+
+ def uninstall
+ begin
+ # Remove everything, including the binaries.
+ execute("#{@@gem} uninstall -x -a #{self[:name]}")
+ rescue ExecutionFailure => detail
+ raise Puppet::Error, "Could not uninstall %s: %s" %
+ [self[:name], detail]
+ end
+ end
+
+ def update
+ self.install(false)
+ end
+ end
+end
+
+# $Id$