Class: Fhcap::Tasks::TaskBase
- Inherits:
-
Object
- Object
- Fhcap::Tasks::TaskBase
- Includes:
- FhcapHelper, KnifeHelper, ProvidersHelper, ReposHelper
- Defined in:
- lib/fhcap/tasks/task_base.rb
Direct Known Subclasses
Chef::ChefTaskBase, Clean, Cluster::ClusterTaskBase, Cluster::List, Dns::Aws::CreateRecord, Dns::Aws::DeleteRecord, Dns::Aws::ListRecords, Dns::CreateRecord, Dns::DeleteRecord, Dns::ListRecords, Knife::Add, Knife::List, Knife::Remove, Misc::CreateSslCert, Misc::PlatformBump, Provider::Add, Provider::List, Provider::Remove, Repo::Add, Repo::Checkout, Repo::List, Repo::Remove, Setup
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#thor ⇒ Object
readonly
Returns the value of attribute thor.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #ask_config(required, config) ⇒ Object
- #color_pad(string) ⇒ Object
- #exit_with_error(msg) ⇒ Object
-
#initialize(options) ⇒ TaskBase
constructor
A new instance of TaskBase.
- #set_color(*args) ⇒ Object
- #suppress_stdout(out = true, err = false) ⇒ Object
- #table_header(*args) ⇒ Object
- #table_row(*args) ⇒ Object
- #with_progress(title, &block) ⇒ Object
Methods included from KnifeHelper
#chef_server_config_for, #chef_server_config_hash_for, #chef_server_names, #delete_chef_object, #knife_config, #knife_config_dir, #knife_config_file_for, #knife_data_bag_delete, #knife_download, #knife_environment_delete, #knife_upload, #local_chef_server?, #with_chef_server, #with_local_chef_server
Methods included from ProvidersHelper
#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config
Methods included from ReposHelper
#find_cluster, #find_cluster_pwds, #find_cluster_pwds_with_repo, #find_cluster_with_repo, #find_data_bag, #find_data_bag_item, #find_environment, #find_repo_item, #find_role, #get_cookbook_deps, #get_cookbook_meta, #get_cookbook_versions, #get_cookbooks, #get_dirty_cookbooks, #get_entry_dependencies, #get_modified_cookbooks, #git_diff, #is_dirty?, #modified?, #repo_cfg, #repo_clusters_dir, #repo_cookbook_paths, #repo_dir, #repo_names, #repo_paths, #repos_config, #repos_dir, #run_inside_repo_dir
Methods included from FhcapHelper
Constructor Details
#initialize(options) ⇒ TaskBase
Returns a new instance of TaskBase.
18 19 20 21 22 23 |
# File 'lib/fhcap/tasks/task_base.rb', line 18 def initialize() @options = @config = [:config] @thor = [:thor] @verbose = [:verbose] end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/fhcap/tasks/task_base.rb', line 16 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/fhcap/tasks/task_base.rb', line 16 def @options end |
#thor ⇒ Object (readonly)
Returns the value of attribute thor.
16 17 18 |
# File 'lib/fhcap/tasks/task_base.rb', line 16 def thor @thor end |
#verbose ⇒ Object (readonly)
Returns the value of attribute verbose.
16 17 18 |
# File 'lib/fhcap/tasks/task_base.rb', line 16 def verbose @verbose end |
Instance Method Details
#ask_config(required, config) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/fhcap/tasks/task_base.rb', line 25 def ask_config(required, config) required.each do |key, cfg| if !config[key] || [:interactive] msg = cfg[:msg] || "#{key}:" = cfg[:options] || {} [:default] = config[key] if cfg[:boolean] config[key] = thor.yes? "#{msg} y/n?" else config[key] = thor.ask msg, end end end end |
#color_pad(string) ⇒ Object
104 105 106 |
# File 'lib/fhcap/tasks/task_base.rb', line 104 def color_pad(string) string + set_color("", :white) end |
#exit_with_error(msg) ⇒ Object
40 41 42 43 |
# File 'lib/fhcap/tasks/task_base.rb', line 40 def exit_with_error(msg) thor.say_status 'error', msg, :red exit(-1) end |
#set_color(*args) ⇒ Object
108 109 110 |
# File 'lib/fhcap/tasks/task_base.rb', line 108 def set_color(*args) thor.shell.set_color(*args) end |
#suppress_stdout(out = true, err = false) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/fhcap/tasks/task_base.rb', line 45 def suppress_stdout(out=true, err=false) = Time.now.to_i err_file = File.join(Dir.tmpdir, "fhcap-cli-#{}.err") out_file = File.join(Dir.tmpdir, "fhcap-cli-#{}.out") begin orig_stderr = $stderr.clone orig_stdout = $stdout.clone $stderr.reopen File.new(err_file, 'w') if err $stdout.reopen File.new(out_file, 'w') if out retval = yield rescue Exception => e $stdout.reopen orig_stdout $stderr.reopen orig_stderr puts File.open(out_file, "r").read if File.exists?(out_file) && out puts File.open(err_file, "r").read if File.exists?(err_file) && err raise e ensure $stdout.reopen orig_stdout $stderr.reopen orig_stderr FileUtils.rm(out_file) if File.exists?(out_file) FileUtils.rm(err_file) if File.exists?(err_file) end retval end |
#table_header(*args) ⇒ Object
92 93 94 95 96 |
# File 'lib/fhcap/tasks/task_base.rb', line 92 def table_header(*args) args.collect do |name| set_color(name.to_s, :green) end end |
#table_row(*args) ⇒ Object
98 99 100 101 102 |
# File 'lib/fhcap/tasks/task_base.rb', line 98 def table_row(*args) args.collect do |name| color_pad(name.to_s) end end |
#with_progress(title, &block) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fhcap/tasks/task_base.rb', line 70 def with_progress(title, &block) long_running_task = fork(&block) progress = fork do = ProgressBar.create(title: title, total: nil) trap "INT" do .total = .progress + 1 .clear exit end loop do .increment sleep 0.5 end end Process.wait(long_running_task) Process.kill(2, progress) end |