Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Created December 9, 2012 22:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoskings/4247306 to your computer and use it in GitHub Desktop.
Save benhoskings/4247306 to your computer and use it in GitHub Desktop.
class BabushkaProvisioner < Vagrant::Provisioners::Base
class Config < Vagrant::Config::Base
attr_accessor :args
attr_accessor :deps
def initialize
@deps = []
end
def dep(dep_spec, args = {})
deps << [dep_spec, args]
end
end
def self.config_class
BabushkaProvisioner::Config
end
def provision!
bootstrap_babushka! unless remote_host.test('babushka --version')
config.deps.each do |(dep_name, dep_args)|
remote_command(
'babushka',
'--update --defaults --colour', [config.args].flatten(1).join(' '),
"'#{dep_name}'",
*arg_pairs(dep_args)
)
# Disconnect after running each dep, so a new session is started
# each time, for things like setting the locale.
disconnect!
end
end
private
def bootstrap_babushka!
require 'net/http'
env[:ui].info("Installing babushka on #{host_name}.")
tmpfile = File.expand_path("/tmp/babushka_me_up")
File.open(tmpfile, 'w') {|f| f.write `curl https://babushka.me/up` }
remote_host.upload(tmpfile, "/tmp/babushka_me_up")
remote_command('bash /tmp/babushka_me_up')
end
def remote_command *cmd
logged_cmd = cmd.map {|i| i.sub(/^(.{50})(.{3}).*/m, '\1...').sub(/\n/, ' ') }.join(' ') # the command, with long args truncated
env[:ui].info "#{host_name}$ #{logged_cmd}"
remote_host.sudo(cmd.join(' ')) do |type, data|
if [:stderr, :stdout].include?(type)
env[:ui].info(data.chomp, :prefix => false)
end
end
end
def arg_pairs arg_hash
arg_hash.keys.map {|k| "#{k}='#{arg_hash[k]}'" }
end
def disconnect!
remote_host.instance_variable_get('@connection').close
end
def remote_host
env[:vm].channel
end
def host_name
env[:vm].config.vm.host_name
end
end
require './plugins/babushka_provisioner'
Vagrant::Config.run do |config|
config.vm.box = "precise64"
config.vm.box_url = 'http://files.vagrantup.com/precise64.box'
config.vm.provision BabushkaProvisioner do |babs|
babs.dep 'benhoskings:ruby.src', :version => '1.9.3', :patchlevel => 'p327'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment