diff options
author | Marc A. Paradise <marc.paradise@gmail.com> | 2019-04-08 12:23:10 -0400 |
---|---|---|
committer | Marc A. Paradise <marc.paradise@gmail.com> | 2019-04-24 13:25:58 -0400 |
commit | 5aa8f462bbfc3667e501866a8319087d57741f4a (patch) | |
tree | 3780200e6f6f113a8d00f53b4c5932ecea680b37 /lib/chef/knife/bootstrap.rb | |
parent | 44c3e411344a0c9139d01ef942f92f7d42a62a89 (diff) | |
download | chef-5aa8f462bbfc3667e501866a8319087d57741f4a.tar.gz |
deprecate old version of changed CLI flags.
This adds a lightweight deprecation mechanism
in order to warn the human that they're using deprecated flags,
and to continue to allow Bootstrap to work even if that happens.
Signed-off-by: Marc A. Paradise <marc.paradise@gmail.com>
Diffstat (limited to 'lib/chef/knife/bootstrap.rb')
-rw-r--r-- | lib/chef/knife/bootstrap.rb | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/lib/chef/knife/bootstrap.rb b/lib/chef/knife/bootstrap.rb index 90b6e5c3c5..32666b2d90 100644 --- a/lib/chef/knife/bootstrap.rb +++ b/lib/chef/knife/bootstrap.rb @@ -338,6 +338,28 @@ class Chef Chef::Config[:knife][:bootstrap_vault_item] } + # OPTIONAL: This can be exposed as an class method on Knife + # subclasses instead - that would let us move deprecation handling + # up into the base clase. + DEPRECATED_FLAGS = { + ssh_user: [:connection_user, "USER"], + ssh_port: [:connection_port, "PORT"], + winrm_user: [:connection_user, "USER"], + winrm_port: [:connection_port, "USER"], + ssl_peer_fingerprint: [:winrm_ssl_peer_fingerprint, "FINGERPRINT"], + winrm_authentication_protocol: [:winrm_auth_method, "AUTH-METHOD"] + } + + DEPRECATED_FLAGS.each do |flag, new_flag_config| + new_flag, arg = new_flag_config + flag_name = flag.to_s.gsub("_", "-") + long = "--#{flag_name} #{arg}" + new_long = options[new_flag][:long] + + option(flag, long: long, + description: "#{long} is deprecated. Use #{new_long} instead.") + end + attr_accessor :client_builder attr_accessor :chef_vault_handler attr_reader :target_host @@ -459,7 +481,38 @@ class Chef Erubis::Eruby.new(template).evaluate(bootstrap_context) end + # Check deprecated flags are used; map them to their new keys, + # and print a warning. Will not map a value to a new key if the + # CLI flag for that new key has also been specified. + # If both old and new flags are specified, this will warn + # and take the new flag value. + # This can be moved up to the base knife class if it's agreeable. + def warn_and_map_deprecated_flags + DEPRECATED_FLAGS.each do |old_key, new_flag_config| + new_key, _ = new_flag_config + if (config.key?(old_key) && config_source(old_key) == :cli) + # TODO - do we want the same warnings for knife config keys + # in absence of CLI keys? + if config.key?(new_key) && config_source(new_key) == :cli + new_key_name = "--#{new_key.to_s.gsub("_", "-")}" + old_key_name = "--#{old_key.to_s.gsub("_", "-")}" + ui.warn <<~EOM + You provided both #{new_key_name} and #{old_key_name}. + Using: '#{new_key_name.split(" ").first} #{config[new_key]}' because #{old_key_name} is deprecated. + EOM + else + config[new_key] = config[old_key] + unless Chef::Config[:silence_deprecation_warnings] == true + ui.warn options[old_key][:description] + end + end + end + end + end + def run + warn_and_map_deprecated_flags + validate_name_args! validate_protocol! validate_first_boot_attributes! @@ -882,14 +935,6 @@ class Chef end end - # Tells us where a config value has come from , - # :cli_config, :knife_config, :not_found - def config_source(key, knife_config_key = nil) - return :cli_config if config.key? key - return :knife_config if config.key?(key) || config.key?(knife_config_key) - :not_found - end - def upload_bootstrap(content) script_name = target_host.base_os == :windows ? "bootstrap.bat" : "bootstrap.sh" remote_path = target_host.normalize_path(File.join(target_host.temp_dir, script_name)) |