Created
July 10, 2012 12:30
-
-
Save wutali/3082989 to your computer and use it in GitHub Desktop.
Vagrant provisioner of Fabric.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 validate(env, errors) | |
errors.add("fabfile does not exist.") if not File.exist?(fabfile_path) | |
which_fabric_path = `which #{fabric_path}` | |
errors.add("fabric command does not exist.") if not $?.success? | |
fabfile_package = fabfile_path.gsub(".py", "").gsub("/", ".") | |
output = `#{python_path} -c "exec 'import #{fabfile_package}'" &> /dev/null` | |
errors.add("#{fabfile_package} could not import.") if not $?.success? | |
for task in tasks | |
package_path = "#{fabfile_package}.#{task}".gsub(".__init__", "") | |
packages = package_path.split(".") | |
output = `#{python_path} -c "exec 'from #{packages[0..-2].join('.')} import #{packages[-1]}'" &> /dev/null` | |
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} --config=#{config.fabfile_path} --user=#{user} --hosts=#{host} --port=#{port} #{config.tasks.join(' ')}" | |
end | |
end | |
end | |
end |
Nice. Has this been included in Vagrant core?
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".
Got any usage examples?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting, I'm playing with someting similar, thanks for posting ..