Class: CORL::Plugin::Node
- Inherits:
-
Object
- Object
- CORL::Plugin::Node
- Includes:
- Parallel
- Defined in:
- lib/core/plugin/node.rb
Class Method Summary collapse
-
.build_info(namespace, plugin_type, data) ⇒ Object
—————————————————————————– Utilities.
-
.translate(data) ⇒ Object
—.
-
.translate_reference(reference) ⇒ Object
—.
Instance Method Summary collapse
-
#action(provider, options = {}) ⇒ Object
—.
- #add_agent(provider, settings) ⇒ Object
- #agent(provider) ⇒ Object
-
#agents ⇒ Object
—.
-
#attach_keys(keypair) ⇒ Object
—.
-
#bootstrap(local_path, options = {}) ⇒ Object
—.
- #bootstrap_script ⇒ Object
-
#bootstrap_script=(bootstrap) ⇒ Object
—.
-
#build(options = {}) ⇒ Object
—————————————————————————– Machine operations.
- #build_provider(provider, plugin, config) ⇒ Object
- #build_provisioner(name, plugin, provider, config) ⇒ Object
- #build_provisioners(provider, collection, config) ⇒ Object
- #build_time ⇒ Object
-
#build_time=(time) ⇒ Object
—.
-
#clear_remote_cache ⇒ Object
—.
-
#cli ⇒ Object
—.
-
#cli_capture(cli_command, *args) ⇒ Object
—————————————————————————– CLI utilities.
-
#cli_check(cli_command, *args) ⇒ Object
—.
-
#command(command, options = {}) ⇒ Object
—.
-
#create(options = {}) ⇒ Object
—.
-
#create_facts_post(data, names) ⇒ Object
—.
-
#create_image(options = {}) ⇒ Object
—.
-
#create_machine(name, provider, options = {}) ⇒ Object
—.
- #custom_facts ⇒ Object
-
#custom_facts=(facts) ⇒ Object
—.
-
#delete_facts_post(data, old_data) ⇒ Object
—.
-
#delete_keys ⇒ Object
—.
-
#delete_remote_cache_setting(keys) ⇒ Object
—.
-
#destroy(options = {}) ⇒ Object
—.
-
#download(remote_path, local_path, options = {}) ⇒ Object
—.
- #ensure_custom_facts(facts) ⇒ Object
-
#exec(options = {}) ⇒ Object
—.
-
#fact_var ⇒ Object
—————————————————————————– Machine information.
- #fact_var=(facts) ⇒ Object
-
#facts(reset = false, clone = true) ⇒ Object
—.
-
#filter_output(type, data) ⇒ Object
—.
-
#hiera_override_dir ⇒ Object
—.
-
#hiera_var ⇒ Object
—.
- #hiera_var=(hiera) ⇒ Object
-
#home(env_var = 'HOME', reset = false) ⇒ Object
—.
-
#hostname ⇒ Object
—.
-
#id(reset = false) ⇒ Object
—.
- #image(reset = false) ⇒ Object
-
#image=(image) ⇒ Object
—.
-
#image_id(image) ⇒ Object
—————————————————————————– Image utilities.
-
#image_search_text(image) ⇒ Object
—.
-
#images(search_terms = [], options = {}) ⇒ Object
—.
-
#keypair ⇒ Object
—.
- #keypair=(keypair) ⇒ Object
-
#local? ⇒ Boolean
—.
-
#local_machine ⇒ Object
—.
- #local_machine=(local_machine) ⇒ Object
-
#localize ⇒ Object
—.
-
#lookup(property, default = nil, options = {}) ⇒ Object
—.
-
#machine ⇒ Object
—.
- #machine=(machine) ⇒ Object
-
#machine_config {|config| ... } ⇒ Object
—————————————————————————– Settings groups.
- #machine_type(reset = false) ⇒ Object
-
#machine_type_id(machine_type) ⇒ Object
—————————————————————————– Machine type utilities.
-
#machine_types ⇒ Object
—.
-
#method_missing(method, *args, &code) ⇒ Object
—.
-
#normalize(reload) ⇒ Object
—————————————————————————– Node plugin interface.
- #private_ip(reset = false) ⇒ Object
- #private_key ⇒ Object
-
#private_key=(private_key) ⇒ Object
—.
- #profiles ⇒ Object
-
#profiles=(profiles) ⇒ Object
—.
-
#provisioner_info ⇒ Object
—.
-
#provisioners ⇒ Object
—.
-
#public_ip(reset = false) ⇒ Object
—.
- #public_key ⇒ Object
-
#public_key=(public_key) ⇒ Object
—.
-
#reload(options = {}) ⇒ Object
—.
-
#remote_cache_setting(keys, default = nil, format = false) ⇒ Object
—.
- #remove_agent(provider) ⇒ Object
- #remove_custom_facts(*facts) ⇒ Object
-
#remove_plugin ⇒ Object
—.
-
#render_image(image) ⇒ Object
—.
-
#render_machine_type(machine_type) ⇒ Object
—.
-
#run ⇒ Object
—.
-
#running? ⇒ Boolean
—————————————————————————– Checks.
-
#save(options = {}) ⇒ Object
—.
-
#send_files(local_path, remote_path, files = nil, permission = '0644', options = {}, &code) ⇒ Object
—.
-
#set_remote_cache_setting(keys, value) ⇒ Object
—.
-
#ssh_path(home_env_var = 'HOME', reset = false) ⇒ Object
—.
- #ssh_port ⇒ Object
-
#ssh_port=(ssh_port) ⇒ Object
—.
-
#start(options = {}) ⇒ Object
—.
-
#state(reset = false) ⇒ Object
—.
-
#stop(options = {}) ⇒ Object
—.
-
#terminal(options = {}) ⇒ Object
—.
-
#translate_reference(reference) ⇒ Object
—.
-
#upload(local_path, remote_path, options = {}) ⇒ Object
—.
-
#usable_image?(image) ⇒ Boolean
—.
- #user ⇒ Object
-
#user=(user) ⇒ Object
—.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &code) ⇒ Object
48 49 50 51 52 |
# File 'lib/core/plugin/node.rb', line 48 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(namespace, plugin_type, data) ⇒ Object
Utilities
1332 1333 1334 1335 |
# File 'lib/core/plugin/node.rb', line 1332 def self.build_info(namespace, plugin_type, data) data = data.split(/\s*,\s*/) if data.is_a?(String) super(namespace, plugin_type, data) end |
.translate(data) ⇒ Object
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
# File 'lib/core/plugin/node.rb', line 1339 def self.translate(data) = super(data) case data when String = { :name => data } when Hash = data end if .has_key?(:name) if matches = translate_reference([:name]) [:provider] = matches[:provider] [:name] = matches[:name] logger.debug("Translating node options: #{.inspect}") end end end |
.translate_reference(reference) ⇒ Object
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 |
# File 'lib/core/plugin/node.rb', line 1362 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
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 |
# File 'lib/core/plugin/node.rb', line 931 def action(provider, = {}) codes :network_load_error config = Config.ensure().defaults({ :net_remote => :edit, :net_provider => network.plugin_provider }) logger.info("Executing remote action #{provider} with encoded arguments: #{config.export.inspect}") provider = provider.to_s.gsub('_', ' ') action_agent = !!(provider =~ /^agent\s/) encoded_config = Util::CLI.encode(Util::Data.clean(config.export)) action_config = extended_config(:action, { :command => provider, :data => { :encoded => encoded_config } }) action_config[:data][:log_level] = Nucleon.log_level if Nucleon.log_level quiet = config.get(:quiet, false) parallel = config.get(:parallel, true) command_base = 'corl' command_base = "NUCLEON_NO_PARALLEL=1 #{command_base}" unless parallel command_base = "NUCLEON_NO_COLOR=1 #{command_base}" if action_agent result = command(command_base, { :subcommand => action_config, :as_admin => true, :quiet => quiet }) do |op, data| yield(op, data) if block_given? end # Update local network configuration so we capture any updates unless action_agent if result.status == code.success && ! network.load({ :remote => config[:net_remote], :pull => true }) result.status = code.network_load_error end end result end |
#add_agent(provider, settings) ⇒ Object
387 388 389 390 391 392 393 394 395 |
# File 'lib/core/plugin/node.rb', line 387 def add_agent(provider, settings) set_setting([ :agents, provider ], Config.new(settings).export) save({ :message => "Agent #{provider} starting up on #{plugin_provider} #{plugin_name}", :remote => extension_set(:add_agent_remote, :edit), :push => true }) end |
#agent(provider) ⇒ Object
383 384 385 |
# File 'lib/core/plugin/node.rb', line 383 def agent(provider) search([ :agents, provider ], {}, :hash) end |
#agents ⇒ Object
379 380 381 |
# File 'lib/core/plugin/node.rb', line 379 def agents hash(myself[:agents]) end |
#attach_keys(keypair) ⇒ Object
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 |
# File 'lib/core/plugin/node.rb', line 606 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.({ :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) myself.keypair = Util::SSH.generate({ :private_key => keypair.private_key }) myself.keypair.store(network.key_cache_directory, plugin_name) 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
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 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
# File 'lib/core/plugin/node.rb', line 1010 def bootstrap(local_path, = {}) config = Config.ensure() 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') bootstrap_scripts = config.get_array(:bootstrap_scripts) user_home = config[:home] auth_files = config.get_array(:auth_files) reboot = config.get(:reboot, true) dev_build = config.get(:dev_build, false) ruby_version = config.get(:ruby_version, nil) 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', { :quiet => true }) 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, :admin_command => 'sudo' }) 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', { :quiet => true }) 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) environment = "HOSTNAME='#{hostname}' " environment << "DEVELOPMENT_BUILD=1 " if dev_build environment << "RUBY_RVM_VERSION='#{ruby_version}' " if ruby_version script_names = bootstrap_scripts.empty? ? '' : bootstrap_scripts.join(' ') myself.bootstrap_script = remote_script result = command("#{environment} #{remote_script} #{script_names}", { :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 if reboot && ! 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 |
#bootstrap_script ⇒ Object
373 374 375 |
# File 'lib/core/plugin/node.rb', line 373 def bootstrap_script myself[:bootstrap] end |
#bootstrap_script=(bootstrap) ⇒ Object
369 370 371 |
# File 'lib/core/plugin/node.rb', line 369 def bootstrap_script=bootstrap myself[:bootstrap] = bootstrap end |
#build(options = {}) ⇒ Object
Machine operations
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/core/plugin/node.rb', line 551 def build( = {}) config = Config.ensure() success = true # TODO: Figure out what's going on with the parallel implementation here. parallel_original = ENV['NUCLEON_NO_PARALLEL'] ENV['NUCLEON_NO_PARALLEL'] = 'true' status = parallel(:build_provider, network.builders, config) success = false if status.values.include?(false) if success if array(config[:providers]).empty? || array(config[:providers]).include?("package") status = parallel(:build_provisioners, provisioners, config) success = false if status.values.include?(false) end if success myself.build_time = Time.now.to_s if success if config.delete(:save, true) success("Saving successful build", { :i18n => false }) success = save(extended_config(:build, { :message => config.get(:message, "Built #{plugin_provider} node: #{plugin_name}"), :remote => config.get(:remote, :edit) })) end end end ENV['NUCLEON_NO_PARALLEL'] = parallel_original success end |
#build_provider(provider, plugin, config) ⇒ Object
586 587 588 589 590 591 |
# File 'lib/core/plugin/node.rb', line 586 def build_provider(provider, plugin, config) if array(config[:providers]).empty? || array(config[:providers]).include?(provider.to_s) #info("Building #{provider} components", { :i18n => false }) plugin.build(myself, config) end end |
#build_provisioner(name, plugin, provider, config) ⇒ Object
599 600 601 602 |
# File 'lib/core/plugin/node.rb', line 599 def build_provisioner(name, plugin, provider, config) #info("Building #{provider} #{name} provisioner components", { :i18n => false }) plugin.build(myself, config) end |
#build_provisioners(provider, collection, config) ⇒ Object
593 594 595 596 597 |
# File 'lib/core/plugin/node.rb', line 593 def build_provisioners(provider, collection, config) #info("Building #{provider} provisioner collection", { :i18n => false }) status = parallel(:build_provisioner, collection, provider, config) status.values.include?(false) ? false : true end |
#build_time ⇒ Object
363 364 365 |
# File 'lib/core/plugin/node.rb', line 363 def build_time myself[:build] end |
#build_time=(time) ⇒ Object
359 360 361 |
# File 'lib/core/plugin/node.rb', line 359 def build_time=time myself[:build] = time end |
#clear_remote_cache ⇒ Object
544 545 546 |
# File 'lib/core/plugin/node.rb', line 544 def clear_remote_cache run.node_cache({ :clear => true }).status == code.success end |
#cli ⇒ Object
895 896 897 |
# File 'lib/core/plugin/node.rb', line 895 def cli @cli_interface end |
#cli_capture(cli_command, *args) ⇒ Object
CLI utilities
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 |
# File 'lib/core/plugin/node.rb', line 1390 def cli_capture(cli_command, *args) result = exec({ :commands => [ [ cli_command, args ].flatten.join(' ') ], :quiet => true }) result = result.first if result.status == code.success && ! result.output.empty? result.output else nil end end |
#cli_check(cli_command, *args) ⇒ Object
1403 1404 1405 1406 |
# File 'lib/core/plugin/node.rb', line 1403 def cli_check(cli_command, *args) result = cli.send(cli_command, args) result.status == code.success ? true : false end |
#command(command, options = {}) ⇒ Object
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 |
# File 'lib/core/plugin/node.rb', line 901 def command(command, = {}) config = Config.ensure() as_admin = config.delete(:as_admin, false) remove_command = false unless command.is_a?(CORL::Plugin::Command) command = CORL.command(Config.new({ :command => command }, {}, true, false).import(config), :bash) remove_command = true end admin_command = '' if as_admin admin_command = config.delete(:admin_command, 'sudo -i') admin_command = extension_set(:admin_command, admin_command, config) else config.delete(:admin_command) end config[:commands] = [ "#{admin_command} #{command.to_s}".strip ] results = exec(config) do |op, data| yield(op, data) if block_given? end CORL.remove_plugin(command) if remove_command results.first end |
#create(options = {}) ⇒ Object
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 |
# File 'lib/core/plugin/node.rb', line 680 def create( = {}) success = true if machine config = Config.ensure() 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_facts_post(data, names) ⇒ Object
459 460 461 462 463 464 465 466 467 |
# File 'lib/core/plugin/node.rb', line 459 def create_facts_post(data, names) fact_values = {} names.each do |name| name = name.to_sym fact_values[name] = data[name] end ensure_custom_facts(fact_values) data end |
#create_image(options = {}) ⇒ Object
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 |
# File 'lib/core/plugin/node.rb', line 1211 def create_image( = {}) success = true if machine && machine.running? config = Config.ensure() 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
674 675 676 |
# File 'lib/core/plugin/node.rb', line 674 def create_machine(name, provider, = {}) CORL.create_plugin(:CORL, :machine, provider, extended_config(name, ).import({ :meta => { :parent => myself }})) end |
#custom_facts ⇒ Object
304 305 306 |
# File 'lib/core/plugin/node.rb', line 304 def custom_facts search(:facts, {}, :hash) end |
#custom_facts=(facts) ⇒ Object
300 301 302 |
# File 'lib/core/plugin/node.rb', line 300 def custom_facts=facts myself[:facts] = hash(facts) end |
#delete_facts_post(data, old_data) ⇒ Object
471 472 473 474 |
# File 'lib/core/plugin/node.rb', line 471 def delete_facts_post(data, old_data) remove_custom_facts(*old_data.keys) data end |
#delete_keys ⇒ Object
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 |
# File 'lib/core/plugin/node.rb', line 642 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.(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 |
#delete_remote_cache_setting(keys) ⇒ Object
535 536 537 538 539 540 |
# File 'lib/core/plugin/node.rb', line 535 def delete_remote_cache_setting(keys) run.node_cache({ :name => keys.flatten.join('.'), :delete => true }).status == code.success end |
#destroy(options = {}) ⇒ Object
1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 |
# File 'lib/core/plugin/node.rb', line 1286 def destroy( = {}) config = Config.ensure() success = true if extension_check(:destroy, { :config => config }) logger.info("Destroying node: #{plugin_name}") yield(:config, config) if block_given? if machine && machine.created? # Shut down machine success = machine.destroy(config.export) myself.machine = nil else logger.warn("Node #{plugin_name} does not have an attached machine or is not created so cannot be destroyed") end # 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), :message => config.get(:message, "Destroying node #{plugin_name}"), :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 success end |
#download(remote_path, local_path, options = {}) ⇒ Object
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 |
# File 'lib/core/plugin/node.rb', line 711 def download(remote_path, local_path, = {}) success = false if machine && machine.running? config = Config.ensure() hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config }, {}, true, false) quiet = config.get(:quiet, false) if extension_check(:download, hook_config) logger.info("Downloading from #{plugin_name}") info("Starting download of #{remote_path} to #{local_path}", { :i18n => false }) unless quiet 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| info("#{name}: Sent #{received} of #{total}", { :i18n => false }) unless quiet yield(:progress, { :name => name, :received => received, :total => total }) end if success && block_given? info("Successfully finished download of #{remote_path} to #{local_path}", { :i18n => false }) unless quiet 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 |
#ensure_custom_facts(facts) ⇒ Object
308 309 310 311 312 |
# File 'lib/core/plugin/node.rb', line 308 def ensure_custom_facts(facts) hash(facts).each do |name, value| set_setting([ :facts, name ], value) end end |
#exec(options = {}) ⇒ Object
822 823 824 825 826 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 852 853 854 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 822 def exec( = {}) default_error = Util::Shell::Result.new(:error, 255) results = [ default_error ] if local? && local_machine || machine && machine.running? config = Config.ensure() 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 else active_machine = machine end if commands = config.get(:commands, nil) quiet = config.get(:quiet, false) begin test = active_machine.exec(Util::Data.array(commands), config.export) do |type, command, data| data = filter_output(type, data).rstrip unless quiet unless local? unless data.empty? if type == :error warn(data, { :i18n => false }) else info(data, { :i18n => false }) end end end yield(:progress, { :type => type, :command => command, :data => data }) if block_given? end data end results = test if test rescue => error default_error.append_errors(error.) 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 |
#fact_var ⇒ Object
Machine information
422 423 424 |
# File 'lib/core/plugin/node.rb', line 422 def fact_var @facts end |
#fact_var=(facts) ⇒ Object
426 427 428 |
# File 'lib/core/plugin/node.rb', line 426 def fact_var=facts @facts = facts end |
#facts(reset = false, clone = true) ⇒ Object
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/core/plugin/node.rb', line 432 def facts(reset = false, clone = true) if reset || fact_var.nil? default_facts = extended_config(:hiera_default_facts, { :fqdn => hostname, :hostname => hostname.gsub(/\..*$/, ''), :corl_provider => plugin_provider.to_s }) unless local? result = run.node_facts({ :quiet => true }) if result.status == code.success node_facts = Util::Data.symbol_map(Util::Data.parse_json(result.errors)) end end unless node_facts node_facts = local? ? Util::Data.merge([ CORL.facts, custom_facts ]) : custom_facts end self.fact_var = Config.new(node_facts, {}, true, false).defaults(default_facts).export end return fact_var.clone if clone fact_var end |
#filter_output(type, data) ⇒ Object
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 |
# File 'lib/core/plugin/node.rb', line 1410 def filter_output(type, data) if type == :output # Hide redundant Facter output if data =~ /^Already evaluated [a-z]+ at [^,]+, reevaluating anyways$/ data = '' end elsif type == :error # Hide Rubinius SAFE warning if data == 'WARNING: $SAFE is not supported on Rubinius.' data = '' end end data end |
#hiera_override_dir ⇒ Object
488 489 490 |
# File 'lib/core/plugin/node.rb', line 488 def hiera_override_dir network.hiera_override_dir end |
#hiera_var ⇒ Object
478 479 480 |
# File 'lib/core/plugin/node.rb', line 478 def hiera_var @hiera end |
#hiera_var=(hiera) ⇒ Object
482 483 484 |
# File 'lib/core/plugin/node.rb', line 482 def hiera_var=hiera @hiera = hiera end |
#home(env_var = 'HOME', reset = false) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/core/plugin/node.rb', line 189 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 |
#hostname ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/core/plugin/node.rb', line 144 def hostname hostname = setting(:hostname) unless hostname hostname = plugin_name end 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
125 126 127 128 |
# File 'lib/core/plugin/node.rb', line 125 def id(reset = false) myself[:id] = machine.plugin_name if machine && ( reset || setting(:id).nil? ) setting(:id) end |
#image(reset = false) ⇒ Object
293 294 295 296 |
# File 'lib/core/plugin/node.rb', line 293 def image(reset = false) myself[:image] = machine.image if machine && ( reset || myself[:image].nil? ) myself[:image] end |
#image=(image) ⇒ Object
289 290 291 |
# File 'lib/core/plugin/node.rb', line 289 def image=image myself[:image] = image end |
#image_id(image) ⇒ Object
Image utilities
1441 1442 1443 |
# File 'lib/core/plugin/node.rb', line 1441 def image_id(image) image.id end |
#image_search_text(image) ⇒ Object
1453 1454 1455 |
# File 'lib/core/plugin/node.rb', line 1453 def image_search_text(image) image.to_s end |
#images(search_terms = [], options = {}) ⇒ Object
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 |
# File 'lib/core/plugin/node.rb', line 250 def images(search_terms = [], = {}) config = Config.ensure() 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 |
#keypair ⇒ Object
95 96 97 |
# File 'lib/core/plugin/node.rb', line 95 def keypair @keypair end |
#keypair=(keypair) ⇒ Object
99 100 101 |
# File 'lib/core/plugin/node.rb', line 99 def keypair=keypair @keypair = keypair end |
#local? ⇒ Boolean
78 79 80 |
# File 'lib/core/plugin/node.rb', line 78 def local? @local_context ? true : false end |
#local_machine ⇒ Object
115 116 117 |
# File 'lib/core/plugin/node.rb', line 115 def local_machine @local_machine end |
#local_machine=(local_machine) ⇒ Object
119 120 121 |
# File 'lib/core/plugin/node.rb', line 119 def local_machine=local_machine @local_machine = local_machine end |
#localize ⇒ Object
56 57 58 59 |
# File 'lib/core/plugin/node.rb', line 56 def localize @local_context = true myself.local_machine = create_machine(:local_machine, :physical) end |
#lookup(property, default = nil, options = {}) ⇒ Object
494 495 496 497 498 499 500 501 502 503 504 505 506 507 |
# File 'lib/core/plugin/node.rb', line 494 def lookup(property, default = nil, = {}) unless local? config = Config.ensure().import({ :properties => [ property ], :quiet => true }) result = run.node_lookup(config) if result.status == code.success result_data = Util::Data.parse_json(result.errors) return Util::Data.value(result_data[property.to_s], default) end return default end [:hiera_scope] = Util::Data.prefix('::', facts, '') lookup_base(property, default, ) end |
#machine ⇒ Object
105 106 107 |
# File 'lib/core/plugin/node.rb', line 105 def machine @machine end |
#machine=(machine) ⇒ Object
109 110 111 |
# File 'lib/core/plugin/node.rb', line 109 def machine=machine @machine = machine end |
#machine_config {|config| ... } ⇒ Object
Settings groups
410 411 412 413 414 415 416 417 |
# File 'lib/core/plugin/node.rb', line 410 def machine_config name = setting(:id) name = nil if name.nil? || name.empty? config = Config.new({ :name => name }, {}, true, false) yield(config) if block_given? config end |
#machine_type(reset = false) ⇒ Object
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/core/plugin/node.rb', line 232 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
1428 1429 1430 |
# File 'lib/core/plugin/node.rb', line 1428 def machine_type_id(machine_type) machine_type.id end |
#machine_types ⇒ Object
228 229 230 |
# File 'lib/core/plugin/node.rb', line 228 def machine_types # Must be set at machine level (queried) machine.machine_types if machine end |
#normalize(reload) ⇒ Object
Node plugin interface
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 44 |
# File 'lib/core/plugin/node.rb', line 12 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 myself[:settings] = [ "all", plugin_provider.to_s, plugin_name.to_s ] | array(setting(:settings)) 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 warn(result.errors, { :i18n => false }) 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
137 138 139 140 |
# File 'lib/core/plugin/node.rb', line 137 def private_ip(reset = false) myself[:private_ip] = machine.private_ip if machine && ( reset || setting(:private_ip).nil? ) setting(:private_ip) end |
#private_key ⇒ Object
209 210 211 212 213 |
# File 'lib/core/plugin/node.rb', line 209 def private_key config_key = myself[:private_key] return File.(config_key, network.directory) if config_key nil end |
#private_key=(private_key) ⇒ Object
205 206 207 |
# File 'lib/core/plugin/node.rb', line 205 def private_key=private_key myself[:private_key] = private_key end |
#profiles ⇒ Object
326 327 328 |
# File 'lib/core/plugin/node.rb', line 326 def profiles [ array(myself[:profiles]), lookup_array(:profiles).reverse ].flatten end |
#profiles=(profiles) ⇒ Object
322 323 324 |
# File 'lib/core/plugin/node.rb', line 322 def profiles=profiles myself[:profiles] = array(profiles) end |
#provisioner_info ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/core/plugin/node.rb', line 332 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 |
#provisioners ⇒ Object
349 350 351 352 353 354 355 |
# File 'lib/core/plugin/node.rb', line 349 def provisioners provisioners = {} provisioner_info.each do |provider, node_profiles| provisioners[provider] = network.provisioners(provider) end provisioners end |
#public_ip(reset = false) ⇒ Object
132 133 134 135 |
# File 'lib/core/plugin/node.rb', line 132 def public_ip(reset = false) myself[:public_ip] = machine.public_ip if machine && ( reset || setting(:public_ip).nil? ) setting(:public_ip) end |
#public_key ⇒ Object
221 222 223 224 |
# File 'lib/core/plugin/node.rb', line 221 def public_key config_key = myself[:public_key] return File.(config_key, network.directory) if config_key end |
#public_key=(public_key) ⇒ Object
217 218 219 |
# File 'lib/core/plugin/node.rb', line 217 def public_key=public_key myself[:public_key] = public_key end |
#reload(options = {}) ⇒ Object
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 |
# File 'lib/core/plugin/node.rb', line 1182 def reload( = {}) success = true if machine && machine.created? config = Config.ensure() 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 |
#remote_cache_setting(keys, default = nil, format = false) ⇒ Object
511 512 513 514 515 516 517 518 519 520 521 |
# File 'lib/core/plugin/node.rb', line 511 def remote_cache_setting(keys, default = nil, format = false) cache_id = keys.flatten.join('.') result = run.node_cache({ :name => cache_id }) if result.status == code.success value = Util::Data.value(Util::Data.parse_json(result.errors), default) else value = default end filter(value, format) end |
#remove_agent(provider) ⇒ Object
397 398 399 400 401 402 403 404 405 |
# File 'lib/core/plugin/node.rb', line 397 def remove_agent(provider) delete_setting([ :agents, provider ]) save({ :message => "Agent #{provider} shutting down on #{plugin_provider} #{plugin_name}", :remote => extension_set(:remove_agent_remote, :edit), :push => true }) end |
#remove_custom_facts(*facts) ⇒ Object
314 315 316 317 318 |
# File 'lib/core/plugin/node.rb', line 314 def remove_custom_facts(*facts) facts.each do |name| delete_setting([ :facts, name ]) end end |
#remove_plugin ⇒ Object
63 64 65 66 |
# File 'lib/core/plugin/node.rb', line 63 def remove_plugin CORL.remove_plugin(local_machine) if local_machine CORL.remove_plugin(machine) if machine end |
#render_image(image) ⇒ Object
1447 1448 1449 |
# File 'lib/core/plugin/node.rb', line 1447 def render_image(image) '' end |
#render_machine_type(machine_type) ⇒ Object
1434 1435 1436 |
# File 'lib/core/plugin/node.rb', line 1434 def render_machine_type(machine_type) '' end |
#run ⇒ Object
972 973 974 |
# File 'lib/core/plugin/node.rb', line 972 def run @action_interface end |
#running? ⇒ Boolean
Checks
71 72 73 74 |
# File 'lib/core/plugin/node.rb', line 71 def running? return machine.running? if machine false end |
#save(options = {}) ⇒ Object
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 |
# File 'lib/core/plugin/node.rb', line 1111 def save( = {}) config = Config.ensure() # 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 remote = config.get(:remote, :edit) remote = nil unless network.remote(remote) network.save(config.import({ :commit => true, :allow_empty => true, :message => config.get(:message, "Saving #{plugin_provider} node #{plugin_name}"), :remote => remote })) end |
#send_files(local_path, remote_path, files = nil, permission = '0644', options = {}, &code) ⇒ Object
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 |
# File 'lib/core/plugin/node.rb', line 789 def send_files(local_path, remote_path, files = nil, = '0644', = {}, &code) local_path = File.(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, | code.call(op, ) if code end send_success = cli_check(:chmod, , 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 |
#set_remote_cache_setting(keys, value) ⇒ Object
525 526 527 528 529 530 531 |
# File 'lib/core/plugin/node.rb', line 525 def set_remote_cache_setting(keys, value) run.node_cache({ :name => keys.flatten.join('.'), :value => Util::Data.to_json(value, false), :json => true }).status == code.success end |
#ssh_path(home_env_var = 'HOME', reset = false) ⇒ Object
198 199 200 201 |
# File 'lib/core/plugin/node.rb', line 198 def ssh_path(home_env_var = 'HOME', reset = false) home = home(home_env_var, reset) home ? File.join(home, '.ssh') : nil end |
#ssh_port ⇒ Object
182 183 184 185 |
# File 'lib/core/plugin/node.rb', line 182 def ssh_port myself[:ssh_port] = 22 if myself[:ssh_port].nil? myself[:ssh_port] end |
#ssh_port=(ssh_port) ⇒ Object
177 178 179 180 |
# File 'lib/core/plugin/node.rb', line 177 def ssh_port=ssh_port myself[:ssh_port] = ssh_port machine.init_ssh(ssh_port) if machine end |
#start(options = {}) ⇒ Object
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 |
# File 'lib/core/plugin/node.rb', line 1142 def start( = {}) success = true if machine config = Config.ensure() 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 config.get(:bootstrap, false) && bootstrap_script 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
160 161 162 163 |
# File 'lib/core/plugin/node.rb', line 160 def state(reset = false) myself[:state] = machine.state if machine && ( reset || setting(:state).nil? ) setting(:state) end |
#stop(options = {}) ⇒ Object
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 |
# File 'lib/core/plugin/node.rb', line 1241 def stop( = {}) success = true if machine && machine.running? config = Config.ensure() 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
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 |
# File 'lib/core/plugin/node.rb', line 978 def terminal( = {}) myself.status = code.unknown_status if machine && machine.running? config = Config.ensure() if extension_check(:terminal, { :config => config }) logger.info("Launching terminal for node: #{plugin_name}") if local? && local_machine active_machine = local_machine else active_machine = machine end config[:private_keys] = private_key config[:port] = ssh_port 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
1383 1384 1385 |
# File 'lib/core/plugin/node.rb', line 1383 def translate_reference(reference) myself.class.translate_reference(reference) end |
#upload(local_path, remote_path, options = {}) ⇒ Object
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 |
# File 'lib/core/plugin/node.rb', line 750 def upload(local_path, remote_path, = {}) success = false if machine && machine.running? config = Config.ensure() hook_config = Config.new({ :local_path => local_path, :remote_path => remote_path, :config => config }, {}, true, false) quiet = config.get(:quiet, false) if extension_check(:upload, hook_config) logger.info("Uploading to #{plugin_name}") info("Starting upload of #{local_path} to #{remote_path}", { :i18n => false }) unless quiet 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| info("#{name}: Sent #{sent} of #{total}", { :i18n => false }) unless quiet yield(:progress, { :name => name, :sent => sent, :total => total }) end if success && block_given? info("Successfully finished upload of #{local_path} to #{remote_path}", { :i18n => false }) unless quiet 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
84 85 86 |
# File 'lib/core/plugin/node.rb', line 84 def usable_image?(image) true end |
#user ⇒ Object
171 172 173 |
# File 'lib/core/plugin/node.rb', line 171 def user myself[:user] end |
#user=(user) ⇒ Object
167 168 169 |
# File 'lib/core/plugin/node.rb', line 167 def user=user myself[:user] = user end |