Class: KnifeUploader::UploaderRunListCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/chef/knife/uploader_run_list.rb

Direct Known Subclasses

UploaderRunListDiff, UploaderRunListUpload

Instance Method Summary collapse

Methods inherited from BaseCommand

#debug, #diff, #diff_color, #get_chef_repo_path, #get_knife_config_path, #initialize, #locate_config_value, #parsed_knife_config, #report_errors, #ridley, #run

Constructor Details

This class inherits a constructor from KnifeUploader::BaseCommand

Instance Method Details

#filtered_chef_nodesObject



22
23
24
25
26
27
# File 'lib/chef/knife/uploader_run_list.rb', line 22

def filtered_chef_nodes
  ridley.node.all.sort_by {|node| node.name }.to_enum
                 .lazy_select {|node| node.name =~ @pattern }
                 .lazy_map {|node| ridley.node.find(node.name) }
                 .lazy_select {|node| node.chef_environment == @env_name }
end

#set_run_listsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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/chef/knife/uploader_run_list.rb', line 29

def set_run_lists
  run_lists_file_path = File::join(get_chef_repo_path, 'run_lists', "#{@env_name}.json")
  target_run_lists = File.open(run_lists_file_path) {|f| JSON.load(f) }

  filtered_chef_nodes.each do |node|

    debug("Comparing run lists for node #{node.name}")
    old_run_list = node.run_list
    new_run_list = []

    # Concatenate all matching patterns. This allows to specify some common parts of run lists
    # only once.
    target_run_lists.each do |pattern, run_list|
      if node.name =~ /\A#{pattern}\Z/
        new_run_list += run_list
      end
    end
    debug("New run list for node #{node.name}: #{new_run_list}")

    next if old_run_list == new_run_list

    unless new_run_list
      ui.warn("No new run list defined for node #{node.name}, skipping")
      next
    end

    ui.info("\n" + (@dry_run ? 'Would modify' : 'Modifying') +
            " the run list for node #{node.name}:\n" +
            diff_color(old_run_list.join("\n") + "\n",
                       new_run_list.join("\n") + "\n"))

    unless @dry_run
      node.run_list = new_run_list
      node.save
    end
  end

  ui.info("Finished #{@dry_run ? 'showing differences for' : 'setting'} run lists in " +
          "environment #{@env_name}")
end

#validate_argumentsObject



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

def validate_arguments
  if name_args.size != 1
    ui.fatal("Exactly one argument expected: environment")
    show_usage
    exit(1)
  end

  @env_name = name_args[0]
end