Skip to content

Commit

Permalink
Merge pull request #2 from Makr91/patch-1
Browse files Browse the repository at this point in the history
fix: add synced folders
  • Loading branch information
MarkProminic authored Nov 19, 2024
2 parents b64181d + 69bc1c6 commit 70eaae3
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/vagrant-scp-sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@

require 'vagrant-scp-sync/version'
require 'vagrant-scp-sync/plugin'
require 'vagrant-scp-sync/errors'

module VagrantPlugins
module ScpSync
def self.source_root
@source_root ||= Pathname.new(File.expand_path("../../", __FILE__))
end
end
end
108 changes: 108 additions & 0 deletions lib/vagrant-scp-sync/action/scp_sync.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
require "vagrant/util/platform"
require "vagrant/util/subprocess"

module VagrantPlugins
module ScpSync
class ScpSyncHelper
def self.scp_single(machine, opts)
ssh_info = machine.ssh_info
raise Vagrant::Errors::SSHNotReady if ssh_info.nil?

source_files = opts[:guestpath]
target_files = opts[:hostpath]
target_files = File.expand_path(target_files, machine.env.root_path)
target_files = Vagrant::Util::Platform.fs_real_path(target_files).to_s

if Vagrant::Util::Platform.windows?
target_files = Vagrant::Util::Platform.cygwin_path(target_files)
end

if !source_files.end_with?("/")
source_files += "/"
end

if !target_files.end_with?("/")
target_files += "/"
end

opts[:owner] ||= ssh_info[:username]
opts[:group] ||= ssh_info[:username]

username = ssh_info[:username]
host = ssh_info[:host]
proxy_command = if @ssh_info[:proxy_command]
"-o ProxyCommand='#{@ssh_info[:proxy_command]}' "
else
''
end

excludes = ['.vagrant/', 'Vagrantfile']
excludes += Array(opts[:exclude]).map(&:to_s) if opts[:exclude]
excludes.uniq!

args = nil
args = Array(opts[:args]).dup if opts[:args]
#args ||= ["--verbose", "--archive", "--delete", "-z", "--copy-links"]

#if Vagrant::Util::Platform.windows? && !args.any? { |arg| arg.start_with?("--chmod=") }
# args << "--chmod=ugo=rwX"

# args << "--no-perms" if args.include?("--archive") || args.include?("-a")
#end

#args << "--no-owner" unless args.include?("--owner") || args.include?("-o")
#args << "--no-group" unless args.include?("--group") || args.include?("-g")

if opts[:direction] == :upload || opts[:direction].nil?
source = "'#{source_files}'"
target = "#{username}@#{host}:'#{target_files}'"
elsif opts[:direction] == :download
source = "#{username}@#{host}:'#{source_files}'"
target = "'#{target_files}'"
end

command = [
'scp',
'-r',
'-o StrictHostKeyChecking=no',
'-o UserKnownHostsFile=/dev/null',
"-o port=#{@ssh_info[:port]}",
'-o LogLevel=ERROR',
proxy_command,
@ssh_info[:private_key_path].map { |k| "-i '#{k}'" }.join(' '),
source,
target
].join(' ')

command_opts = {}
command_opts[:workdir] = machine.env.root_path.to_s

machine.ui.info(I18n.t(
"vagrant.scp_folder", source_files: source_files, target_files: target_files))
if excludes.length > 1
machine.ui.info(I18n.t(
"vagrant.scp_folder_excludes", excludes: excludes.inspect))
end

if machine.guest.capability?(:scp_pre)
machine.guest.capability(:scp_pre, opts)
end

command = command + [command_opts]

r = Vagrant::Util::Subprocess.execute(*command)
if r.exit_code != 0
raise Vagrant::Errors::SyncedFolderScpSyncError,
command: command.inspect,
source_files: source_files,
target_files: target_files,
stderr: r.stderr
end

if machine.guest.capability?(:scp_post)
machine.guest.capability(:scp_post, opts)
end
end
end
end
end
File renamed without changes.
12 changes: 12 additions & 0 deletions lib/vagrant-scp-sync/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "vagrant"

module Vagrant
module Errors
class SyncedFolderScpSyncError < VagrantError
error_key(:scp_sync_error, "vagrant_scp_sync.errors")
end
class SCPNotFound < VagrantError
error_key(:scp_installed_error, "vagrant_scp_sync.errors")
end
end
end
46 changes: 44 additions & 2 deletions lib/vagrant-scp-sync/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# frozen_string_literal: true

begin
require "vagrant"
rescue LoadError
raise "The vagrant-scp-sync plugin must be run within Vagrant."
end

if Vagrant::VERSION < "2"
raise "The vagrant-scp-sync plugin is only compatible with Vagrant 2+"
end

module VagrantPlugins
module ScpSync
# This defines the class for the plugin vagrant-scp-sync
Expand All @@ -10,8 +20,40 @@ class Plugin < Vagrant.plugin('2')
DESC

command 'scp' do
require_relative 'commands/scp'
Command::ScpSync
setup_logging
setup_i18n
require_relative "command"
Command
end

synced_folder("scp", 5) do
require_relative "synced_folder"
SyncedFolder
end

def self.setup_i18n
I18n.load_path << File.expand_path("locales/en.yml", ScpSync.source_root)
I18n.reload!
end

def self.setup_logging
require "log4r"

level = nil
begin
level = Log4r.const_get(ENV["VAGRANT_LOG"].upcase)
rescue NameError
level = nil
end

level = nil if !level.is_a?(Integer)

if level
logger = Log4r::Logger.new("vagrant_scp_sync")
logger.outputters = Log4r::Outputter.stderr
logger.level = level
logger = nil
end
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions lib/vagrant-scp-sync/synced_folders.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "log4r"

require "vagrant/util/subprocess"
require "vagrant/util/which"

require_relative "action/scp_sync"

module VagrantPlugins
module ScpSync
class SyncedFolder < Vagrant.plugin("2", :synced_folder)
include Vagrant::Util

def initialize(*args)
super

@logger = Log4r::Logger.new("vagrant_scp_sync")
end

def usable?(machine, raise_error=false)
scp_path = Which.which("scp")
return true if scp_path
return false if !raise_error
raise Vagrant::Errors::SCPNotFound
end

def prepare(machine, folders, opts)
end

def enable(machine, folders, opts)
ssh_info = machine.ssh_info

if ssh_info[:private_key_path].empty? && ssh_info[:password]
machine.ui.warn(I18n.t("vagrant.scp_ssh_password"))
end

folders.each do |id, folder_opts|
ScpSyncHelper.scp_single(machine, folder_opts)
end
end
end
end
end
15 changes: 14 additions & 1 deletion locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ en:
vagrant_scp_sync:
errors:
not_yet_implemented: |-
Configuration is not yet implemented
Configuration is not yet implemented
scp_sync_error: |-
There was an error when attemping to sync folders using scp.
Please inspect the error message below for more info.
Host path: %{hostpath}
Guest path: %{guestpath}
Error: %{stderr}
Full command causing error:
%{command}
scp_installed_error: |-
SCP was not detected as installed

0 comments on commit 70eaae3

Please sign in to comment.