Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix the windows hyperv smb synced folder creation/destruction #12933

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions plugins/hosts/windows/cap/smb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ def self.smb_cleanup(env, machine, opts)
@@logger.debug("shares to be removed: #{prune_shares}")

if prune_shares.size > 0
machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.prune_warning") + "\n")
sleep UAC_PROMPT_WAIT
@@logger.info("remove shares: #{prune_shares}")
result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: true)
result = Vagrant::Util::PowerShell.execute(script_path, *prune_shares, sudo: false)
if result.exit_code != 0
failed_name = result.stdout.to_s.sub("share name: ", "")
raise SyncedFolderSMB::Errors::PruneShareFailed,
Expand Down Expand Up @@ -100,14 +98,8 @@ def self.smb_prepare(env, machine, folders, opts)
]
end
if !shares.empty?
uac_notified = false
shares.each_slice(10) do |s_shares|
if !uac_notified
machine.env.ui.warn("\n" + I18n.t("vagrant_sf_smb.uac.create_warning") + "\n")
uac_notified = true
sleep(UAC_PROMPT_WAIT)
end
result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: true)
result = Vagrant::Util::PowerShell.execute(script_path, *s_shares, sudo: false)
if result.exit_code != 0
share_path = result.stdout.to_s.sub("share path: ", "")
raise SyncedFolderSMB::Errors::DefineShareFailed,
Expand Down
29 changes: 14 additions & 15 deletions plugins/hosts/windows/scripts/set_share.ps1
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

# The names of the user are language dependent!
$objSID = New-Object System.Security.Principal.SecurityIdentifier("S-1-1-0")
$objUser = $objSID.Translate([System.Security.Principal.NTAccount])

$grant = "$objUser,Full"

for ($i=0; $i -le $args.length; $i = $i + 3) {
for ($i = 0; ($i+2) -lt $args.length; $i = $i + 3) {
$path = $args[$i]
$share_name = $args[$i+1]
$share_id = $args[$i+2]


if ($path -eq $null) {
Write-Warning "empty path argument encountered - complete"
exit 0
if (!$path) {
Write-Error "error - no share path provided"
exit 1
}

if ($share_name -eq $null) {
if (!$share_name) {
Write-Output "share path: ${path}"
Write-Error "error - no share name provided"
exit 1
}

if ($share_id -eq $null) {
if (!$share_id) {
Write-Output "share path: ${path}"
Write-Error "error - no share ID provided"
exit 1
}

$result = net share $share_id=$path /unlimited /GRANT:$grant /REMARK:"${share_name}"
if ($LastExitCode -ne 0) {
$host.ui.WriteLine("share path: ${path}")
$host.ui.WriteErrorLine("error ${result}")
exit 1
}
New-SmbShare `
-Name $share_id `
-Path $path `
-FullAccess $objUser `
-Description $share_name
}
exit 0
12 changes: 6 additions & 6 deletions plugins/hosts/windows/scripts/unset_share.ps1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

ForEach ($share_name in $args) {
$result = net share $share_name /DELETE /YES
if ($LastExitCode -ne 0) {
Write-Output "share name: ${share_name}"
Write-Error "error - ${result}"
exit 1
}
Remove-SmbShare `
-Name $share_name `
-Force
}
Write-Output "share removal completed"
exit 0
7 changes: 0 additions & 7 deletions templates/locales/synced_folder_smb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ en:
incorrect_credentials: |-
Credentials incorrect. Please try again.

uac:
prune_warning: |-
Vagrant requires administrator access for pruning SMB shares and
may request access to complete removal of stale shares.
create_warning: |-
Vagrant requires administrator access to create SMB shares and
may request access to complete setup of configured shares.
errors:
not_supported: |-
It appears your machine doesn't support SMB, has not been
Expand Down
17 changes: 3 additions & 14 deletions test/unit/plugins/hosts/windows/cap/smb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@
end
after{ subject.smb_cleanup(env, machine, options) }

it "should pause after warning user" do
expect(machine.env.ui).to receive(:warn).and_call_original
expect(subject).to receive(:sleep)
end

it "should remove owned shares" do
expect(Vagrant::Util::PowerShell).to receive(:execute) do |*args|
expect(args).to include("vgt-CUSTOM_ID-1")
Expand All @@ -122,7 +117,7 @@
end

it "should remove all shares in single call" do
expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once
expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: false).once
end

context "when no shares are defined" do
Expand Down Expand Up @@ -182,14 +177,8 @@
expect(folders["/second/path"][:smb_id]).to eq("ID1")
end

it "should pause after warning user" do
expect(machine.env.ui).to receive(:warn).and_call_original
expect(subject).to receive(:sleep)
subject.smb_prepare(env, machine, folders, options)
end

it "should add all shares in single call" do
expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: true).once
expect(Vagrant::Util::PowerShell).to receive(:execute).with(any_args, sudo: false).once
subject.smb_prepare(env, machine, folders, options)
end

Expand Down Expand Up @@ -236,7 +225,7 @@
after{ subject.smb_prepare(env, machine, folders, options) }

it "should execute multiple powershell commands" do
expect(Vagrant::Util::PowerShell).to receive(:execute).twice.with(any_args, sudo: true)
expect(Vagrant::Util::PowerShell).to receive(:execute).twice.with(any_args, sudo: false)
end
end
end
Expand Down