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, #knife_config, #knife_config_dir, #knife_config_file_for

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_entry_dependencies, #get_modified_cookbooks, #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

#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
  @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] || 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



79
80
81
# File 'lib/fhcap/tasks/task_base.rb', line 79

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



83
84
85
# File 'lib/fhcap/tasks/task_base.rb', line 83

def set_color(*args)
  thor.shell.set_color(*args)
end

#table_header(*args) ⇒ Object



67
68
69
70
71
# File 'lib/fhcap/tasks/task_base.rb', line 67

def table_header(*args)
  args.collect do |name|
    set_color(name.to_s, :green)
  end
end

#table_row(*args) ⇒ Object



73
74
75
76
77
# File 'lib/fhcap/tasks/task_base.rb', line 73

def table_row(*args)
  args.collect do |name|
    color_pad(name.to_s)
  end
end

#with_progress(title, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fhcap/tasks/task_base.rb', line 45

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