\FF\D8\FF\E0\00JFIF\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<F<2PFAFZUP_xxnnx\F5\AF\B9\91\C8\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\DB\00CUZZxix‚\EB\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\FF\C0\00\00b\00d\00\FF\C4\00\00\00\00\00\00\00\00\00\00\00\00\00\00\FF\C4\00\00\00\00\00\00\00\00\00\00\001A!Q\FF\C4\00\00\00\00\00\00\00\00\00\00\00\00\00\FF\C4\00\00\00\00\00\00\00\00\00\00!1AQ\FF\DA\00\00\00?\00\F6\00\00\00\00\00\00\00\00\00\00\A0\00\00\C5\CF\F8\E7Ó\FCjD~\B9^\AD\%\B3]\D8cs-\BBs,-\A0\00"\80\00%\B83rÏ^K~F\A4ea\00E\00\C6\EE=u\B1\9B\D1\00@\00r\BE8\F9:\FE5#.M\00\E7\CB\C9q\CBq\D6\F2\BE\B7\C7>\C9n5×\D6?\BDê\9Ds\EBp\9F[`8m\B7)o\B5\E8\E6I\99\FE3]]A2\BA\8Cw\D6E\93\\DEv\C8\009\F2\F1NI?uc\\F5\EA\96k\xN<~buv\EA\C8\D7	\8B\84\CEcxI\BBg\AE\9E=\D6+n\EC\80\C8A\8C\AE\EB\CF\D5\DA\E9"2\A4\B9j5\EB\F3W\B63\96\B30Yu\DA\FC8\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$\FEj\C4e\A9\DB\~\95\A7\A5\80EK\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<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>C///</title>
</head>
#--
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Author:: Tim Hinderliter (<tim@chef.io>)
# Copyright:: Copyright 2009-2016, Daniel DeLeo
# Copyright:: Copyright (c) Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require_relative "../recipe"
require_relative "../run_context"
require_relative "../config"
require_relative "../client"
require_relative "../cookbook/cookbook_collection"
require_relative "../cookbook_loader"
require_relative "../run_list/run_list_expansion"
require_relative "../formatters/base"
require_relative "../formatters/doc"
require_relative "../formatters/minimal"
require_relative "../dist"

module Shell
  class ShellSession
    include Singleton

    def self.session_type(type = nil)
      @session_type = type if type
      @session_type
    end

    attr_accessor :node, :compile, :recipe, :json_configuration
    attr_reader :node_attributes, :client

    def initialize
      @node_built = false
      formatter = Chef::Formatters.new(Chef::Config.formatter, STDOUT, STDERR)
      @events = Chef::EventDispatch::Dispatcher.new(formatter)
    end

    def node_built?
      !!@node_built
    end

    def reset!
      loading do
        rebuild_node
        @node = client.node
        shorten_node_inspect
        Shell::Extensions.extend_context_node(@node)
        rebuild_context
        node.consume_attributes(node_attributes) if node_attributes
        @recipe = Chef::Recipe.new(nil, nil, run_context)
        Shell::Extensions.extend_context_recipe(@recipe)
        @node_built = true
      end
    end

    def node_attributes=(attrs)
      @node_attributes = attrs
      @node.consume_attributes(@node_attributes)
    end

    def resource_collection
      run_context.resource_collection
    end

    attr_writer :run_context

    def run_context
      @run_context ||= rebuild_context
    end

    def definitions
      nil
    end

    def cookbook_loader
      nil
    end

    def save_node
      raise "Not Supported! #{self.class.name} doesn't support #save_node, maybe you need to run #{Chef::Dist::SHELL} in client mode?"
    end

    def rebuild_context
      raise "Not Implemented! :rebuild_collection should be implemented by subclasses"
    end

    private

    def loading
      show_loading_progress
      begin
        yield
      rescue => e
        loading_complete(false)
        raise e
      else
        loading_complete(true)
      end
    end

    def show_loading_progress
      print "Loading"
      @loading = true
      @dot_printer = Thread.new do
        while @loading
          print "."
          sleep 0.5
        end
      end
    end

    def loading_complete(success)
      @loading = false
      @dot_printer.join
      msg = success ? "done.\n\n" : "epic fail!\n\n"
      print msg
    end

    def shorten_node_inspect
      def @node.inspect # rubocop:disable Lint/NestedMethodDefinition
        "<Chef::Node:0x#{object_id.to_s(16)} @name=\"#{name}\">"
      end
    end

    def rebuild_node
      raise "Not Implemented! :rebuild_node should be implemented by subclasses"
    end

  end

  class StandAloneSession < ShellSession

    session_type :standalone

    def rebuild_context
      cookbook_collection = Chef::CookbookCollection.new({})
      @run_context = Chef::RunContext.new(@node, cookbook_collection, @events) # no recipes
      @run_context.load(Chef::RunList::RunListExpansionFromDisk.new("_default", [])) # empty recipe list
    end

    private

    def rebuild_node
      Chef::Config[:solo_legacy_mode] = true
      @client = Chef::Client.new(json_configuration, Chef::Config[:shell_config])
      @client.run_ohai
      @client.load_node
      @client.build_node
    end

  end

  class SoloLegacySession < ShellSession

    session_type :solo_legacy_mode

    def definitions
      @run_context.definitions
    end

    def rebuild_context
      @run_status = Chef::RunStatus.new(@node, @events)
      Chef::Cookbook::FileVendor.fetch_from_disk(Chef::Config[:cookbook_path])
      cl = Chef::CookbookLoader.new(Chef::Config[:cookbook_path])
      cl.load_cookbooks
      cookbook_collection = Chef::CookbookCollection.new(cl)
      @run_context = Chef::RunContext.new(node, cookbook_collection, @events)
      @run_context.load(Chef::RunList::RunListExpansionFromDisk.new("_default", []))
      @run_status.run_context = run_context
    end

    private

    def rebuild_node
      # Tell the client we're chef solo so it won't try to contact the server
      Chef::Config[:solo_legacy_mode] = true
      @client = Chef::Client.new(json_configuration, Chef::Config[:shell_config])
      @client.run_ohai
      @client.load_node
      @client.build_node
    end

  end

  class ClientSession < ShellSession

    session_type :client

    def definitions
      @run_context.definitions
    end

    def save_node
      @client.save_node
    end

    def rebuild_context
      @run_status = Chef::RunStatus.new(@node, @events)
      Chef::Cookbook::FileVendor.fetch_from_remote(Chef::ServerAPI.new(Chef::Config[:chef_server_url]))
      cookbook_hash = @client.sync_cookbooks
      cookbook_collection = Chef::CookbookCollection.new(cookbook_hash)
      @run_context = Chef::RunContext.new(node, cookbook_collection, @events)
      @run_context.load(@node.run_list.expand(@node.chef_environment))
      @run_status.run_context = run_context
    end

    private

    def rebuild_node
      # Make sure the client knows this is not chef solo
      Chef::Config[:solo_legacy_mode] = false
      @client = Chef::Client.new(json_configuration, Chef::Config[:shell_config])
      @client.run_ohai
      @client.register
      @client.load_node
      @client.build_node
    end

  end

  class SoloSession < ClientSession

    session_type :solo

  end

  class DoppelGangerClient < Chef::Client

    attr_reader :node_name

    def initialize(node_name)
      @node_name = node_name
      @ohai = Ohai::System.new
    end

    # Run the very smallest amount of ohai we can get away with and still
    # hope to have things work. Otherwise we're not very good doppelgangers
    def run_ohai
      @ohai.require_plugin("os")
    end

    # DoppelGanger implementation of build_node. preserves as many of the node's
    # attributes, and does not save updates to the server
    d