begin require "vagrant" rescue LoadError raise "The vagrant-unison plugin must be run within Vagrant." end # This is a sanity check to make sure no one is attempting to install # this into an early Vagrant version. if Vagrant::VERSION < "1.1.0" raise "The vagrant-unison plugin is only compatible with Vagrant 1.1+" end module VagrantPlugins module Unison class Plugin < Vagrant.plugin("2") name "Unison" description <<-DESC This plugin syncs files over SSH from a local folder to your Vagrant VM (local or on AWS). DESC config "sync" do require_relative "config" Config end command "sync" do # Setup logging and i18n setup_logging setup_i18n #Return the command require_relative "command" Command end command "sync-repeat" do # Setup logging and i18n setup_logging setup_i18n #Return the command require_relative "command" CommandRepeat end command "sync-cleanup" do # Setup logging and i18n setup_logging setup_i18n #Return the command require_relative "command" CommandCleanup end command "sync-interact" do # Setup logging and i18n setup_logging setup_i18n #Return the command require_relative "command" CommandInteract end provisioner "start-unison" do require "log4r" require "vagrant" require "thread" require 'listen' require 'fileutils' require_relative 'unison_paths' require_relative 'ssh_command' require_relative 'shell_command' require_relative 'unison_sync' class StartUnisonProvisioner < Vagrant.plugin("2", :provisioner) def initialize(machine, config) @@machine = machine super end def configure(root_config) end def provision unison_paths = UnisonPaths.new(@@machine.env, @@machine) guest_path = unison_paths.guest host_path = unison_paths.host user_name = @@machine.ssh_info[:username] @@machine.ui.info(host_path) @@machine.ui.info(guest_path) @@machine.env.ui.info "Unisoning changes from {host}::#{host_path} --> {guest VM}::#{guest_path}" # Create the guest path @@machine.communicate.sudo("mkdir -p '#{guest_path}'") @@machine.communicate.sudo("chown #{user_name} '#{guest_path}'") ssh_command = SshCommand.new(@@machine, unison_paths) command = ShellCommand.new(@@machine, unison_paths, ssh_command) #load @@machine.env.root_path.to_s + '/include-common/load-ini/load-ini.rb' load ENV['host_proj_dir'] + '/include-common/load-ini/load_ini.rb' properties = load_ini() if (properties.has_key?('UNISONFLAGS')) unisonflags = ' '+properties['UNISONFLAGS'].chomp.chomp('"').reverse.chomp.chomp('"').reverse+' ' else unisonflags = '' end slug = properties['SLUG'].chomp.chomp('"').reverse.chomp.chomp('"').reverse unison_dir = ENV['host_proj_dir'] + '/.vagrant/unison-files/' FileUtils.rm_rf(unison_dir) Dir.mkdir(unison_dir) command.repeat = true command.terse = true command = 'UNISON='+unison_dir+' '+command.to_s + unisonflags + ' > ' + @@machine.env.root_path.to_s + '/.vagrant/unison.log 2>&1 &' @@machine.env.ui.info "Running #{command}" system(command) end def cleanup end end StartUnisonProvisioner end # This initializes the internationalization strings. def self.setup_i18n I18n.load_path << File.expand_path("locales/en.yml", Unison.source_root) I18n.reload! end # This sets up our log level to be whatever VAGRANT_LOG is. def self.setup_logging require "log4r" level = nil begin level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase) rescue NameError # This means that the logging constant wasn't found, # which is fine. We just keep `level` as `nil`. But # we tell the user. level = nil end # Some constants, such as "true" resolve to booleans, so the # above error checking doesn't catch it. This will check to make # sure that the log level is an integer, as Log4r requires. level = nil if !level.is_a?(Integer) # Set the logging level on all "vagrant" namespaced # logs as long as we have a valid level. if level logger = Log4r::Logger.new("vagrant_sync") logger.outputters = Log4r::Outputter.stderr logger.level = level logger = nil end end end end end