Class: Puppet::FileServing::Metadata

Inherits:
Base show all
Extended by:
Indirector
Includes:
Util::Checksums
Defined in:
lib/puppet/file_serving/metadata.rb

Overview

A class that handles retrieving file metadata.

API:

  • public

Defined Under Namespace

Classes: MetaStat, WindowsStat

Constant Summary collapse

PARAM_ORDER =

API:

  • public

[:mode, :ftype, :owner, :group]

Constants included from Indirector

Indirector::BadNameRegexp

Instance Attribute Summary collapse

Attributes inherited from Base

#links, #relative_path, #source

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Indirector

configure_routes, indirects

Methods included from Util::Checksums

#checksum?, #ctime, #ctime_file, #known_checksum_types, #md5, #md5_file, #md5_hex_length, #md5_stream, #md5lite, #md5lite_file, #mtime, #mtime_file, #mtime_stream, #none, #none_file, #none_stream, #sha1, #sha1_file, #sha1_hex_length, #sha1_stream, #sha1lite, #sha1lite_file, #sha256, #sha256_file, #sha256_hex_length, #sha256_stream, #sha256lite, #sha256lite_file, #sumdata, #sumtype

Methods inherited from Base

absolute?, #exist?, #full_path, #stat

Methods included from Util::MethodHelper

#requiredopts, #set_options, #symbolize_options

Constructor Details

#initialize(path, data = {}) ⇒ Metadata

Returns a new instance of Metadata.

API:

  • public



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet/file_serving/metadata.rb', line 144

def initialize(path,data={})
  @owner       = data.delete('owner')
  @group       = data.delete('group')
  @mode        = data.delete('mode')
  if checksum = data.delete('checksum')
    @checksum_type = checksum['type']
    @checksum      = checksum['value']
  end
  @checksum_type ||= Puppet[:digest_algorithm]
  @ftype       = data.delete('type')
  @destination = data.delete('destination')
  super(path,data)
end

Instance Attribute Details

#checksumObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def checksum
  @checksum
end

#checksum_typeObject

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def checksum_type
  @checksum_type
end

#destinationObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def destination
  @destination
end

#ftypeObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def ftype
  @ftype
end

#groupObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def group
  @group
end

#modeObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def mode
  @mode
end

#ownerObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def owner
  @owner
end

#pathObject (readonly)

API:

  • public



15
16
17
# File 'lib/puppet/file_serving/metadata.rb', line 15

def path
  @path
end

Class Method Details

.from_data_hash(data) ⇒ Object

API:

  • public



175
176
177
# File 'lib/puppet/file_serving/metadata.rb', line 175

def self.from_data_hash(data)
  new(data.delete('path'), data)
end

.from_pson(data) ⇒ Object

API:

  • public



194
195
196
197
# File 'lib/puppet/file_serving/metadata.rb', line 194

def self.from_pson(data)
  Puppet.deprecation_warning("from_pson is being removed in favour of from_data_hash.")
  self.from_data_hash(data)
end

Instance Method Details

#collect(source_permissions = nil) ⇒ Object

Retrieve the attributes for this file, relative to a base directory. Note that Puppet::FileSystem.stat(path) raises Errno::ENOENT if the file is absent and this method does not catch that exception.

API:

  • public



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/puppet/file_serving/metadata.rb', line 119

def collect(source_permissions = nil)
  real_path = full_path

  stat = collect_stat(real_path, source_permissions)
  @owner = stat.owner
  @group = stat.group
  @ftype = stat.ftype

  # We have to mask the mode, yay.
  @mode = stat.mode & 007777

  case stat.ftype
  when "file"
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", real_path).to_s
  when "directory" # Always just timestamp the directory.
    @checksum_type = "ctime"
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", path).to_s
  when "link"
    @destination = Puppet::FileSystem.readlink(real_path)
    @checksum = ("{#{@checksum_type}}") + send("#{@checksum_type}_file", real_path).to_s rescue nil
  else
    raise ArgumentError, "Cannot manage files of type #{stat.ftype}"
  end
end

#collect_stat(path, source_permissions) ⇒ Object

API:

  • public



106
107
108
109
110
111
112
113
114
# File 'lib/puppet/file_serving/metadata.rb', line 106

def collect_stat(path, source_permissions)
  stat = stat()

  if Puppet.features.microsoft_windows?
    WindowsStat.new(stat, path, source_permissions)
  else
    MetaStat.new(stat, source_permissions)
  end
end

#to_data_hashObject

API:

  • public



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/puppet/file_serving/metadata.rb', line 158

def to_data_hash
  super.update(
    {
      'owner'        => owner,
      'group'        => group,
      'mode'         => mode,
      'checksum'     => {
        'type'   => checksum_type,
        'value'  => checksum
      },
      'type'         => ftype,
      'destination'  => destination,

    }
  )
end

#to_pson(*args) ⇒ Object

API:

  • public



190
191
192
# File 'lib/puppet/file_serving/metadata.rb', line 190

def to_pson(*args)
  to_pson_data_hash.to_pson(*args)
end

#to_pson_data_hashObject

API:

  • public



180
181
182
183
184
185
186
187
188
# File 'lib/puppet/file_serving/metadata.rb', line 180

def to_pson_data_hash
  {
    'document_type' => 'FileMetadata',
    'data'          => to_data_hash,
    'metadata'      => {
      'api_version' => 1
      }
  }
end