1
+ # UI Object for console interactions.
2
+ @ui = Vagrant ::UI ::Colored . new
1
3
2
- # Determine paths
4
+ # Install required plugins if not present.
5
+ required_plugins = %w( vagrant-triggers )
6
+ required_plugins . each do |plugin |
7
+ need_restart = false
8
+ unless Vagrant . has_plugin? plugin
9
+ system "vagrant plugin install #{ plugin } "
10
+ need_restart = true
11
+ end
12
+ exec "vagrant #{ ARGV . join ( ' ' ) } " if need_restart
13
+ end
14
+
15
+ # Determine paths.
3
16
vagrant_root = File . dirname ( __FILE__ ) # Vagrantfile location
4
- vagrant_mount = vagrant_root . gsub ( /[a-zA-Z]:/ , '' ) # Trim Windows drive letters
5
- vagrant_folder = File . basename ( vagrant_root ) # Folder name only. Used as the SMB share name
17
+ vagrant_mount_point = vagrant_root . gsub ( /[a-zA-Z]:/ , '' ) # Trim Windows drive letters.
18
+ vagrant_folder_name = File . basename ( vagrant_root ) # Folder name only. Used as the SMB share name.
6
19
7
20
# Use vagrant.yml for local VM configuration overrides.
8
21
require 'yaml'
9
22
if !File . exist? ( vagrant_root + '/vagrant.yml' )
10
- raise 'Configuration file not found! Please copy vagrant.yml.dist to vagrant.yml and try again.'
23
+ @ui . error 'Configuration file not found! Please copy vagrant.yml.dist to vagrant.yml and try again.'
24
+ exit
11
25
end
12
- vconfig = YAML ::load_file ( vagrant_root + '/vagrant.yml' )
26
+ $ vconfig = YAML ::load_file ( vagrant_root + '/vagrant.yml' )
13
27
14
- # Determine if we are on Windows host or not
28
+ # Determine if we are on Windows host or not.
15
29
is_windows = Vagrant ::Util ::Platform . windows?
16
30
if is_windows
17
31
require 'win32ole'
18
- # Determine if Vagrant was launched from the elevated command prompt
32
+ # Determine if Vagrant was launched from the elevated command prompt.
19
33
running_as_admin = ( ( `reg query HKU\\ S-1-5-19 2>&1` =~ /ERROR/ ) . nil? && is_windows )
20
-
21
- # Method to create a network share on Windows using elevated command prompt
22
- def windows_net_share ( share , path )
34
+
35
+ # Run command in an elevated shell.
36
+ def windows_elevated_shell ( args )
23
37
command = 'cmd.exe'
24
- args = "/C net share #{ share } =#{ path } /grant:everyone,FULL || timeout 5"
25
- puts args
38
+ args = "/C #{ args } || timeout 10"
26
39
shell = WIN32OLE . new ( 'Shell.Application' )
27
40
shell . ShellExecute ( command , args , nil , 'runas' )
28
41
end
42
+
43
+ # Method to create the user and SMB network share on Windows.
44
+ def windows_net_share ( share_name , path )
45
+ # Add the vagrant user if it does not exist.
46
+ smb_username = $vconfig[ 'synced_folders' ] [ 'smb_username' ]
47
+ smb_password = $vconfig[ 'synced_folders' ] [ 'smb_password' ]
48
+
49
+ command_user = "net user #{ smb_username } || net user #{ smb_username } #{ smb_password } /add"
50
+ @ui . info "Adding vagrant user"
51
+ windows_elevated_shell command_user
52
+
53
+ # Add the SMB share if it does not exist.
54
+ command_share = "net share #{ share_name } || net share #{ share_name } =#{ path } /grant:#{ smb_username } ,FULL"
55
+ @ui . info "Adding vagrant SMB share"
56
+ windows_elevated_shell command_share
57
+
58
+ # Set folder permissions.
59
+ command_permissions = "icacls #{ path } /grant #{ smb_username } :(OI)(CI)M"
60
+ @ui . info "Setting folder permissions"
61
+ windows_elevated_shell command_permissions
62
+ end
63
+
64
+ # Method to remove the user and SMB network share on Windows.
65
+ def windows_net_share_remove ( share_name )
66
+ smb_username = $vconfig[ 'synced_folders' ] [ 'smb_username' ]
67
+
68
+ command_user = "net user #{ smb_username } /delete || echo 'User #{ smb_username } does not exist' && timeout 10"
69
+ windows_elevated_shell command_user
70
+
71
+ command_share = "net share #{ share_name } /delete || echo 'Share #{ share_name } does not exist' && timeout 10"
72
+ windows_elevated_shell command_share
73
+ end
29
74
else
30
75
# Determine if Vagrant was launched with sudo (as root).
31
76
running_as_root = ( Process . uid == 0 )
34
79
# Vagrant should NOT be run as root/admin.
35
80
if running_as_root
36
81
# || running_as_admin
37
- raise "Vagrant should be run as a regular user to avoid issues."
82
+ @ui . error "Vagrant should be run as a regular user to avoid issues."
83
+ exit
38
84
end
39
85
40
86
######################################################################
@@ -45,63 +91,74 @@ Vagrant.require_version ">= 1.6.3"
45
91
Vagrant . configure ( "2" ) do |config |
46
92
config . vm . define "boot2docker"
47
93
48
- config . vm . box = "dduportal/boot2docker"
94
+ config . vm . box = "blinkreaction/boot2docker"
95
+ config . vm . box_version = "1.6.0"
49
96
config . vm . box_check_update = false
50
97
51
98
## Network ##
52
99
53
100
# The default box private network IP is 192.168.10.10
54
101
# Configure additional IP addresses in vagrant.yml
55
- vconfig [ 'hosts' ] . each do |host |
102
+ $ vconfig[ 'hosts' ] . each do |host |
56
103
config . vm . network "private_network" , ip : host [ 'ip' ]
57
- end unless vconfig [ 'hosts' ] . nil?
104
+ end unless $ vconfig[ 'hosts' ] . nil?
58
105
59
106
####################################################################
60
107
## Synced folders configuration ##
61
108
62
- synced_folders = vconfig [ 'synced_folders' ]
109
+ synced_folders = $ vconfig[ 'synced_folders' ]
63
110
# nfs: better performance on Mac
64
111
if synced_folders [ 'type' ] == "nfs" && !is_windows
65
- config . vm . synced_folder vagrant_root , vagrant_mount ,
112
+ config . vm . synced_folder vagrant_root , vagrant_mount_point ,
66
113
type : "nfs" ,
67
114
mount_options : [ "nolock" , "vers=3" , "tcp" ]
68
115
config . nfs . map_uid = Process . uid
69
116
config . nfs . map_gid = Process . gid
70
117
# smb: better performance on Windows. Requires Vagrant to be run with admin privileges.
71
118
elsif synced_folders [ 'type' ] == "smb" && is_windows
72
- config . vm . synced_folder vagrant_root , vagrant_mount ,
119
+ config . vm . synced_folder vagrant_root , vagrant_mount_point ,
73
120
type : "smb" ,
74
121
smb_username : synced_folders [ 'smb_username' ] ,
75
122
smb_password : synced_folders [ 'smb_password' ]
76
- # smb2: experimental, does not require running vagrant as admin, requires initial manual setup .
123
+ # smb2: experimental, does not require running vagrant as admin.
77
124
elsif synced_folders [ 'type' ] == "smb2" && is_windows
78
- # Create the share on the Windows host
79
- #windows_net_share vagrant_share, vagrant_root
80
- #Mount the share in boot2docker
125
+ # Create the share before 'up'.
126
+ config . trigger . before :up , :stdout => true , :force => true do
127
+ info 'Setting up SMB user and share'
128
+ windows_net_share vagrant_folder_name , vagrant_root
129
+ end
130
+
131
+ # Remove the share after 'halt'.
132
+ config . trigger . after :destroy , :stdout => true , :force => true do
133
+ info 'Removing SMB user and share'
134
+ windows_net_share_remove vagrant_folder_name
135
+ end
136
+
137
+ # Mount the share in boot2docker.
81
138
config . vm . provision "shell" , run : "always" do |s |
82
139
s . inline = <<-SCRIPT
83
140
mkdir -p vagrant $2
84
141
mount -t cifs -o uid=`id -u docker`,gid=`id -g docker`,sec=ntlm,username=$3,pass=$4 //192.168.10.1/$1 $2
85
142
SCRIPT
86
- s . args = "#{ vagrant_folder } #{ vagrant_mount } #{ vconfig [ 'synced_folders' ] [ 'smb_username' ] } #{ vconfig [ 'synced_folders' ] [ 'smb_password' ] } "
143
+ s . args = "#{ vagrant_folder_name } #{ vagrant_mount_point } #{ $ vconfig[ 'synced_folders' ] [ 'smb_username' ] } #{ $ vconfig[ 'synced_folders' ] [ 'smb_password' ] } "
87
144
end
88
145
# rsync: the best performance, cross-platform platform, one-way only. Run `vagrant rsync-auto` to start auto sync.
89
146
elsif synced_folders [ 'type' ] == "rsync"
90
147
# Only sync explicitly listed folders.
91
148
if ( synced_folders [ 'folders' ] ) . nil?
92
- puts "WARNING: 'folders' list cannot be empty when using 'rsync' sync type. Please check your vagrant.yml file."
149
+ @ui . warn "WARNING: 'folders' list cannot be empty when using 'rsync' sync type. Please check your vagrant.yml file."
93
150
else
94
151
for synced_folder in synced_folders [ 'folders' ] do
95
- config . vm . synced_folder "#{ vagrant_root } /#{ synced_folder } " , "#{ vagrant_mount } /#{ synced_folder } " ,
152
+ config . vm . synced_folder "#{ vagrant_root } /#{ synced_folder } " , "#{ vagrant_mount_point } /#{ synced_folder } " ,
96
153
type : "rsync" ,
97
154
rsync__exclude : ".git/" ,
98
155
rsync__args : [ "--verbose" , "--archive" , "--delete" , "-z" , "--chmod=ugo=rwX" ]
99
156
end
100
157
end
101
158
# vboxfs: reliable, cross-platform and terribly slow performance
102
159
else
103
- puts "WARNING: defaulting to the slowest sync option (vboxfs)"
104
- config . vm . synced_folder vagrant_root , vagrant_mount
160
+ @ui . warn "WARNING: defaulting to the slowest folder sync option (vboxfs)"
161
+ config . vm . synced_folder vagrant_root , vagrant_mount_point
105
162
end
106
163
107
164
# Make host SSH keys available to containers on /.ssh
@@ -111,18 +168,18 @@ Vagrant.configure("2") do |config|
111
168
112
169
######################################################################
113
170
114
- ## VirtualBox VM settings
171
+ ## VirtualBox VM settings.
115
172
116
173
config . vm . provider "virtualbox" do |v |
117
- v . gui = vconfig [ 'v.gui' ] # Set to true for debugging. Will unhide VM's primary console screen.
118
- v . name = vagrant_folder + "_boot2docker" # VirtualBox VM name
119
- v . cpus = vconfig [ 'v.cpus' ] # CPU settings. VirtualBox works much better with a single CPU.
120
- v . memory = vconfig [ 'v.memory' ] # Memory settings.
174
+ v . gui = $ vconfig[ 'v.gui' ] # Set to true for debugging. Will unhide VM's primary console screen.
175
+ v . name = vagrant_folder_name + "_boot2docker" # VirtualBox VM name.
176
+ v . cpus = $ vconfig[ 'v.cpus' ] # CPU settings. VirtualBox works much better with a single CPU.
177
+ v . memory = $ vconfig[ 'v.memory' ] # Memory settings.
121
178
end
122
179
123
180
## Provisioning scripts ##
124
181
125
- # Allow Mac OS X docker client to connect to Docker without TLS auth
182
+ # Allow Mac OS X docker client to connect to Docker without TLS auth.
126
183
# https://github.com/deis/deis/issues/2230#issuecomment-72701992
127
184
config . vm . provision "shell" do |s |
128
185
s . inline = <<-SCRIPT
@@ -135,10 +192,20 @@ Vagrant.configure("2") do |config|
135
192
# https://github.com/docker/compose/issues/598#issuecomment-67762456
136
193
config . vm . provision "shell" , run : "always" do |s |
137
194
s . inline = <<-SCRIPT
138
- echo 'docker run --rm -it \
139
- -v $(pwd):$(pwd) -v /var/run/docker.sock:/var/run/docker.sock \
140
- -e COMPOSE_PROJECT_NAME=$(basename $(pwd)) -w="$(pwd)" \
141
- blinkreaction/docker-compose $*' > /usr/local/bin/docker-compose
195
+ DC_SCRIPT='
196
+ #/bin/sh
197
+
198
+ # Check if we are in an interactive shell and use "-it" flags if so.
199
+ interactive=$([ -t 0 ] && echo "-it")
200
+
201
+ # Run docker-compose in a container
202
+ docker run --rm $interactive \
203
+ -v $(pwd):$(pwd) -v /var/run/docker.sock:/var/run/docker.sock \
204
+ -e COMPOSE_PROJECT_NAME=$(basename $(pwd)) -w="$(pwd)" \
205
+ blinkreaction/docker-compose:1.2.0 $*
206
+ '
207
+
208
+ echo "$DC_SCRIPT" > /usr/local/bin/docker-compose
142
209
chmod +x /usr/local/bin/docker-compose
143
210
echo 'alias fig=docker-compose' >> /home/docker/.ashrc
144
211
SCRIPT
@@ -150,10 +217,10 @@ Vagrant.configure("2") do |config|
150
217
echo "export VAGRANT_ROOT=$1" >> /home/docker/.profile
151
218
echo "cd $1" >> /home/docker/.ashrc
152
219
SCRIPT
153
- s . args = "#{ vagrant_mount } "
220
+ s . args = "#{ vagrant_mount_point } "
154
221
end
155
222
156
- # dsh script lookup wrapper (Drude Shell)
223
+ # dsh script lookup wrapper (Drude Shell).
157
224
# https://github.com/blinkreaction/drude
158
225
config . vm . provision "shell" , run : "always" do |s |
159
226
s . inline = <<-SCRIPT
@@ -184,4 +251,16 @@ Vagrant.configure("2") do |config|
184
251
SCRIPT
185
252
end
186
253
254
+ # Automatically start containers if docker-compose.yml is present in the current directory.
255
+ # See "autostart" property in vagrant.yml.
256
+ if File . file? ( './docker-compose.yml' ) && $vconfig[ 'compose_autostart' ]
257
+ config . vm . provision "shell" , run : "always" , privileged : false do |s |
258
+ s . inline = <<-SCRIPT
259
+ cd $1
260
+ docker-compose up -d
261
+ SCRIPT
262
+ s . args = "#{ vagrant_mount_point } "
263
+ end
264
+ end
265
+
187
266
end
0 commit comments