Skip to content

Instantly share code, notes, and snippets.

@benhoskings
Created September 24, 2013 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhoskings/6678686 to your computer and use it in GitHub Desktop.
Save benhoskings/6678686 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!
# Only install babushka if it's not already instealled on the remote.
bootstrap_babushka! unless remote_host.test('babushka --version')
# Run each named dep in order, failing fast.
config.deps.all? do |(dep_name, dep_args)|
remote_command(
'babushka',
'--update --defaults --colour', [config.args].flatten(1).join(' '),
"'#{dep_name}'",
*arg_pairs(dep_args)
).tap {
# 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!
env[:ui].info("Installing babushka on #{host_name}.")
tmpfile = "/tmp/babushka_me_up"
File.open(tmpfile, 'w') {|f| f.write `curl https://babushka.me/up` }
remote_host.upload(tmpfile, tmpfile)
remote_command("sh #{tmpfile}")
end
def remote_command *cmd
# Truncate long arguments (like ssh pubkeys, etc) so as not to spam the terminal.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment