Class: Fhcap::Tasks::TaskBase

Inherits:
Object
  • Object
show all
Includes:
FhcapHelper, KnifeHelper, ProvidersHelper, ReposHelper
Defined in:
lib/fhcap/tasks/task_base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cluster_template_names

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)
  @options = options
  @config = options[:config]
  @thor = options[:thor]
  @verbose = options[:verbose]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/fhcap/tasks/task_base.rb', line 16

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/fhcap/tasks/task_base.rb', line 16

def options
  @options
end

#thorObject (readonly)

Returns the value of attribute thor.



16
17
18
# File 'lib/fhcap/tasks/task_base.rb', line 16

def thor
  @thor
end

#verboseObject (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] || options[:interactive]
      msg = cfg[:msg] || "#{key}:"
      options = cfg[:options] || {}
      options[:default] = config[key]
      if cfg[:boolean]
        config[key] = thor.yes? "#{msg} y/n?"
      else
        config[key] = thor.ask msg, options
      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)
  timestamp = Time.now.to_i
  err_file = File.join(Dir.tmpdir, "fhcap-cli-#{timestamp}.err")
  out_file = File.join(Dir.tmpdir, "fhcap-cli-#{timestamp}.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 = ProgressBar.create(title: title, total: nil)

    trap "INT" do
      progressbar.total = progressbar.progress + 1
      progressbar.clear
      exit
    end

    loop do
      progressbar.increment
      sleep 0.5
    end
  end

  Process.wait(long_running_task)
  Process.kill(2, progress)
end