Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add environment attribute set/unset #222

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions lib/chef/knife/spork-environment-attribute-set.rb
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions lib/chef/knife/spork-environment-attribute-unset.rb
Original file line number Diff line number Diff line change
@@ -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