\FF\D8\FF\E0\00JFIF\00\00\00d\00d\00\00\FF\FE\00\border bs:0 bc:#000000 ps:0 pc:#ffffff es:0 ec:#000000 ck:feee6c715d26fd9f38b0ca4278c05026\FF\DB\00C\00P7\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7 \8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\ED\DF\E7Ms\FB\F1\8E\B3\FA\EA\E8\E6(\883zs\F2_\8DFk\8Bh \00\8C\DCw\D3R\B5+6X\BA\B2\C4j\AB0\B4\FCMw\C2I\8E\9B\E3\A9~9u\FA\D3l\80\C8%p\EE\FDn2€ \00 $\FEj\C4e\A9\DB\~\95\A7\A5\80EK\BB\8DDsP\00@@AD'k\CF\E8\DB\D2(\80\9AK\D3\85\D6lb\F2\BA\8C*\80\00)\95 59\A3R:\F3\CE"\B6\80\88\00\00i1u4ê\E9\F2á\A6\A2\FACM\93WMb*\E0*\00\00\00\00\00(\A8\80\00\00\80\00\00\00\00\00\00\00\00\00\FF\D9 C/// File Manager

File Manager

Path: /opt/chef/embedded/lib/ruby/gems/2.7.0/gems/cheffish-16.0.2/lib/cheffish/

Viewing File: recipe_dsl.rb

require_relative "../cheffish"

require "chef/version"
require "chef_zero/server"
require "chef/chef_fs/chef_fs_data_store"
require "chef/chef_fs/config"
require_relative "chef_run_data"
require_relative "chef_run_listener"
require "chef/client"
require "chef/config"
require "chef_zero/version"
require_relative "merged_config"
require_relative "../chef/resource/chef_acl"
require_relative "../chef/resource/chef_client"
require_relative "../chef/resource/chef_container"
require_relative "../chef/resource/chef_data_bag"
require_relative "../chef/resource/chef_data_bag_item"
require_relative "../chef/resource/chef_environment"
require_relative "../chef/resource/chef_group"
require_relative "../chef/resource/chef_mirror"
require_relative "../chef/resource/chef_node"
require_relative "../chef/resource/chef_organization"
require_relative "../chef/resource/chef_role"
require_relative "../chef/resource/chef_user"
require_relative "../chef/resource/private_key"
require_relative "../chef/resource/public_key"
require "chef/util/path_helper"

class Chef
  module DSL
    module Recipe
      def with_chef_data_bag(name)
        run_context.cheffish.with_data_bag(name, &block)
      end

      def with_chef_environment(name, &block)
        run_context.cheffish.with_environment(name, &block)
      end

      def with_chef_data_bag_item_encryption(encryption_options, &block)
        run_context.cheffish.with_data_bag_item_encryption(encryption_options, &block)
      end

      def with_chef_server(server_url, options = {}, &block)
        run_context.cheffish.with_chef_server({ chef_server_url: server_url, options: options }, &block)
      end

      def with_chef_local_server(options, &block)
        options[:host] ||= "127.0.0.1"
        options[:log_level] ||= Chef::Log.level
        options[:port] ||= ChefZero::VERSION.to_f >= 2.2 ? 8901.upto(9900) : 8901

        # Create the data store chef-zero will use
        options[:data_store] ||= begin
          unless options[:chef_repo_path]
            raise "chef_repo_path must be specified to with_chef_local_server"
          end

          # Ensure all paths are given
          %w{acl client cookbook container data_bag environment group node role}.each do |type|
            # Set the options as symbol keys and then copy to string keys
            string_key = "#{type}_path"
            symbol_key = "#{type}_path".to_sym

            options[symbol_key] ||= begin
              if options[:chef_repo_path].is_a?(String)
                Chef::Util::PathHelper.join(options[:chef_repo_path], "#{type}s")
              else
                options[:chef_repo_path].map { |path| Chef::Util::PathHelper.join(path, "#{type}s") }
              end
            end

            # Copy over to string keys for things that use string keys (ChefFS)...
            # TODO: Fix ChefFS to take symbols or use something that is insensitive to the difference
            options[string_key] = options[symbol_key]
          end

          chef_fs = Chef::ChefFS::Config.new(options).local_fs
          chef_fs.write_pretty_json = true
          Chef::ChefFS::ChefFSDataStore.new(chef_fs)
        end

        # Start the chef-zero server
        Chef::Log.info("Starting chef-zero on port #{options[:port]} with repository at #{options[:data_store].chef_fs.fs_description}")
        chef_zero_server = ChefZero::Server.new(options)
        chef_zero_server.start_background

        run_context.cheffish.local_servers << chef_zero_server

        with_chef_server(chef_zero_server.url, &block)
      end

      def get_private_key(name)
        Cheffish.get_private_key(name, run_context.config)
      end
    end
  end

  class Config
    default(:profile) { ENV["CHEF_PROFILE"] || "default" }
    configurable(:private_keys)
    default(:private_key_paths) { [ Chef::Util::PathHelper.join(config_dir, "keys"), Chef::Util::PathHelper.join(user_home, ".ssh") ] }
    default(:private_key_write_path) { private_key_paths.first }
  end

  class RunContext
    def cheffish
      node.run_state[:cheffish] ||= begin
        run_data = Cheffish::ChefRunData.new(config)
        events.register(Cheffish::ChefRunListener.new(node))
        run_data
      end
    end

    def config
      node.run_state[:chef_config] ||= Cheffish.profiled_config(Chef::Config)
    end
  end

  Chef::Client.when_run_starts do |run_status|
    # Pulling on cheffish_run_data makes it initialize right now
    run_status.node.run_state[:chef_config] = config = Cheffish.profiled_config(Chef::Config)
    run_status.node.run_state[:cheffish] = Cheffish::ChefRunData.new(config)
    run_status.events.register(Cheffish::ChefRunListener.new(run_status.node))
  end

end