Skip to content

Instantly share code, notes, and snippets.

@btm
Created September 28, 2012 00:44
Show Gist options
  • Save btm/3797326 to your computer and use it in GitHub Desktop.
Save btm/3797326 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Copyright 2012 Opscode <[email protected]>
#
# 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.
#
# Loads a node from a json file but saves 'roles' and 'recipes' from the existing node.
#
# 2012-09-27 Initial writeup - Bryan McLellan <[email protected]>
require 'rubygems'
require 'json'
require 'chef/mash'
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 = repo_node
new_node['automatic'] = Mash.new
new_node['automatic']['recipes'] = current_node['automatic']['recipes']
new_node['automatic']['roles'] = current_node['automatic']['roles']
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