Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Forked from wutali/fabric.rb
Created October 20, 2012 08:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shimizukawa/3922718 to your computer and use it in GitHub Desktop.
Save shimizukawa/3922718 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from fabric.api import task, sudo
@task(default=True)
def install(package_name):
sudo('DEBIAN_FRONTEND=noninteractive apt-get -q -y install {0}'.format(package_name), pty=False)
module Vagrant
module Provisioners
class Fabric < Base
class Config < Vagrant::Config::Base
attr_accessor :fabfile_path
attr_accessor :fabric_path
attr_accessor :python_path
attr_writer :tasks
def _default_fabfile_path
File.exist?("fabfile.py") ? "fabfile.py" : "fabfile/__init__.py"
end
def _default_fabric_path
"fab"
end
def _default_python_path
"/usr/bin/python"
end
def execute(command)
output = ''
IO.popen(command, "w+") do |f|
f.close_write
output = f.read
end
output
end
def validate(env, errors)
errors.add("fabfile does not exist.") if not File.exist?(fabfile_path)
command = "#{fabric_path} -V"
output = execute(command)
errors.add("fabric command does not exist.") if not $?.success?
command = "#{fabric_path} -f #{fabfile_path} -l"
output = execute(command)
errors.add("#{fabfile_path} could not recognize by fabric.") if not $?.success?
for task in tasks
task = task.split(':').first
command = "#{fabric_path} -f #{fabfile_path} -d #{task}"
output = execute(command)
errors.add("#{task} task does not exist.") if not $?.success?
end
end
def fabfile_path
@fabfile_path || _default_fabfile_path
end
def fabric_path
@fabric_path || _default_fabric_path
end
def python_path
@python_path || _default_python_path
end
def tasks
@tasks ||= []
end
# Adds a recipe to the run list
def add_task(name)
tasks << name
end
end
def self.config_class
Config
end
def provision!
ssh_info = env[:vm].ssh.info
user = ssh_info[:username]
host = ssh_info[:host]
port = ssh_info[:port]
private_key = ssh_info[:private_key_path]
system "#{config.fabric_path} -i #{private_key} --fabfile=#{config.fabfile_path} --user=#{user} --hosts=#{host} --port=#{port} #{config.tasks.join(' ')}"
end
end
end
end
# -*- mode: ruby -*-
# vi: set ft=ruby :
require "./fabric_provisioner.rb"
Vagrant::Config.run do |config|
# snip
config.vm.provision Vagrant::Provisioners::Fabric do |fab|
fab.fabric_path = '/path/to/fab'
fab.python_path = '/path/to/python'
fab.add_task 'apt:openssl'
fab.add_task 'apt:apache2'
# snip
end
@wutali
Copy link

wutali commented Jul 28, 2013

I have published the new version of vagrant-fabric plugin here (https://rubygems.org/gems/vagrant-fabric).
You can install it with just typing "vagrant plugin install vagrant-fabric".

And I changed my source code using your forked version.
Thank you for your contribution :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment