Class: EasyType::SourceDir

Inherits:
Puppet::Parameter
  • Object
show all
Includes:
Helpers
Defined in:
lib/easy_type/source_dir.rb

Overview

This class allows you to use a source anywhere you’d like. The source can be a:

  • local file

  • a file:/ type url

  • a puppet:/ type url

  • a http(s):// type url

If the file is a zip file, a tar file, a tgz or a tar.z, you can also auto extract the file to a certain directory.

By default the file will be fetches, uncompressed and unzipped (or untarred). You can control this however by:

auto_uncompress false
auto_unzip false

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#camelize, #convert_csv_data_to_hash, #get_puppet_file, included

Constructor Details

#initialize(options = {}) ⇒ SourceDir

Returns a new instance of SourceDir.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/easy_type/source_dir.rb', line 130

def initialize(options = {})
  if defined?(@@to_cleanup).nil?
    @@to_cleanup = []
    at_exit do
      Puppet.debug "Cleaning files created by source_dir attributes."
      @@to_cleanup.each do |file| 
        Puppet.debug "cleaning file/directory #{file}..."
        FileUtils.rm_rf(file)
      end
    end
  end
  @@fetched_files ||= {}
  super(**options)
  self
end

Class Method Details

.auto_uncompress(value) ⇒ Object

Control the automatic uncompresing of the file



174
175
176
# File 'lib/easy_type/source_dir.rb', line 174

def self.auto_uncompress(value)
  @do_uncompress = value
end

.auto_unpack(value) ⇒ Object

Control the automatic unpacking (unzip or untar) of the file



167
168
169
# File 'lib/easy_type/source_dir.rb', line 167

def self.auto_unpack(value)
  @do_unpack = value
end

.inherited(subclass) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
123
124
125
126
127
128
# File 'lib/easy_type/source_dir.rb', line 40

def self.inherited(subclass)
  subclass.desc <<-'DESC'
    A source directory, but can also contain a zip or a tar file.

    This parameter can contain the following types of values:

    * `puppet:` URIs, which point to files in modules or Puppet file server
    mount points.
    * Fully qualified paths to locally available files (including files on NFS
    shares or Windows mapped drives).
    * `file:` URIs, which behave the same as local file paths.
    * `http:` URIs, which point to files served by common web servers

    The normal form of a `puppet:` URI is:

    `puppet:///modules/<MODULE NAME>/<FILE PATH>`

    This will fetch a file from a module on the Puppet master (or from a
    local module when using Puppet apply). Given a `modulepath` of
    `/etc/puppetlabs/code/modules`, the example above would resolve to
    `/etc/puppetlabs/code/modules/<MODULE NAME>/files/<FILE PATH>`.

    ## Container file

    When the file is a container file, it will automaticaly be extracted. At this point in
    time the follwoing container types are supported:

    - zip
    - tar

    ## Compressed files

    When the file is compressed, it will be uncompressed before beeing procesed further. This means that for example
    a file `https://www.puppet.com/files/all.tar.gz` will be uncompressed before being unpackes with `tar`

    ## Examples

    Here are some examples:

    ### Regular directories

        ... { '...':
          ...
          source  => '/home/software',
          ...
        }

    The `/home/software` will be used by the custom type. Other Puppet code must make sure the directory contains the right files.

    ### A puppet url containing a zip file

        ... { '...':
          ...
          source  => 'puppet:///modules/software/software.zip',
          tmp_dir => '/tmp/mysoftware'
          ...
        }

    The `software.zip` file will be fetched from the puppet server software module and put in `/tmp/mysoftware`, it will be unzipped and used for the actions
    in the custom type. The file will be temporary put in


    ### A http url containing a tar file

        ... { '...':
          ...
          source  => 'http:///www.enterprisemodules.com/software/software.tar',
          tmp_dir => '/tmp/mysoftware'
          ...
        }


    The `software.tar` file will be fetched from the named web server and put in `/tmp/mysoftware`, it will be untarred and
    used for the actions in the custom type.

    ### A file url containing a compressed tar file

        ... { '...':
          ...
          source  => 'file:///nfsshare/software/software.tar.Z',
          tmp_dir => '/tmp/mysoftware'
          ...
        }

    The `software.tar.Z` file will be fetched from the named directory, it will be uncompressed and then untarred on and put in `/tmp/mysoftware`
    and used for the actions in the custom type.

  DESC
end

Instance Method Details

#cleanObject



212
213
214
215
# File 'lib/easy_type/source_dir.rb', line 212

def clean
  Puppet.debug "Deprecated clean function called. You can remove this call"
  # Do nothing, let the at exit handler do this
end

#normalize_file_name(file_spec) ⇒ Object

Make it so that file names from bith Unix and Windows can be used. Normalize the value to something Ruby/Puppet can use.



182
183
184
# File 'lib/easy_type/source_dir.rb', line 182

def normalize_file_name( file_spec)
  file_spec.gsub("\\","/")
end

#process(user = nil, group = nil) ⇒ Object

Process the parameter. If the source is not yet fetched, if will first fetch, uncompress and unpack the file.

The worflow is:

1) Decide on location of ‘tmp_dir`. If not specfied it is `/tmp/$title` 2) If the file is a remote file, copy it to the `tmp_dir` 3) If the file is compressed, uncompress it. In the same `tmp_dir` 4) If the file is a container (zip or tar) unpack it to the `tmp_dir$file_name`



198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/easy_type/source_dir.rb', line 198

def process(user = nil, group = nil)
    ensure_tmp_dir( user, group)
    @original_source = normalize_file_name(@value)
    if fetched?
      @value = @@fetched_files[@original_source]
    else
      fetch
      uncompress(user, group)
      unpack(user, group)
    end
    custom_action if defined?(custom_action)
    normalize_file_name(@value)
end