Skip to content

Instantly share code, notes, and snippets.

@btm
Created September 28, 2012 00:29
Show Gist options
  • Select an option

  • Save btm/3797265 to your computer and use it in GitHub Desktop.

Select an option

Save btm/3797265 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Copyright 2012 Opscode <legal@opscode.com>
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
#
# Merges a node json on disk in a repository with the current node object
# on the chef server. DeepMerge can't tell at what level you want it to
# overwrite instead of merge, so everything is merged. (Think run lists).
#
# 2012-09-27 Initial writeup - Bryan McLellan <btm@opscode.com>
require 'rubygems'
require 'json'
require 'chef/mash'
require 'chef/mixin/deep_merge'
if ARGV.length != 1
puts STDERR, "Missing node name argument"
puts STDERR, "merge_node.rb NODE_NAME"
exit 1
end
unless File.directory?('nodes')
puts STDERR, "Cannot find nodes directory, run from chef-repo"
exit 1
end
def fetch_node(node_name)
result = system("knife node show #{node_name} -Fj -l > nodes/.tmp.#{node_name}.json")
unless result
puts STDERR, "Unable to get node #{node_name} from server"
exit 1
end
end
def upload_node(node_name)
result = system("knife node from file nodes/.tmp.#{node_name}.json")
unless result
puts STDERR, "Unable to save node #{node_name} to server"
exit 1
end
end
node_name = ARGV[0]
fetch_node(node_name)
current_node = JSON.parse(IO.read("nodes/.tmp.#{node_name}.json"))
repo_node = JSON.parse(IO.read("nodes/#{node_name}.json"))
new_node = Chef::Mixin::DeepMerge.merge(current_node, repo_node)
node_file = File.open("nodes/.tmp.#{node_name}.json", "w")
node_file.write(JSON.pretty_generate(new_node))
node_file.close
upload_node(node_name)
#File.delete("nodes/.tmp.#{node_name}.json")
puts "Merged node #{node_name} from repository"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment