Class: FPM::Cookery::Packager

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fpm/cookery/packager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, config = {}) ⇒ Packager

Returns a new instance of Packager.



20
21
22
23
# File 'lib/fpm/cookery/packager.rb', line 20

def initialize(recipe, config = {})
  @recipe = recipe
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/fpm/cookery/packager.rb', line 18

def config
  @config
end

#recipeObject (readonly)

Returns the value of attribute recipe.



18
19
20
# File 'lib/fpm/cookery/packager.rb', line 18

def recipe
  @recipe
end

Instance Method Details

#add_scripts(recipe, input) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/fpm/cookery/packager.rb', line 222

def add_scripts(recipe, input)
  error = false
  scripts = [:pre_install, :post_install, :pre_uninstall, :post_uninstall]

  scripts.each do |script|
    unless recipe.send(script).nil?
      script_file = FPM::Cookery::Path.new(recipe.send(script))

      # If the script file is an absolute path, just use that path.
      # Otherwise consider the location relative to the recipe.
      unless script_file.absolute?
        script_file = File.expand_path("../#{script_file.to_s}", recipe.filename)
      end

      if File.exists?(script_file)
        input.add_script(script, File.read(script_file.to_s))
      else
        Log.error "#{script} script '#{script_file}' is missing"
        error = true
      end
    end
  end

  exit(1) if error
end


183
184
185
# File 'lib/fpm/cookery/packager.rb', line 183

def build_cookie_name(name)
  (recipe.builddir/".build-cookie-#{name.gsub(/[^\w]/,'_')}").to_s
end

#build_package(recipe, config) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fpm/cookery/packager.rb', line 187

def build_package(recipe, config)
  recipe.pkgdir.mkdir
  Dir.chdir(recipe.pkgdir) do
    version = FPM::Cookery::Package::Version.new(recipe, @target, config)
    maintainer = FPM::Cookery::Package::Maintainer.new(recipe, config)

    input = recipe.input(config)

    input.version = version
    input.maintainer = maintainer.to_s
    input.vendor = version.vendor if version.vendor
    input.epoch = version.epoch if version.epoch

    add_scripts(recipe, input)

    output_class = FPM::Package.types[@target]

    output = input.convert(output_class)

    recipe.run_lifecycle_hook(:before_package_create, output)
    begin
      output.output(output.to_s)
      recipe.run_lifecycle_hook(:after_package_create, output)
    rescue FPM::Package::FileAlreadyExists
      Log.info "Removing existing package file: #{output.to_s}"
      FileUtils.rm_f(output.to_s)
      retry
    ensure
      input.cleanup if input
      output.cleanup if output
      Log.info "Created package: #{File.join(Dir.pwd, output.to_s)}"
    end
  end
end

#cleanupObject



38
39
40
41
42
43
44
# File 'lib/fpm/cookery/packager.rb', line 38

def cleanup
  Log.info "Cleanup!"
  # TODO(sissel): do some sanity checking to make sure we don't
  # accidentally rm -rf the wrong thing.
  FileUtils.rm_rf(recipe.builddir)
  FileUtils.rm_rf(recipe.destdir)
end

#dispenseObject



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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fpm/cookery/packager.rb', line 61

def dispense
  env = ENV.to_hash
  package_name = "#{recipe.name}-#{recipe.version}"
  platform = FPM::Cookery::Facts.platform
  target = FPM::Cookery::Facts.target

  Log.info "Starting package creation for #{package_name} (#{platform}, #{target})"
  Log.info ''

  # RecipeInspector.verify!(recipe)
  if config.fetch(:dependency_check, true)
    recipe.run_lifecycle_hook(:before_dependency_installation)
    DependencyInspector.verify!(recipe.depends, recipe.build_depends)
    recipe.run_lifecycle_hook(:after_dependency_installation)
  end

  recipe.installing = false

  if defined? recipe.source_handler()
    source = recipe.source_handler

    recipe.cachedir.mkdir
    Dir.chdir(recipe.cachedir) do
      recipe.run_lifecycle_hook(:before_source_download)
      Log.info "Fetching source: #{source.source_url}"
      source.fetch(:quiet => config[:quiet])
      recipe.run_lifecycle_hook(:after_source_download)

      if source.checksum?
        SourceIntegrityCheck.new(recipe).tap do |check|
          if check.checksum_missing?
            Log.warn 'Recipe does not provide a checksum. (sha256, sha1 or md5)'
            Log.puts <<-__WARN
  Digest:   #{check.digest}
  Checksum: #{check.checksum_actual}
  Filename: #{check.filename}

            __WARN
          elsif check.error?
            Log.error 'Integrity check failed!'
            Log.puts <<-__ERROR
  Digest:            #{check.digest}
  Checksum expected: #{check.checksum_expected}
  Checksum actual:   #{check.checksum_actual}
  Filename:          #{check.filename}

            __ERROR
            exit 1
          end #end checksum missing
        end #end check
      end #end checksum
    end #end chdir cachedir

    recipe.builddir.mkdir
    Dir.chdir(recipe.builddir) do
      recipe.run_lifecycle_hook(:before_source_extraction)
      extract_cookie = extract_cookie_name(package_name)

      # Do not extract source again because it might destroy changes
      # that have been made to the source. (like patches)
      if File.exists?(extract_cookie)
        extracted_source = File.read(extract_cookie).chomp
        Log.debug "Extract cookie exists, using existing source directory: #{extracted_source}"
      else
        extracted_source = source.extract
        File.open(extract_cookie, 'w', 0644) {|f| f.puts(extracted_source) }
      end

      if recipe.extracted_source
        Log.debug "Using custom extracted source dir: #{recipe.builddir(recipe.extracted_source)}"
        extracted_source = recipe.extracted_source
      end

      recipe.run_lifecycle_hook(:after_source_extraction, recipe.builddir(extracted_source))

      Log.info "Using source directory: #{extracted_source}"

      Dir.chdir(extracted_source) do
        #Source::Patches.new(recipe.patches).apply!

        build_cookie = build_cookie_name(package_name)

        if File.exists?(build_cookie)
          Log.warn "Skipping build of #{recipe.name} because build cookie found (#{build_cookie})," \
                   " use \"fpm-cook clean\" to rebuild!"
        else
          recipe.run_lifecycle_hook(:before_build)
          Log.info "Building in #{File.expand_path(extracted_source, recipe.builddir)}"
          recipe.build and FileUtils.touch(build_cookie)
          recipe.run_lifecycle_hook(:after_build)
        end

        FileUtils.rm_rf(recipe.destdir) unless keep_destdir?
        recipe.destdir.mkdir unless File.exists?(recipe.destdir)

        begin
          recipe.installing = true
          Log.info "Installing into #{recipe.destdir}"
          recipe.run_lifecycle_hook(:before_install)
          recipe.install
          recipe.run_lifecycle_hook(:after_install)
        ensure
          recipe.installing = false
        end
      end #end chdir extracted_source
    end #end chdir builddir
  end #end defined source_handler

  if skip_package?
    Log.info "Package building disabled"
  else
    build_package(recipe, config)
  end
ensure
  # Make sure we reset the environment.
  ENV.replace(env)
end


179
180
181
# File 'lib/fpm/cookery/packager.rb', line 179

def extract_cookie_name(name)
  (recipe.builddir/".extract-cookie-#{name.gsub(/[^\w]/,'_')}").to_s
end

#install_build_depsObject



46
47
48
49
50
51
# File 'lib/fpm/cookery/packager.rb', line 46

def install_build_deps
  recipe.run_lifecycle_hook(:before_dependency_installation)
  DependencyInspector.verify!([], recipe.build_depends)
  recipe.run_lifecycle_hook(:after_dependency_installation)
  Log.info("Build dependencies installed!")
end

#install_depsObject



53
54
55
56
57
58
# File 'lib/fpm/cookery/packager.rb', line 53

def install_deps
  recipe.run_lifecycle_hook(:before_dependency_installation)
  DependencyInspector.verify!(recipe.depends, recipe.build_depends)
  recipe.run_lifecycle_hook(:after_dependency_installation)
  Log.info("All dependencies installed!")
end

#keep_destdir?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/fpm/cookery/packager.rb', line 29

def keep_destdir?
  !!config[:keep_destdir]
end

#skip_package?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/fpm/cookery/packager.rb', line 25

def skip_package?
  !!config[:skip_package]
end

#target=(target) ⇒ Object



33
34
35
36
# File 'lib/fpm/cookery/packager.rb', line 33

def target=(target)
  # TODO(sissel): do sanity checking
  @target = target
end