-
-
Save shimizukawa/3922718 to your computer and use it in GitHub Desktop.
Vagrant provisioner of Fabric. 元ネタ: https://groups.google.com/d/msg/vagrant-up/DXz7gAumeI8/lqrcT1L98G8J
This file contains hidden or 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
# -*- 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) |
This file contains hidden or 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 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 |
This file contains hidden or 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
# -*- 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 :)