class Chef::Knife::EnvironmentSync

EnvironmentSync class

Public Instance Methods

compare(env, chef_env, local_env) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 45
def compare(env, chef_env, local_env)
  msg = "--> Checking #{env}".ljust(50)
  opts = config[:abbrev] ? {} : { array_path: true }
  diff = ::HashDiff.diff(chef_env, local_env, opts)
  diff.empty? ? show_success(msg) : show_error(msg, diff)
end
env_from_chef(env) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 63
def env_from_chef(env)
  Chef::Environment.load(env).to_hash
rescue Net::HTTPServerException
  ui.error("Environment '#{env}' could not be found on chef server")
end
env_from_file(env) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 69
def env_from_file(env)
  files = find_env_file(env.tr('-', '_'))
  if files.empty?
    ui.error("Environment '#{env}' could not be found on disk")
  elsif files.count > 1
    ui.error("Duplicate file environment for '#{env}'")
  else
    loader.object_from_file(files.first).to_hash
  end
end
find_env_file(env) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 52
def find_env_file(env)
  config_path = Chef::Config[:environment_path]
  env_paths = config_path.is_a?(String) ? [config_path] : config_path
  env_paths.map do |env_path|
    path_glob =
      File.join(Chef::Util::PathHelper.escape_glob_dir(env_path), '**')
    path_glob << "/#{env.gsub(/[_-]/, '{-,_}')}.{json,rb}"
    Dir.glob(path_glob)
  end.flatten
end
loader() click to toggle source
# File lib/chef/knife/environment_sync.rb, line 41
def loader
  @loader ||= Core::ObjectLoader.new(Chef::Environment, ui)
end
run() click to toggle source
# File lib/chef/knife/environment_sync.rb, line 80
def run
  envs = name_args.empty? ? Chef::Environment.list.keys : name_args
  envs.delete('_default')
  envs.each do |env|
    chef_env = env_from_chef(env)
    local_env = env_from_file(env)
    next if chef_env.nil? || local_env.nil?

    compare(env, chef_env, local_env)
  end
end
show_error(msg, diff) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 96
def show_error(msg, diff)
  ui.info(msg + ui.color('difference(s) found', :yellow))
  pp(diff) if config[:diff]
end
show_success(msg) click to toggle source
# File lib/chef/knife/environment_sync.rb, line 92
def show_success(msg)
  ui.info(msg + ui.color('properly synchronized.', :green))
end