Class: GoulahKnifePlugins::EnvironmentDiff

Inherits:
Chef::Knife
  • Object
show all
Defined in:
lib/chef/knife/env-diff.rb

Instance Method Summary collapse

Instance Method Details

#get_cookbook_version(data) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/chef/knife/env-diff.rb', line 74

def get_cookbook_version(data)
  if data['versions'].empty?
    'none'
  else
    data['versions'].first['version']
  end
end

#get_env_cookbooks(env) ⇒ Object



70
71
72
# File 'lib/chef/knife/env-diff.rb', line 70

def get_env_cookbooks(env)
  rest.get_rest("environments/#{env}/cookbooks")
end

#runObject



24
25
26
27
28
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/env-diff.rb', line 24

def run
  if name_args.size < 2
    ui.error("You need to supply at least two environments")
    show_usage
    exit 1
  end

  firstenv  = name_args.first
  otherenvs = name_args.slice(1, name_args.length - 1)

  from_env   = {}
  from_env[firstenv] = get_env_cookbooks(firstenv)

  to_env   = {}
  otherenvs.each do |env_name|
    to_env[env_name] = get_env_cookbooks(env_name)
  end

  ui.msg "diffing environment " + firstenv + " against " + otherenvs.join(', ') + "\n\n"

  from_env.each_value do |from_cookbooks|
    from_cookbooks.each do |from_cookbook, from_data|
      diff_versions = {}

      from_version = get_cookbook_version(from_data)

      to_env.each do |to_env, to_cookbooks|
        to_version = get_cookbook_version(to_cookbooks[from_cookbook])

        if from_version != to_version || from_version.nil?
          diff_versions[to_env] = to_version
        end
      end

      unless diff_versions.empty?
        ui.msg "cookbook: "+ from_cookbook
        ui.msg " #{firstenv} version: "+ (from_version || 'none')
        diff_versions.each do |env, version|
          ui.msg " #{env} version: "+ version
        end
        ui.msg "\n"
      end
    end
  end
end