Skip to content

Instantly share code, notes, and snippets.

@jedi4ever
Created March 17, 2011 12:15
Show Gist options
  • Save jedi4ever/874222 to your computer and use it in GitHub Desktop.
Save jedi4ever/874222 to your computer and use it in GitHub Desktop.
could not find a good example on the new rake integration + integrated logging It's ugly but working
class RakeLogger < Vagrant::UI
def initialize(env,logger)
super(env)
@logger = logger
end
[[:warn, :yellow], [:error, :red], [:info, nil], [:confirm, :green]].each do |method, color|
class_eval <<-CODE
def #{method}(message, opts=nil)
@logger.warn("\#{line_reset}\#{format_message(message, opts)}")
end
CODE
#@logger.warn("\#{line_reset}\#{format_message(message, opts)}", #{color.inspect})
end
[:ask, :no?, :yes?].each do |method|
class_eval <<-CODE
def #{method}(message, opts=nil)
opts ||= {}
@shell.send(#{method.inspect}, format_message(message, opts), opts[:color])
end
CODE
end
def report_progress(progress, total, show_parts=true)
percent = (progress.to_f / total.to_f) * 100
line = "Progress: #{percent.to_i}%"
line << " (#{progress} / #{total})" if show_parts
line = "#{line_reset}#{line}"
@logger.info(line)
#@logger.info(line, nil, false)
end
protected
def format_message(message, opts=nil)
opts = { :prefix => true }.merge(opts || {})
message = "[#{env.resource}] #{message}" if opts[:prefix]
message
end
def line_reset
reset = "\r"
reset += "\e[0K" unless Mario::Platform.windows?
reset
end
end
require 'vagrant'
require 'vagrant/cli'
require './lib/rakelogger'
require 'logger'
logger = Logger.new(STDOUT)
env=Vagrant::Environment.new(:cwd => ".")
env.ui = RakeLogger.new(env,logger)
env.load!
task :default => [:status]
desc "Show status"
task :status do
puts "status:"
puts "-------"
env.vms.each do |id, vm|
puts "Name:#{vm.vm.name} - Id:#{id} - Created:#{vm.created?}"
end
end
desc "build"
task :build do
if !env.vms[:default].vm.running?
Vagrant::CLI.start(["up"], :env => env)
else
Vagrant::CLI.start(["provision"], :env => env)
end
end
desc "clean"
task :clean do
system "rm *.box"
Vagrant::CLI.start(["destroy", "-f"],:env => env)
end
desc "cleanbuild"
task :cleanbuild do
Rake::Task["clean"].execute
Rake::Task["build"].execute
end
desc "package"
task :package do
if env.vms[:default].vm.running?
puts "Halting the machine"
Vagrant::CLI.start(["halt"], :env => env)
end
puts "Packaging the machine"
system "rm *.box"
Vagrant::CLI.start(["package", "--base" , env.vms[:default].vm.name, "-f", "Vagrantfile"],:env => env)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment