Class: Puppet::Network::Handler::FileServer

Inherits:
Handler
  • Object
show all
Defined in:
lib/vendor/puppet/network/handler/fileserver.rb

Defined Under Namespace

Classes: Mount, PluginMount

Constant Summary collapse

CHECKPARAMS =
[:mode, :type, :owner, :group, :checksum]
MODULES =

Special filserver module for puppet’s module system

"modules"
PLUGINS =
"plugins"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ FileServer

Create a new fileserving module.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 95

def initialize(hash = {})
  @mounts = {}
  @files = {}

  @local = hash[:Local]

  @noreadconfig = true if hash[:Config] == false

  @passed_configuration_path = hash[:Config]

  if hash.include?(:Mount)
    @passedconfig = true
    raise Puppet::DevError, "Invalid mount hash #{hash[:Mount].inspect}" unless hash[:Mount].is_a?(Hash)

    hash[:Mount].each { |dir, name|
      self.mount(dir, name) if FileTest.exists?(dir)
    }
    self.mount(nil, MODULES)
    self.mount(nil, PLUGINS)
  else
    @passedconfig = false
    if configuration
      readconfig(false) # don't check the file the first time.
    else
      create_default_mounts
    end
  end
end

Instance Attribute Details

#localObject

Returns the value of attribute local.



21
22
23
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 21

def local
  @local
end

Class Method Details

.paramsObject



35
36
37
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 35

def self.params
  CHECKPARAMS.dup
end

Instance Method Details

#configurationObject

If the configuration file exists, then create (if necessary) a LoadedFile object to manage it; else, return nil.



41
42
43
44
45
46
47
48
49
50
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 41

def configuration
  # Short-circuit the default case.
  return @configuration if defined?(@configuration)

  config_path = @passed_configuration_path || Puppet[:fileserverconfig]
  return nil unless FileTest.exist?(config_path)

  # The file exists but we don't have a LoadedFile instance for it.
  @configuration = Puppet::Util::LoadedFile.new(config_path)
end

#create_default_mountsObject

Create our default mounts for modules and plugins. This is duplicated code, but I’m not really worried about that.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 54

def create_default_mounts
  @mounts = {}
  Puppet.debug "No file server configuration file; autocreating #{MODULES} mount with default permissions"
  mount = Mount.new(MODULES)
  mount.allow("*")
  @mounts[MODULES] = mount

  Puppet.debug "No file server configuration file; autocreating #{PLUGINS} mount with default permissions"
  mount = PluginMount.new(PLUGINS)
  mount.allow("*")
  @mounts[PLUGINS] = mount
end

#describe(url, links = :follow, client = nil, clientip = nil) ⇒ Object

Describe a given file. This returns all of the manageable aspects of that file.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 69

def describe(url, links = :follow, client = nil, clientip = nil)
  links = links.intern if links.is_a? String

  mount, path = convert(url, client, clientip)

  mount.debug("Describing #{url} for #{client}") if client

  # use the mount to resolve the path for us.
  return "" unless full_path = mount.file_path(path, client)

   = Puppet::FileServing::Metadata.new(url, :path => full_path, :links => links)

  return "" unless .exist?

  begin
    .collect
  rescue => detail
    puts detail.backtrace if Puppet[:trace]
    Puppet.err detail
    return ""
  end

  .attributes_with_tabs
end

#list(url, links = :ignore, recurse = false, ignore = false, client = nil, clientip = nil) ⇒ Object

List a specific directory’s contents.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 125

def list(url, links = :ignore, recurse = false, ignore = false, client = nil, clientip = nil)
  mount, path = convert(url, client, clientip)

  mount.debug "Listing #{url} for #{client}" if client

  return "" unless mount.path_exists?(path, client)

  desc = mount.list(path, recurse, ignore, client)

  if desc.length == 0
    mount.notice "Got no information on //#{mount}/#{path}"
    return ""
  end

  desc.collect { |sub| sub.join("\t") }.join("\n")
end

#local?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 142

def local?
  self.local
end

#mount(path, name) ⇒ Object

Mount a new directory with a name.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 152

def mount(path, name)
  if @mounts.include?(name)
    if @mounts[name] != path
      raise FileServerError, "#{@mounts[name].path} is already mounted at #{name}"
    else
      # it's already mounted; no problem
      return
    end
  end

  # Let the mounts do their own error-checking.
  @mounts[name] = Mount.new(name, path)
  @mounts[name].info "Mounted #{path}"

  @mounts[name]
end

#mounted?(name) ⇒ Boolean

Is a given mount available?

Returns:

  • (Boolean)


147
148
149
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 147

def mounted?(name)
  @mounts.include?(name)
end

#retrieve(url, links = :ignore, client = nil, clientip = nil) ⇒ Object

Retrieve a file from the local disk and pass it to the remote client.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 171

def retrieve(url, links = :ignore, client = nil, clientip = nil)
  links = links.intern if links.is_a? String

  mount, path = convert(url, client, clientip)

  mount.info "Sending #{url} to #{client}" if client

  unless mount.path_exists?(path, client)
    mount.debug "#{mount} reported that #{path} does not exist"
    return ""
  end

  links = links.intern if links.is_a? String

  if links == :ignore and FileTest.symlink?(path)
    mount.debug "I think that #{path} is a symlink and we're ignoring them"
    return ""
  end

  str = mount.read_file(path, client)

  if @local
    return str
  else
    return CGI.escape(str)
  end
end

#umount(name) ⇒ Object



199
200
201
# File 'lib/vendor/puppet/network/handler/fileserver.rb', line 199

def umount(name)
  @mounts.delete(name) if @mounts.include? name
end