Class: Fhcap::Tasks::Chef::ChefTaskBase
- Inherits:
-
TaskBase
- Object
- TaskBase
- Fhcap::Tasks::Chef::ChefTaskBase
show all
- Defined in:
- lib/fhcap/tasks/chef/chef_task_base.rb
Direct Known Subclasses
Fhcap::Tasks::Chef::Cookbook::List, Fhcap::Tasks::Chef::Cookbook::ListArtifacts, Fhcap::Tasks::Chef::Cookbook::UpdateArtifact, Fhcap::Tasks::Chef::Cookbook::UpdateChangelog, Fhcap::Tasks::Chef::Cookbook::UpdateConfig, Fhcap::Tasks::Chef::Cookbook::UpdateMetadata, Fhcap::Tasks::Chef::Cookbook::UpdateReadme, Fhcap::Tasks::Chef::Cookbook::UpdateVersion, Environments::Create, Environments::Destroy, Environments::PromoteCookbooks, Provisioning::ChefProvisioningTaskBase, Server::Bootstrap, Server::CreateUser, Server::Info, Server::Provision
Instance Attribute Summary
Attributes inherited from TaskBase
#config, #options, #thor, #verbose
Instance Method Summary
collapse
Methods inherited from TaskBase
#ask_config, #color_pad, #exit_with_error, #initialize, #set_color, #table_header, #table_row, #with_progress
#chef_server_config_for, #chef_server_config_hash_for, #chef_server_names, #knife_config
#provider_availability_zones, #provider_config, #provider_credentials, #provider_for, #provider_names, #provider_names_for, #provider_regions, #provider_type, #providers_config
#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
#cluster_template_names
Instance Method Details
#chef_server_environments ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 37
def chef_server_environments
unless @chef_server_environments
@chef_server_environments = {}
config[:knife].each do |name, cfg|
cmd = "environment list -F json"
resp = run_knife_cmd(cmd, name)
envs = JSON.parse(resp)
@chef_server_environments[name] = envs
end
end
@chef_server_environments
end
|
#chef_servers_for_environment(environment) ⇒ Object
31
32
33
34
35
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 31
def chef_servers_for_environment(environment)
chef_server_environments.collect do |server, environments|
server.to_s if environments.include? environment
end.compact
end
|
#knife_config_dir ⇒ Object
50
51
52
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 50
def knife_config_dir
config[:knife_dir] || File.join(config.default_dir, '.chef')
end
|
#knife_config_file_for(name) ⇒ Object
54
55
56
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 54
def knife_config_file_for(name)
File.join(knife_config_dir, "knife-#{name}.rb")
end
|
#run_chef_client_cmd(cmd, server, capture = false) ⇒ Object
72
73
74
75
76
77
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 72
def run_chef_client_cmd(cmd, server, capture=false)
cmd = "cd #{File.expand_path("..", knife_config_dir)} && chef-client -z #{cmd} -c #{knife_config_file_for(server)}"
resp = thor.run(cmd, :capture => capture)
thor.say resp if verbose && !capture
resp
end
|
#run_knife_cmd(cmd, server, capture = true) ⇒ Object
65
66
67
68
69
70
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 65
def run_knife_cmd(cmd, server, capture=true)
cmd = "cd #{File.expand_path("..", knife_config_dir)} && knife #{cmd} -c #{knife_config_file_for(server)}"
resp = thor.run(cmd, :capture => capture)
thor.say resp if verbose && !capture
resp
end
|
#run_knife_ssh_cmd(query, cmd, server, options) ⇒ Object
58
59
60
61
62
63
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 58
def run_knife_ssh_cmd(query, cmd, server, options)
user = options[:knife_user] || 'hadmin'
attr = options[:knife_attr] || 'cloud.public_ipv4'
knife_options = options[:knife_options]
run_knife_cmd("ssh \"#{query}\" \"#{cmd}\" -x #{user} -a #{attr} #{knife_options}", server, false)
end
|
#select_chef_server(environment, chef_server, fail_on_missing = true) ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/fhcap/tasks/chef/chef_task_base.rb', line 10
def select_chef_server(environment, chef_server, fail_on_missing=true)
$selected_chef_server = {} unless $selected_chef_server
unless $selected_chef_server[environment]
if chef_server
$selected_chef_server[environment] = chef_server
else
chef_servers = chef_servers_for_environment(environment)
if chef_servers.empty?
exit_with_error("Unable to locate chef server for #{environment}") if fail_on_missing
elsif chef_servers.length == 1
$selected_chef_server[environment] = chef_servers.first
else
server = thor.ask("Multiple servers configured for environment '#{environment}'. Which one should be used?", {:limited_to => chef_servers})
$selected_chef_server[environment] = server
end
end
end
$selected_chef_server[environment]
end
|