Skip to content

Commit

Permalink
export DSL to json
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Dec 18, 2012
1 parent 8dd896b commit 3789442
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Capfile
@@ -0,0 +1,8 @@
load 'deploy' if respond_to?(:namespace) # cap2 differentiator

# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'

Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }

load 'config/deploy' # remove this line to skip loading any of the default tasks
110 changes: 110 additions & 0 deletions config/deploy.rb
@@ -0,0 +1,110 @@
require 'bundler/capistrano'
set :bundle_without, [:deployment]
set :bundle_flags, '--binstubs --quiet --deployment'

default_run_options[:pty] = true

depend :remote, :command, 'ruby'

ssh_options[:forward_agent] = true
set :application, "agilysys-chef-repo"

set :scm, :git

# Package the repository and upload to server
set :repository, ".git"
set :deploy_via, :copy
set :copy_cache, true
set :checkout, 'export'
set :copy_exclude, ['.git/*', 'vendor/*']

# Or deploy via git server
# set :repository, "git@github.com:doitian/chef-solo-repo.git"
# set :checkout, 'export'
# set :deploy_via, :remote_cache

set :branch, 'master'
set :keep_releases, 2
set :user, "ubuntu"
set :use_sudo, true

class ServerDSL
def initialize(f)
@options = {}
eval(File.read(f))
end
def node
@node ||= FakeNode.new
end
def method_missing(name, *args)
args.size == 0 ? @options[name] : @options[name] = args.first
end

class FakeNode
def [](*args); end
def []=(*args); end
def set(*args); self; end
alias_method :normal, :set
alias_method :default, :set
alias_method :override, :set
end
end

on :load do
ARGV.each do |arg|
if arg =~ /^servers\/.+\.rb$/
s = ServerDSL.new(arg)
server s.host, :app, :user => s.user, :port => s.port, :ssh_options => s.ssh_options
end
end
end

Dir[File.expand_path('../../servers/*.rb', __FILE__)].each do |f|
relative_filename = 'servers/' + File.basename(f)
desc "Deploy to #{File.basename(relative_filename).sub(/\.rb$/, '')}"
task relative_filename do
# place holder to eat missing task error, the file has been loaded in
# on :load block
end
end

task :inspect_servers do
p find_servers
end

namespace :deploy do
task :default do
update
top.chef.default
end
task :symlink_cache_dir do
run ["mkdir -p #{shared_path}/cache",
"ln -sfn #{shared_path}/cache #{current_release}/.chef/solo/cache"].join(' && ')
end
task :setup_directory_owner do
run "#{try_sudo} chown -R $USER: #{deploy_to}"
end
end

namespace :chef do
task :inspect_json do
find_servers_for_task(current_task).each do |s|
system("bundle exec thor 'server:#{s.host}'")
end
end

task :generate_json do
run "cd #{current_release} && bundle exec rake 'server:$CAPISTRANO:HOST$'"
end

task :default do
generate_json
cmd = ["cd #{current_release}",
"bin/chef-solo -c solo.rb -j json/servers/$CAPISTRANO:HOST$.json -l info"].join(' && ')
run "#{try_sudo} bash -l -c '#{cmd}'"
end
end

after 'deploy:setup', 'deploy:setup_directory_owner'
after 'deploy:update', 'deploy:symlink_cache_dir'
after 'deploy', 'deploy:cleanup'
Empty file removed servers/.gitkeep
Empty file.
8 changes: 8 additions & 0 deletions servers/vagrant-vm-sample.rb
@@ -0,0 +1,8 @@
user 'vagrant'
host '127.0.0.1'
port 2222
ssh_options(:keys => '~/.vagrant.d/insecure_private_key')

node.set[:name] = 'localhost-sample'

run_list ['recipe[hello-world]']
28 changes: 28 additions & 0 deletions tasks/server.thor
@@ -0,0 +1,28 @@
require 'chef'

class Chef::Node
def host(host = nil)
@host = host || @host
end
def user(*args); end
def port(*args); end
def ssh_options(*args); end
end

class Server < Thor
Dir['servers/*.rb'].each do |f|
node = Chef::Node.new
node.instance_eval File.read(f)
host = node.host
json = node.normal_attrs.to_hash
json['run_list'] = node.run_list

desc host, "dump json for #{host} into json/servers/#{host}.json"

define_method host do
File.open("json/servers/#{host}.json", 'w') do |out|
out.write(JSON.dump(json))
end
end
end
end

0 comments on commit 3789442

Please sign in to comment.