summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Duffield <tom@chef.io>2017-03-17 11:47:27 -0500
committerGitHub <noreply@github.com>2017-03-17 11:47:27 -0500
commit8855f47e49d2926bc2821025b75c57dadd0eb977 (patch)
treeee625bfc3892eee69f3fde031baabf9ff8aad34a
parent44363a16a0e876b0466fb3d6f4927a39db39fc10 (diff)
parent4b9fd225c8cc2fbc52e8965285f50d578c6d51ba (diff)
downloadchef-8855f47e49d2926bc2821025b75c57dadd0eb977.tar.gz
Merge pull request #5898 from chef/afiune/COOL-336/chef-shell-in-zero-mode
Properly use chef-shell in SoloSession by deprecating old behavior into SoloLegacySession
-rw-r--r--.gitignore1
-rw-r--r--RELEASE_NOTES.md5
-rw-r--r--lib/chef/shell.rb31
-rw-r--r--lib/chef/shell/shell_session.rb16
-rw-r--r--spec/data/shef-config.rb2
-rw-r--r--spec/functional/shell_spec.rb18
-rw-r--r--spec/unit/shell/shell_session_spec.rb35
7 files changed, 99 insertions, 9 deletions
diff --git a/.gitignore b/.gitignore
index 03d53ceb10..aaf9fa045e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -43,6 +43,7 @@ kitchen-tests/nodes/*
# Temporary files present during spec runs
spec/data/test-dir
+spec/data/nodes
/config/
# acceptance binstubs
diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md
index 6ebd586b7b..88b4d64144 100644
--- a/RELEASE_NOTES.md
+++ b/RELEASE_NOTES.md
@@ -100,3 +100,8 @@ confusion. A simple example is `property :hash` which overrides the Object#hash
is placed into the Chef::ResourceCollection which uses a Hash internally which expects to call Object#hash to get a unique id for the
object. Attempting to create `property :action` would also override the Chef::Resource#action method which is unlikely to end well for
the user. Overriding inherited properties is still supported.
+
+### `chef-shell` now supports solo and legacy solo modes
+
+Running `chef-shell -s` or `chef-shell --solo` will give you an experience consistent with `chef-solo`. `chef-shell --solo-legacy-mode`
+will give you an experience consistent with `chef-solo --legacy-mode`.
diff --git a/lib/chef/shell.rb b/lib/chef/shell.rb
index e8c3704553..6d6d1437e2 100644
--- a/lib/chef/shell.rb
+++ b/lib/chef/shell.rb
@@ -27,6 +27,7 @@ require "chef/config"
require "chef/config_fetcher"
require "chef/shell/shell_session"
+require "chef/workstation_config_loader"
require "chef/shell/ext"
require "chef/json_compat"
require "chef/util/path_helper"
@@ -62,6 +63,12 @@ module Shell
irb = IRB::Irb.new
+ if solo_mode?
+ # Setup the mocked ChefServer
+ Chef::Config.local_mode = true
+ Chef::LocalMode.setup_server_connectivity
+ end
+
init(irb.context.main)
irb_conf[:IRB_RC].call(irb.context) if irb_conf[:IRB_RC]
@@ -74,6 +81,13 @@ module Shell
catch(:IRB_EXIT) do
irb.eval_input
end
+ ensure
+ # We destroy the mocked ChefServer
+ Chef::LocalMode.destroy_server_connectivity if solo_mode?
+ end
+
+ def self.solo_mode?
+ Chef::Config[:solo]
end
def self.setup_logger
@@ -167,8 +181,9 @@ module Shell
def self.client_type
type = Shell::StandAloneSession
- type = Shell::SoloSession if Chef::Config[:shell_solo]
- type = Shell::ClientSession if Chef::Config[:client]
+ type = Shell::SoloSession if solo_mode?
+ type = Shell::SoloLegacySession if Chef::Config[:solo_legacy_shell]
+ type = Shell::ClientSession if Chef::Config[:client]
type = Shell::DoppelGangerSession if Chef::Config[:doppelganger]
type
end
@@ -226,7 +241,7 @@ FOOTER
:default => true,
:boolean => true
- option :shell_solo,
+ option :solo_shell,
:short => "-s",
:long => "--solo",
:description => "chef-solo session",
@@ -239,6 +254,12 @@ FOOTER
:description => "chef-client session",
:boolean => true
+ option :solo_legacy_shell,
+ :long => "--solo-legacy-mode",
+ :description => "chef-solo legacy session",
+ :boolean => true,
+ :proc => proc { Chef::Config[:solo_legacy_mode] = true }
+
option :json_attribs,
:short => "-j JSON_ATTRIBS",
:long => "--json-attributes JSON_ATTRIBS",
@@ -313,10 +334,12 @@ FOOTER
config_file_to_try
elsif dot_chef_dir && ::File.exist?(File.join(dot_chef_dir, "chef_shell.rb"))
File.join(dot_chef_dir, "chef_shell.rb")
- elsif config[:solo]
+ elsif config[:solo_legacy_shell]
Chef::Config.platform_specific_path("/etc/chef/solo.rb")
elsif config[:client]
Chef::Config.platform_specific_path("/etc/chef/client.rb")
+ elsif config[:solo]
+ Chef::WorkstationConfigLoader.new(nil, Chef::Log).config_location
else
nil
end
diff --git a/lib/chef/shell/shell_session.rb b/lib/chef/shell/shell_session.rb
index 3034e1cf5d..41d5bd64a0 100644
--- a/lib/chef/shell/shell_session.rb
+++ b/lib/chef/shell/shell_session.rb
@@ -159,9 +159,9 @@ module Shell
end
- class SoloSession < ShellSession
+ class SoloLegacySession < ShellSession
- session_type :solo
+ session_type :solo_legacy_mode
def definitions
@run_context.definitions
@@ -191,10 +191,14 @@ module Shell
end
- class ClientSession < SoloSession
+ class ClientSession < ShellSession
session_type :client
+ def definitions
+ @run_context.definitions
+ end
+
def save_node
@client.save_node
end
@@ -223,6 +227,12 @@ module Shell
end
+ class SoloSession < ClientSession
+
+ session_type :solo
+
+ end
+
class DoppelGangerClient < Chef::Client
attr_reader :node_name
diff --git a/spec/data/shef-config.rb b/spec/data/shef-config.rb
index 02d3610e55..efd601d6d8 100644
--- a/spec/data/shef-config.rb
+++ b/spec/data/shef-config.rb
@@ -7,3 +7,5 @@ ohai[:disabled_plugins] << "solaris2::cpu" << "solaris2::dmi" << "solaris2::file
ohai[:disabled_plugins] << "solaris2::virtualization" << "solaris2::zpools"
ohai[:disabled_plugins] << "c" << "php" << "mono" << "groovy" << "lua" << "erlang"
ohai[:disabled_plugins] << "kernel" << "linux::filesystem" << "ruby"
+chef_repo_path File.dirname(__FILE__)
+cookbook_path "#{chef_repo_path}/cookbooks"
diff --git a/spec/functional/shell_spec.rb b/spec/functional/shell_spec.rb
index 636162fb16..8c8d7ba482 100644
--- a/spec/functional/shell_spec.rb
+++ b/spec/functional/shell_spec.rb
@@ -135,6 +135,24 @@ describe Shell do
expect(exitstatus).to eq(0)
end
+ context "on solo mode" do
+ it "starts correctly" do
+ output, exitstatus = run_chef_shell_with("--solo")
+ expect(output).to include("done")
+ expect(exitstatus).to eq(0)
+ end
+
+ it "should be able to use the API" do
+ output, exitstatus = run_chef_shell_with("-s") do |out, keyboard|
+ simple_api_get = "api.get('data')"
+ keyboard.puts(simple_api_get)
+ read_until(out, simple_api_get)
+ end
+ expect(output).to include("{}")
+ expect(exitstatus).to eq(0)
+ end
+ end
+
it "sets the override_runlist from the command line" do
output, exitstatus = run_chef_shell_with("-o 'override::foo,override::bar'") do |out, keyboard|
show_recipes_code = %q[puts "#{node["recipes"].inspect}"]
diff --git a/spec/unit/shell/shell_session_spec.rb b/spec/unit/shell/shell_session_spec.rb
index 259e6096a4..170767fbd6 100644
--- a/spec/unit/shell/shell_session_spec.rb
+++ b/spec/unit/shell/shell_session_spec.rb
@@ -77,6 +77,37 @@ describe Shell::ClientSession do
end
+describe Shell::SoloSession do
+ before do
+ Chef::Config[:shell_config] = { :override_runlist => [Chef::RunList::RunListItem.new("shell::override")] }
+ @chef_rest = double("Chef::ServerAPI")
+ @session = Shell::SoloSession.instance
+ @node = Chef::Node.build("foo")
+ @session.node = @node
+ @client = double("Chef::Client.new",
+ :run_ohai => true,
+ :load_node => true,
+ :build_node => true,
+ :register => true,
+ :sync_cookbooks => {})
+ end
+
+ it "builds the node's run_context with the proper environment" do
+ @session.instance_variable_set(:@client, @client)
+ @expansion = Chef::RunList::RunListExpansion.new(@node.chef_environment, [])
+
+ expect(@node.run_list).to receive(:expand).with(@node.chef_environment).and_return(@expansion)
+ expect(Chef::ServerAPI).to receive(:new).with(Chef::Config[:chef_server_url]).and_return(@chef_rest)
+ @session.rebuild_context
+ end
+
+ it "passes the shell CLI args to the client" do
+ expect(Chef::Client).to receive(:new).with(nil, Chef::Config[:shell_config]).and_return(@client)
+ @session.send(:rebuild_node)
+ end
+
+end
+
describe Shell::StandAloneSession do
before do
Chef::Config[:shell_config] = { :override_runlist => [Chef::RunList::RunListItem.new("shell::override")] }
@@ -128,11 +159,11 @@ describe Shell::StandAloneSession do
end
-describe Shell::SoloSession do
+describe Shell::SoloLegacySession do
before do
Chef::Config[:shell_config] = { :override_runlist => [Chef::RunList::RunListItem.new("shell::override")] }
Chef::Config[:shell_solo] = true
- @session = Shell::SoloSession.instance
+ @session = Shell::SoloLegacySession.instance
@node = Chef::Node.new
@events = Chef::EventDispatch::Dispatcher.new
@run_context = @session.run_context = Chef::RunContext.new(@node, {}, @events)