Module: Coral::Plugin

Defined in:
lib/coral_core/plugin.rb,
lib/coral_core/plugin/node.rb,
lib/coral_core/plugin_base.rb,
lib/coral_core/plugin/command.rb,
lib/coral_core/plugin/machine.rb,
lib/coral_core/plugin/network.rb

Defined Under Namespace

Classes: Base, Command, Machine, Network, Node

Constant Summary collapse

@@load_info =

Plugin instances

{}
@@types =
{}
@@plugins =
{}
@@gems =

{}
@@core =
nil
@@initialized =

false

Class Method Summary collapse

Class Method Details

.add_build_info(type, file) ⇒ Object




148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/coral_core/plugin.rb', line 148

def self.add_build_info(type, file)
  type = type.to_sym
  
  @@load_info[type] = {} unless @@load_info.has_key?(type)
  
  components = file.split(File::SEPARATOR)
  provider   = components.pop.sub(/\.rb/, '').to_sym
  directory  = components.join(File::SEPARATOR) 
  
  puts 'Loading ' + type.to_s + ' plugin: ' + provider.to_s
      
  unless @@load_info[type].has_key?(provider)
    data = {
      :type      => type,
      :provider  => provider,        
      :directory => directory,
      :file      => file
    }
    @@load_info[type][provider] = data
  end
end

.autoloadObject




201
202
203
204
205
206
207
# File 'lib/coral_core/plugin.rb', line 201

def self.autoload
  @@load_info.keys.each do |type|
    @@load_info[type].each do |provider, plugin|
      coral_require(plugin[:directory], provider)
    end      
  end 
end

.coreObject


Plugins and resources



71
72
73
# File 'lib/coral_core/plugin.rb', line 71

def self.core
  return @@core
end

.create_instance(type, provider, options = {}) ⇒ Object




19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/coral_core/plugin.rb', line 19

def self.create_instance(type, provider, options = {})
  type     = type.to_sym
  provider = provider.to_sym
     
  return nil unless @@types.has_key?(type)
  
  options  = translate_type(type, options)
  provider = options.delete(:provider).to_sym if options.has_key?(:provider)    
  info     = @@load_info[type][provider] if Util::Data.exists?(@@load_info, [ type, provider ])
      
  if info
    options       = translate(type, provider, options)      
    instance_name = "#{provider}_" + Coral.sha1(options)
    
    @@plugins[type] = {} unless @@plugins.has_key?(type)
    
    unless instance_name && @@plugins[type].has_key?(instance_name)
      info[:instance_name] = instance_name
      options[:meta]       = info
      
      plugin = Coral.class_const([ :coral, type, provider ]).new(type, provider, options)
      
      @@plugins[type][instance_name] = plugin 
    end
         
    return @@plugins[type][instance_name]
  end      
  return nil
end

.define_type(type_info) ⇒ Object




113
114
115
116
117
118
119
# File 'lib/coral_core/plugin.rb', line 113

def self.define_type(type_info)
  if type_info.is_a?(Hash)
    type_info.each do |type, default_provider|
      @@types[type.to_sym] = default_provider
    end
  end
end

.gems(reset = false) ⇒ Object




94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/coral_core/plugin.rb', line 94

def self.gems(reset = false)
  if reset || Util::Data.empty?(@@gems)
    if defined?(Gem) 
      if ! defined?(Bundler) && Gem::Specification.respond_to?(:latest_specs)
        Gem::Specification.latest_specs(true).each do |spec|
          register_gem(spec)
        end
      else
        Gem.loaded_specs.each do |name, spec|
          register_gem(spec)
        end     
      end
    end
  end
  return @@gems
end

.get_instance(type, name) ⇒ Object




51
52
53
54
55
56
57
58
# File 'lib/coral_core/plugin.rb', line 51

def self.get_instance(type, name)
  if @@plugins.has_key?(type)
    @@plugins[type].each do |instance_name, plugin|
      return plugin if plugin.name.to_s == name.to_s
    end
  end
  return nil  
end

.initializeObject




215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/coral_core/plugin.rb', line 215

def self.initialize
  unless @@initialized
    # Register core plugins
    register(File.join(File.dirname(__FILE__), '..', 'coral'))      
    
    # Register external Gem defined plugins
    gems(true)
    
    # Autoload the registered plugins
    autoload
         
    @@initialized = true
  end    
end

.initialized?Boolean


Returns:

  • (Boolean)


232
233
234
# File 'lib/coral_core/plugin.rb', line 232

def self.initialized?
  return @@initialized
end

.plugins(type = nil) ⇒ Object




135
136
137
138
139
140
141
142
143
144
# File 'lib/coral_core/plugin.rb', line 135

def self.plugins(type = nil)
  results = {}
  
  if type
    results[type] = @@plugins[type] if @@plugins.has_key?(type)
  else
    results = @@plugins
  end    
  return results
end

.register(base_path) ⇒ Object




185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/coral_core/plugin.rb', line 185

def self.register(base_path)
  if File.directory?(base_path)
    Dir.glob(File.join(base_path, '*.rb')).each do |file|
      require file
    end
    
    Dir.entries(base_path).each do |path|
      unless path.match(/^\.\.?$/)
        register_type(base_path, path)          
      end
    end
  end  
end

.register_gem(spec) ⇒ Object




77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/coral_core/plugin.rb', line 77

def self.register_gem(spec)
  plugin_path = File.join(spec.full_gem_path, 'lib', 'coral')
  if File.directory?(plugin_path)
    @@gems[spec.name] = {
      :lib_dir => plugin_path,
      :spec    => spec
    }
    if spec.name == 'coral_core'
      @@core = spec
    else
      register(plugin_path) # Autoload plugins and related files  
    end      
  end  
end

.register_type(base_path, plugin_type) ⇒ Object


Plugin autoloading



173
174
175
176
177
178
179
180
181
# File 'lib/coral_core/plugin.rb', line 173

def self.register_type(base_path, plugin_type)
  base_directory = File.join(base_path, plugin_type.to_s)
  
  if File.directory?(base_directory)
    Dir.glob(File.join(base_directory, '*.rb')).each do |file|
      add_build_info(plugin_type, file)
    end
  end
end

.remove_instance(plugin) ⇒ Object




62
63
64
65
66
# File 'lib/coral_core/plugin.rb', line 62

def self.remove_instance(plugin)
  if plugin && plugin.is_a?(Plugin::Base) && @@plugins.has_key?(plugin.plugin_type)
    @@plugins[plugin.plugin_type].delete(plugin.plugin_instance_name)
  end
end

.translate(type, provider, info, method = :translate) ⇒ Object




247
248
249
250
251
# File 'lib/coral_core/plugin.rb', line 247

def self.translate(type, provider, info, method = :translate)
  klass = Coral.class_const([ :coral, type, provider ])          
  info  = klass.send(method, info) if klass.respond_to?(method)
  return info  
end

.translate_type(type, info, method = :translate) ⇒ Object


Utilities



239
240
241
242
243
# File 'lib/coral_core/plugin.rb', line 239

def self.translate_type(type, info, method = :translate)
  klass = Coral.class_const([ :coral, :plugin, type ])          
  info  = klass.send(method, info) if klass.respond_to?(method)
  return info  
end

.type_default(type) ⇒ Object




129
130
131
# File 'lib/coral_core/plugin.rb', line 129

def self.type_default(type)
  return @@types[type.to_sym]
end

.typesObject




123
124
125
# File 'lib/coral_core/plugin.rb', line 123

def self.types
  return @@types.keys
end