class Cany::Recipe

Attributes

defined_hooks[RW]
defined_options[RW]
inner[R]
spec[R]

Public Class Methods

from_name(name) click to toggle source

@api public Looks for the class registered for the given name @param [Symbol] name the name the class is search for @return [Cany::Recipe] Returns the found class or nil @raise UnknownRecipe if there is no recipe registered for this name.

# File lib/cany/recipe.rb, line 26
def self.from_name(name)
  raise UnknownRecipe.new(name) unless @@recipes[name]
  @@recipes[name]
end
hook(name) click to toggle source

@api public Define a new hook @param name

# File lib/cany/recipe.rb, line 153
def hook(name)
  @defined_hooks ||= []
  @defined_hooks  << name
end
new(spec, &configure_block) click to toggle source

Creates a new instance of this recipe @param [Cany::Specification] spec Specification object

# File lib/cany/recipe.rb, line 33
def initialize(spec, &configure_block)
  @spec = spec
  @inner = nil
  @hooks = Hash[(self.class.defined_hooks || []).map do |name|
    [name, Cany.hash_with_array_as_default]
  end]
  @options = Hash[(self.class.defined_options || {}).map do |name, default|
    [name, default.dup]
  end]
  self.class.const_get(:DSL).new(self).exec(&configure_block) if configure_block
end
option(name, default={}) click to toggle source

@api public Define a configure option. These kind of option are design for other recipes not for the user. See Recipe::DSL for this. @param name The name of the option. The option name is scoped

inside a recipe.
# File lib/cany/recipe.rb, line 163
def option(name, default={})
  @defined_options ||= {}
  @defined_options[name] = default
end
register_as(name) click to toggle source

@api public This method should be call in subclasses to register new recipe instances. Cany ignores any recipe subclasses which does not call register_as. If multiple recipes register on the same name the later one will overwrite the earlier one and therefore used by Cany. @param [Symbol] name A ruby symbol as name for the recipe. It should be short and recognizable

# File lib/cany/recipe.rb, line 11
    def self.register_as(name)
      @@recipes ||= {}
      @@recipes[name] = self
      module_eval(<<-EOS, __FILE__, __LINE__)
        def name
          :#{name}
        end
        EOS
    end

Public Instance Methods

binary() click to toggle source

@api public create binary (package) version of this file (means make install)

# File lib/cany/recipe.rb, line 255
def binary
  inner.binary
end
build() click to toggle source

@api public build the program (means ./configure and make)

# File lib/cany/recipe.rb, line 249
def build
  inner.build
end
clean() click to toggle source

@api public clean the build directory from all temporary and created files

# File lib/cany/recipe.rb, line 243
def clean
  inner.clean
end
configure(name, options) click to toggle source

@api public Configure an other recipe @param name The option name @param options The configuration data itself.

# File lib/cany/recipe.rb, line 187
def configure(name, options)
  option(name).merge! options
end
create(creator) click to toggle source

@api public This step is executed to create the distribution specific packages from canspec. The recipe can e.g. add additional dependencies or adjust the package meta data.

# File lib/cany/recipe.rb, line 238
def create(creator)
end
depend(*args) click to toggle source
# File lib/cany/recipe.rb, line 220
def depend(*args)
  @spec.dependencies << create_dep(*args)
end
exec(*args) click to toggle source

@api public Run a command inside the build directory. In most cases it is not needed to call this method directly. Look at the other helper methods.

The method expects as arguments the program name and additional parameters for the program. The arguments can be group with arguments, but must not: @example

exec 'echo', %w(a b)
exec ['echo', 'a', 'b']
exec 'echo', 'a', 'b'

@raise [CommandExecutionFailed] if the executed program exists with a

non-zero exit code.
# File lib/cany/recipe.rb, line 69
def exec(*args)
  args.flatten!
  Cany.logger.info args.join(' ')
  unless system(*args)
    raise CommandExecutionFailed.new args
  end
end
Also aliased as: exec_
exec_(*args)

exec is special name in same situations it may no work but this alias should work always

Alias for: exec
hook(name) click to toggle source
# File lib/cany/recipe.rb, line 169
def hook(name)
  @hooks[name].tap do |hook|
    raise UnknownHook.new name unless hook
  end
end
inner=(inner) click to toggle source

Specify the inner recipe for the current one. @param [Cany::Recipe, nil] inner Inner recipes should should be call between the pre and post

actions of this class. Nil means most inner recipes.
# File lib/cany/recipe.rb, line 48
def inner=(inner)
  @inner = inner
end
install(source, destination) click to toggle source

@api public Install files or directory from the build directory @param source The relative file name to a filename or directory inside the build

directory that should be installed/copied into the destination package

@param destination The diretory name into that the file or directory should be

installed
# File lib/cany/recipe.rb, line 94
def install(source, destination)
  exec 'dh_install', source, destination
end
install_content(filename, content) click to toggle source

@api public Install a file. The content is passed as argument. This method is designed to be used by recipes to create files dynamically. @param [String] filename The absolute file name for the file inside the package. @param [String] content The file content

# File lib/cany/recipe.rb, line 103
def install_content(filename, content)
  FileUtils.mkdir_p File.dirname File.join('debian', spec.name, filename)
  File.open File.join('debian', spec.name, filename), 'w' do |f|
    f.write content
  end
end
install_dir(path) click to toggle source

@api public Installs/creates an empty directory @param [String] path The path name

# File lib/cany/recipe.rb, line 113
def install_dir(path)
  exec 'dh_installdirs', path
end
install_service(*args) click to toggle source

@api public Specify a command call (program + args) that should be installed as service and started automatically. This method should be only call inside the binary step. @param name A short identifier. Used to separate different services. E.g. the name

of the web server that is launched by this command (like puma, unicorn, thin)

@param opts Service behavior options @option opts :user As which user should the command executed (default is root) @option opts :group As which group should the command executed (default is root)

# File lib/cany/recipe.rb, line 134
def install_service(*args)
  recipe(:system).install_service(*args)
end
option(name) click to toggle source

@api public Ask for the current values for a defined option

# File lib/cany/recipe.rb, line 177
def option(name)
  @options[name].tap do |option|
    raise UnknownOption.new name unless option
  end
end
prepare() click to toggle source

@api public Prepares the recipes to run things. This is call exactly once for all recipes before recipes actions are executed.

# File lib/cany/recipe.rb, line 232
def prepare
end
recipe(name) click to toggle source

@api public Access the recipe instance from another loaded recipe of this specification @param name recipe name

# File lib/cany/recipe.rb, line 206
def recipe(name)
  return spec.system_recipe if name == :system
  recipe_class = Recipe.from_name(name)
  @spec.recipes.each do |one_recipe|
    return one_recipe if one_recipe.instance_of? recipe_class
  end
  raise UnloadedRecipe.new name
end
rmtree(*args) click to toggle source

@api public Ensure that the given files or directories are no present. Directories are removed recursively.

# File lib/cany/recipe.rb, line 141
def rmtree(*args)
  args.flatten.each do |path|
    ::FileUtils.remove_entry path if File.exists? path
  end
end
ruby_bin(*args) click to toggle source

@api public Run a ruby task (like gem, bundle, rake …)

The method expects as arguments the program name and additional parameters for the program. See exec for more examples

# File lib/cany/recipe.rb, line 84
def ruby_bin(*args)
  exec RbConfig.ruby, '-S', *args
end
run_hook(name, state) click to toggle source

@api public Run defined actions for a hook @param name hook identification, no error is raised on unknown hooks @param state state that should be executed (:before, :after or :around)

# File lib/cany/recipe.rb, line 195
def run_hook(name, state)
  hook(name)[state].each do |block|
    Cany.logger.info  "run #{block} for hook #{name} in state #{state} ..."
    instance_eval(&block)
  end
end