Skip to content

Instantly share code, notes, and snippets.

@psamit
Forked from mefellows/Vagrantfile
Created November 5, 2016 11:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psamit/4841e6cc070df727b99d99c2ab8ac60b to your computer and use it in GitHub Desktop.
Save psamit/4841e6cc070df727b99d99c2ab8ac60b to your computer and use it in GitHub Desktop.
Example Vagrant Windows SMB Setup
$username = $args[0]
$password = $args[1]
$domain = 'DOMAIN' # Again, update DOMAIN with your company's Domain
Add-Type -AssemblyName System.DirectoryServices.AccountManagement
$ct = [System.DirectoryServices.AccountManagement.ContextType]::Domain
$pc = New-Object System.DirectoryServices.AccountManagement.PrincipalContext $ct,$domain
$valid = $pc.ValidateCredentials("${username}", "${password}")
write-host "Authenticating ${username} against AD..."
if ( $valid -ne $true ) { throw "Invalid $domain Username\\Password" } else { Write-Output "success!" }
exit 0
# Creates a network share from the current working directory
# with the name 'vagrantworkspace'. Also sets up permissions
# such that common user accounts can access it over the network
# so that IIS running inside a Vagrant VM can access it.
#
# This is mainly a workaround for Virtualbox shared folders
# which do not properly work. DO NOT point IIS at Virtualbox
# shares as it may look to be working but they do not. You've
# been warned.
Write-Output "Setting up a network share on local machine ${env:COMPUTERNAME}"
$sharePath = $args[0]
$shareName = $args[1]
$share = "\\${env:COMPUTERNAME}\$shareName"
$shares=[WMICLASS]"WIN32_Share"
Write-Output "Setting up share for local development"
if (net share | Select-String $shareName) {
net share $shareName /delete /y
}
$result = net share vworkspace=$PWD "/grant:Everyone,FULL" /unlimited
Write-Output "Created new share ${share} on folder ${sharePath}"
exit 0
VAGRANTFILE_API_VERSION = "2"
require 'io/console'
# Capture login details if starting up vagrant or provisioning it
# Required for AD operations.
#
# Environment variables prevent explicit user input. Useful for CI.
username = ENV["VAGRANT_USER"] || nil
password = ENV["VAGRANT_PASSWORD"] || nil
# Used to generate a unique VM name registered to AD
computerName, shareComputerName =
if ENV["VAGRANT_COMPUTERNAME"]
[ENV["VAGRANT_COMPUTERNAME"], ENV["COMPUTERNAME"]]
elsif ENV["COMPUTERNAME"]
[ENV["COMPUTERNAME"].sub(/^D/, 'V'), ENV["COMPUTERNAME"]]
else
['v' + `hostname`.sub(".yourdomain.int", "").strip, `hostname`.strip]
end
# Setup local code shares for Vagrant
# NOTE: this feature will be available natively in Vagrant 1.7
sharePath = File.dirname(__FILE__)
shareName = 'vworkspace'
shareGuestPath = 'c:\vworkspace'
# Collect credentials from the user (unless specified through ENV vars)
# Only request creds for provisioning type operations
# NOTE: Replace DOMAIN with your company domain
if ARGV[0] != nil && ['up', 'provision', 'reload'].include?(ARGV[0])
if !username
print "Please enter your DOMAIN username: "
username = "DOMAIN\\" + STDIN.gets.chomp
end
if !password
print "Please enter your password (input is hidden): "
password = STDIN.noecho(&:gets).chomp
end
puts ""
# Let's fail early and loud if the users'
# credentials supplied are not actually valid.
# Sorry Mac\Linux users - this only works on Windows!
share = false
message = "Unable to create share. Skipping."
if Gem.win_platform?
system("powershell.exe ./.box/authenticate.ps1 #{username} #{password}")
if !$?.success?
puts "Authentication failed, please try again."
exit 1
end
# Create Local Share
share = system("powershell.exe ./.box/setup-share.ps1 #{sharePath} #{shareName}")
elsif RUBY_PLATFORM.match("darwin")
share = system("sh ./.box/setup-share-mac.sh")
else
message = "Sorry, we haven't created automatic SMB sharing support for your platform yet."
share = false
end
if !share
puts "#{message}"
end
end
# Actual Vagrantfile.
#
# Vagrant is responsible for running your infrastructure!
#
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "dev-base-v3"
guest_port = 5895
config.vm.network :forwarded_port, guest: 3389, host: 3299, id: "rdp", auto_correct: false
config.vm.network :forwarded_port, guest: 5985, host: guest_port, id: "winrm", auto_correct: false
config.vm.network :forwarded_port, guest: 80, host: 8001, id: "web", auto_correct: false
config.vm.network :forwarded_port, guest: 443, host: 8444, id: "ssl", auto_correct: false
config.winrm.guest_port = guest_port
config.winrm.host = "localhost"
config.vm.host_name = computerName
config.vm.synced_folder ".", "/vagrant"
# This creates network access into the guest from the host.
#
# If you prefer to manualy set the IP of the guest,
# this is how you do it:
#
# config.vm.network "private_network", ip: "192.168.3.1"
config.vm.network "private_network", type: "dhcp"
config.vm.provider "virtualbox" do |v|
v.gui = true
end
# Connect to the created network share each time the box is started
$setup_share = <<SCRIPT
$secpasswd = ConvertTo-SecureString "#{password}" -AsPlainText -Force
$DOMAIN_CREDENTIALS = New-Object System.Management.Automation.PSCredential ("#{username}", $secpasswd)
$env:DOMAIN_COMPUTERNAME="#{computerName}"
$env:DOMAIN_USERNAME="#{username}"
$host_path = "\\\\#{shareComputerName}\\#{shareName}"
net use * $host_path /user:#{username} #{password} /persistent:yes
SCRIPT
# The main provisioning script
$do_something = <<SCRIPT
# ...
SCRIPT
config.vm.provision "shell", inline: $setup_share, run: always
config.vm.provision "shell", inline: $do_something
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment