Skip to content
This repository has been archived by the owner on Dec 1, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/v0.12.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Makarov committed May 7, 2015
2 parents 98af7b8 + 4b09a85 commit 8c5ce7d
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 65 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 0.12.0 (2015-05-07)

- Use our own vagrant box - [blinkreaction/boot2docker](https://vagrantcloud.com/blinkreaction/boxes/boot2docker)
- Updated Docker to v1.6.0
- Updated Docker Compose to v1.2.0
- [SMB2 (experimental)] sync folder option for Windows - complete automation of SMB sharing setup.
- Automatically start containers if docker-compose.yml is present in the Vagrantfile directory (single project mode)
- Miscellaneous code cleanup

## 0.11.1 (2015-04-09)

- Hotfix: added check for empty hosts in vagrant.yml
Expand Down
33 changes: 12 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ The best balance between performance and convenience can be achieved with NFS on
Additional steps are required to get SMB or rsync to work on Windows. [See below](#synced-folders-win).
In addition to the stock SMB synced folders option this box provides an experimental one: [SMB2](#synced-folders-smb2).
With the **SMB2** option you will receive several "elevated command prompt" prompts which you accept.
No need to enter usernames and passwords unlike the stock SMB option Vagrant ships with.
<a name="synced-folders-mac"></a>
### Mac
Expand Down Expand Up @@ -87,6 +91,14 @@ To use the SMB synced folder type:
While using SMB you have to control Vagrant from an elevated (run as admin) Git Bash shell.
<a name="synced-folders-smb2"></a>
**SMB2 (experimental option)**
This is an experimental option.
Compared to **SMB**, **SMB2** does not require running vagrant as admin and does not prompt for username and password.
You will receive several "elevated command prompt" prompts which you accept.
Vagrant will automatically create a user, set correct file permissions, create the SMB share, and mount it.
**Enabling rsync**
rsync is not natively available on Windows.
Expand All @@ -100,27 +112,6 @@ To use rsync on Windows:
3. Provide an explicit list of folders to sync in the `vagrant.yml` file (`folders` sequence).
4. Reload the VM: `vagrant reload`
**SMB2 (experimental option)**
This is an experimental option.
Compared to `smb`, `smb2` does not require running vagrant as admin, but requires initial manual setup:
1. Create a Windows user with a password (e.g. `vagrant:<password>`)
2. Share the `<Projects>` directory.
> The share name has to match the directory name.
> E.g. share `C:\Work\Projects` as `Projects`
3. Give the user created in step 1 full access to the share.
4. Update `vagrant.yml`:
> ...
> type: 'smb2'
> ...
> smb_username: '<username>'
> smb_password: '<password>'
> ...
5. Reload the VM (`vagrant reload`)
<a name="vm-settings"></a>
## VirtualBox VM settings
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.1
0.12.0
159 changes: 119 additions & 40 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,76 @@
# UI Object for console interactions.
@ui = Vagrant::UI::Colored.new

# Determine paths
# Install required plugins if not present.
required_plugins = %w(vagrant-triggers)
required_plugins.each do |plugin|
need_restart = false
unless Vagrant.has_plugin? plugin
system "vagrant plugin install #{plugin}"
need_restart = true
end
exec "vagrant #{ARGV.join(' ')}" if need_restart
end

# Determine paths.
vagrant_root = File.dirname(__FILE__) # Vagrantfile location
vagrant_mount = vagrant_root.gsub(/[a-zA-Z]:/, '') # Trim Windows drive letters
vagrant_folder = File.basename(vagrant_root) # Folder name only. Used as the SMB share name
vagrant_mount_point = vagrant_root.gsub(/[a-zA-Z]:/, '') # Trim Windows drive letters.
vagrant_folder_name = File.basename(vagrant_root) # Folder name only. Used as the SMB share name.

# Use vagrant.yml for local VM configuration overrides.
require 'yaml'
if !File.exist?(vagrant_root + '/vagrant.yml')
raise 'Configuration file not found! Please copy vagrant.yml.dist to vagrant.yml and try again.'
@ui.error 'Configuration file not found! Please copy vagrant.yml.dist to vagrant.yml and try again.'
exit
end
vconfig = YAML::load_file(vagrant_root + '/vagrant.yml')
$vconfig = YAML::load_file(vagrant_root + '/vagrant.yml')

# Determine if we are on Windows host or not
# Determine if we are on Windows host or not.
is_windows = Vagrant::Util::Platform.windows?
if is_windows
require 'win32ole'
# Determine if Vagrant was launched from the elevated command prompt
# Determine if Vagrant was launched from the elevated command prompt.
running_as_admin = ((`reg query HKU\\S-1-5-19 2>&1` =~ /ERROR/).nil? && is_windows)

# Method to create a network share on Windows using elevated command prompt
def windows_net_share(share, path)
# Run command in an elevated shell.
def windows_elevated_shell(args)
command = 'cmd.exe'
args = "/C net share #{share}=#{path} /grant:everyone,FULL || timeout 5"
puts args
args = "/C #{args} || timeout 10"
shell = WIN32OLE.new('Shell.Application')
shell.ShellExecute(command, args, nil, 'runas')
end

# Method to create the user and SMB network share on Windows.
def windows_net_share(share_name, path)
# Add the vagrant user if it does not exist.
smb_username = $vconfig['synced_folders']['smb_username']
smb_password = $vconfig['synced_folders']['smb_password']

command_user = "net user #{smb_username} || net user #{smb_username} #{smb_password} /add"
@ui.info "Adding vagrant user"
windows_elevated_shell command_user

# Add the SMB share if it does not exist.
command_share = "net share #{share_name} || net share #{share_name}=#{path} /grant:#{smb_username},FULL"
@ui.info "Adding vagrant SMB share"
windows_elevated_shell command_share

# Set folder permissions.
command_permissions = "icacls #{path} /grant #{smb_username}:(OI)(CI)M"
@ui.info "Setting folder permissions"
windows_elevated_shell command_permissions
end

# Method to remove the user and SMB network share on Windows.
def windows_net_share_remove(share_name)
smb_username = $vconfig['synced_folders']['smb_username']

command_user = "net user #{smb_username} /delete || echo 'User #{smb_username} does not exist' && timeout 10"
windows_elevated_shell command_user

command_share = "net share #{share_name} /delete || echo 'Share #{share_name} does not exist' && timeout 10"
windows_elevated_shell command_share
end
else
# Determine if Vagrant was launched with sudo (as root).
running_as_root = (Process.uid == 0)
Expand All @@ -34,7 +79,8 @@ end
# Vagrant should NOT be run as root/admin.
if running_as_root
# || running_as_admin
raise "Vagrant should be run as a regular user to avoid issues."
@ui.error "Vagrant should be run as a regular user to avoid issues."
exit
end

######################################################################
Expand All @@ -45,63 +91,74 @@ Vagrant.require_version ">= 1.6.3"
Vagrant.configure("2") do |config|
config.vm.define "boot2docker"

config.vm.box = "dduportal/boot2docker"
config.vm.box = "blinkreaction/boot2docker"
config.vm.box_version = "1.6.0"
config.vm.box_check_update = false

## Network ##

# The default box private network IP is 192.168.10.10
# Configure additional IP addresses in vagrant.yml
vconfig['hosts'].each do |host|
$vconfig['hosts'].each do |host|
config.vm.network "private_network", ip: host['ip']
end unless vconfig['hosts'].nil?
end unless $vconfig['hosts'].nil?

####################################################################
## Synced folders configuration ##

synced_folders = vconfig['synced_folders']
synced_folders = $vconfig['synced_folders']
# nfs: better performance on Mac
if synced_folders['type'] == "nfs" && !is_windows
config.vm.synced_folder vagrant_root, vagrant_mount,
config.vm.synced_folder vagrant_root, vagrant_mount_point,
type: "nfs",
mount_options: ["nolock", "vers=3", "tcp"]
config.nfs.map_uid = Process.uid
config.nfs.map_gid = Process.gid
# smb: better performance on Windows. Requires Vagrant to be run with admin privileges.
elsif synced_folders['type'] == "smb" && is_windows
config.vm.synced_folder vagrant_root, vagrant_mount,
config.vm.synced_folder vagrant_root, vagrant_mount_point,
type: "smb",
smb_username: synced_folders['smb_username'],
smb_password: synced_folders['smb_password']
# smb2: experimental, does not require running vagrant as admin, requires initial manual setup.
# smb2: experimental, does not require running vagrant as admin.
elsif synced_folders['type'] == "smb2" && is_windows
# Create the share on the Windows host
#windows_net_share vagrant_share, vagrant_root
#Mount the share in boot2docker
# Create the share before 'up'.
config.trigger.before :up, :stdout => true, :force => true do
info 'Setting up SMB user and share'
windows_net_share vagrant_folder_name, vagrant_root
end

# Remove the share after 'halt'.
config.trigger.after :destroy, :stdout => true, :force => true do
info 'Removing SMB user and share'
windows_net_share_remove vagrant_folder_name
end

# Mount the share in boot2docker.
config.vm.provision "shell", run: "always" do |s|
s.inline = <<-SCRIPT
mkdir -p vagrant $2
mount -t cifs -o uid=`id -u docker`,gid=`id -g docker`,sec=ntlm,username=$3,pass=$4 //192.168.10.1/$1 $2
SCRIPT
s.args = "#{vagrant_folder} #{vagrant_mount} #{vconfig['synced_folders']['smb_username']} #{vconfig['synced_folders']['smb_password']}"
s.args = "#{vagrant_folder_name} #{vagrant_mount_point} #{$vconfig['synced_folders']['smb_username']} #{$vconfig['synced_folders']['smb_password']}"
end
# rsync: the best performance, cross-platform platform, one-way only. Run `vagrant rsync-auto` to start auto sync.
elsif synced_folders['type'] == "rsync"
# Only sync explicitly listed folders.
if (synced_folders['folders']).nil?
puts "WARNING: 'folders' list cannot be empty when using 'rsync' sync type. Please check your vagrant.yml file."
@ui.warn "WARNING: 'folders' list cannot be empty when using 'rsync' sync type. Please check your vagrant.yml file."
else
for synced_folder in synced_folders['folders'] do
config.vm.synced_folder "#{vagrant_root}/#{synced_folder}", "#{vagrant_mount}/#{synced_folder}",
config.vm.synced_folder "#{vagrant_root}/#{synced_folder}", "#{vagrant_mount_point}/#{synced_folder}",
type: "rsync",
rsync__exclude: ".git/",
rsync__args: ["--verbose", "--archive", "--delete", "-z", "--chmod=ugo=rwX"]
end
end
# vboxfs: reliable, cross-platform and terribly slow performance
else
puts "WARNING: defaulting to the slowest sync option (vboxfs)"
config.vm.synced_folder vagrant_root, vagrant_mount
@ui.warn "WARNING: defaulting to the slowest folder sync option (vboxfs)"
config.vm.synced_folder vagrant_root, vagrant_mount_point
end

# Make host SSH keys available to containers on /.ssh
Expand All @@ -111,18 +168,18 @@ Vagrant.configure("2") do |config|

######################################################################

## VirtualBox VM settings
## VirtualBox VM settings.

config.vm.provider "virtualbox" do |v|
v.gui = vconfig['v.gui'] # Set to true for debugging. Will unhide VM's primary console screen.
v.name = vagrant_folder + "_boot2docker" # VirtualBox VM name
v.cpus = vconfig['v.cpus'] # CPU settings. VirtualBox works much better with a single CPU.
v.memory = vconfig['v.memory'] # Memory settings.
v.gui = $vconfig['v.gui'] # Set to true for debugging. Will unhide VM's primary console screen.
v.name = vagrant_folder_name + "_boot2docker" # VirtualBox VM name.
v.cpus = $vconfig['v.cpus'] # CPU settings. VirtualBox works much better with a single CPU.
v.memory = $vconfig['v.memory'] # Memory settings.
end

## Provisioning scripts ##

# Allow Mac OS X docker client to connect to Docker without TLS auth
# Allow Mac OS X docker client to connect to Docker without TLS auth.
# https://github.com/deis/deis/issues/2230#issuecomment-72701992
config.vm.provision "shell" do |s|
s.inline = <<-SCRIPT
Expand All @@ -135,10 +192,20 @@ Vagrant.configure("2") do |config|
# https://github.com/docker/compose/issues/598#issuecomment-67762456
config.vm.provision "shell", run: "always" do |s|
s.inline = <<-SCRIPT
echo 'docker run --rm -it \
-v $(pwd):$(pwd) -v /var/run/docker.sock:/var/run/docker.sock \
-e COMPOSE_PROJECT_NAME=$(basename $(pwd)) -w="$(pwd)" \
blinkreaction/docker-compose $*' > /usr/local/bin/docker-compose
DC_SCRIPT='
#/bin/sh
# Check if we are in an interactive shell and use "-it" flags if so.
interactive=$([ -t 0 ] && echo "-it")
# Run docker-compose in a container
docker run --rm $interactive \
-v $(pwd):$(pwd) -v /var/run/docker.sock:/var/run/docker.sock \
-e COMPOSE_PROJECT_NAME=$(basename $(pwd)) -w="$(pwd)" \
blinkreaction/docker-compose:1.2.0 $*
'
echo "$DC_SCRIPT" > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
echo 'alias fig=docker-compose' >> /home/docker/.ashrc
SCRIPT
Expand All @@ -150,10 +217,10 @@ Vagrant.configure("2") do |config|
echo "export VAGRANT_ROOT=$1" >> /home/docker/.profile
echo "cd $1" >> /home/docker/.ashrc
SCRIPT
s.args = "#{vagrant_mount}"
s.args = "#{vagrant_mount_point}"
end

# dsh script lookup wrapper (Drude Shell)
# dsh script lookup wrapper (Drude Shell).
# https://github.com/blinkreaction/drude
config.vm.provision "shell", run: "always" do |s|
s.inline = <<-SCRIPT
Expand Down Expand Up @@ -184,4 +251,16 @@ Vagrant.configure("2") do |config|
SCRIPT
end

# Automatically start containers if docker-compose.yml is present in the current directory.
# See "autostart" property in vagrant.yml.
if File.file?('./docker-compose.yml') && $vconfig['compose_autostart']
config.vm.provision "shell", run: "always", privileged: false do |s|
s.inline = <<-SCRIPT
cd $1
docker-compose up -d
SCRIPT
s.args = "#{vagrant_mount_point}"
end
end

end
10 changes: 7 additions & 3 deletions vagrant.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
synced_folders:
# nfs: better performance on Mac, recommended.
# smb: better performance on Windows. Requires Vagrant to be run with admin privileges.
# smb2: experimental, does not require running vagrant as admin, requires initial manual setup.
# smb2: experimental, does not require running vagrant as admin.
# rsync: the best performance, cross-platform platform, one-way only. Run `vagrant rsync-auto` to start auto sync.
# When using rsync sync type the "folders" list below is mandatory.
# vboxfs (or leave empty): best compatibility and ease of setup, but poor performance.
type: 'nfs'
# smb_user, smb_password - The username and password used for authentication to mount the SMB mount.
# This is usually your Windows username and password, unless you created a dedicated user for vagrant.
smb_username: ''
smb_password: ''
# If using the 'smb2' type above the user and share will be configured automatically.
smb_username: 'vagrant'
smb_password: 'vagrant'
# List of folders to sync with rsync. These should be subfolder names within the <Projects> folder (e.g. "drupal7")
# Uncomment and add folders per the example below as neccessary.
folders:
Expand All @@ -31,3 +32,6 @@ hosts:
#- ip: 192.168.10.11
#- ip: 192.168.10.12
#- ip: 192.168.10.13

# Automatically start containers if docker-compose.yml is present in the current directory (default: false).
compose_autostart: false

0 comments on commit 8c5ce7d

Please sign in to comment.