class CORL::Plugin::Node

Public Class Methods

build_info(namespace, plugin_type, data) click to toggle source
Calls superclass method
     # File lib/core/plugin/node.rb
1387 def self.build_info(namespace, plugin_type, data)
1388   data = data.split(/\s*,\s*/) if data.is_a?(String)
1389   super(namespace, plugin_type, data)
1390 end
translate(data) click to toggle source
Calls superclass method
     # File lib/core/plugin/node.rb
1394 def self.translate(data)
1395   options = super(data)
1396 
1397   case data
1398   when String
1399     options = { :name => data }
1400   when Hash
1401     options = data
1402   end
1403 
1404   if options.has_key?(:name)
1405     if matches = translate_reference(options[:name])
1406       options[:provider] = matches[:provider]
1407       options[:name]     = matches[:name]
1408 
1409       logger.debug("Translating node options: #{options.inspect}")
1410     end
1411   end
1412   options
1413 end
translate_reference(reference) click to toggle source
     # File lib/core/plugin/node.rb
1417 def self.translate_reference(reference)
1418   # ex: rackspace:::web1.staging.example.com
1419   if reference && reference.match(/^\s*([a-zA-Z0-9_-]+):::([^\s]+)\s*$/)
1420     provider = $1
1421     name     = $2
1422 
1423     logger.debug("Translating node reference: #{provider}  #{name}")
1424 
1425     info = {
1426       :provider => provider,
1427       :name     => name
1428     }
1429 
1430     logger.debug("Project reference info: #{info.inspect}")
1431     return info
1432   end
1433   nil
1434 end

Public Instance Methods

action(provider, options = {}) { |op, data| ... } click to toggle source
     # File lib/core/plugin/node.rb
 978 def action(provider, options = {})
 979   codes :network_load_error
 980 
 981   config = Config.ensure(options).defaults({
 982     :net_remote   => :edit,
 983     :net_provider => network.plugin_provider
 984   })
 985 
 986   logger.info("Executing remote action #{provider} with encoded arguments: #{config.export.inspect}")
 987 
 988   provider       = provider.to_s.gsub('_', ' ')
 989   action_agent   = !!(provider =~ /^agent\s/)
 990   encoded_config = Util::CLI.encode(Util::Data.clean(config.export))
 991   action_config  = extended_config(:action, {
 992     :command => provider,
 993     :data    => { :encoded  => encoded_config }
 994   })
 995   action_config[:data][:log_level] = Nucleon.log_level if Nucleon.log_level
 996 
 997   quiet    = config.get(:quiet, false)
 998   parallel = config.get(:parallel, true)
 999 
1000   command_base  = 'corl'
1001   command_base  = "NUCLEON_NO_PARALLEL=1 #{command_base}" unless parallel
1002   command_base  = "NUCLEON_NO_COLOR=1 #{command_base}" if action_agent
1003 
1004   result = command(command_base, Util::Data.clean({
1005     :subcommand    => action_config,
1006     :as_admin      => true,
1007     :admin_options => config.get(:admin_options, nil),
1008     :admin_command => config.get(:admin_command, nil),
1009     :quiet         => quiet
1010   })) do |op, data|
1011     yield(op, data) if block_given?
1012   end
1013 
1014   # Update local network configuration so we capture any updates
1015   unless action_agent
1016     if result.status == code.success && ! network.load({ :remote => config[:net_remote], :pull => true })
1017       result.status = code.network_load_error
1018     end
1019   end
1020   result
1021 end
add_agent(provider, options) click to toggle source
    # File lib/core/plugin/node.rb
428 def add_agent(provider, options)
429   if local?
430     set_cache_setting([ :agents, provider ], Config.new(options).export)
431   else
432     remote_set_cache_setting([ :agents, provider ], Config.new(options).export)
433   end
434 end
agent(provider) click to toggle source
    # File lib/core/plugin/node.rb
397 def agent(provider)
398   if local?
399     agent = cache_setting([ :agents, provider ], {}, :hash)
400   else
401     agent = remote_cache_setting([ :agents, provider ], {}, :hash)
402   end
403   Util::Data.symbol_map(agent)
404 end
agent_running(provider) click to toggle source
    # File lib/core/plugin/node.rb
406 def agent_running(provider)
407   if local?
408     agent_options = agent(provider)
409     return false unless agent_options.has_key?(:pid)
410 
411     Process.kill(0, agent_options[:pid].to_i)
412     true
413   else
414     result = run.node_agents
415 
416     if result.status == code.success
417       result_data = Util::Data.parse_json(result.errors)
418       running     = Util::Data.value(result_data[provider.to_s]["running"], false)
419     end
420     return running
421   end
422 rescue Errno::ESRCH # No process (local)
423   false
424 rescue Errno::EPERM # The process exists, but no permission to send signal to it (local)
425   true
426 end
agents() click to toggle source
    # File lib/core/plugin/node.rb
388 def agents
389   if local?
390     agents = cache_setting([ :agents ], {}, :hash)
391   else
392     agents = remote_cache_setting([ :agents ], {}, :hash)
393   end
394   Util::Data.symbol_map(agents)
395 end
attach_keys(keypair) click to toggle source
    # File lib/core/plugin/node.rb
647 def attach_keys(keypair)
648   base_name   = "#{plugin_provider}-#{plugin_name}"
649   save_config = {
650     :pull    => false,
651     :push    => false,
652     :message => "Updating SSH keys for node #{plugin_provider} (#{plugin_name})"
653   }
654 
655   active   = machine && machine.running?
656   ssh_user = user ? user : 'root'
657   result   = run.node_authorize({ :public_key => keypair.ssh_key, :ssh_user => ssh_user }) if active
658   success  = false
659 
660   if ! active || result.status == code.success
661     private_key = network.attach_data(:keys, "#{base_name}-id_#{keypair.type}", keypair.encrypted_key)
662     public_key  = network.attach_data(:keys, "#{base_name}-id_#{keypair.type}.pub", keypair.ssh_key)
663 
664     if private_key && public_key
665       FileUtils.chmod(0600, private_key)
666       FileUtils.chmod(0644, public_key)
667 
668       myself.keypair = Util::SSH.generate({ :private_key => keypair.private_key })
669       myself.keypair.store(network.key_cache_directory, plugin_name)
670 
671       save_config[:files] = [ private_key, public_key ]
672 
673       myself.private_key = private_key
674       myself.public_key  = public_key
675 
676       success = save(extended_config(:key_save, save_config))
677     end
678   end
679   success
680 end
bootstrap(local_path, options = {}) { |"send_#{op}".to_sym, data| ... } click to toggle source
     # File lib/core/plugin/node.rb
1065 def bootstrap(local_path, options = {})
1066   config      = Config.ensure(options)
1067   myself.status = code.unknown_status
1068 
1069   bootstrap_name    = 'bootstrap'
1070   bootstrap_path    = config.get(:bootstrap_path, File.join(CORL.lib_path, '..', bootstrap_name))
1071   bootstrap_glob    = config.get(:bootstrap_glob, '**/*.sh')
1072   bootstrap_init    = config.get(:bootstrap_init, 'bootstrap.sh')
1073   bootstrap_scripts = config.get_array(:bootstrap_scripts)
1074 
1075   user_home  = config[:home]
1076   auth_files = config.get_array(:auth_files)
1077 
1078   reboot       = config.get(:reboot, true)
1079   dev_build    = config.get(:dev_build, false)
1080   ruby_version = config.get(:ruby_version, nil)
1081 
1082   codes :local_path_not_found,
1083         :home_path_lookup_failure,
1084         :auth_upload_failure,
1085         :root_auth_copy_failure,
1086         :bootstrap_upload_failure,
1087         :bootstrap_exec_failure,
1088         :reload_failure
1089 
1090   if File.directory?(local_path)
1091     if user_home || user_home = home(config.get(:home_env_var, 'HOME'), config.get(:force, false))
1092       myself.status = code.success
1093 
1094       # Transmit authorisation / credential files
1095       package_files = [ '.fog', '.netrc', '.google-privatekey.p12', '.vimrc' ]
1096       auth_files.each do |file|
1097         package_files << file.gsub(local_path + '/', '')
1098       end
1099       send_success = send_files(local_path, user_home, package_files, '0600', { :quiet => true }) do |op, data|
1100         yield("send_#{op}".to_sym, data) if block_given?
1101         data
1102       end
1103       unless send_success
1104         myself.status = code.auth_upload_failure
1105       end
1106 
1107       if user.to_sym != config.get(:root_user, :root).to_sym
1108         auth_files     = package_files.collect { |path| "'#{path}'"}
1109         root_home_path = config.get(:root_home, '/root')
1110 
1111         result        = command("cp #{auth_files.join(' ')} #{root_home_path}", { :as_admin => true, :admin_options => '' })
1112         myself.status = code.root_auth_copy_failure unless result.status == code.success
1113       end
1114 
1115       # Send bootstrap package
1116       if status == code.success
1117         remote_bootstrap_path = File.join(user_home, bootstrap_name)
1118 
1119         cli.rm('-Rf', remote_bootstrap_path)
1120         send_success = send_files(bootstrap_path, remote_bootstrap_path, nil, '0700', { :quiet => true }) do |op, data|
1121           yield("send_#{op}".to_sym, data) if block_given?
1122           data
1123         end
1124         unless send_success
1125           myself.status = code.bootstrap_upload_failure
1126         end
1127 
1128         # Execute bootstrap process
1129         if status == code.success
1130           remote_script = File.join(remote_bootstrap_path, bootstrap_init)
1131           environment   = "HOSTNAME='#{hostname}' "
1132           environment  << "DEVELOPMENT_BUILD=1 " if dev_build
1133           environment  << "RUBY_RVM_VERSION='#{ruby_version}' " if ruby_version
1134           script_names  = bootstrap_scripts.empty? ? '' : bootstrap_scripts.join(' ')
1135 
1136           myself.bootstrap_script = remote_script
1137 
1138           result = command("#{environment} #{remote_script} #{script_names}", { :as_admin => true }) do |op, data|
1139             yield("exec_#{op}".to_sym, data) if block_given?
1140             data
1141           end
1142 
1143           if result.status == code.success
1144             # Reboot the machine
1145             if reboot && ! reload
1146               warn('corl.core.node.bootstrap.reload')
1147               myself.status = code.reload_failure
1148             end
1149           else
1150             warn('corl.core.node.bootstrap.status', { :script => remote_script, :status => result.status })
1151             myself.status = code.bootstrap_exec_failure
1152           end
1153         end
1154       end
1155     else
1156       myself.status = code.home_path_lookup_failure
1157     end
1158   else
1159     myself.status = code.local_path_not_found
1160   end
1161   status == code.success
1162 end
bootstrap_script() click to toggle source
    # File lib/core/plugin/node.rb
382 def bootstrap_script
383   myself[:bootstrap]
384 end
bootstrap_script=(bootstrap) click to toggle source
    # File lib/core/plugin/node.rb
378 def bootstrap_script=bootstrap
379   myself[:bootstrap] = bootstrap
380 end
build(options = {}) click to toggle source
    # File lib/core/plugin/node.rb
592 def build(options = {})
593   config  = Config.ensure(options)
594   success = true
595 
596   # TODO: Figure out what's going on with the parallel implementation here.
597   parallel_original          = ENV['NUCLEON_NO_PARALLEL']
598   ENV['NUCLEON_NO_PARALLEL'] = 'true'
599 
600   status  = parallel(:build_provider, network.builders, config)
601   success = false if status.values.include?(false)
602 
603   if success
604     if array(config[:providers]).empty? || array(config[:providers]).include?("package")
605       status  = parallel(:build_provisioners, provisioners, config)
606       success = false if status.values.include?(false)
607     end
608 
609     if success
610       myself.build_time = Time.now.to_s if success
611 
612       if config.delete(:save, true)
613         success("Saving successful build", { :i18n => false })
614 
615         success = save(extended_config(:build, {
616           :message => config.get(:message, "Built #{plugin_provider} node: #{plugin_name}"),
617           :remote  => config.get(:remote, :edit)
618         }))
619       end
620     end
621   end
622 
623   ENV['NUCLEON_NO_PARALLEL'] = parallel_original
624   success
625 end
build_provider(provider, plugin, config) click to toggle source
    # File lib/core/plugin/node.rb
627 def build_provider(provider, plugin, config)
628   if array(config[:providers]).empty? || array(config[:providers]).include?(provider.to_s)
629     #info("Building #{provider} components", { :i18n => false })
630     plugin.build(myself, config)
631   end
632 end
build_provisioner(name, plugin, provider, config) click to toggle source
    # File lib/core/plugin/node.rb
640 def build_provisioner(name, plugin, provider, config)
641   #info("Building #{provider} #{name} provisioner components", { :i18n => false })
642   plugin.build(myself, config)
643 end
build_provisioners(provider, collection, config) click to toggle source
    # File lib/core/plugin/node.rb
634 def build_provisioners(provider, collection, config)
635   #info("Building #{provider} provisioner collection", { :i18n => false })
636   status = parallel(:build_provisioner, collection, provider, config)
637   status.values.include?(false) ? false : true
638 end
build_time() click to toggle source
    # File lib/core/plugin/node.rb
372 def build_time
373   myself[:build]
374 end
build_time=(time) click to toggle source
    # File lib/core/plugin/node.rb
368 def build_time=time
369   myself[:build] = time
370 end
clear_remote_cache() click to toggle source
    # File lib/core/plugin/node.rb
585 def clear_remote_cache
586   run.node_cache({ :clear => true }).status == code.success
587 end
cli() click to toggle source
    # File lib/core/plugin/node.rb
939 def cli
940   @cli_interface
941 end
cli_capture(cli_command, *args) click to toggle source
     # File lib/core/plugin/node.rb
1445 def cli_capture(cli_command, *args)
1446   result = exec({ :commands => [ [ cli_command, args ].flatten.join(' ') ], :quiet => true })
1447   result = result.first
1448 
1449   if result.status == code.success && ! result.output.empty?
1450     result.output
1451   else
1452     nil
1453   end
1454 end
cli_check(cli_command, *args) click to toggle source
     # File lib/core/plugin/node.rb
1458 def cli_check(cli_command, *args)
1459   result = cli.send(cli_command, args)
1460   result.status == code.success ? true : false
1461 end
command(command, options = {}) { |op, data| ... } click to toggle source
    # File lib/core/plugin/node.rb
945 def command(command, options = {})
946   config         = Config.ensure(options)
947   as_admin       = config.delete(:as_admin, false)
948   remove_command = false
949 
950   unless command.is_a?(CORL::Plugin::Command)
951     command        = CORL.command(Config.new({ :command => command }, {}, true, false).import(config), :bash)
952     remove_command = true
953   end
954 
955   admin_command = ''
956   if as_admin
957     admin_options         = config.delete(:admin_options, '-i')
958     default_admin_command = password ? "echo '#{password}' | sudo -S #{admin_options}" : "sudo #{admin_options}"
959 
960     admin_command = config.delete(:admin_command, default_admin_command)
961     admin_command = extension_set(:admin_command, admin_command, config)
962   else
963     config.delete(:admin_command)
964   end
965 
966   config[:commands] = [ "#{admin_command} #{command.to_s}".strip ]
967 
968   results = exec(config) do |op, data|
969     yield(op, data) if block_given?
970   end
971 
972   CORL.remove_plugin(command) if remove_command
973   results.first
974 end
create(options = {}) { |:config, config| ... } click to toggle source
    # File lib/core/plugin/node.rb
722 def create(options = {})
723   success = true
724 
725   if machine
726     config = Config.ensure(options)
727 
728     clear_cache
729 
730     if extension_check(:create, { :config => config })
731       logger.info("Creating node: #{plugin_name}")
732 
733       yield(:config, config) if block_given?
734       success = machine.create(config.export)
735 
736       if success && block_given?
737         process_success = yield(:process, config)
738         success         = process_success if process_success == false
739       end
740 
741       if success
742         extension(:create_success, { :config => config })
743       end
744     end
745   else
746     logger.warn("Node #{plugin_name} does not have an attached machine so cannot be created")
747   end
748   success
749 end
create_facts_post(data, names) click to toggle source
    # File lib/core/plugin/node.rb
500 def create_facts_post(data, names)
501   fact_values = {}
502   names.each do |name|
503     name              = name.to_sym
504     fact_values[name] = data[name]
505   end
506   ensure_custom_facts(fact_values)
507   data
508 end
create_image(options = {}) { |:config, config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1266 def create_image(options = {})
1267   success = true
1268 
1269   if machine && machine.running?
1270     config = Config.ensure(options)
1271 
1272     if extension_check(:create_image, { :config => config })
1273       logger.info("Executing node: #{plugin_name}")
1274 
1275       yield(:config, config) if block_given?
1276       success = machine.create_image(config.export)
1277       success = save(config) if success
1278 
1279       if success && block_given?
1280         process_success = yield(:process, config)
1281         success         = process_success if process_success == false
1282       end
1283 
1284       if success
1285         extension(:create_image_success, { :config => config })
1286       end
1287     end
1288   else
1289     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot create an image")
1290   end
1291   success
1292 end
create_machine(name, provider, options = {}) click to toggle source
    # File lib/core/plugin/node.rb
716 def create_machine(name, provider, options = {})
717   CORL.create_plugin(:CORL, :machine, provider, extended_config(name, options).import({ :meta => { :parent => myself }}))
718 end
custom_facts() click to toggle source
    # File lib/core/plugin/node.rb
313 def custom_facts
314   search(:facts, {}, :hash)
315 end
custom_facts=(facts) click to toggle source
    # File lib/core/plugin/node.rb
309 def custom_facts=facts
310   myself[:facts] = hash(facts)
311 end
delete_facts_post(data, old_data) click to toggle source
    # File lib/core/plugin/node.rb
512 def delete_facts_post(data, old_data)
513   remove_custom_facts(*old_data.keys)
514   data
515 end
delete_keys() click to toggle source
    # File lib/core/plugin/node.rb
684 def delete_keys
685   private_key = myself[:private_key]
686   public_key  = myself[:public_key]
687 
688   keys = []
689   keys << private_key if private_key
690   keys << public_key if public_key
691 
692   success = true
693 
694   unless keys.empty?
695     files = network.delete_attachments(keys)
696 
697     if files && ! files.empty?
698       delete_setting(:private_key)
699       delete_setting(:public_key)
700 
701       success = save(extended_config(:key_delete, {
702         :files   => [ private_key, public_key ],
703         :pull    => false,
704         :push    => false,
705         :message => "Removing SSH keys for node #{plugin_provider} (#{plugin_name})"
706       }))
707     else
708       success = false
709     end
710   end
711   success
712 end
delete_remote_cache_setting(keys) click to toggle source
    # File lib/core/plugin/node.rb
576 def delete_remote_cache_setting(keys)
577   run.node_cache({
578     :name   => keys.flatten.join('.'),
579     :delete => true
580   }).status == code.success
581 end
destroy(options = {}) { |:config, config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1341 def destroy(options = {})
1342   config  = Config.ensure(options)
1343   success = true
1344 
1345   if extension_check(:destroy, { :config => config })
1346     logger.info("Destroying node: #{plugin_name}")
1347 
1348     yield(:config, config) if block_given?
1349 
1350     if machine && machine.created?
1351       # Shut down machine
1352       success = machine.destroy(config.export)
1353       myself.machine = nil
1354     else
1355       logger.warn("Node #{plugin_name} does not have an attached machine or is not created so cannot be destroyed")
1356     end
1357 
1358     # Remove SSH keys
1359     if success && delete_keys
1360       # Remove node information
1361       network.delete_node(plugin_provider, plugin_name, false)
1362 
1363       network.save({
1364         :commit  => true,
1365         :remote  => config.get(:remote, :edit),
1366         :message => config.get(:message, "Destroying node #{plugin_name}"),
1367         :push    => true
1368       })
1369     end
1370 
1371     if success && block_given?
1372       process_success = yield(:process, config)
1373       success         = process_success if process_success == false
1374     end
1375 
1376     if success
1377       extension(:destroy_success, { :config => config })
1378       clear_cache
1379     end
1380   end
1381   success
1382 end
download(remote_path, local_path, options = {}) { |:config, hook_config| ... } click to toggle source
    # File lib/core/plugin/node.rb
753 def download(remote_path, local_path, options = {})
754   success = false
755 
756   if machine && machine.running?
757     config      = Config.ensure(options)
758     hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config }, {}, true, false)
759     quiet       = config.get(:quiet, false)
760     progress    = config.get(:progress, true)
761 
762     if extension_check(:download, hook_config)
763       logger.info("Downloading from #{plugin_name}")
764 
765       info("Starting download of #{remote_path} to #{local_path}", { :i18n => false }) unless quiet
766       yield(:config, hook_config) if block_given?
767 
768       active_machine = local? ? local_machine : machine
769 
770       success = active_machine.download(remote_path, local_path, config.export) do |name, received, total|
771         info("#{name}: Sent #{received} of #{total}", { :i18n => false }) if progress && ! quiet
772         yield(:progress, { :name => name, :received => received, :total => total })
773       end
774 
775       if success && block_given?
776         info("Successfully finished download of #{remote_path} to #{local_path}", { :i18n => false }) unless quiet
777         process_success = yield(:process, hook_config)
778         success         = process_success if process_success == false
779       end
780 
781       if success
782         extension(:download_success, hook_config)
783       end
784     end
785   else
786     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot download")
787   end
788   success
789 end
ensure_custom_facts(facts) click to toggle source
    # File lib/core/plugin/node.rb
317 def ensure_custom_facts(facts)
318   hash(facts).each do |name, value|
319     set_setting([ :facts, name ], value)
320   end
321 end
exec(options = {}) { |:config, config| ... } click to toggle source
    # File lib/core/plugin/node.rb
866 def exec(options = {})
867   default_error = Util::Shell::Result.new(:error, 255)
868   results       = [ default_error ]
869 
870   if local? && local_machine || machine && machine.running?
871     config = Config.ensure(options)
872 
873     if extension_check(:exec, { :config => config })
874       logger.info("Executing node: #{plugin_name}")
875 
876       yield(:config, config) if block_given?
877 
878       if local? && local_machine
879         active_machine = local_machine
880       else
881         active_machine = machine
882       end
883 
884       if commands = config.get(:commands, nil)
885         quiet = config.get(:quiet, false)
886 
887         begin
888           test = active_machine.exec(Util::Data.array(commands), config.export) do |type, command, data|
889             data = filter_output(type, data).rstrip
890 
891             unless quiet
892               unless local?
893                 unless data.empty?
894                   if type == :error
895                     warn(data, { :i18n => false })
896                   else
897                     info(data, { :i18n => false })
898                   end
899                 end
900               end
901               yield(:progress, { :type => type, :command => command, :data => data }) if block_given?
902             end
903             data
904           end
905           results = test if test
906 
907         rescue => error
908           default_error.append_errors(error.message)
909         end
910       else
911         default_error.append_errors("No execution command")
912       end
913 
914       if results
915         success = true
916         results.each do |result|
917           success = false if result.status != code.success
918         end
919         if success
920           yield(:process, config) if block_given?
921           extension(:exec_success, { :config => config, :results => results })
922         end
923       else
924         default_error.append_errors("No execution results")
925       end
926     else
927       default_error.append_errors("Execution prevented by exec hook")
928     end
929   else
930     default_error.append_errors("No attached machine")
931 
932     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot execute commands")
933   end
934   results
935 end
fact_var() click to toggle source
    # File lib/core/plugin/node.rb
463 def fact_var
464   @facts
465 end
fact_var=(facts) click to toggle source
    # File lib/core/plugin/node.rb
467 def fact_var=facts
468   @facts = facts
469 end
facts(reset = false, clone = true) click to toggle source
    # File lib/core/plugin/node.rb
473 def facts(reset = false, clone = true)
474   if reset || fact_var.nil?
475     default_facts = extended_config(:hiera_default_facts, {
476       :fqdn          => hostname,
477       :hostname      => hostname.gsub(/\..*$/, ''),
478       :corl_provider => plugin_provider.to_s
479     })
480 
481     unless local?
482       result = run.node_facts({ :quiet => true })
483 
484       if result.status == code.success
485         node_facts = Util::Data.symbol_map(Util::Data.parse_json(result.errors))
486       end
487     end
488     unless node_facts
489       node_facts = local? ? Util::Data.merge([ CORL.facts, custom_facts ]) : custom_facts
490     end
491 
492     self.fact_var = Config.new(node_facts, {}, true, false).defaults(default_facts).export
493   end
494   return fact_var.clone if clone
495   fact_var
496 end
filter_output(type, data) click to toggle source
     # File lib/core/plugin/node.rb
1465 def filter_output(type, data)
1466   if type == :output
1467     # Hide redundant Facter output
1468     if data =~ /^Already evaluated [a-z]+ at [^,]+, reevaluating anyways$/
1469       data = ''
1470     end
1471   elsif type == :error
1472     if data.include?('stdin: is not a tty') ||
1473        data.include?('unable to re-open stdin') ||
1474        data.include?('[sudo] password for') ||
1475        data.include?('sudo: unable to resolve host') ||
1476        data == 'WARNING: $SAFE is not supported on Rubinius.'
1477       data = ''
1478     end
1479   end
1480   data
1481 end
hiera_override_dir() click to toggle source
    # File lib/core/plugin/node.rb
529 def hiera_override_dir
530   network.hiera_override_dir
531 end
hiera_var() click to toggle source
    # File lib/core/plugin/node.rb
519 def hiera_var
520   @hiera
521 end
hiera_var=(hiera) click to toggle source
    # File lib/core/plugin/node.rb
523 def hiera_var=hiera
524   @hiera = hiera
525 end
home(env_var = 'HOME', reset = false) click to toggle source
    # File lib/core/plugin/node.rb
198 def home(env_var = 'HOME', reset = false)
199   if reset || myself[:user_home].nil?
200     myself[:user_home] = cli_capture(:echo, '$' + env_var.to_s.gsub('$', '')) if machine
201   end
202   myself[:user_home]
203 end
hostname() click to toggle source
    # File lib/core/plugin/node.rb
143 def hostname
144   hostname = setting(:hostname)
145 
146   unless hostname
147     hostname = plugin_name
148   end
149 
150   if hostname.to_s != ui.resource.to_s
151     ui.resource = Util::Console.colorize(hostname, @class_color)
152     logger      = hostname
153   end
154   hostname
155 end
id(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
124 def id(reset = false)
125   myself[:id] = machine.plugin_name if machine && ( reset || setting(:id).nil? )
126   setting(:id)
127 end
image(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
302 def image(reset = false)
303   myself[:image] = machine.image if machine && ( reset || myself[:image].nil? )
304   myself[:image]
305 end
image=(image) click to toggle source
    # File lib/core/plugin/node.rb
298 def image=image
299   myself[:image] = image
300 end
image_id(image) click to toggle source
     # File lib/core/plugin/node.rb
1499 def image_id(image)
1500   image.id
1501 end
image_search_text(image) click to toggle source
     # File lib/core/plugin/node.rb
1511 def image_search_text(image)
1512   image.to_s
1513 end
images(search_terms = [], options = {}) click to toggle source
    # File lib/core/plugin/node.rb
259 def images(search_terms = [], options = {})
260   config = Config.ensure(options)
261   images = []
262 
263   if machine
264     loaded_images = machine.images
265 
266     if loaded_images
267       require_all = config.get(:require_all, false)
268 
269       loaded_images.each do |image|
270         if usable_image?(image)
271           include_image = ( search_terms.empty? ? true : require_all )
272           image_text    = image_search_text(image)
273 
274           search_terms.each do |term|
275             if config.get(:match_case, false)
276               success = image_text.match(/#{term}/)
277             else
278               success = image_text.match(/#{term}/i)
279             end
280 
281             if require_all
282               include_image = false unless success
283             else
284               include_image = true if success
285             end
286           end
287 
288           images << image if include_image
289         end
290       end
291     end
292   end
293   images
294 end
keypair() click to toggle source
   # File lib/core/plugin/node.rb
94 def keypair
95   @keypair
96 end
keypair=(keypair) click to toggle source
    # File lib/core/plugin/node.rb
 98 def keypair=keypair
 99   @keypair = keypair
100 end
local?() click to toggle source
   # File lib/core/plugin/node.rb
77 def local?
78   @local_context ? true : false
79 end
local_machine() click to toggle source
    # File lib/core/plugin/node.rb
114 def local_machine
115   @local_machine
116 end
local_machine=(local_machine) click to toggle source
    # File lib/core/plugin/node.rb
118 def local_machine=local_machine
119   @local_machine = local_machine
120 end
localize() click to toggle source
   # File lib/core/plugin/node.rb
55 def localize
56   @local_context       = true
57   myself.local_machine = create_machine(:local_machine, :physical)
58 end
lookup(property, default = nil, options = {}) click to toggle source
    # File lib/core/plugin/node.rb
535 def lookup(property, default = nil, options = {})
536   unless local?
537     config = Config.ensure(options).import({ :properties => [ property ], :quiet => true })
538     result = run.node_lookup(config)
539 
540     if result.status == code.success
541       result_data = Util::Data.parse_json(result.errors)
542       return Util::Data.value(result_data[property.to_s], default)
543     end
544     return default
545   end
546   options[:hiera_scope] = Util::Data.prefix('::', facts, '')
547   lookup_base(property, default, options)
548 end
machine() click to toggle source
    # File lib/core/plugin/node.rb
104 def machine
105   @machine
106 end
machine=(machine) click to toggle source
    # File lib/core/plugin/node.rb
108 def machine=machine
109   @machine = machine
110 end
machine_config() { |config| ... } click to toggle source
    # File lib/core/plugin/node.rb
451 def machine_config
452   name   = setting(:id)
453   name   = nil if name.nil? || name.empty?
454   config = Config.new({ :name => name }, {}, true, false)
455 
456   yield(config) if block_given?
457   config
458 end
machine_type(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
241 def machine_type(reset = false)
242   myself[:machine_type] = machine.machine_type if machine && ( reset || myself[:machine_type].nil? )
243   machine_type          = myself[:machine_type]
244 
245   if machine_type.nil? && machine
246     if types = machine_types
247       unless types.empty?
248         machine_type          = machine_type_id(types.first)
249         myself[:machine_type] = machine_type
250       end
251     end
252   end
253 
254   machine_type
255 end
machine_type_id(machine_type) click to toggle source
     # File lib/core/plugin/node.rb
1486 def machine_type_id(machine_type)
1487   machine_type.id
1488 end
machine_types() click to toggle source
    # File lib/core/plugin/node.rb
237 def machine_types # Must be set at machine level (queried)
238   machine.machine_types if machine
239 end
method_missing(method, *args, &code) click to toggle source
   # File lib/core/plugin/node.rb
47 def method_missing(method, *args, &code)
48   action(method, *args) do |op, data|
49     code.call(op, data) if code
50   end
51 end
normalize(reload) click to toggle source
Calls superclass method
   # File lib/core/plugin/node.rb
12 def normalize(reload)
13   super
14 
15   @class_color = :purple
16 
17   export.each do |name, value|
18     myself[name] = value
19   end
20 
21   ui.resource = Util::Console.colorize(hostname, @class_color)
22   logger      = hostname
23 
24   myself[:settings] = [ "all", plugin_provider.to_s, plugin_name.to_s ] | array(setting(:settings))
25 
26   unless reload
27     @cli_interface = Util::Liquid.new do |method, args, &code|
28       result = command([ method, args ].flatten.join(' '), { :as_admin => true }) do |op, data|
29         code.call(op, data) if code
30       end
31       if result
32         warn(result.errors, { :i18n => false }) unless result.errors.empty?
33       end
34       result
35     end
36 
37     @action_interface = Util::Liquid.new do |method, args, &code|
38       action(method, *args) do |op, data|
39         code.call(op, data) if code
40       end
41     end
42   end
43 end
password() click to toggle source
    # File lib/core/plugin/node.rb
180 def password
181   @password
182 end
password=(password) click to toggle source
    # File lib/core/plugin/node.rb
176 def password=password
177   @password = password
178 end
private_ip(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
136 def private_ip(reset = false)
137   myself[:private_ip] = machine.private_ip if machine && ( reset || setting(:private_ip).nil? )
138   setting(:private_ip)
139 end
private_key() click to toggle source
    # File lib/core/plugin/node.rb
218 def private_key
219   config_key = myself[:private_key]
220   return File.expand_path(config_key, network.directory) if config_key
221   nil
222 end
private_key=(private_key) click to toggle source
    # File lib/core/plugin/node.rb
214 def private_key=private_key
215   myself[:private_key] = private_key
216 end
profiles() click to toggle source
    # File lib/core/plugin/node.rb
335 def profiles
336   [ array(myself[:profiles]), lookup_array(:profiles).reverse ].flatten
337 end
profiles=(profiles) click to toggle source
    # File lib/core/plugin/node.rb
331 def profiles=profiles
332   myself[:profiles] = array(profiles)
333 end
provisioner_info() click to toggle source
    # File lib/core/plugin/node.rb
341 def provisioner_info
342   provisioner_info = {}
343 
344   # Compose needed provisioners and profiles
345   profiles.each do |profile|
346     if info = Plugin::Provisioner.translate_reference(profile)
347       provider = info[:provider]
348 
349       provisioner_info[provider] = { :profiles => [] } unless provisioner_info.has_key?(provider)
350       provisioner_info[provider][:profiles] += info[:profiles]
351     end
352   end
353   provisioner_info
354 end
provisioners() click to toggle source
    # File lib/core/plugin/node.rb
358 def provisioners
359   provisioners = {}
360   provisioner_info.each do |provider, node_profiles|
361     provisioners[provider] = network.provisioners(provider)
362   end
363   provisioners
364 end
public_ip(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
131 def public_ip(reset = false)
132   myself[:public_ip] = machine.public_ip if machine && ( reset || setting(:public_ip).nil? )
133   setting(:public_ip)
134 end
public_key() click to toggle source
    # File lib/core/plugin/node.rb
230 def public_key
231   config_key = myself[:public_key]
232   return File.expand_path(config_key, network.directory) if config_key
233 end
public_key=(public_key) click to toggle source
    # File lib/core/plugin/node.rb
226 def public_key=public_key
227   myself[:public_key] = public_key
228 end
reload(options = {}) { |:config, config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1237 def reload(options = {})
1238   success = true
1239 
1240   if machine && machine.created?
1241     config = Config.ensure(options)
1242 
1243     if extension_check(:reload, { :config => config })
1244       logger.info("Reloading node: #{plugin_name}")
1245 
1246       yield(:config, config) if block_given?
1247       success = machine.reload(config.export)
1248 
1249       if success && block_given?
1250         process_success = yield(:process, config)
1251         success         = process_success if process_success == false
1252       end
1253 
1254       if success
1255         extension(:reload_success, { :config => config })
1256       end
1257     end
1258   else
1259     logger.warn("Node #{plugin_name} does not have an attached machine or is not created so cannot be reloaded")
1260   end
1261   success
1262 end
remote_cache_setting(keys, default = nil, format = false) click to toggle source
    # File lib/core/plugin/node.rb
552 def remote_cache_setting(keys, default = nil, format = false)
553   cache_id = keys.flatten.join('.')
554   result   = run.node_cache({ :name => cache_id })
555 
556   if result.status == code.success
557     value = Util::Data.value(Util::Data.parse_json(result.errors), default)
558   else
559     value = default
560   end
561   filter(value, format)
562 end
remove_agent(provider) click to toggle source
    # File lib/core/plugin/node.rb
436 def remove_agent(provider)
437   if local?
438     agent_options = agent(provider)
439     if agent_options && agent_running(provider)
440       Process.kill("SIGINT",  agent_options[:pid].to_i)
441     end
442     delete_cache_setting([ :agents, provider ])
443   else
444     remote_delete_cache_setting([ :agents, provider ])
445   end
446 end
remove_custom_facts(*facts) click to toggle source
    # File lib/core/plugin/node.rb
323 def remove_custom_facts(*facts)
324   facts.each do |name|
325     delete_setting([ :facts, name ])
326   end
327 end
remove_plugin() click to toggle source
   # File lib/core/plugin/node.rb
62 def remove_plugin
63   CORL.remove_plugin(local_machine) if local_machine
64   CORL.remove_plugin(machine) if machine
65 end
render_image(image) click to toggle source
     # File lib/core/plugin/node.rb
1505 def render_image(image)
1506   ''
1507 end
render_machine_type(machine_type) click to toggle source
     # File lib/core/plugin/node.rb
1492 def render_machine_type(machine_type)
1493   ''
1494 end
run() click to toggle source
     # File lib/core/plugin/node.rb
1025 def run
1026   @action_interface
1027 end
running?() click to toggle source
   # File lib/core/plugin/node.rb
70 def running?
71   return machine.running? if machine
72   false
73 end
save(options = {}) { |config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1166 def save(options = {})
1167   config = Config.ensure(options)
1168 
1169   # Record machine parameters
1170   if block_given?
1171     # Provider or external configuration preparation
1172     yield(config)
1173   else
1174     # Default configuration preparation
1175     id(true)
1176     public_ip(true)
1177     private_ip(true)
1178     state(true)
1179 
1180     machine_type(false)
1181     image(false)
1182   end
1183 
1184   remote = config.get(:remote, :edit)
1185   remote = nil unless network.remote(remote)
1186 
1187   network.save(config.import({
1188     :commit      => true,
1189     :allow_empty => true,
1190     :message     => config.get(:message, "Saving #{plugin_provider} node #{plugin_name}"),
1191     :remote      => remote
1192   }))
1193 end
send_files(local_path, remote_path, files = nil, permission = '0644', options = {}, &code) click to toggle source
    # File lib/core/plugin/node.rb
833 def send_files(local_path, remote_path, files = nil, permission = '0644', options = {}, &code)
834   local_path = File.expand_path(local_path)
835   return false unless File.directory?(local_path) || File.exists?(local_path)
836 
837   success = true
838 
839   send_file = lambda do |local_file, remote_file|
840     send_success = upload(local_file, remote_file, options) do |op, upload_options|
841       code.call(op, upload_options) if code
842     end
843     send_success = cli_check(:chmod, permission, remote_file) if send_success
844     send_success
845   end
846 
847   if files && files.is_a?(Array)
848     files.flatten.each do |rel_file_name|
849       local_file  = "#{local_path}/#{rel_file_name}"
850       remote_file = "#{remote_path}/#{rel_file_name}"
851 
852       if File.exists?(local_file)
853         send_success = send_file.call(local_file, remote_file)
854         success      = false unless send_success
855       end
856     end
857   else
858     send_success = send_file.call(local_path, remote_path)
859     success      = false unless send_success
860   end
861   success
862 end
set_remote_cache_setting(keys, value) click to toggle source
    # File lib/core/plugin/node.rb
566 def set_remote_cache_setting(keys, value)
567   run.node_cache({
568     :name  => keys.flatten.join('.'),
569     :value => Util::Data.to_json(value, false),
570     :json  => true
571   }).status == code.success
572 end
ssh_path(home_env_var = 'HOME', reset = false) click to toggle source
    # File lib/core/plugin/node.rb
207 def ssh_path(home_env_var = 'HOME', reset = false)
208   home = home(home_env_var, reset)
209   home ? File.join(home, '.ssh') : nil
210 end
ssh_port() click to toggle source
    # File lib/core/plugin/node.rb
191 def ssh_port
192   myself[:ssh_port] = 22 if myself[:ssh_port].nil?
193   myself[:ssh_port]
194 end
ssh_port=(ssh_port) click to toggle source
    # File lib/core/plugin/node.rb
186 def ssh_port=ssh_port
187   myself[:ssh_port] = ssh_port
188   machine.init_ssh(ssh_port) if machine
189 end
start(options = {}) { |:config, config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1197 def start(options = {})
1198   success = true
1199 
1200   if machine
1201     config = Config.ensure(options)
1202 
1203     if extension_check(:start, { :config => config })
1204       logger.info("Starting node: #{plugin_name}")
1205 
1206       yield(:config, config) if block_given?
1207       success = machine.start(config.export)
1208       success = save(config) if success
1209 
1210       if success
1211         if config.get(:bootstrap, false) && bootstrap_script
1212           result = command("HOSTNAME='#{hostname}' #{bootstrap_script}", { :as_admin => true }) do |op, data|
1213             yield("bootstrap_#{op}".to_sym, data) if block_given?
1214             data
1215           end
1216           success = false unless result.status == code.success && reload
1217         end
1218       end
1219 
1220       if success && block_given?
1221         process_success = yield(:process, config)
1222         success         = process_success if process_success == false
1223       end
1224 
1225       if success
1226         extension(:start_success, { :config => config })
1227       end
1228     end
1229   else
1230     logger.warn("Node #{plugin_name} does not have an attached machine so cannot be started")
1231   end
1232   success
1233 end
state(reset = false) click to toggle source
    # File lib/core/plugin/node.rb
159 def state(reset = false)
160   myself[:state] = machine.state if machine && ( reset || setting(:state).nil? )
161   setting(:state)
162 end
stop(options = {}) { |:config, config| ... } click to toggle source
     # File lib/core/plugin/node.rb
1296 def stop(options = {})
1297   success = true
1298 
1299   if machine && machine.running?
1300     config = Config.ensure(options)
1301 
1302     if extension_check(:stop, { :config => config })
1303       logger.info("Stopping node: #{plugin_name}")
1304 
1305       yield(:config, config) if block_given?
1306       success = machine.stop(config.export)
1307 
1308       myself.machine = nil
1309 
1310       override_settings = false
1311       override_settings = yield(:finalize, config) if success && block_given?
1312 
1313       if success && ! override_settings
1314         delete_setting(:id)
1315         delete_setting(:public_ip)
1316         delete_setting(:private_ip)
1317         delete_setting(:ssh_port)
1318         delete_setting(:build)
1319 
1320         myself[:state] = :stopped
1321       end
1322       success = save(config)
1323 
1324       if success && block_given?
1325         process_success = yield(:process, config)
1326         success         = process_success if process_success == false
1327       end
1328 
1329       if success
1330         extension(:stop_success, { :config => config })
1331       end
1332     end
1333   else
1334     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot be stopped")
1335   end
1336   success
1337 end
terminal(options = {}) click to toggle source
     # File lib/core/plugin/node.rb
1031 def terminal(options = {})
1032   myself.status = code.unknown_status
1033 
1034   if machine && machine.running?
1035     config = Config.ensure(options)
1036 
1037     if extension_check(:terminal, { :config => config })
1038       logger.info("Launching terminal for node: #{plugin_name}")
1039 
1040       if local? && local_machine
1041         active_machine = local_machine
1042       else
1043         active_machine = machine
1044       end
1045 
1046       config[:key_dir]      = network.key_cache_directory
1047       config[:key_name]     = plugin_name
1048       config[:private_keys] = private_key
1049       config[:port]         = ssh_port
1050 
1051       myself.status = active_machine.terminal(user, config.export)
1052 
1053       if status == code.success
1054         extension(:exec_success, { :config => config })
1055       end
1056     end
1057   else
1058     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot launch terminal")
1059   end
1060   status == code.success
1061 end
translate_reference(reference) click to toggle source
     # File lib/core/plugin/node.rb
1438 def translate_reference(reference)
1439   myself.class.translate_reference(reference)
1440 end
upload(local_path, remote_path, options = {}) { |:config, hook_config| ... } click to toggle source
    # File lib/core/plugin/node.rb
793 def upload(local_path, remote_path, options = {})
794   success = false
795 
796   if machine && machine.running?
797     config      = Config.ensure(options)
798     hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config }, {}, true, false)
799     quiet       = config.get(:quiet, false)
800     progress    = config.get(:progress, true)
801 
802     if extension_check(:upload, hook_config)
803       logger.info("Uploading to #{plugin_name}")
804 
805       info("Starting upload of #{local_path} to #{remote_path}", { :i18n => false }) unless quiet
806       yield(:config, hook_config) if block_given?
807 
808       active_machine = local? ? local_machine : machine
809 
810       success = active_machine.upload(local_path, remote_path, config.export) do |name, sent, total|
811         info("#{name}: Sent #{sent} of #{total}", { :i18n => false }) if progress && ! quiet
812         yield(:progress, { :name => name, :sent => sent, :total => total })
813       end
814 
815       if success && block_given?
816         info("Successfully finished upload of #{local_path} to #{remote_path}", { :i18n => false }) unless quiet
817         process_success = yield(:process, hook_config)
818         success         = process_success if process_success == false
819       end
820 
821       if success
822         extension(:upload_success, hook_config)
823       end
824     end
825   else
826     logger.warn("Node #{plugin_name} does not have an attached machine or is not running so cannot upload")
827   end
828   success
829 end
usable_image?(image) click to toggle source
   # File lib/core/plugin/node.rb
83 def usable_image?(image)
84   true
85 end
user() click to toggle source
    # File lib/core/plugin/node.rb
170 def user
171   myself[:user]
172 end
user=(user) click to toggle source
    # File lib/core/plugin/node.rb
166 def user=user
167   myself[:user] = user
168 end