providers/hyperv: clean up provider since we moved out SMB

This commit is contained in:
Mitchell Hashimoto 2014-02-26 16:05:37 -08:00
parent dd6f1083b0
commit 41bc86c490
6 changed files with 79 additions and 242 deletions

View File

@ -71,7 +71,6 @@ module VagrantPlugins
b.use WaitForCommunicator, [:running]
b.use SyncedFolders
#b.use ShareFolders
#b.use SyncFolders
end
end
@ -138,7 +137,6 @@ module VagrantPlugins
autoload :MessageNotRunning, action_root.join('message_not_running')
autoload :SyncFolders, action_root.join('sync_folders')
autoload :ReadGuestIP, action_root.join('read_guest_ip')
autoload :ShareFolders, action_root.join('share_folders')
autoload :WaitForIPAddress, action_root.join("wait_for_ip_address")
end
end

View File

@ -1,123 +0,0 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
require "debugger"
require "vagrant/util/subprocess"
module VagrantPlugins
module HyperV
module Action
class ShareFolders
def initialize(app, env)
@app = app
end
def call(env)
@env = env
smb_shared_folders
prepare_smb_share
# A BIG Clean UP
# There should be a communicator class which branches between windows
# and Linux
if @smb_shared_folders.length > 0
env[:ui].info('Mounting shared folders with VM, This process may take few minutes.')
end
if env[:machine].config.vm.guest == :windows
env[:ui].info "Mounting shared folders to windows is under development."
# mount_shared_folders_to_windows
elsif env[:machine].config.vm.guest == :linux
mount_shared_folders_to_linux
end
@app.call(env)
end
def smb_shared_folders
@smb_shared_folders = {}
@env[:machine].config.vm.synced_folders.each do |id, data|
# Ignore disabled shared folders
next if data[:disabled]
# Collect all SMB shares
next unless data[:smb]
# This to prevent overwriting the actual shared folders data
@smb_shared_folders[id] = data.dup
end
end
def prepare_smb_share
@smb_shared_folders.each do |id, data|
begin
hostpath = File.expand_path(data[:hostpath], @env[:root_path])
host_share_username = @env[:machine].provider_config.host_share.username
options = {:path => hostpath,
:share_name => data[:share_name],
:host_share_username => host_share_username}
response = @env[:machine].provider.driver.execute('set_smb_share.ps1', options)
if response["message"] == "OK"
@env[:ui].info "Successfully created SMB share for #{hostpath} with name #{data[:share_name]}"
end
rescue Error::SubprocessError => e
@env[:ui].info e.message
end
end
end
def ssh_info
@ssh_info || @env[:machine].ssh_info
end
def mount_shared_folders_to_windows
result = @env[:machine].provider.driver.execute('host_info.ps1', {})
@smb_shared_folders.each do |id, data|
begin
options = { :share_name => data[:share_name],
:guest_path => data[:guestpath].gsub("/", "\\"),
:guest_ip => ssh_info[:host],
:username => ssh_info[:username],
:host_ip => result["host_ip"],
:password => @env[:machine].provider_config.guest.password,
:host_share_username => @env[:machine].provider_config.host_share.username,
:host_share_password => @env[:machine].provider_config.host_share.password}
@env[:ui].info("Linking #{data[:share_name]} to Guest at #{data[:guestpath]} ...")
@env[:machine].provider.driver.execute('mount_share.ps1', options)
rescue Error::SubprocessError => e
@env[:ui].info "Failed to link #{data[:share_name]} to Guest"
@env[:ui].info e.message
end
end
end
def mount_shared_folders_to_linux
# Find Host Machine's credentials
result = @env[:machine].provider.driver.execute('host_info.ps1', {})
host_share_username = @env[:machine].provider_config.host_share.username
host_share_password = @env[:machine].provider_config.host_share.password
@smb_shared_folders.each do |id, data|
begin
# Mount the Network drive to Guest VM
@env[:ui].info("Linking #{data[:share_name]} to Guest at #{data[:guestpath]} ...")
# Create a location in guest to guestpath
@env[:machine].communicate.sudo("mkdir -p #{data[:guestpath]}")
owner = data[:owner] || ssh_info[:username]
group = data[:group] || ssh_info[:username]
mount_options = "-o rw,username=#{host_share_username},pass=#{host_share_password},"
mount_options += "sec=ntlm,file_mode=0777,dir_mode=0777,"
mount_options += "uid=`id -u #{owner}`,gid=`id -g #{group}`,rw #{data[:guestpath]}"
command = "mount -t cifs //#{result["host_ip"]}/#{data[:share_name]} #{mount_options}"
@env[:machine].communicate.sudo(command)
rescue RuntimeError => e
@env[:ui].error(e.message)
end
end
end
end
end
end
end

View File

@ -1,25 +0,0 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
# Include the following modules
$presentDir = Split-Path -parent $PSCommandPath
$modules = @()
$modules += $presentDir + "\utils\write_messages.ps1"
forEach ($module in $modules) { . $module }
try {
$hostname = $(whoami)
$ip = (Get-WmiObject -class win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').ipaddress[0]
$resultHash = @{
host_name = "$username"
host_ip = "$ip"
}
$result = ConvertTo-Json $resultHash
Write-Output-Message $result
}
catch {
Write-Error-Message $_
}

View File

@ -1,30 +0,0 @@
Param(
[Parameter(Mandatory=$true)]
[string]$path,
[Parameter(Mandatory=$true)]
[string]$share_name,
[Parameter(Mandatory=$true)]
[string]$host_share_username
)
$ErrorAction = "Stop"
# See all available shares and check alert user for
# existing/conflicting share name
$shared_folders = net share
$reg = "$share_name(\s+)$path(\s)"
$existing_share = $shared_folders -Match $reg
if ($existing_share) {
# Always clear the existing share name and create a new one
net share $share_name /delete /y
}
$computer_name = $(Get-WmiObject Win32_Computersystem).name
$grant_permission = "$computer_name\$host_share_username,Full"
$result = net share $share_name=$path /unlimited /GRANT:$grant_permission
if ($result -Match "$share_name was shared successfully.") {
exit 0
}
$host.ui.WriteErrorLine("Error: $result")
exit 1

View File

@ -1,62 +1,56 @@
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Open Technologies, Inc.
# All Rights Reserved. Licensed under the MIT License.
#--------------------------------------------------------------------------
param (
[string]$share_name = $(throw "-share_name is required."),
[string]$guest_path = $(throw "-guest_path is required."),
[string]$guest_ip = $(throw "-guest_ip is required."),
[string]$username = $(throw "-username is required."),
[string]$password = $(throw "-password is required."),
[string]$host_ip = $(throw "-host_ip is required."),
[string]$host_share_username = $(throw "-host_share_username is required."),
[string]$host_share_password = $(throw "-host_share_password is required.")
)
# Include the following modules
$presentDir = Split-Path -parent $PSCommandPath
$modules = @()
$modules += $presentDir + "\utils\create_session.ps1"
$modules += $presentDir + "\utils\write_messages.ps1"
forEach ($module in $modules) { . $module }
try {
function Mount-File($share_name, $guest_path, $host_path, $host_share_username, $host_share_password) {
try {
# TODO: Check for folder exist.
# Use net use and prompt for password
$guest_path = $guest_path.replace("/", "\")
# Map a network drive to the guest machine
$result = net use * $host_path /user:$host_share_username $host_share_password /persistent:yes
$mapped_drive = (($result -match "\w:") -split (" "))[1]
Write-Host cmd /c mklink /d $guest_path $mapped_drive
# If a folder exist remove it.
if (Test-Path $guest_path) {
$junction = Get-Item $guest_path
$junction.Delete()
}
cmd /c mklink /d $guest_path $mapped_drive
} catch {
return $_
}
}
$response = Create-Remote-Session $guest_ip $username $password
if (!$response["session"] -and $response["error"]) {
Write-Error-Message $response["error"]
return
}
$host_path = "\\$host_ip\$share_name"
$host_share_username = "$host_ip\$host_share_username"
$result = Invoke-Command -Session $response["session"] -ScriptBlock ${function:Mount-File} -ArgumentList $share_name, $guest_path, $host_path, $host_share_username, $host_share_password -ErrorAction "stop"
Remove-PSSession -Id $response["session"].Id
Write-Error-Message $result
}
catch {
Write-Error-Message "Failed to mount files VM $_"
return
}
param (
[string]$share_name = $(throw "-share_name is required."),
[string]$guest_path = $(throw "-guest_path is required."),
[string]$guest_ip = $(throw "-guest_ip is required."),
[string]$username = $(throw "-username is required."),
[string]$password = $(throw "-password is required."),
[string]$host_ip = $(throw "-host_ip is required."),
[string]$host_share_username = $(throw "-host_share_username is required."),
[string]$host_share_password = $(throw "-host_share_password is required.")
)
# Include the following modules
$presentDir = Split-Path -parent $PSCommandPath
$modules = @()
$modules += $presentDir + "\utils\create_session.ps1"
$modules += $presentDir + "\utils\write_messages.ps1"
forEach ($module in $modules) { . $module }
try {
function Mount-File($share_name, $guest_path, $host_path, $host_share_username, $host_share_password) {
try {
# TODO: Check for folder exist.
# Use net use and prompt for password
$guest_path = $guest_path.replace("/", "\")
# Map a network drive to the guest machine
$result = net use * $host_path /user:$host_share_username $host_share_password /persistent:yes
$mapped_drive = (($result -match "\w:") -split (" "))[1]
Write-Host cmd /c mklink /d $guest_path $mapped_drive
# If a folder exist remove it.
if (Test-Path $guest_path) {
$junction = Get-Item $guest_path
$junction.Delete()
}
cmd /c mklink /d $guest_path $mapped_drive
} catch {
return $_
}
}
$response = Create-Remote-Session $guest_ip $username $password
if (!$response["session"] -and $response["error"]) {
Write-Error-Message $response["error"]
return
}
$host_path = "\\$host_ip\$share_name"
$host_share_username = "$host_ip\$host_share_username"
$result = Invoke-Command -Session $response["session"] -ScriptBlock ${function:Mount-File} -ArgumentList $share_name, $guest_path, $host_path, $host_share_username, $host_share_password -ErrorAction "stop"
Remove-PSSession -Id $response["session"].Id
Write-Error-Message $result
}
catch {
Write-Error-Message "Failed to mount files VM $_"
return
}

View File

@ -138,6 +138,29 @@ module VagrantPlugins
JSON.parse(r.stdout)["ip_addresses"]
end
=begin
def mount_shared_folders_to_windows
result = @env[:machine].provider.driver.execute('host_info.ps1', {})
@smb_shared_folders.each do |id, data|
begin
options = { :share_name => data[:share_name],
:guest_path => data[:guestpath].gsub("/", "\\"),
:guest_ip => ssh_info[:host],
:username => ssh_info[:username],
:host_ip => result["host_ip"],
:password => @env[:machine].provider_config.guest.password,
:host_share_username => @env[:machine].provider_config.host_share.username,
:host_share_password => @env[:machine].provider_config.host_share.password}
@env[:ui].info("Linking #{data[:share_name]} to Guest at #{data[:guestpath]} ...")
@env[:machine].provider.driver.execute('mount_share.ps1', options)
rescue Error::SubprocessError => e
@env[:ui].info "Failed to link #{data[:share_name]} to Guest"
@env[:ui].info e.message
end
end
end
=end
end
end
end