Skip to content

Instantly share code, notes, and snippets.

@btm
Created September 28, 2012 01:24
Show Gist options
  • Save btm/3797459 to your computer and use it in GitHub Desktop.
Save btm/3797459 to your computer and use it in GitHub Desktop.
script for loading a node from json, expanding the run list and saving.
#!/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.
#
# Loads a node from a json file, expands roles + recipes, saves it to the server
#
# 2012-09-27 Initial writeup - Bryan McLellan <btm@opscode.com>
require 'rubygems'
require 'chef/node'
require 'chef/config'
require 'chef/log'
require 'chef/rest'
require 'json'
unless ARGV.length == 2
puts STDERR, "Missing arguments"
puts STDERR, "./load_expanded_node.rb .chef/knife.rb node_name.json"
config_file = ARGV[0]
node_file = ARGV[1]
puts "Loading Chef configuration from #{config_file}"
Chef::Config.from_file(config_file)
if File.exists?(node_file)
puts "Loading node from file #{node_file}"
node_json = JSON.parse(IO.read("#{node_file}"))
else
puts STDERR, "Unable to find node file: '#{node_file}'"
exit 1
end
node = Chef::Node.json_create(node_json)
puts "Expanding run list for #{node.name}"
puts "Current run list: #{node.run_list}"
node.expand!
puts "Expanded recipes: #{node.recipes.inspect}"
puts "Expanded roles: #{node.roles.inspect}"
puts "Saving node"
node.save
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment