Issue with vagrant while using vagrant up - laravel-5

on installing fresh vagrant instance, I am getting this error while using vagrant up. Here is the error
Vagrant failed to initialize at a very early stage:
There is a syntax error in the following Vagrantfile. The syntax error
message is reproduced below for convenience:
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:141: syntax error, unexpected tPOW
...ype: folder["type"] ||= nil, **options
... ^
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:157: syntax error, unexpected keyword_do_block, expecting keyword_end
settings["sites"].each do |site|
^
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:207: syntax error, unexpected keyword_do_block, expecting keyword_end
settings["databases"].each do |db|
^
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:208: syntax error, unexpected tSTRING_BEG, expecting keyword_end
config.vm.provision "shell" do |s|
^
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:208: syntax error, unexpected keyword_do_block, expecting keyword_end
config.vm.provision "shell" do |s|
^
/mnt/c/Users/bilal/Homestead/scripts/homestead.rb:220: syntax error, unexpected keyword_end, expecting $end
Here is my VagrantFile content
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'json'
require 'yaml'
VAGRANTFILE_API_VERSION ||= "2"
confDir = $confDir ||= File.expand_path("~/.homestead")
homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
aliasesPath = confDir + "/aliases"
require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')
Vagrant.require_version '>= 1.8.4'
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if File.exist? aliasesPath then
config.vm.provision "file", source: aliasesPath, destination: "~/.bash_aliases"
end
if File.exist? homesteadYamlPath then
settings = YAML::load(File.read(homesteadYamlPath))
elsif File.exist? homesteadJsonPath then
settings = JSON.parse(File.read(homesteadJsonPath))
end
Homestead.configure(config, settings)
if File.exist? afterScriptPath then
config.vm.provision "shell", path: afterScriptPath, privileged: false
end
if defined? VagrantPlugins::HostsUpdater
config.hostsupdater.aliases = settings['sites'].map { |site| site['map'] }
end
end
============================================================
And there is the YAML file
---
ip: "192.168.10.10"
memory: 2048
cpus: 1
provider: virtualbox
authorize: ~/.ssh/id_rsa.pub
keys:
- ~/.ssh/id_rsa
folders:
- map: ~/Code
to: /mnt/e/XAMPP/htdocs
sites:
- map: homestead.app
to: /mnt/e/XAMPP/htdocs/cap/web
databases:
- homestead

Solved that issue by upgrading Vagrant to 1.8.4.

Related

How to write to a file provisioning from Vagrantfile

Hi I'm trying to add a Directory Index directive to the default VirtualHost for Apache from the Vagrantfile. I'm wondering if there is a way to edit a file from the Vagrantfile (I'm usung inline SHELL). I know I could copy an entire VH file to the guest machine, but I want to know how to write into files if possible.
Thanks!
You can do that with ansible like this:
config.vm.provision "ansible_local" do |ansible|
ansible.verbose = "vv"
ansible.become = true # execute as root
ansible.playbook = "relative_path_to_ansible_file/playbook.yml"
end
or with a shell
Vagrant.configure("2") do |config|
config.vm.provision "shell" do |s|
s.inline = "echo $1"
s.args = "'hello, world!'"
end
end
https://www.vagrantup.com/docs/provisioning/shell.html

vagrant provision for dev and production

We started using Vagrant to setup our development environment.
Now we would like to use the same Vagrantfile also in production/staging.
When I use the same vagrantfile that contains a virtualbox provider and gce I get the error
An active machine was found with a different provider. Vagrant
currently allows each machine to be brought up with only a single
provider at a time. A future version will remove this limitation.
Until then, please destroy the existing machine to up with a new
provider.
Machine name: default
Active provider: virtualbox
Requested provider: google
Is there a way I can vagrant up virtualbox and gce?
Vagrant.has_plugin?("nugrant")
Vagrant.require_version ">= 1.6.3"
Vagrant.configure("2") do |config|
config.vm.provider "virtualbox"
config.vm.box = "yungsang/boot2docker"
config.vm.provider :google do |google, override|
override.vm.box = "gce"
google.google_project_id = $GOOGLE_PROJECT_ID
google.google_client_email = $GOOGLE_CLIENT_EMAIL
google.google_key_location = $GOOGLE_KEY_LOCATION
# Override provider defaults
google.name = "name"
google.image = "ubuntu-1404-trusty-v20141212"
google.machine_type = "n1-standard-1"
google.zone = "europe-west1-c"
google.metadata = {'custom' => 'metadata', 'production' => 'app'}
google.tags = ['vagrantbox', 'prod']
override.ssh.username = $LOCAL_USER
override.ssh.private_key_path = $LOCAL_SSH_KEY
config.vm.define :prod do |prod|
prod.vm.provision :shell, inline: 'echo I am executed in prod only!!'
end
end
config.vm.synced_folder ".", "/vagrant"
# Fix busybox/udhcpc issue
config.vm.provision :shell do |s|
s.inline = <<-EOT
if ! grep -qs ^nameserver /etc/resolv.conf; then
sudo /sbin/udhcpc
fi
cat /etc/resolv.conf
EOT
end
# Adjust datetime after suspend and resume
config.vm.provision :shell do |s|
s.inline = <<-EOT
sudo /usr/local/bin/ntpclient -s -h pool.ntp.org
date
EOT
end
# Login docker hub
config.vm.provision :shell do |s|
s.inline = "/usr/bin/docker $#"
s.args = ["login", "-u", config.user.docker.username, "-p", config.user.docker.password, "-e", config.user.docker.email]
end
config.vm.provision :docker do |d|
d.pull_images "nginx"
d.pull_images "mongodb"
d.pull_images "java"
end
ACTICE_SPRING_PROFILE = "te"
config.vm.provision :docker do |d|
# provision docker stuff here
end
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 443, host: 8443
end
Update:
I tried to solve the problem with a multi vm setup, but now I am facing the problem that vagrant uses an outside-in provisioning, so my GCE specific scripts like gce.vm.provision :shell, inline: 'curl -sSL https://get.docker.com/ubuntu/ | sudo sh' are executed at the end not at the beginning.
Vagrant.configure("2") do |config|
config.vm.define "dev", primary: true do |dev|
dev.vm.provider "virtualbox"
dev.vm.box = "yungsang/boot2docker"
dev.vm.synced_folder ".", "/vagrant"
override.vm.provision :shell, inline: 'echo "Provision DEV"'
# Fix busybox/udhcpc issue
dev.vm.provision :shell do |s|
s.inline = <<-EOT
if ! grep -qs ^nameserver /etc/resolv.conf; then
sudo /sbin/udhcpc
fi
cat /etc/resolv.conf
EOT
end
# Adjust datetime after suspend and resume
dev.vm.provision :shell do |s|
s.inline = <<-EOT
sudo /usr/local/bin/ntpclient -s -h pool.ntp.org
date
EOT
end
dev.vm.network "forwarded_port", guest: 80, host: 8080
dev.vm.network "forwarded_port", guest: 443, host: 8443
end
config.vm.define "gce", autostart: false do |gce|
gce.vm.provider :google do |google, override|
override.vm.box = "gce"
google.google_project_id = $GOOGLE_PROJECT_ID
google.google_client_email = $GOOGLE_CLIENT_EMAIL
google.google_key_location = $GOOGLE_KEY_LOCATION
# Override provider defaults
google.name = "z-rechnung-#{TARGET_ENV}"
google.image = "ubuntu-1404-trusty-v20141212"
google.machine_type = "n1-standard-1"
google.zone = "europe-west1-c"
google.metadata = {'environment' => "#{TARGET_ENV}"}
google.tags = ['vagrantbox', "#{TARGET_ENV}"]
override.ssh.username = $LOCAL_USER
override.ssh.private_key_path = $LOCAL_SSH_KEY
gce.vm.provision :shell, inline: 'sudo apt-get update -y'
gce.vm.provision :shell, inline: 'sudo apt-get upgrade -y'
gce.vm.provision :shell, inline: 'curl -sSL https://get.docker.com/ubuntu/ | sudo sh'
end
end
# Login docker hub
config.vm.provision :shell do |s|
s.inline = "/usr/bin/docker $#"
s.args = ["login", "-u", config.user.docker.username, "-p", config.user.docker.password, "-e", config.user.docker.email]
end
config.vm.provision :docker do |d|
d.pull_images ....
end
config.vm.provision :docker do |d|
d.run "image" ....
end
The right answer is to remember that your Vagrantfile is just a Ruby program, first and foremost, and the execution of this program is to result in a datastructure that the CLI sub-commands traverse.
So, create functions that add provisioners to configuration, then call them in the "inside". For example,
def provisioner_one(config)
config.vm.provision :shell, 'echo hello'
end
Vagrant.configure('2') do |config|
# stuff here
config.vm.define 'dev' do |dev, override|
# whatever else here
provisioner_one(dev)
# other stuff here
end
# more other stuff here
end
This will DWIM.

Ruby unexpected ':', expecting kEND

I want to set up vagrant on my Ubuntu, when "vagrant up", it always give me the following error
syntax error, unexpected ':', expecting kEND
config.vm.provision :shell, path: "vagrantprov.sh"
I checked the Vagrantfile, it should be OK, can anyone tell me where the error is? Thanks.
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "1024"]
end
config.vm.provision :shell, path: "vagrantprov.sh"
end
Ruby <1.9? Old fashioned hash syntax style is required for old Ruby version
config.vm.provision :shell, :path => "vagrantprov.sh"
What version of Ruby are you running? The named args syntax (path: "...") is supported from on 1.9 and above, perhaps you have a lower Ruby version?
(1.8)
1.8.7 :001 > puts "a", b: 1
SyntaxError: compile error
(irb):1: syntax error, unexpected ':', expecting $end
(1.9)
1.9.3p429 :001 > puts "a", b: 1
a
{:b=>1}
=> nil
Ruby < 1.9 :
:a => 1
Ruby >= 1.9 :
a : 1

Vagrant edit owner of folder throws syntax error

Hi I am trying to edit the owner of a laravel directory that I have, I am doing this via the config file for vagrant as below:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "precise64"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.network :private_network, ip: "192.168.33.21"
config.vm.provision :shell, :path => "install.sh"
config.vm.synced_folder ".", "/var/www”
config.vm.share_folder "v-root", "/tempus2/app/storage", ".", :owner => "www-data"
end
however I get the following error:
Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tIDENTIFIER, expecting
keyword_end
config.vm.share_folder "v-root", "/tempus2/app/storage...
^
/Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
...config.vm.share_folder "v-root", "/tempus2/app/storage", "."...
... ^
/Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
...v-root", "/tempus2/app/storage", ".", :owner => "www-data"
... ^
/Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tSTRING_BEG, expecting '('
...t", "/tempus2/app/storage", ".", :owner => "www-data"
... ^
/Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tIDENTIFIER, expecting keyword_end
...p/storage", ".", :owner => "www-data"
... ^
/Users/guti/Public/Sites/Vagrantfile:13: syntax error, unexpected tSTRING_BEG, expecting keyword_do or '{' or '('
/Users/guti/Public/Sites/Vagrantfile:13: unterminated string meets end of file
What is the correct syntax for www-data? also how can I extend the config file to change the permissions for this folder and all subdirectories to 777?
All i want to do is make the tempus2/app/storage directory writeable by www-data for laravel purposes. THe full directory on my machince is User/[username]/Public/Sites/tempus2/ and my vm is /var/www/tempus2 or when i ssh into vagrant cd /vagrant/tempus2. but how can i change the permissions on this folder for my vagrant vm?
share_folder isn't available in Vagrant 1.1+ (that's the old syntax), its now synced_folder. Also it looks like your double quote on the "/var/www" line is not actually a double quote character, probably copy and pasted from somewhere.

How do I include variables in my VagrantFile?

Can anyone guide me to how do I include variables in my VagrantFile? I am trying to inject configs into the Vagrantfile from an external file so that I can distribute the config to my colleagues without having them to hardcode configs directly on the Vagrantfile.
I had thought that since it was Ruby based I could just include a Ruby file but I get an error
Message: unintialized constant MyVars
My VagrantFile simplified
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'vagrant.rb'
include MyVars
Vagrant.configure("2") do |config|
# Web
config.vm.define :joe do |joe|
joe.vm.box = "precise64_4.2.12"
joe.vm.hostname = WEBVMNAME
joe.vm.network :private_network, ip: "192.168.140.141"
# Port Forwarding
joe.vm.network :forwarded_port, guest: 22, host: 2201
joe.vm.network :forwarded_port, guest: 80, host: 8080
# Bootstrap Bash Script
joe.vm.provision :shell, :path => "bootstrap.sh"
end
end
And vagrant.rb contains
module MyVars
WEBVMNAME = "rex"
end
Do note that I am also a newbie at Ruby so I am not sure as well if its just the syntax I got wrong?
Edit: Updated code I am using
I use the approach of https://puphpet.com, I create a file config.yaml in the same directory of the Vagrantfile and...
In my Vagrantfile:
# encoding: utf-8
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
current_dir = File.dirname(File.expand_path(__FILE__))
configs = YAML.load_file("#{current_dir}/config.yaml")
vagrant_config = configs['configs'][configs['configs']['use']]
Vagrant.configure('2') do |config|
config.vm.network 'public_network', ip: vagrant_config['public_ip']
...
In my config.yaml:
---
configs:
use: 'home'
office:
public_ip: '192.168.2.117'
<more variables>...
home:
public_ip: '192.168.1.117'
<more variables>...
Use require_relative:
require_relative 'vagrant.rb'
include MyVars
# ...
Try changing your require to this:
require './vagrant'
I created a library directory:
require './lib/cfpEnvironment.rb'
include CFPEnvironment
And then did the scripting of what I need to be dynamic, defining the variables in the module created...
CFPPorts.select{ |key, value| value.numeric? }.each { |key, value|
config.vm.network :forwarded_port, guest: value, host: value
}
Thanks to #Matt and #strager for their answers above!

Resources