diff options
author | Tim Smith <tsmith84@gmail.com> | 2021-02-12 16:20:22 -0800 |
---|---|---|
committer | Tim Smith <tsmith84@gmail.com> | 2021-02-12 16:29:23 -0800 |
commit | 6aa5769621606d5881aabb753fa6be73006c8de2 (patch) | |
tree | 813008d5279fbc0d2137990a51fa28993a9e1925 | |
parent | 01531f0d868af7cd5377f9c88429bcb427273b11 (diff) | |
download | chef-6aa5769621606d5881aabb753fa6be73006c8de2.tar.gz |
Remove the legacy chef-apply command
chef-apply is a low value command with a high value name which conflicts with the actual code of the chef run command. It's been targeted for Infra Client 17 removal.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rwxr-xr-x | chef-bin/bin/chef-apply | 24 | ||||
-rw-r--r-- | chef-utils/lib/chef-utils/dist.rb | 8 | ||||
-rw-r--r-- | distro/templates/powershell/chef/chef.psm1.erb | 6 | ||||
-rw-r--r-- | lib/chef/application/apply.rb | 248 | ||||
-rw-r--r-- | lib/chef/applications.rb | 1 | ||||
-rw-r--r-- | spec/unit/application/apply_spec.rb | 116 |
6 files changed, 2 insertions, 401 deletions
diff --git a/chef-bin/bin/chef-apply b/chef-bin/bin/chef-apply index 92c350b85b..d6c44a3194 100755 --- a/chef-bin/bin/chef-apply +++ b/chef-bin/bin/chef-apply @@ -1,24 +1,4 @@ #!/usr/bin/env ruby -# -# ./chef-apply - Run a single chef recipe -# -# Author:: Bryan W. Berry (<bryan.berry@gmail.com>) -# Copyright:: Copyright 2012-2016, Bryan W. Berry -# 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. -$:.unshift(File.join(__dir__, "..", "lib")) -require "chef/application/apply" - -Chef::Application::Apply.new.run(enforce_license: true) +puts "As of Chef Infra Client 17 the chef-apply command is no longer included as part of Chef Infra Client" +exit! diff --git a/chef-utils/lib/chef-utils/dist.rb b/chef-utils/lib/chef-utils/dist.rb index c4fe72960d..0ab6674f18 100644 --- a/chef-utils/lib/chef-utils/dist.rb +++ b/chef-utils/lib/chef-utils/dist.rb @@ -2,14 +2,6 @@ module ChefUtils # This class is not fully implemented, depending on it is not recommended! module Dist - class Apply - # The chef-apply product name - PRODUCT = "Chef Infra Apply" - - # The chef-apply binary - EXEC = "chef-apply" - end - class Automate # name of the automate product PRODUCT = "Chef Automate" diff --git a/distro/templates/powershell/chef/chef.psm1.erb b/distro/templates/powershell/chef/chef.psm1.erb index 35344d907d..2bd54c8c18 100644 --- a/distro/templates/powershell/chef/chef.psm1.erb +++ b/distro/templates/powershell/chef/chef.psm1.erb @@ -411,11 +411,6 @@ function Run-RubyCommand($command, $argList) { Run-ExecutableAndWait $ruby """$ruby"" '$commandPath' $fortifiedArgString" } - -function <%= ChefUtils::Dist::Apply::EXEC %> { - Run-RubyCommand '<%= ChefUtils::Dist::Apply::EXEC %>' $args -} - function <%= ChefUtils::Dist::Infra::CLIENT %> { Run-RubyCommand '<%= ChefUtils::Dist::Infra::CLIENT %>' $args } @@ -440,7 +435,6 @@ function knife { Run-RubyCommand 'knife' $args } -Export-ModuleMember -function <%= ChefUtils::Dist::Apply::EXEC %> Export-ModuleMember -function <%= ChefUtils::Dist::Infra::CLIENT %> Export-ModuleMember -function <%= ChefUtils::Dist::Infra::EXEC %>-service-manager Export-ModuleMember -function <%= ChefUtils::Dist::Infra::SHELL %> diff --git a/lib/chef/application/apply.rb b/lib/chef/application/apply.rb deleted file mode 100644 index 3559f8e416..0000000000 --- a/lib/chef/application/apply.rb +++ /dev/null @@ -1,248 +0,0 @@ -# -# Author:: Bryan W. Berry (<bryan.berry@gmail.com>) -# Author:: Daniel DeLeo (<dan@kallistec.com>) -# Copyright:: Copyright 2012-2016, Bryan W. Berry -# Copyright:: Copyright 2012-2016, Daniel DeLeo -# 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 "../../chef" -require_relative "../application" -require_relative "../client" -require_relative "../config" -require_relative "../config_fetcher" -require_relative "../log" -require "fileutils" unless defined?(FileUtils) -require "tempfile" unless defined?(Tempfile) -require_relative "../providers" -require_relative "../resources" -require "chef-utils/dist" unless defined?(ChefUtils::Dist) -require "license_acceptance/cli_flags/mixlib_cli" - -class Chef::Application::Apply < Chef::Application - include LicenseAcceptance::CLIFlags::MixlibCLI - - banner "Usage: #{ChefUtils::Dist::Apply::EXEC} [RECIPE_FILE | -e RECIPE_TEXT | -s] [OPTIONS]" - - option :execute, - short: "-e RECIPE_TEXT", - long: "--execute RECIPE_TEXT", - description: "Execute resources supplied in a string.", - proc: nil - - option :stdin, - short: "-s", - long: "--stdin", - description: "Execute resources read from STDIN.", - boolean: true - - option :json_attribs, - short: "-j JSON_ATTRIBS", - long: "--json-attributes JSON_ATTRIBS", - description: "Load attributes from a JSON file or URL.", - proc: nil - - option :force_logger, - long: "--force-logger", - description: "Use logger output instead of formatter output.", - boolean: true, - default: false - - option :force_formatter, - long: "--force-formatter", - description: "Use formatter output instead of logger output.", - boolean: true, - default: false - - option :formatter, - short: "-F FORMATTER", - long: "--format FORMATTER", - description: "The output format to use.", - proc: lambda { |format| Chef::Config.add_formatter(format) } - - option :log_level, - short: "-l LEVEL", - long: "--log_level LEVEL", - description: "Set the log level (trace, debug, info, warn, error, fatal).", - proc: lambda { |l| l.to_sym } - - option :log_location_cli, - short: "-L LOGLOCATION", - long: "--logfile LOGLOCATION", - description: "Set the log file location, defaults to STDOUT - recommended for daemonizing." - - option :always_dump_stacktrace, - long: "--[no-]always-dump-stacktrace", - boolean: true, - default: false, - description: "Always dump the stacktrace regardless of the log_level setting." - - option :help, - short: "-h", - long: "--help", - description: "Show this help message.", - on: :tail, - boolean: true, - show_options: true, - exit: 0 - - option :version, - short: "-v", - long: "--version", - description: "Show #{ChefUtils::Dist::Infra::PRODUCT} version.", - boolean: true, - proc: lambda { |v| puts "#{ChefUtils::Dist::Infra::PRODUCT}: #{::Chef::VERSION}" }, - exit: 0 - - option :why_run, - short: "-W", - long: "--why-run", - description: "Enable whyrun mode.", - boolean: true - - option :yaml, - long: "--yaml", - description: "Parse recipe as YAML", - boolean: true, - default: false - - option :profile_ruby, - long: "--[no-]profile-ruby", - description: "Dump complete Ruby call graph stack of entire #{ChefUtils::Dist::Infra::PRODUCT} run (expert only).", - boolean: true, - default: false - - option :color, - long: "--[no-]color", - boolean: true, - default: true, - description: "Use colored output, defaults to enabled." - - option :minimal_ohai, - long: "--minimal-ohai", - description: "Only run the bare minimum Ohai plugins #{ChefUtils::Dist::Infra::PRODUCT} needs to function.", - boolean: true - - attr_reader :json_attribs - - def initialize - super - end - - def reconfigure - parse_options - Chef::Config.merge!(config) - configure_logging - Chef::Config.export_proxies - Chef::Config.init_openssl - parse_json - end - - def parse_json - if Chef::Config[:json_attribs] - config_fetcher = Chef::ConfigFetcher.new(Chef::Config[:json_attribs]) - @json_attribs = config_fetcher.fetch_json - end - end - - def read_recipe_file(file_name) - if file_name.nil? - Chef::Application.fatal!("No recipe file was provided", Chef::Exceptions::RecipeNotFound.new) - else - recipe_path = File.expand_path(file_name) - unless File.exist?(recipe_path) - Chef::Application.fatal!("No file exists at #{recipe_path}", Chef::Exceptions::RecipeNotFound.new) - end - recipe_fh = open(recipe_path) - recipe_text = recipe_fh.read - [recipe_text, recipe_fh] - end - end - - def get_recipe_and_run_context - Chef::Config[:solo_legacy_mode] = true - @chef_client = Chef::Client.new(@json_attribs) - @chef_client.run_ohai - @chef_client.load_node - @chef_client.build_node - run_context = if @chef_client.events.nil? - Chef::RunContext.new(@chef_client.node, {}) - else - Chef::RunContext.new(@chef_client.node, {}, @chef_client.events) - end - recipe = Chef::Recipe.new("(#{ChefUtils::Dist::Apply::EXEC} cookbook)", "(#{ChefUtils::Dist::Apply::EXEC} recipe)", run_context) - [recipe, run_context] - end - - # write recipe to temp file, so in case of error, - # user gets error w/ context - def temp_recipe_file - @recipe_fh = Tempfile.open("recipe-temporary-file") - @recipe_fh.write(@recipe_text) - @recipe_fh.rewind - @recipe_filename = @recipe_fh.path - end - - def run_chef_recipe - if config[:execute] - @recipe_text = config[:execute] - temp_recipe_file - elsif config[:stdin] - @recipe_text = STDIN.read - temp_recipe_file - else - unless ARGV[0] - puts opt_parser - Chef::Application.exit! "No recipe file provided", Chef::Exceptions::RecipeNotFound.new - end - @recipe_filename = ARGV[0] - @recipe_text, @recipe_fh = read_recipe_file @recipe_filename - end - recipe, run_context = get_recipe_and_run_context - if config[:yaml] || File.extname(@recipe_filename) == ".yml" - logger.info "Parsing recipe as YAML" - recipe.from_yaml(@recipe_text) - else - recipe.instance_eval(@recipe_text, @recipe_filename, 1) - end - runner = Chef::Runner.new(run_context) - catch(:end_client_run_early) do - - runner.converge - ensure - @recipe_fh.close - - end - Chef::Platform::Rebooter.reboot_if_needed!(runner) - end - - def run_application - parse_options - run_chef_recipe - Chef::Application.exit! "Exiting", 0 - rescue SystemExit - raise - rescue Exception => e - Chef::Application.debug_stacktrace(e) - Chef::Application.fatal!("#{e.class}: #{e.message}", e) - end - - # Get this party started - def run(enforce_license: false) - reconfigure - check_license_acceptance if enforce_license - run_application - end - -end diff --git a/lib/chef/applications.rb b/lib/chef/applications.rb index a30b765c77..4c2dca3981 100644 --- a/lib/chef/applications.rb +++ b/lib/chef/applications.rb @@ -1,4 +1,3 @@ require_relative "application/client" require_relative "application/knife" require_relative "application/solo" -require_relative "application/apply" diff --git a/spec/unit/application/apply_spec.rb b/spec/unit/application/apply_spec.rb deleted file mode 100644 index f071dbd981..0000000000 --- a/spec/unit/application/apply_spec.rb +++ /dev/null @@ -1,116 +0,0 @@ -# -# Author:: Bryan W. Berry (<bryan.berry@gmail.com>) -# Copyright:: Copyright 2012-2016, Bryan W. Berry -# 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 "spec_helper" - -describe Chef::Application::Apply do - - before do - @app = Chef::Application::Apply.new - allow(@app).to receive(:configure_logging).and_return(true) - allow(Chef::Log).to receive(:debug).with("FIPS mode is enabled.") - @recipe_text = "package 'nyancat'" - Chef::Config[:solo_legacy_mode] = true - end - - describe "configuring the application" do - it "should set solo mode to true" do - @app.reconfigure - expect(Chef::Config[:solo_legacy_mode]).to be_truthy - end - end - describe "read_recipe_file" do - before do - @recipe_file_name = "foo.rb" - @recipe_path = File.expand_path(@recipe_file_name) - @recipe_file = double("Tempfile (mock)", read: @recipe_text) - allow(@app).to receive(:open).with(@recipe_path).and_return(@recipe_file) - allow(File).to receive(:exist?).with(@recipe_path).and_return(true) - allow(Chef::Application).to receive(:fatal!).and_return(true) - end - - it "should read text properly" do - expect(@app.read_recipe_file(@recipe_file_name)[0]).to eq(@recipe_text) - end - - it "should return a file_handle" do - expect(@app.read_recipe_file(@recipe_file_name)[1]).to be_instance_of(RSpec::Mocks::Double) - end - - describe "when recipe is nil" do - it "should raise a fatal with the missing filename message" do - expect(Chef::Application).to receive(:fatal!).with("No recipe file was provided", - Chef::Exceptions::RecipeNotFound.new) - @app.read_recipe_file(nil) - end - end - - describe "when recipe doesn't exist" do - before do - allow(File).to receive(:exist?).with(@recipe_path).and_return(false) - end - it "should raise a fatal with the file doesn't exist message" do - expect(Chef::Application).to receive(:fatal!).with(/^No file exists at/, - Chef::Exceptions::RecipeNotFound.new) - @app.read_recipe_file(@recipe_file_name) - end - end - end - - describe "temp_recipe_file" do - before do - @app.instance_variable_set(:@recipe_text, @recipe_text) - @app.temp_recipe_file - @recipe_fh = @app.instance_variable_get(:@recipe_fh) - end - it "should open a tempfile" do - expect(@recipe_fh.path).to match(/.*recipe-temporary-file.*/) - end - it "should write recipe text to the tempfile" do - expect(@recipe_fh.read).to eq(@recipe_text) - end - it "should save the filename for later use" do - expect(@recipe_fh.path).to eq(@app.instance_variable_get(:@recipe_filename)) - end - end - describe "recipe_file_arg" do - before do - ARGV.clear - end - it "should exit and log message" do - expect(Chef::Log).to receive(:debug).with(/^No recipe file provided/) - expect { @app.run }.to raise_error(SystemExit) { |e| expect(e.status).to eq(1) } - end - - end - describe "when the json_attribs configuration option is specified" do - let(:json_attribs) { { "a" => "b" } } - let(:config_fetcher) { double(Chef::ConfigFetcher, fetch_json: json_attribs) } - let(:json_source) { "https://foo.com/foo.json" } - - before do - Chef::Config[:json_attribs] = json_source - expect(Chef::ConfigFetcher).to receive(:new).with(json_source) - .and_return(config_fetcher) - end - - it "reads the JSON attributes from the specified source" do - @app.reconfigure - expect(@app.json_attribs).to eq(json_attribs) - end - end -end |