Class: Sunshine::DependencyLib

Inherits:
Object
  • Object
show all
Defined in:
lib/sunshine/dependency_lib.rb

Overview

DependencyLib is a simple class for building and handling depenedencies. A dependency tree can be defined by inheriting the DependencyLib class, and dependencies can be defined through dependency instantiation methods:

dependency_lib.instance_eval do

  yum 'ruby', :pkg => 'ruby-devel'

  yum 'rubygems', :requires => 'ruby'

  gem 'rdoc', :requires => 'rubygems'

  gem 'ri', :requires => 'rubygems'

end

Calling the install for rdoc will then check and install all of its parent dependencies as well:

dependency_lib.install 'rdoc', 'ri'

Dependencies may also be generic and/or have custom bash scripts for installs, uninstalls, and presence checks:

dependency 'custom' do
  requires  'yum', 'ruby'
  install   'sudo yum install custom'
  uninstall 'sudo yum remove custom'
  check     'yum list installed custom'
end

See the Dependency class for more information.

Defined Under Namespace

Classes: MissingDependency

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDependencyLib

Returns a new instance of DependencyLib.



52
53
54
# File 'lib/sunshine/dependency_lib.rb', line 52

def initialize
  @dependencies = Hash.new
end

Instance Attribute Details

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



50
51
52
# File 'lib/sunshine/dependency_lib.rb', line 50

def dependencies
  @dependencies
end

Class Method Details

.dependency_typesObject

Array of all dependency classes. Appended to automatically when DependencyLib::Dependency is inherited.



45
46
47
# File 'lib/sunshine/dependency_lib.rb', line 45

def self.dependency_types
  @dependency_types ||= []
end

.sudo=(value) ⇒ Object

Define if sudo should be used



194
195
196
197
198
# File 'lib/sunshine/dependency_lib.rb', line 194

def self.sudo= value
  dependency_types.each do |dep_class|
    dep_class.sudo = value
  end
end

Instance Method Details

#[](key) ⇒ Object

Returns a dependency hash by type:

DependencyLib['name'] #=> {:yum => <Yum...>, :apt => <Apt...>, ...}


61
62
63
# File 'lib/sunshine/dependency_lib.rb', line 61

def [](key)
  @dependencies[key]
end

#add(dep) ⇒ Object

Add a dependency to the dependencies hash.



69
70
71
# File 'lib/sunshine/dependency_lib.rb', line 69

def add dep
  (@dependencies[dep.name] ||= []).unshift dep
end

#exist?(key) ⇒ Boolean

Checks for the existance of a dependency by name

Returns:

  • (Boolean)


77
78
79
# File 'lib/sunshine/dependency_lib.rb', line 77

def exist? key
  @dependencies.has_key? key
end

#get(name, options = {}) ⇒ Object

Get a dependency object by name. Supports passing :type => :pkg_manager if dependencies with the same name but different package managers exist:

dependencies.get 'daemon', :type => Gem
#=> <Gem @name="daemon"...>

For an ‘nginx’ dependency defined for both apt and yum, where the yum dependency object was added to the tree last. Returns nil if no matching dependency type is found:

dependencies.get 'nginx'
#=> <Yum @name="nginx"...>

dependencies.get 'nginx', :type => Apt
#=> <Apt @name="nginx"...>

Use the :prefer option if a certain dependency type is prefered but will fall back to whatever first dependency is available:

dependencies.yum 'my_dep'
dependencies.get 'my_dep', :prefer => Apt
#=> <Yum @name="my_dep"...>

Both the :type and the :prefer options support passing arrays to search from best to least acceptable candidate:

dependencies.yum 'my_dep'
dependencies.apt 'my_dep'
dependencies.get 'my_dep', :type => [Tpkg, Yum]
#=> <Yum @name="my_dep"...>


110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sunshine/dependency_lib.rb', line 110

def get name, options={}
  return unless exist? name

  deps      = @dependencies[name]
  dep_types = [*(options[:type] || options[:prefer])].compact

  return deps.first if dep_types.empty?

  dep_types.each do |dep_type|
    deps.each do |dep|
      return dep if dep_type === dep
    end
  end

  return deps.first unless options[:type]
end

#install(*deps) ⇒ Object

Install one or more dependencies:

dependencies.install 'dep1', 'dep2', options_hash

See DependencyLib#get and Dependency#install! for supported options.

Note: If a Dependency object is passed and the :type option is set, DependencyLib will attempt to find and install a dependency of class :type with the same name as the passed Dependency object:

my_dep = dependencies.yum "my_dep_yum_only"
dependencies.install my_dep, :type => Apt
#=> "No dependency 'my_dep' [Sunshine::Apt]"


142
143
144
# File 'lib/sunshine/dependency_lib.rb', line 142

def install(*deps)
  send_each(:install!, *deps)
end

#send_each(method, *deps) ⇒ Object

Get and call method on each dependency passed



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/sunshine/dependency_lib.rb', line 162

def send_each(method, *deps)
  options = Hash === deps.last ? deps.delete_at(-1).dup : {}

  #if options[:call].respond_to? :pkg_manager
  #  options[:prefer] ||= options[:call].pkg_manager
  #end

  deps.each do |dep_name|
    dep = if Dependency === dep_name
            if options[:type] && !(options[:type] === dep_name)
              get(dep_name.name, options)
            else
              dep_name
            end
          else
            get(dep_name, options)
          end

    raise MissingDependency,
      "No dependency '#{dep_name}' [#{options[:type] || "any"}]" if !dep

    # Remove :type so dependencies of other types than dep can be installed
    options.delete(:type)

    dep.send method, options
  end
end

#uninstall(*deps) ⇒ Object

Uninstall one or more dependencies:

dependencies.uninstall 'dep1', 'dep2', options_hash

See DependencyLib#get and Dependency#uninstall! for supported options.



154
155
156
# File 'lib/sunshine/dependency_lib.rb', line 154

def uninstall(*deps)
  send_each(:uninstall!, *deps)
end