Class: Nucleon::Plugin::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/network.rb

Instance Method Summary collapse

Instance Method Details

#add_node(provider, name, options = {}) {|:preprocess, hook_config| ... } ⇒ Object

Yields:

  • (:preprocess, hook_config)


231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/core/plugin/network.rb', line 231

def add_node(provider, name, options = {})
  config = Config.ensure(options)
  
  keypair = config.delete(:keypair, nil)
  return false unless keypair && keypair.is_a?(Util::SSH::Keypair)
      
  remote_name = config.delete(:remote, :edit)
  
  node_options = Util::Data.clean({ 
    :settings     => array(config.delete(:groups, [])) | [ "server" ], 
    :region       => config.delete(:region, nil),
    :machine_type => config.delete(:machine_type, nil),
    :public_ip    => config.delete(:public_ip, nil),
    :image        => config.delete(:image, nil),
    :user         => config.delete(:user, :root),
    :hostname     => name
  })
  
  # Set node data
  node = set_node(provider, name, node_options)
  hook_config = { :node => node, :remote => remote_name, :config => config }
  success     = true
  
  yield(:preprocess, hook_config) if block_given?
  
  if ! node.local? && node.attach_keys(keypair) && extension_check(:add_node, hook_config)
    # Create new node / machine
    success = node.create do |op, data|
      block_given? ? yield("create_#{op}".to_sym, data) : data
    end
    
    if success && node.save({ :remote => remote_name, :message => "Created machine #{name} on #{provider}" })
      # Bootstrap new machine
      success = node.bootstrap(home, extended_config(:bootstrap, config)) do |op, data|
        block_given? ? yield("bootstrap_#{op}".to_sym, data) : data
      end  
      
      if success
        seed_project = config.get(:project_reference, nil)
        save_config  = { :commit => true, :remote => remote_name, :push => true }
                       
        if seed_project && remote_name
          # Reset project remote
          seed_info = Plugin::Project.translate_reference(seed_project)
                  
          if seed_info
            seed_url    = seed_info[:url]
            seed_branch = seed_info[:revision] if seed_info[:revision]
          else
            seed_url = seed_project                
          end
          set_remote(:origin, seed_url) if remote_name.to_sym == :edit
          set_remote(remote_name, seed_url)
          save_config[:pull] = false
        end
        
        # Save network changes (preliminary)
        success = node.save(extended_config(:node_save, save_config))
      
        if success && seed_project
          # Seed machine with remote project reference
          result = node.seed({
            :project_reference => seed_project,
            :project_branch    => seed_branch
          }) do |op, data|
            yield("seed_#{op}".to_sym, data)
          end
          success = result.status == code.success
        end
      
        # Update local network project
        success = load({ :remote => remote_name, :pull => true }) if success
      end
    end
  end    
  success 
end

#attach_data(type, name, data, options = {}) ⇒ Object




211
212
213
214
215
216
217
218
219
# File 'lib/core/plugin/network.rb', line 211

def attach_data(type, name, data, options = {})
  attach_config = Config.ensure(options).import({ :type => :source })
  attached_data = nil    
  
  if data.is_a?(String)
    attached_data = config.attach(type, name, data, attach_config)
  end  
  attached_data
end

#attach_files(type, name, files, options = {}) ⇒ Object




195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/core/plugin/network.rb', line 195

def attach_files(type, name, files, options = {})
  attach_config  = Config.ensure(options).import({ :type => :file })
  included_files = []    
  files          = [ files ] unless files.is_a?(Array)
  
  files.each do |file|
    if file
      attached_file = config.attach(type, name, file, attach_config)
      included_files << attached_file unless attached_file.nil?
    end  
  end    
  included_files
end

#batch(node_references, default_provider = nil, parallel = true, &code) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/core/plugin/network.rb', line 326

def batch(node_references, default_provider = nil, parallel = true, &code)
  success = true
  
  if has_nodes? && ! node_references.empty?
    # Execute action on selected nodes      
    nodes = nodes_by_reference(node_references, default_provider)
    
    if parallel
      values = []
      nodes.each do |node|
        values << Celluloid::Future.new(node, &code)
      end
      values  = values.map { |future| future.value }
      success = false if values.include?(false)
    else
      nodes.each do |node|
        proc_success = code.call(node)
        if proc_success == false
          success = false
          break
        end
      end  
    end
  end
  success
end

#build_directoryObject




47
48
49
# File 'lib/core/plugin/network.rb', line 47

def build_directory
  File.join(directory, 'build')
end

#cacheObject




53
54
55
# File 'lib/core/plugin/network.rb', line 53

def cache
  config.cache
end

#delete_attachments(ids, options = {}) ⇒ Object




223
224
225
# File 'lib/core/plugin/network.rb', line 223

def delete_attachments(ids, options = {})
  config.delete_attachments(ids, options)    
end

#directoryObject




41
42
43
# File 'lib/core/plugin/network.rb', line 41

def directory
  config.directory
end

#each_node_config(provider = nil) ⇒ Object


Utilities



312
313
314
315
316
317
318
319
320
# File 'lib/core/plugin/network.rb', line 312

def each_node_config(provider = nil)
  node_config.export.each do |node_provider, nodes|
    if provider.nil? || provider == node_provider
      nodes.each do |name, info|
        yield(node_provider, name, info)
      end
    end
  end
end

#has_nodes?(provider = nil) ⇒ Boolean


Checks

Returns:

  • (Boolean)


23
24
25
# File 'lib/core/plugin/network.rb', line 23

def has_nodes?(provider = nil)   
  node_config(provider).export.empty? ? false : true
end

#homeObject




35
36
37
# File 'lib/core/plugin/network.rb', line 35

def home
  extension_set(:home, ( ENV['USER'] == 'root' ? '/root' : ENV['HOME'] )) 
end

#ignore(files) ⇒ Object




59
60
61
# File 'lib/core/plugin/network.rb', line 59

def ignore(files)
  config.ignore(files)
end

#load(options = {}) ⇒ Object


Operations



183
184
185
# File 'lib/core/plugin/network.rb', line 183

def load(options = {})
  config.load(options)
end

#local_node(require_new = false) ⇒ Object




145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/core/plugin/network.rb', line 145

def local_node(require_new = false)
  ip_address = CORL.public_ip  
  local_node = node_by_ip(ip_address, require_new)
      
  if local_node.nil?
    name       = Util::Data.ensure_value(lookup(:fqdn), ip_address)    
    local_node = CORL.node(name, extended_config(:local_node).import({ :meta => { :parent => myself }}), :local) 
  else
    local_node.network = myself
    local_node.normalize(true)
    local_node.localize               
  end    
  local_node
end

#node_by_ip(public_ip, require_new = false) ⇒ Object




136
137
138
139
140
141
# File 'lib/core/plugin/network.rb', line 136

def node_by_ip(public_ip, require_new = false)
  each_node_config do |provider, name, info|
    return node(provider, name, require_new) if info[:public_ip] == public_ip  
  end
  nil
end

#node_groupsObject




75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/core/plugin/network.rb', line 75

def node_groups
  groups = {}
  
  each_node_config do |provider, name, info|
    search_node(provider, name, :settings, [], :array).each do |group|
      group = group.to_sym
      groups[group] = [] unless groups.has_key?(group)
      groups[group] << { :provider => provider, :name => name }
    end
  end
  groups
end

#node_info(references, default_provider = nil) ⇒ Object




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
# File 'lib/core/plugin/network.rb', line 90

def node_info(references, default_provider = nil)
  groups    = node_groups
  node_info = {}
  
  default_provider = Manager.connection.type_default(:node) if default_provider.nil?
      
  references.each do |reference|
    info = Plugin::Node.translate_reference(reference)
    info = { :provider => default_provider, :name => reference } unless info
    name = info[:name].to_sym     
    
    # Check for group membership
    if groups.has_key?(name)
      groups[name].each do |member_info|
        provider = member_info[:provider].to_sym
        
        node_info[provider] = [] unless node_info.has_key?(provider)        
        node_info[provider] << member_info[:name]
      end
    else
      # Not a group
      provider = info[:provider].to_sym
      
      if node_config.export.has_key?(provider)
        node_found = false
        
        each_node_config(provider) do |node_provider, node_name, node|
          if node_name == name
            node_info[node_provider] = [] unless node_info.has_key?(node_provider)        
            node_info[node_provider] << node_name
            node_found = true
            break
          end
        end
        
        unless node_found
          # TODO:  Error or something?
        end
      end 
    end     
  end    
  node_info  
end

#nodes_by_reference(references, default_provider = nil, require_new = false) ⇒ Object




162
163
164
165
166
167
168
169
170
171
# File 'lib/core/plugin/network.rb', line 162

def nodes_by_reference(references, default_provider = nil, require_new = false)
  nodes = []
  
  node_info(references, default_provider).each do |provider, names|
    names.each do |name|
      nodes << node(provider, name, require_new)
    end
  end
  nodes  
end

#normalize(reload) ⇒ Object


Cloud plugin interface



11
12
13
14
15
16
17
18
# File 'lib/core/plugin/network.rb', line 11

def normalize(reload)
  super
  
  logger.info("Initializing sub configuration from source with: #{myself._export.inspect}")
  myself.config = CORL.configuration(Config.new(myself._export).import({ :autosave => false, :create => false })) unless reload
  
  ignore('build')
end

#remote(name) ⇒ Object




65
66
67
# File 'lib/core/plugin/network.rb', line 65

def remote(name)
  config.remote(name)
end

#save(options = {}) ⇒ Object




189
190
191
# File 'lib/core/plugin/network.rb', line 189

def save(options = {})
  config.save(options)
end

#set_remote(name, location) ⇒ Object



69
70
71
# File 'lib/core/plugin/network.rb', line 69

def set_remote(name, location)
  config.set_remote(name, location)
end

#test_node(provider, options = {}) ⇒ Object




175
176
177
178
# File 'lib/core/plugin/network.rb', line 175

def test_node(provider, options = {})
  config = Config.ensure(options).import({ :meta => { :parent => myself } })
  CORL.node(:test, config.export, provider)
end