class KnifeUploader::BaseCommand

Public Class Methods

new(args) click to toggle source
Calls superclass method
# File lib/chef/knife/uploader_base.rb, line 127
def initialize(args)
  super
  @pattern = locate_config_value(:pattern)
  if @pattern
    @pattern = Regexp.new(@pattern)
  else
    @pattern = //  # matches anything
  end
end

Public Instance Methods

debug(msg) click to toggle source
# File lib/chef/knife/uploader_base.rb, line 145
def debug(msg)
  if locate_config_value(:debug)
    ui.info("DEBUG: #{msg}")
  end
end
diff(a, b) click to toggle source
# File lib/chef/knife/uploader_base.rb, line 137
def diff(a, b)
  ::Diffy::Diff.new(a, b, :context => 2)
end
diff_color(a, b) click to toggle source
# File lib/chef/knife/uploader_base.rb, line 141
def diff_color(a, b)
  diff(a, b).to_s(ui.color? ? :color : :text)
end
get_chef_repo_path() click to toggle source
# File lib/chef/knife/uploader_base.rb, line 173
def get_chef_repo_path
  unless @chef_repo_path
    path_list = parsed_knife_config.get_cookbook_path_list
    path_list.each do |cookbooks_path|
      [cookbooks_path, File.expand_path('..', cookbooks_path)].each do |path|
        if ['.git', 'data_bags', 'environments', 'roles'].map do |subdir_name|
          File.directory?(File.join(path, subdir_name))
        end.all?
          @chef_repo_path = path
        end
      end
    end

    raise "No chef repository checkout path could be determined using " +
          "cookbook paths #{path_list}" unless @chef_repo_path

    debug("Identified Chef repo path: #{@chef_repo_path}")
  end

  @chef_repo_path
end
get_knife_config_path() click to toggle source
# File lib/chef/knife/uploader_base.rb, line 161
def get_knife_config_path
  locate_config_value(:config_file, :required)
end
locate_config_value(key, kind = :optional) click to toggle source
# File lib/chef/knife/uploader_base.rb, line 151
def locate_config_value(key, kind = :optional)
  raise unless [:required, :optional].include?(kind)
  key = key.to_sym
  value = config[key] || Chef::Config[:knife][key]
  if kind == :required && value.nil?
    raise "#{key} not specified"
  end
  value
end
parsed_knife_config() click to toggle source
# File lib/chef/knife/uploader_base.rb, line 165
def parsed_knife_config
  unless @parsed_knife_config
    @parsed_knife_config = KnifeConfigParser.new(get_knife_config_path)
  end

  @parsed_knife_config
end
report_errors() { || ... } click to toggle source
# File lib/chef/knife/uploader_base.rb, line 226
def report_errors(&block)
  begin
    yield
  rescue => exception
    ui.fatal("#{exception}: #{exception.backtrace.join("\n")}")
    raise exception
  end
end
ridley() click to toggle source
# File lib/chef/knife/uploader_base.rb, line 195
def ridley
  unless @ridley
    knife_conf_path = get_knife_config_path

    # Check file existence (Ridley will throw a confusing error).
    raise "File #{knife_conf_path} does not exist" unless File.file?(knife_conf_path)

    @ridley = Ridley.from_chef_config(knife_conf_path, :ssl => { :verify => false })
    data_bag_secret_file_path = @ridley.options[:encrypted_data_bag_secret]
    unless data_bag_secret_file_path
      raise "No encrypted data bag secret location specified in #{knife_conf_path}"
    end

    unless File.file?(data_bag_secret_file_path)
      raise "File #{data_bag_secret_file_path} does not exist"
    end

    # The encrypted data bag secret has to be the value, even though the readme in Ridley 1.5.2
    # says it can also be a file name, so we have to re-create the Ridley object.
    @ridley = Ridley.new(
      server_url: @ridley.server_url,
      client_name: @ridley.client_name,
      client_key: @ridley.client_key,
      encrypted_data_bag_secret: IO.read(data_bag_secret_file_path),
      ssl: { verify: false }
    )
  end

  @ridley
end
run() click to toggle source
# File lib/chef/knife/uploader_base.rb, line 235
def run
  begin
    run_internal
  ensure
    # Cleanup code can be added here.
  end
end