Skip to content

Instantly share code, notes, and snippets.

Last active December 23, 2015 11:49
What would you like to do?
Rakefile with Chef upload_all and delete_all
require 'rubygems'
require 'chef'
require 'fileutils'
TOPDIR = File.expand_path(File.dirname(__FILE__))
load 'chef/tasks/chef_repo.rake'
environments = FileList[File.join(TOPDIR, 'environments', '*.json')].exclude(File.join(TOPDIR, 'environments', '_default.json'))
desc "Update environments"
task :environments => environments
environments.each do |fn|
task fn do
env = fn.pathmap('%f')
system("knife environment from file #{env}")
# The 'knife upload' command for roles only picks up .json files, so we need
# some custom handling.
rb_roles = FileList[File.join(TOPDIR, 'roles', '*.rb')]
desc "Update roles ending in .rb"
task :rb_roles => rb_roles
rb_roles.each do |fn|
task fn do
system("knife role from file #{fn}")
desc "Upload everything to the server"
task :upload_all => [ :environments, :rb_roles ] do
system("knife upload cookbooks")
system("knife upload roles")
system("knife upload data_bags")
desc "Remove everything from the current organization"
task :delete_all do
system('knife cookbook bulk delete "$" -y')
system('knife role bulk delete "$" -y')
system('for i in `knife data bag list`; do knife data bag delete $i -y; done')
system('for i in `knife environment list|grep -v "_default"`; do knife environment delete $i -y; done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment