Removing the symlink type finally.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1900 980ebf18-57e1-0310-9a29-db15c13687c0
This commit is contained in:
Родитель
cdd1e6e19e
Коммит
d3b76d6e0d
|
@ -1,189 +0,0 @@
|
|||
require 'etc'
|
||||
require 'puppet/type/state'
|
||||
require 'puppet/type/pfile'
|
||||
|
||||
module Puppet
|
||||
newtype(:symlink) do
|
||||
@doc = "Create symbolic links to existing files. **This type is deprecated;
|
||||
use file instead.**"
|
||||
#newstate(:ensure) do
|
||||
ensurable do
|
||||
require 'etc'
|
||||
attr_accessor :file
|
||||
|
||||
desc "Create a link to another file. Currently only symlinks
|
||||
are supported, and attempts to replace normal files with
|
||||
links will currently fail, while existing but incorrect symlinks
|
||||
will be removed."
|
||||
|
||||
validate do |value|
|
||||
unless value == :absent or value =~ /^#{File::SEPARATOR}/
|
||||
raise Puppet::Error, "Invalid symlink %s" % value
|
||||
end
|
||||
end
|
||||
|
||||
nodefault
|
||||
|
||||
def create
|
||||
begin
|
||||
unless File.symlink(self.should,self.parent[:path])
|
||||
self.fail "Could not create symlink '%s'" %
|
||||
self.parent[:path]
|
||||
end
|
||||
rescue => detail
|
||||
self.fail "Cannot create symlink '%s': %s" %
|
||||
[self.parent[:path],detail]
|
||||
end
|
||||
end
|
||||
|
||||
def remove
|
||||
if FileTest.symlink?(self.parent[:path])
|
||||
begin
|
||||
File.unlink(self.parent[:path])
|
||||
rescue
|
||||
self.fail "Failed to remove symlink '%s'" %
|
||||
self.parent[:path]
|
||||
end
|
||||
elsif FileTest.exists?(self.parent[:path])
|
||||
self.fail "Cannot remove normal file '%s'" %
|
||||
self.parent[:path]
|
||||
else
|
||||
@parent.debug("Symlink '%s' does not exist" %
|
||||
self.parent[:path])
|
||||
end
|
||||
end
|
||||
|
||||
def retrieve
|
||||
stat = nil
|
||||
|
||||
if FileTest.symlink?(self.parent[:path])
|
||||
self.is = File.readlink(self.parent[:path])
|
||||
return
|
||||
else
|
||||
self.is = :absent
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
# this is somewhat complicated, because it could exist and be
|
||||
# a link
|
||||
def sync
|
||||
case self.should
|
||||
when :absent
|
||||
self.remove()
|
||||
return :symlink_removed
|
||||
when /^#{File::SEPARATOR}/
|
||||
if FileTest.symlink?(self.parent[:path])
|
||||
path = File.readlink(self.parent[:path])
|
||||
if path != self.should
|
||||
self.remove()
|
||||
self.create()
|
||||
return :symlink_changed
|
||||
else
|
||||
self.info "Already in sync"
|
||||
return nil
|
||||
end
|
||||
elsif FileTest.exists?(self.parent[:path])
|
||||
self.fail "Cannot replace normal file '%s'" %
|
||||
self.parent[:path]
|
||||
else
|
||||
self.create()
|
||||
return :symlink_created
|
||||
end
|
||||
else
|
||||
raise Puppet::DevError, "Got invalid symlink value %s" %
|
||||
self.should
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
attr_reader :stat, :params
|
||||
|
||||
copyparam(Puppet.type(:file), :path)
|
||||
|
||||
newparam(:recurse) do
|
||||
desc "If target is a directory, recursively create
|
||||
directories (using `file`'s `source` parameter) and link all
|
||||
contained files. For instance:
|
||||
|
||||
# The Solaris Blastwave repository installs everything
|
||||
# in /opt/csw; link it into /usr/local
|
||||
symlink { \"/usr/local\":
|
||||
ensure => \"/opt/csw\",
|
||||
recurse => true
|
||||
}
|
||||
|
||||
|
||||
Note that this does not link directories -- any directories
|
||||
are created in the destination, and any files are linked over."
|
||||
|
||||
munge do |value|
|
||||
@stat = nil
|
||||
@target = @parent.state(:ensure).should
|
||||
|
||||
self.setparent(@target)
|
||||
end
|
||||
|
||||
def setparent(value)
|
||||
# we want to remove our state, because we're creating children
|
||||
# to do the links
|
||||
if FileTest.exist?(@target)
|
||||
@stat = File.lstat(@target)
|
||||
else
|
||||
@setparent = false
|
||||
return
|
||||
end
|
||||
|
||||
# if we're a directory, then we descend into it; we only actually
|
||||
# link to real files
|
||||
unless @stat.directory?
|
||||
return
|
||||
end
|
||||
|
||||
@parent.delete(:ensure)
|
||||
|
||||
recurse = value
|
||||
# we might have a string, rather than a number
|
||||
if recurse.is_a?(String)
|
||||
if recurse =~ /^[0-9]+$/
|
||||
recurse = Integer(recurse)
|
||||
#elsif recurse =~ /^inf/ # infinite recursion
|
||||
else # anything else is infinite recursion
|
||||
recurse = true
|
||||
end
|
||||
end
|
||||
|
||||
# are we at the end of the recursion?
|
||||
if recurse == 0
|
||||
return
|
||||
end
|
||||
|
||||
# okay, we're not going to recurse ourselves here; instead, we're
|
||||
# going to rely on the fact that we've already got all of that
|
||||
# working in pfile
|
||||
|
||||
args = {
|
||||
:path => @parent[:path],
|
||||
:linkmaker => true,
|
||||
:recurse => recurse,
|
||||
:source => @target
|
||||
}
|
||||
|
||||
dir = Puppet.type(:file).implicitcreate(args)
|
||||
@parent.push dir
|
||||
@setparent = true
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(hash)
|
||||
tmphash = hash.to_hash
|
||||
super
|
||||
@arghash = tmphash
|
||||
@arghash.delete(self.class.namevar)
|
||||
#@arghash = self.argclean(hash.dup)
|
||||
#@arghash.delete(self.class.namevar)
|
||||
end
|
||||
end # Puppet.type(:symlink)
|
||||
end
|
||||
|
||||
# $Id$
|
|
@ -1,117 +0,0 @@
|
|||
#!/usr/bin/env ruby
|
||||
|
||||
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
|
||||
|
||||
require 'puppet'
|
||||
require 'puppettest'
|
||||
|
||||
# $Id$
|
||||
|
||||
class TestSymlink < Test::Unit::TestCase
|
||||
include PuppetTest::FileTesting
|
||||
def mktmpfile
|
||||
# because luke's home directory is on nfs, it can't be used for testing
|
||||
# as root
|
||||
tmpfile = tempfile()
|
||||
File.open(tmpfile, "w") { |f| f.puts rand(100) }
|
||||
@@tmpfiles.push tmpfile
|
||||
return tmpfile
|
||||
end
|
||||
|
||||
def mktmpdir
|
||||
dir = File.join(tmpdir(), "puppetlinkdir")
|
||||
unless FileTest.exists?(dir)
|
||||
Dir.mkdir(dir)
|
||||
end
|
||||
@@tmpfiles.push dir
|
||||
return dir
|
||||
end
|
||||
|
||||
def tmplink
|
||||
link = File.join(tmpdir(), "puppetlinktest")
|
||||
@@tmpfiles.push link
|
||||
return link
|
||||
end
|
||||
|
||||
def newlink(hash = {})
|
||||
hash[:name] = tmplink()
|
||||
unless hash.include?(:ensure)
|
||||
hash[:ensure] = mktmpfile()
|
||||
end
|
||||
|
||||
link = Puppet.type(:symlink).create(hash)
|
||||
return link
|
||||
end
|
||||
|
||||
def test_target
|
||||
link = nil
|
||||
file = mktmpfile()
|
||||
assert_nothing_raised() {
|
||||
link = newlink()
|
||||
}
|
||||
assert_nothing_raised() {
|
||||
link.retrieve
|
||||
}
|
||||
# we might already be in sync
|
||||
assert(!link.insync?())
|
||||
assert_apply(link)
|
||||
assert_nothing_raised() {
|
||||
link.retrieve
|
||||
}
|
||||
assert(link.insync?())
|
||||
end
|
||||
|
||||
def test_recursion
|
||||
source = mktmpdir()
|
||||
FileUtils.cd(source) {
|
||||
mkranddirsandfiles()
|
||||
}
|
||||
|
||||
link = nil
|
||||
assert_nothing_raised {
|
||||
link = newlink(:ensure => source, :recurse => true)
|
||||
}
|
||||
comp = newcomp(link)
|
||||
cycle(comp)
|
||||
|
||||
path = link.name
|
||||
assert(FileTest.directory?(path), "Did not make %s" % path)
|
||||
list = file_list(path)
|
||||
FileUtils.cd(path) {
|
||||
list.each { |file|
|
||||
unless FileTest.directory?(file)
|
||||
assert(FileTest.symlink?(file), "file %s is not a symlink" %
|
||||
file)
|
||||
target = File.readlink(file)
|
||||
assert_equal(target,File.join(source,file.sub(/^\.\//,'')))
|
||||
end
|
||||
}
|
||||
}
|
||||
end
|
||||
|
||||
def disabled_test_createdrecursion
|
||||
source = tempfile()
|
||||
file = File.join(source, "file")
|
||||
dest = tempfile()
|
||||
link = File.join(dest, "file")
|
||||
|
||||
objects = []
|
||||
objects << Puppet.type(:file).create(
|
||||
:path => source,
|
||||
:ensure => "directory"
|
||||
)
|
||||
objects << Puppet.type(:file).create(
|
||||
:path => file,
|
||||
:ensure => "file"
|
||||
)
|
||||
objects << Puppet.type(:symlink).create(
|
||||
:path => dest,
|
||||
:ensure => source,
|
||||
:recurse => true
|
||||
)
|
||||
|
||||
assert_apply(*objects)
|
||||
|
||||
assert(FileTest.symlink?(link), "Link was not created")
|
||||
end
|
||||
end
|
Загрузка…
Ссылка в новой задаче