Class: Nucleon::Plugin::Node

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/core/plugin/node.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &code) ⇒ Object




47
48
49
50
51
# File 'lib/core/plugin/node.rb', line 47

def method_missing(method, *args, &code)  
  action(method, *args) do |op, data|
    code.call(op, data) if code
  end
end

Class Method Details

.build_info(type, data) ⇒ Object


Utilities



1046
1047
1048
1049
# File 'lib/core/plugin/node.rb', line 1046

def self.build_info(type, data)  
  data = data.split(/\s*,\s*/) if data.is_a?(String)
  super(type, data)
end

.translate(data) ⇒ Object




1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
# File 'lib/core/plugin/node.rb', line 1053

def self.translate(data)
  options = super(data)
  
  case data        
  when String
    options = { :name => data }
  when Hash
    options = data
  end
  
  if options.has_key?(:name)
    if matches = translate_reference(options[:name])
      options[:provider] = matches[:provider]
      options[:name]     = matches[:name]
      
      logger.debug("Translating node options: #{options.inspect}")  
    end
  end
  options
end

.translate_reference(reference) ⇒ Object




1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
# File 'lib/core/plugin/node.rb', line 1076

def self.translate_reference(reference)
  # ex: rackspace:::web1.staging.example.com
  if reference && reference.match(/^\s*([a-zA-Z0-9_-]+):::([^\s]+)\s*$/)
    provider = $1
    name     = $2
    
    logger.debug("Translating node reference: #{provider}  #{name}")
    
    info = {
      :provider => provider,
      :name     => name
    }
    
    logger.debug("Project reference info: #{info.inspect}")
    return info
  end
  nil
end

Instance Method Details

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



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/core/plugin/node.rb', line 665

def action(provider, options = {})
  codes :network_load_error
  
  config = Config.ensure(options).defaults({
    :log_level    => Nucleon.log_level,
    :net_remote   => :edit, 
    :net_provider => network.plugin_provider 
  })
  
  logger.info("Executing remote action #{provider} with encoded arguments: #{config.export.inspect}")
  
  encoded_config = Util::CLI.encode(Util::Data.clean(config.export))
  action_config  = extended_config(:action, {
    :command => provider, 
    :data    => { :encoded => encoded_config }
  })
  
  result = command(:corl, { :subcommand => action_config, :as_admin => true }) do |op, data|
    yield(op, data) if block_given?  
  end
  
  # Update local network configuration so we capture any updates
  if result.status == code.success && ! network.load({ :remote => config[:net_remote], :pull => true })
    result.status = code.network_load_error
  end
  result
end

#attach_keys(keypair) ⇒ Object




359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/core/plugin/node.rb', line 359

def attach_keys(keypair)
  base_name   = "#{plugin_provider}-#{plugin_name}"
  save_config = { 
    :pull    => false, 
    :push    => false,
    :message => "Updating SSH keys for node #{plugin_provider} (#{plugin_name})" 
  }
  
  active  = machine && machine.running?
  result  = run.authorize({ :public_key => keypair.ssh_key }) if active
  success = false
  
  if ! active || result.status == code.success        
    private_key = network.attach_data(:keys, "#{base_name}-id_#{keypair.type}", keypair.encrypted_key)
    public_key  = network.attach_data(:keys, "#{base_name}-id_#{keypair.type}.pub", keypair.ssh_key)
  
    if private_key && public_key
      FileUtils.chmod(0600, private_key)
      FileUtils.chmod(0644, public_key)
    
      save_config[:files] = [ private_key, public_key ]
  
      myself.private_key = private_key
      myself.public_key  = public_key
  
      success = save(extended_config(:key_save, save_config))
    end
  end
  success
end

#bootstrap(local_path, options = {}) ⇒ Object




735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
# File 'lib/core/plugin/node.rb', line 735

def bootstrap(local_path, options = {})
  config      = Config.ensure(options)
  myself.status = code.unknown_status
  
  bootstrap_name = 'bootstrap'    
  bootstrap_path = config.get(:bootstrap_path, File.join(CORL.lib_path, '..', bootstrap_name))
  bootstrap_glob = config.get(:bootstrap_glob, '**/*.sh')
  bootstrap_init = config.get(:bootstrap_init, 'bootstrap.sh')
  
  user_home  = config[:home]
  auth_files = config.get_array(:auth_files)
  
  codes :local_path_not_found,
        :home_path_lookup_failure,
        :auth_upload_failure,
        :root_auth_copy_failure,
        :bootstrap_upload_failure,
        :bootstrap_exec_failure,
        :reload_failure
  
  if File.directory?(local_path)      
    if user_home || user_home = home(config.get(:home_env_var, 'HOME'), config.get(:force, false))
      myself.status = code.success
      
      # Transmit authorisation / credential files
      package_files = [ '.fog', '.netrc', '.google-privatekey.p12', '.vimrc' ]
      auth_files.each do |file|
        package_files << file.gsub(local_path + '/', '')
      end
      send_success = send_files(local_path, user_home, package_files, '0600') do |op, data|
        yield("send_#{op}".to_sym, data) if block_given?
        data
      end
      unless send_success
        myself.status = code.auth_upload_failure
      end
      
      if user.to_sym != config.get(:root_user, :root).to_sym
        auth_files     = package_files.collect { |path| "'#{path}'"}
        root_home_path = config.get(:root_home, '/root')
        
        result        = command("cp #{auth_files.join(' ')} #{root_home_path}", { :as_admin => true })
        myself.status = code.root_auth_copy_failure unless result.status == code.success  
      end
  
      # Send bootstrap package
      if status == code.success
        remote_bootstrap_path = File.join(user_home, bootstrap_name)
        
        cli.rm('-Rf', remote_bootstrap_path)
        send_success = send_files(bootstrap_path, remote_bootstrap_path, nil, '0700') do |op, data|
          yield("send_#{op}".to_sym, data) if block_given?
          data
        end
        unless send_success
          myself.status = code.bootstrap_upload_failure
        end
        
        # Execute bootstrap process
        if status == code.success
          remote_script = File.join(remote_bootstrap_path, bootstrap_init)
          
          myself[:bootstrap] = remote_script
          
          result = command("HOSTNAME='#{hostname}' #{remote_script}", { :as_admin => true }) do |op, data|
            yield("exec_#{op}".to_sym, data) if block_given?
            data
          end
          
          if result.status == code.success
            # Reboot the machine
            unless reload
              warn('corl.core.node.bootstrap.reload')
              myself.status = code.reload_failure 
            end
          else
            warn('corl.core.node.bootstrap.status', { :script => remote_script, :status => result.status })
            myself.status = code.bootstrap_exec_failure
          end
        end
      end
    else
      myself.status = code.home_path_lookup_failure            
    end
  else
    myself.status = code.local_path_not_found
  end
  status == code.success
end

#build(options = {}) ⇒ Object


Machine operations



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
352
353
354
355
# File 'lib/core/plugin/node.rb', line 327

def build(options = {})
  config  = Config.ensure(options)
  success = true
  
  provisioners.each do |provider, collection|
    ui.info("Building #{provider} provisioner collection")
        
    collection.each do |name, provisioner|
      ui.info("Building #{name} provisioner")
      
      unless provisioner.build(options)
        success = false
        break
      end
    end
  end
  
  if success
    ui.success("Saving successful build")
    
    myself[:build] = Time.now.to_s
  
    success = save(extended_config(:build, {
      :message => config.get(:message, "Built #{plugin_provider} node: #{plugin_name}"),
      :remote  => config.get(:remote, :edit)  
    }))
  end
  success
end

#build_timeObject




308
309
310
# File 'lib/core/plugin/node.rb', line 308

def build_time
  myself[:build]
end

#cliObject




633
634
635
# File 'lib/core/plugin/node.rb', line 633

def cli
  @cli_interface
end

#cli_capture(cli_command, *args) ⇒ Object


CLI utilities



1104
1105
1106
1107
1108
1109
1110
1111
1112
# File 'lib/core/plugin/node.rb', line 1104

def cli_capture(cli_command, *args)
  result = cli.send(cli_command, args)
           
  if result.status == code.success && ! result.output.empty? 
    result.output
  else
    nil
  end  
end

#cli_check(cli_command, *args) ⇒ Object




1116
1117
1118
1119
# File 'lib/core/plugin/node.rb', line 1116

def cli_check(cli_command, *args)
  result = cli.send(cli_command, args)
  result.status == code.success ? true : false  
end

#command(command, options = {}) ⇒ Object



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/core/plugin/node.rb', line 641

def command(command, options = {})
  config   = Config.ensure(options)
  as_admin = config.delete(:as_admin, false)
  
  unless command.is_a?(CORL::Plugin::Command)
    command = CORL.command(Config.new({ :command => command }).import(config), :bash)
  end
  
  admin_command = ''
  if as_admin
    admin_command = 'sudo' if user.to_s != 'root'
    admin_command = extension_set(:admin_command, admin_command, config)
  end
  
  results = exec({ :commands => [ "#{admin_command} #{command.to_s}".strip ] }) do |op, data|
    yield(op, data) if block_given?  
  end    
  results.first 
end

#create(options = {}) ⇒ Object




430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/core/plugin/node.rb', line 430

def create(options = {})
  success = true
  
  if machine
    config = Config.ensure(options)
    
    clear_cache
    
    if extension_check(:create, { :config => config })
      logger.info("Creating node: #{plugin_name}")
    
      yield(:config, config) if block_given?      
      success = machine.create(config.export)
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:create_success, { :config => config })
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine so cannot be created")
  end
  success
end

#create_image(options = {}) ⇒ Object




924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
# File 'lib/core/plugin/node.rb', line 924

def create_image(options = {})
  success = true
  
  if machine && machine.running?
    config = Config.ensure(options)
    
    if extension_check(:create_image, { :config => config })
      logger.info("Executing node: #{plugin_name}")
    
      yield(:config, config) if block_given?      
      success = machine.create_image(config.export)
      success = save(config) if success
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:create_image_success, { :config => config })
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot create an image")
  end
  success   
end

#create_machine(name, provider, options = {}) ⇒ Object




424
425
426
# File 'lib/core/plugin/node.rb', line 424

def create_machine(name, provider, options = {})
  CORL.create_plugin(:machine, provider, extended_config(name, options).import({ :meta => { :parent => myself }}))
end

#delete_keysObject




392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/core/plugin/node.rb', line 392

def delete_keys
  private_key = myself[:private_key]
  public_key  = myself[:public_key]
  
  keys = []
  keys << private_key if private_key
  keys << public_key if public_key
  
  success = true
  
  unless keys.empty?
    files = network.delete_attachments(keys)
    
    if files && ! files.empty?
      delete_setting(:private_key)
      delete_setting(:public_key)
      
      success = save(extended_config(:key_delete, {
        :files   => [ private_key, public_key ], 
        :pull    => false, 
        :push    => false,
        :message => "Removing SSH keys for node #{plugin_provider} (#{plugin_name})"  
      }))
    else
      success = false
    end
  end
  success
end

#destroy(options = {}) ⇒ Object




999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/core/plugin/node.rb', line 999

def destroy(options = {})    
  success = true
  
  if machine && machine.created?
    config = Config.ensure(options)
    
    if extension_check(:destroy, { :config => config })
      logger.info("Destroying node: #{plugin_name}")
    
      yield(:config, config) if block_given?
      
      # Shut down machine      
      success = machine.destroy(config.export)
      
      myself.machine = nil
      
      # Remove SSH keys
      if success && delete_keys
        # Remove node information
        network.delete_node(plugin_provider, plugin_name, false)
        
        network.save({
          :commit => true, 
          :remote => config.get(:remote, :edit), 
          :push   => true 
        })
      end      
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:destroy_success, { :config => config })
        clear_cache
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not created so cannot be destroyed")
  end
  success    
end

#download(remote_path, local_path, options = {}) ⇒ Object




461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/core/plugin/node.rb', line 461

def download(remote_path, local_path, options = {})
  success = false
  
  if machine && machine.running?
    config      = Config.ensure(options)
    hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config })
    
    if extension_check(:download, hook_config)
      logger.info("Downloading from #{plugin_name}")
    
      render("Starting download of #{remote_path} to #{local_path}") 
      yield(:config, hook_config) if block_given?
      
      active_machine = local? ? local_machine : machine
      
      success = active_machine.download(remote_path, local_path, config.export) do |name, received, total|
        render("#{name}: Sent #{received} of #{total}")
        yield(:progress, { :name => name, :received => received, :total => total })
      end
      
      if success && block_given?
        render("Successfully finished download of #{remote_path} to #{local_path}")
        process_success = yield(:process, hook_config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:download_success, hook_config)
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot download")
  end
  success
end

#exec(options = {}) ⇒ Object



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'lib/core/plugin/node.rb', line 572

def exec(options = {})
  default_error = Util::Shell::Result.new(:error, 255)
  results       = [ default_error ]
  
  if machine && machine.running?
    config = Config.ensure(options)
    
    if extension_check(:exec, { :config => config })
      logger.info("Executing node: #{plugin_name}")
            
      yield(:config, config) if block_given?
      
      if local? && local_machine
        active_machine = local_machine
        
        config[:info_prefix]  = "[#{hostname}] "
        config[:error_prefix] = "[#{hostname}] "
      else
        active_machine = machine
      end
      
      if commands = config.get(:commands, nil)
        results = active_machine.exec(commands, config.export) do |type, command, data|
          unless local?
            if type == :error
              alert(filter_output(type, data))
            else
              render(filter_output(type, data))
            end
          end
          yield(:progress, { :type => type, :command => command, :data => data }) if block_given?   
        end
      else
        default_error.append_errors("No execution command")    
      end
      
      if results
        success = true
        results.each do |result|
          success = false if result.status != code.success  
        end
        if success
          yield(:process, config) if block_given?
          extension(:exec_success, { :config => config, :results => results }) 
        end
      else
        default_error.append_errors("No execution results")
      end
    else
      default_error.append_errors("Execution prevented by exec hook")
    end
  else
    default_error.append_errors("No attached machine")
    
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot execute commands")
  end
  results 
end

#filter_output(type, data) ⇒ Object




1123
1124
1125
# File 'lib/core/plugin/node.rb', line 1123

def filter_output(type, data)
  data  
end

#home(env_var = 'HOME', reset = false) ⇒ Object




160
161
162
163
164
165
# File 'lib/core/plugin/node.rb', line 160

def home(env_var = 'HOME', reset = false)
  if reset || myself[:user_home].nil?
    myself[:user_home] = cli_capture(:echo, '$' + env_var.to_s.gsub('$', '')) if machine
  end
  myself[:user_home]
end

#hostnameObject




119
120
121
122
123
124
125
126
127
# File 'lib/core/plugin/node.rb', line 119

def hostname
  hostname = myself[:hostname]
  
  if hostname.to_s != ui.resource.to_s 
    ui.resource = Util::Console.colorize(hostname, @class_color)
    logger      = hostname
  end
  hostname
end

#id(reset = false) ⇒ Object




100
101
102
103
# File 'lib/core/plugin/node.rb', line 100

def id(reset = false)
  myself[:id] = machine.plugin_name if machine && ( reset || myself[:id].nil? )
  myself[:id]
end

#image(reset = false) ⇒ Object



264
265
266
267
# File 'lib/core/plugin/node.rb', line 264

def image(reset = false)
  myself[:image] = machine.image if machine && ( reset || myself[:image].nil? )
  myself[:image]
end

#image=(image) ⇒ Object




260
261
262
# File 'lib/core/plugin/node.rb', line 260

def image=image
  myself[:image] = image
end

#image_id(image) ⇒ Object


Image utilities



1143
1144
1145
# File 'lib/core/plugin/node.rb', line 1143

def image_id(image)
  image.id
end

#image_search_text(image) ⇒ Object




1155
1156
1157
# File 'lib/core/plugin/node.rb', line 1155

def image_search_text(image)
  image.to_s
end

#images(search_terms = [], options = {}) ⇒ Object




221
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
247
248
249
250
251
252
253
254
255
256
# File 'lib/core/plugin/node.rb', line 221

def images(search_terms = [], options = {})
  config = Config.ensure(options)
  images = []
  
  if machine
    loaded_images = machine.images
  
    if loaded_images
      require_all = config.get(:require_all, false)
      
      loaded_images.each do |image|
        if usable_image?(image)
          include_image = ( search_terms.empty? ? true : require_all )
          image_text    = image_search_text(image)
          
          search_terms.each do |term|
            if config.get(:match_case, false)
              success = image_text.match(/#{term}/)
            else
              success = image_text.match(/#{term}/i)  
            end
            
            if require_all            
              include_image = false unless success
            else
              include_image = true if success  
            end
          end
      
          images << image if include_image
        end
      end
    end
  end
  images
end

#local?Boolean


Checks



63
64
65
# File 'lib/core/plugin/node.rb', line 63

def local?
  @local_context ? true : false
end

#local_machineObject




90
91
92
# File 'lib/core/plugin/node.rb', line 90

def local_machine
  @local_machine
end

#local_machine=(local_machine) ⇒ Object



94
95
96
# File 'lib/core/plugin/node.rb', line 94

def local_machine=local_machine
  @local_machine = local_machine  
end

#localizeObject




55
56
57
58
# File 'lib/core/plugin/node.rb', line 55

def localize
  @local_context       = true
  myself.local_machine = create_machine(:local_machine, :physical)
end

#machineObject




80
81
82
# File 'lib/core/plugin/node.rb', line 80

def machine
  @machine
end

#machine=(machine) ⇒ Object



84
85
86
# File 'lib/core/plugin/node.rb', line 84

def machine=machine
  @machine = machine  
end

#machine_config {|config| ... } ⇒ Object


Settings groups

Yields:

  • (config)


315
316
317
318
319
320
321
322
# File 'lib/core/plugin/node.rb', line 315

def machine_config
  name   = myself[:id]
  name   = myself[:hostname] if name.nil? || name.empty?
  config = Config.new({ :name => name })
  
  yield(config) if block_given?
  config
end

#machine_type(reset = false) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/core/plugin/node.rb', line 203

def machine_type(reset = false)
  myself[:machine_type] = machine.machine_type if machine && ( reset || myself[:machine_type].nil? )
  machine_type          = myself[:machine_type]
  
  if machine_type.nil? && machine
    if types = machine_types
      unless types.empty?
        machine_type          = machine_type_id(types.first)
        myself[:machine_type] = machine_type
      end
    end
  end
  
  machine_type
end

#machine_type_id(machine_type) ⇒ Object


Machine type utilities



1130
1131
1132
# File 'lib/core/plugin/node.rb', line 1130

def machine_type_id(machine_type)
  machine_type.id
end

#machine_typesObject




199
200
201
# File 'lib/core/plugin/node.rb', line 199

def machine_types # Must be set at machine level (queried)
  machine.machine_types if machine
end

#normalize(reload) ⇒ Object


Node plugin interface



11
12
13
14
15
16
17
18
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
# File 'lib/core/plugin/node.rb', line 11

def normalize(reload)
  super
  
  @class_color = :purple
  
  export.each do |name, value|
    myself[name] = value
  end
  
  ui.resource = Util::Console.colorize(hostname, @class_color)
  logger      = hostname
  
  add_groups([ "all", plugin_provider.to_s, plugin_name.to_s ])
  
  unless reload
    @cli_interface = Util::Liquid.new do |method, args, &code|
      result = exec({ :commands => [ [ method, args ].flatten.join(' ') ] }) do |op, data|
        code.call(op, data) if code
      end
      if result
        result = result.first
        alert(result.errors) unless result.errors.empty?
      end
      result
    end
  
    @action_interface = Util::Liquid.new do |method, args, &code|
      action(method, *args) do |op, data|
        code.call(op, data) if code
      end
    end
  end
end

#private_ip(reset = false) ⇒ Object



112
113
114
115
# File 'lib/core/plugin/node.rb', line 112

def private_ip(reset = false)
  myself[:private_ip] = machine.private_ip if machine && ( reset || myself[:private_ip].nil? )
  myself[:private_ip]
end

#private_keyObject



180
181
182
183
184
# File 'lib/core/plugin/node.rb', line 180

def private_key
  config_key = myself[:private_key]
  return File.expand_path(config_key, network.directory) if config_key
  nil
end

#private_key=(private_key) ⇒ Object




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

def private_key=private_key
  myself[:private_key] = private_key
end

#profilesObject



275
276
277
# File 'lib/core/plugin/node.rb', line 275

def profiles
  array(myself[:profiles])
end

#profiles=(profiles) ⇒ Object




271
272
273
# File 'lib/core/plugin/node.rb', line 271

def profiles=profiles
  myself[:profiles] = array(profiles)
end

#provisioner_infoObject




281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/core/plugin/node.rb', line 281

def provisioner_info
  provisioner_info = {}
      
  # Compose needed provisioners and profiles
  profiles.each do |profile|        
    if info = Plugin::Provisioner.translate_reference(profile)
      provider = info[:provider]
          
      provisioner_info[provider] = { :profiles => [] } unless provisioner_info.has_key?(provider)
      provisioner_info[provider][:profiles] += info[:profiles]
    end
  end
  provisioner_info
end

#provisionersObject




298
299
300
301
302
303
304
# File 'lib/core/plugin/node.rb', line 298

def provisioners
  provisioners = {}        
  provisioner_info.each do |provider, node_profiles|
    provisioners[provider] = network.provisioners(provider)
  end    
  provisioners
end

#public_ip(reset = false) ⇒ Object




107
108
109
110
# File 'lib/core/plugin/node.rb', line 107

def public_ip(reset = false)
  myself[:public_ip] = machine.public_ip if machine && ( reset || myself[:public_ip].nil? )
  myself[:public_ip]
end

#public_keyObject



192
193
194
195
# File 'lib/core/plugin/node.rb', line 192

def public_key
  config_key = myself[:public_key]
  return File.expand_path(config_key, network.directory) if config_key
end

#public_key=(public_key) ⇒ Object




188
189
190
# File 'lib/core/plugin/node.rb', line 188

def public_key=public_key
  myself[:public_key] = public_key
end

#reload(options = {}) ⇒ Object




895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/core/plugin/node.rb', line 895

def reload(options = {})
  success = true
  
  if machine && machine.created?
    config = Config.ensure(options)
    
    if extension_check(:reload, { :config => config })
      logger.info("Reloading node: #{plugin_name}")
    
      yield(:config, config) if block_given?      
      success = machine.reload(config.export)
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:reload_success, { :config => config })
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not created so cannot be reloaded")
  end
  success
end

#render_image(image) ⇒ Object




1149
1150
1151
# File 'lib/core/plugin/node.rb', line 1149

def render_image(image)
  ''  
end

#render_machine_type(machine_type) ⇒ Object




1136
1137
1138
# File 'lib/core/plugin/node.rb', line 1136

def render_machine_type(machine_type)
  ''  
end

#runObject




695
696
697
# File 'lib/core/plugin/node.rb', line 695

def run
  @action_interface
end

#save(options = {}) ⇒ Object




827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
# File 'lib/core/plugin/node.rb', line 827

def save(options = {})
  config = Config.ensure(options)
  
  # Record machine parameters
  if block_given?
    # Provider or external configuration preparation
    yield(config)  
  else
    # Default configuration preparation
    id(true)
    public_ip(true)
    private_ip(true)
    state(true)
  
    machine_type(false)
    image(false)
  end
  
  network.save(config.import({ 
    :commit      => true,
    :allow_empty => true,
    :message     => config.get(:message, "Saving #{plugin_provider} node #{plugin_name}"),
    :remote      => config.get(:remote, :edit)
  }))    
end

#send_files(local_path, remote_path, files = nil, permission = '0644', &code) ⇒ Object




537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/core/plugin/node.rb', line 537

def send_files(local_path, remote_path, files = nil, permission = '0644', &code)
  local_path = File.expand_path(local_path)
  return false unless File.directory?(local_path)
  
  success = true
  
  send_file = lambda do |local_file, remote_file|
    send_success = upload(local_file, remote_file) do |op, options|
      code.call(op, options) if code
    end
    send_success = cli_check(:chmod, permission, remote_file) if send_success
    send_success  
  end
  
  if files && files.is_a?(Array)
    files.flatten.each do |rel_file_name|
      local_file  = "#{local_path}/#{rel_file_name}"
      remote_file = "#{remote_path}/#{rel_file_name}"
  
      if File.exists?(local_file)
        send_success = send_file.call(local_file, remote_file) 
        success      = false unless send_success
      end
    end
  else
    send_success = send_file.call(local_path, remote_path)
    success      = false unless send_success
  end
  success
end

#ssh_path(home_env_var = 'HOME', reset = false) ⇒ Object




169
170
171
172
# File 'lib/core/plugin/node.rb', line 169

def ssh_path(home_env_var = 'HOME', reset = false)
  home = home(home_env_var, reset)
  home ? File.join(home, '.ssh') : nil
end

#ssh_portObject



153
154
155
156
# File 'lib/core/plugin/node.rb', line 153

def ssh_port
  myself[:ssh_port] = 22 if myself[:ssh_port].nil?
  myself[:ssh_port]
end

#ssh_port=(ssh_port) ⇒ Object




148
149
150
151
# File 'lib/core/plugin/node.rb', line 148

def ssh_port=ssh_port
  myself[:ssh_port] = ssh_port
  machine.init_ssh(ssh_port) if machine
end

#start(options = {}) ⇒ Object




855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
# File 'lib/core/plugin/node.rb', line 855

def start(options = {})
  success = true
  
  if machine
    config = Config.ensure(options)
    
    if extension_check(:start, { :config => config })
      logger.info("Starting node: #{plugin_name}")
    
      yield(:config, config) if block_given?      
      success = machine.start(config.export)
      success = save(config) if success
      
      if success
        if bootstrap_script = myself[:bootstrap]
          result = command("HOSTNAME='#{hostname}' #{bootstrap_script}", { :as_admin => true }) do |op, data|
            yield("bootstrap_#{op}".to_sym, data) if block_given?
            data
          end
          success = false unless result.status == code.success && reload
        end
      end
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:start_success, { :config => config })
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine so cannot be started")
  end
  success
end

#state(reset = false) ⇒ Object




131
132
133
134
# File 'lib/core/plugin/node.rb', line 131

def state(reset = false)
  myself[:state] = machine.state if machine && ( reset || myself[:state].nil? )
  myself[:state]
end

#stop(options = {}) ⇒ Object




954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
# File 'lib/core/plugin/node.rb', line 954

def stop(options = {})
  success = true
  
  if machine && machine.running?
    config = Config.ensure(options)
    
    if extension_check(:stop, { :config => config })
      logger.info("Stopping node: #{plugin_name}")
    
      yield(:config, config) if block_given?      
      success = machine.stop(config.export)
      
      myself.machine = nil
      
      override_settings = false
      override_settings = yield(:finalize, config) if success && block_given?
      
      if success && ! override_settings
        delete_setting(:id)
        delete_setting(:public_ip)
        delete_setting(:private_ip)
        delete_setting(:ssh_port)
        delete_setting(:build)
      
        myself[:state] = :stopped
      end
      success = save(config)
      
      if success && block_given?
        process_success = yield(:process, config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:stop_success, { :config => config })
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot be stopped")
  end
  success
end

#terminal(options = {}) ⇒ Object




701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/core/plugin/node.rb', line 701

def terminal(options = {})
  myself.status = code.unknown_status
  
  if machine && machine.running?
    config = Config.ensure(options)
    
    if extension_check(:terminal, { :config => config })
      logger.info("Launching terminal for node: #{plugin_name}")
      
      if local? && local_machine
        active_machine = local_machine
        
        config[:info_prefix]  = "[#{hostname}] "
        config[:error_prefix] = "[#{hostname}] "
      else
        active_machine = machine
      end
      
      config[:private_keys] = private_key
      
      myself.status = active_machine.terminal(user, config.export)
      
      if status == code.success
        extension(:exec_success, { :config => config }) 
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot launch terminal")
  end
  status == code.success
end

#translate_reference(reference) ⇒ Object




1097
1098
1099
# File 'lib/core/plugin/node.rb', line 1097

def translate_reference(reference)
  myself.class.translate_reference(reference)
end

#upload(local_path, remote_path, options = {}) ⇒ Object




499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/core/plugin/node.rb', line 499

def upload(local_path, remote_path, options = {})
  success = false
  
  if machine && machine.running?
    config      = Config.ensure(options)
    hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config })
    
    if extension_check(:upload, hook_config)
      logger.info("Uploading to #{plugin_name}")
    
      render("Starting upload of #{local_path} to #{remote_path}") 
      yield(:config, hook_config) if block_given?
      
      active_machine = local? ? local_machine : machine
      
      success = active_machine.upload(local_path, remote_path, config.export) do |name, sent, total|
        render("#{name}: Sent #{sent} of #{total}")
        yield(:progress, { :name => name, :sent => sent, :total => total })  
      end
      
      if success && block_given?
        render("Successfully finished upload of #{local_path} to #{remote_path}")
        process_success = yield(:process, hook_config)
        success         = process_success if process_success == false        
      end
      
      if success
        extension(:upload_success, hook_config)
      end
    end
  else
    logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot upload")
  end
  success
end

#usable_image?(image) ⇒ Boolean




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

def usable_image?(image)
  true
end

#userObject



142
143
144
# File 'lib/core/plugin/node.rb', line 142

def user
  myself[:user]
end

#user=(user) ⇒ Object




138
139
140
# File 'lib/core/plugin/node.rb', line 138

def user=user
  myself[:user] = user
end