From 0c2096070256085b41d521429a45a1332136f09f Mon Sep 17 00:00:00 2001 From: Jose Asuncion Date: Wed, 23 May 2018 17:32:12 -0700 Subject: [PATCH 1/2] add environment attribute set --- .../knife/spork-environment-attribute-set.rb | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 lib/chef/knife/spork-environment-attribute-set.rb diff --git a/lib/chef/knife/spork-environment-attribute-set.rb b/lib/chef/knife/spork-environment-attribute-set.rb new file mode 100644 index 0000000..da9b3d4 --- /dev/null +++ b/lib/chef/knife/spork-environment-attribute-set.rb @@ -0,0 +1,73 @@ +require 'chef/knife' +require 'knife-spork/runner' +require 'set' +require 'json' +require 'chef/mixin/deep_merge' + +module KnifeSpork + class SporkEnvironmentAttributeSet < Chef::Knife + + banner 'knife spork environment attribute set ENVIRONMENT ATTRIBUTE VALUE' + + include KnifeSpork::Runner + + option :no_upload, + :long => '--no_upload', + :description => 'whether or not to upload environment file' + + def run + self.config = Chef::Config.merge!(config) + + if @name_args.empty? + show_usage + ui.error("You must specify a environment name, attribute and value") + exit 1 + end + + environments = @name_args[0].split(",").map { |env| load_specified_environment_group(env) }.flatten + key = @name_args[1].to_s + value = @name_args[2].to_s + params = hashify(key, value) + + run_plugins(:before_environment_attribute_set) + + environments.each do |env| + ui.msg "Modifying #{env}" + environment = load_environment_from_file(env) + + environment.default_attributes = merge(environment.default_attributes, params) + + environment.save unless config[:no_upload] + + save_environment_changes_remote(environment) + end + + run_plugins(:after_environment_attribute_set) + end + + def save_environment_changes_remote(environment) + local_environment = load_environment_from_file(environment) + remote_environment = load_remote_environment(environment) + + if local_environment.default_attributes != remote_environment.default_attributes + save_environment_changes(environment, pretty_print_json(remote_environment.to_hash)) + ui.msg "Done modifying #{environment} at #{Time.now}" + else + ui.msg "Environment #{environment} not modified." + end + end + + def merge(env1, env2) + Chef::Mixin::DeepMerge.merge(env1, env2) + end + + def hashify(string, value) + {}.tap do |h| + keys = string.split(':') + keys.reduce(h) do |h,l| + h[l] = (l == keys.last ? value : {}) + end + end + end + end +end From 25681c40dbf2e4f0cb1c4276d100af5e1178b839 Mon Sep 17 00:00:00 2001 From: Jose Asuncion Date: Tue, 29 May 2018 17:31:15 -0700 Subject: [PATCH 2/2] add knife spork environment attribute unset --- .../spork-environment-attribute-unset.rb | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lib/chef/knife/spork-environment-attribute-unset.rb diff --git a/lib/chef/knife/spork-environment-attribute-unset.rb b/lib/chef/knife/spork-environment-attribute-unset.rb new file mode 100644 index 0000000..f03e767 --- /dev/null +++ b/lib/chef/knife/spork-environment-attribute-unset.rb @@ -0,0 +1,72 @@ +require 'chef/knife' +require 'knife-spork/runner' + +module KnifeSpork + class SporkEnvironmentAttributeUnset < Chef::Knife + + banner 'knife spork environment attribute unset ENVIRONMENT ATTRIBUTE' + + include KnifeSpork::Runner + + option :no_upload, + :long => '--no_upload', + :description => 'whether or not to upload environment file' + + def run + self.config = Chef::Config.merge!(config) + + if @name_args.empty? + show_usage + ui.error("You must specify a environment name and attribute") + exit 1 + end + + environments = @name_args[0].split(",").map { |env| load_specified_environment_group(env) }.flatten + + if environments.length == 0 + ui.error("Environment group #{group} not found.") + exit 2 + end + + run_plugins(:before_environment_attribute_unset) + + + environments.each do |env| + environment = load_environment_from_file(env) + + ui.msg "Modifying #{env}" + unset(@name_args[1], environment) + + save_environment_changes_remote(env) + end + + run_plugins(:after_environment_attribute_unset) + end + + def unset(attributes, environment) + levels = attributes.split(":") + last_key = levels.pop + + last_hash = levels.inject(environment.default_attributes) do |h, k| + h[k] unless h.nil? + end + + unless last_hash.nil? + last_hash.delete(last_key) + environment.save unless config[:no_upload] + ui.msg "Done modifying #{environment} at #{Time.now}" + else + ui.msg "Environment #{environment} not modified." + end + end + + def save_environment_changes_remote(environment) + local_environment = load_environment_from_file(environment) + remote_environment = load_remote_environment(environment) + + if local_environment.default_attributes != remote_environment.default_attributes + save_environment_changes(environment, pretty_print_json(remote_environment.to_hash)) + end + end + end +end