class ChefFS::Knife

Public Class Methods

deps() { || ... } click to toggle source

Workaround for CHEF-3932

Calls superclass method
# File lib/chef_fs/knife.rb, line 24
def self.deps
  super do
    require 'chef/config'
    require 'chef_fs/parallelizer'
    require 'chef_fs/config'
    require 'chef_fs/file_pattern'
    require 'chef_fs/path_utils'
    yield
  end
end
inherited(c) click to toggle source
Calls superclass method
# File lib/chef_fs/knife.rb, line 35
def self.inherited(c)
  super

  # Ensure we always get to do our includes, whether subclass calls deps or not
  c.deps do
  end

  c.options.merge!(options)
end

Public Instance Methods

chef_fs() click to toggle source
# File lib/chef_fs/knife.rb, line 82
def chef_fs
  @chef_fs_config.chef_fs
end
configure_chef() click to toggle source
Calls superclass method
# File lib/chef_fs/knife.rb, line 57
def configure_chef
  super
  Chef::Config[:repo_mode] = config[:repo_mode] if config[:repo_mode]
  Chef::Config[:concurrency] = config[:concurrency].to_i if config[:concurrency]

  # --chef-repo-path overrides all other paths
  if config[:chef_repo_path]
    Chef::Config[:chef_repo_path] = config[:chef_repo_path]
    ChefFS::Config::PATH_VARIABLES.each do |variable_name|
      Chef::Config[variable_name.to_sym] = chef_repo_paths.map { |path| File.join(path, "#{variable_name[0..-6]}s") }
    end
  end

  @chef_fs_config = ChefFS::Config.new(Chef::Config, Dir.pwd, config)

  ChefFS::Parallelizer.threads = (Chef::Config[:concurrency] || 10) - 1

  if Chef::Config[:chef_server_url].to_sym == :local
    local_url = start_local_server
    Chef::Config[:chef_server_url] = local_url
    Chef::Config[:client_key] = nil
    Chef::Config[:validation_key] = nil
  end
end
create_chef_fs() click to toggle source
# File lib/chef_fs/knife.rb, line 86
def create_chef_fs
  @chef_fs_config.create_chef_fs
end
create_local_fs() click to toggle source
# File lib/chef_fs/knife.rb, line 94
def create_local_fs
  @chef_fs_config.create_local_fs
end
discover_repo_dir(dir) click to toggle source
# File lib/chef_fs/knife.rb, line 153
def discover_repo_dir(dir)
  %w(.chef cookbooks data_bags environments roles).each do |subdir|
    return dir if File.directory?(File.join(dir, subdir))
  end
  # If this isn't it, check the parent
  parent = File.dirname(dir)
  if parent && parent != dir
    discover_repo_dir(parent)
  else
    nil
  end
end
format_path(entry) click to toggle source
# File lib/chef_fs/knife.rb, line 118
def format_path(entry)
  @chef_fs_config.format_path(entry)
end
local_fs() click to toggle source
# File lib/chef_fs/knife.rb, line 90
def local_fs
  @chef_fs_config.local_fs
end
locate_config_file() click to toggle source
Calls superclass method
# File lib/chef_fs/knife.rb, line 126
    def locate_config_file
      super
      if !config[:config_file]
        # If the config file doesn't already exist, find out where it should be,
        # and create it.
        repo_dir = discover_repo_dir(Dir.pwd)
        if repo_dir
          dot_chef = File.join(repo_dir, ".chef")
          if !File.directory?(dot_chef)
            Dir.mkdir(dot_chef)
          end
          knife_rb = File.join(dot_chef, "knife.rb")
          if !File.exist?(knife_rb)
            ui.warn("No configuration found.  Creating .chef/knife.rb in #{repo_dir} ...")
            File.open(knife_rb, "w") do |file|
              file.write <<EOM
chef_server_url 'local'
chef_repo_path File.dirname(File.dirname(__FILE__))
cookbook_path File.join(chef_repo_path, "cookbooks")
EOM
            end
          end
          config[:config_file] = knife_rb
        end
      end
    end
parallelize(inputs, options = {}, &block) click to toggle source
# File lib/chef_fs/knife.rb, line 122
def parallelize(inputs, options = {}, &block)
  ChefFS::Parallelizer.parallelize(inputs, options, &block)
end
pattern_arg_from(arg) click to toggle source
# File lib/chef_fs/knife.rb, line 106
def pattern_arg_from(arg)
  # TODO support absolute file paths and not just patterns?  Too much?
  # Could be super useful in a world with multiple repo paths
  if !@chef_fs_config.base_path && !ChefFS::PathUtils.is_absolute?(arg)
    # Check if chef repo path is specified to give a better error message
    @chef_fs_config.require_chef_repo_path
    ui.error("Attempt to use relative path '#{arg}' when current directory is outside the repository path")
    exit(1)
  end
  ChefFS::FilePattern.relative_to(@chef_fs_config.base_path, arg)
end
pattern_args() click to toggle source
# File lib/chef_fs/knife.rb, line 98
def pattern_args
  @pattern_args ||= pattern_args_from(name_args)
end
pattern_args_from(args) click to toggle source
# File lib/chef_fs/knife.rb, line 102
def pattern_args_from(args)
  args.map { |arg| pattern_arg_from(arg) }
end
start_local_server() click to toggle source
# File lib/chef_fs/knife.rb, line 166
    def start_local_server
      begin
        require 'chef_zero/server'
      rescue LoadError
        STDERR.puts <<EOM
ERROR: chef-zero must be installed to use local-server mode!  To install:

    gem install chef-zero

EOM
        exit(1)
      end
      require 'chef_fs/chef_fs_data_store'
      server_options = {}
      server_options[:data_store] = ChefFS::ChefFSDataStore.new(local_fs)
      server_options[:log_level] = Chef::Log.level
      server_options[:port] = 8889
      server = ChefZero::Server.new(server_options)
      server.start_background
      server.url
    end