Class: Chef::Knife::Deps

Inherits:
ChefFS::Knife show all
Defined in:
lib/chef/knife/deps.rb

Constant Summary

Constants inherited from Chef::Knife

CHEF_ORGANIZATION_MANAGEMENT, OFFICIAL_PLUGINS, OPSCODE_HOSTED_CHEF_ACCESS_CONTROL

Instance Attribute Summary collapse

Attributes inherited from Chef::Knife

#name_args, #ui

Instance Method Summary collapse

Methods inherited from ChefFS::Knife

#chef_fs, #configure_chef, #create_chef_fs, #create_local_fs, deps, #discover_repo_dir, #format_path, inherited, #local_fs, #parallelize, #pattern_arg_from, #pattern_args, #pattern_args_from

Methods inherited from Chef::Knife

#api_key, #apply_computed_config, category, chef_config_dir, #cli_keys, common_name, #config_file_settings, config_loader, #config_source, #configure_chef, #create_object, #delete_object, dependency_loaders, deps, #format_rest_error, guess_category, #humanize_exception, #humanize_http_exception, inherited, #initialize, list_commands, load_commands, load_config, load_deps, #maybe_setup_fips, #merge_configs, msg, #noauth_rest, #parse_options, reset_config_loader!, reset_subcommands!, #rest, run, #run_with_pretty_exceptions, #server_url, #show_usage, snake_case_name, subcommand_category, subcommand_class_from, subcommand_files, subcommand_loader, subcommands, subcommands_by_category, #test_mandatory_field, ui, unnamed?, use_separate_defaults?, #username

Methods included from Mixin::ConvertToClassName

#constantize, #convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #normalize_snake_case_name, #snake_case_basename

Constructor Details

This class inherits a constructor from Chef::Knife

Instance Attribute Details

#exit_codeObject

Returns the value of attribute exit_code.



46
47
48
# File 'lib/chef/knife/deps.rb', line 46

def exit_code
  @exit_code
end

Instance Method Details

#dependencies_from_runlist(run_list) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/chef/knife/deps.rb', line 136

def dependencies_from_runlist(run_list)
  chef_run_list = Chef::RunList.new
  chef_run_list.reset!(run_list)
  chef_run_list.map do |run_list_item|
    case run_list_item.type
    when :role
      "/roles/#{run_list_item.name}.json"
    when :recipe
      if run_list_item.name =~ /(.+)::[^:]*/
        "/cookbooks/#{$1}"
      else
        "/cookbooks/#{run_list_item.name}"
      end
    else
      raise "Unknown run list item type #{run_list_item.type}"
    end
  end
end

#get_dependencies(entry) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/knife/deps.rb', line 92

def get_dependencies(entry)
  if entry.parent && entry.parent.path == "/cookbooks"
    entry.chef_object..dependencies.keys.map { |cookbook| "/cookbooks/#{cookbook}" }

  elsif entry.parent && entry.parent.path == "/nodes"
    node = Chef::JSONCompat.parse(entry.read)
    result = []
    if node["chef_environment"] && node["chef_environment"] != "_default"
      result << "/environments/#{node["chef_environment"]}.json"
    end
    if node["run_list"]
      result += dependencies_from_runlist(node["run_list"])
    end
    result

  elsif entry.parent && entry.parent.path == "/roles"
    role = Chef::JSONCompat.parse(entry.read)
    result = []
    if role["run_list"]
      dependencies_from_runlist(role["run_list"]).each do |dependency|
        result << dependency unless result.include?(dependency)
      end
    end
    if role["env_run_lists"]
      role["env_run_lists"].each_pair do |env, run_list|
        dependencies_from_runlist(run_list).each do |dependency|
          result << dependency unless result.include?(dependency)
        end
      end
    end
    result

  elsif !entry.exists?
    raise Chef::ChefFS::FileSystem::NotFoundError.new(entry)

  else
    []
  end
rescue Chef::ChefFS::FileSystem::NotFoundError => e
  ui.error "#{format_path(e.entry)}: No such file or directory"
  self.exit_code = 2
  []
end


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/chef/knife/deps.rb', line 80

def print_dependencies_tree(entry, dependencies, printed = {}, depth = 0)
  dependencies[entry.path] = get_dependencies(entry) unless dependencies[entry.path]
  output "#{"  " * depth}#{format_path(entry)}"
  if !printed[entry.path] && (config[:recurse] || depth == 0)
    printed[entry.path] = true
    dependencies[entry.path].each do |child|
      child_entry = Chef::ChefFS::FileSystem.resolve_path(@root, child)
      print_dependencies_tree(child_entry, dependencies, printed, depth + 1)
    end
  end
end


69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/deps.rb', line 69

def print_flattened_dependencies(entry, dependencies)
  unless dependencies[entry.path]
    dependencies[entry.path] = get_dependencies(entry)
    dependencies[entry.path].each do |child|
      child_entry = Chef::ChefFS::FileSystem.resolve_path(@root, child)
      print_flattened_dependencies(child_entry, dependencies)
    end
    output format_path(entry)
  end
end

#runObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/knife/deps.rb', line 48

def run
  if config[:recurse] == false && !config[:tree]
    ui.error "--no-recurse requires --tree"
    exit(1)
  end
  config[:recurse] = true if config[:recurse].nil?

  @root = config[:remote] ? chef_fs : local_fs
  dependencies = {}
  pattern_args.each do |pattern|
    Chef::ChefFS::FileSystem.list(@root, pattern).each do |entry|
      if config[:tree]
        print_dependencies_tree(entry, dependencies)
      else
        print_flattened_dependencies(entry, dependencies)
      end
    end
  end
  exit exit_code if exit_code
end