Add custom lines to Nginx config with Vagrant / Homestead - laravel

I'm running an Nginx server on a Vagrant Homestead box and I would like to add this piece of code to the server part of the Nginx config file for my site (in '/etc/ngnix/sites-available'):
location /storage/ {
add_header "Access-Control-Allow-Origin" "*";
}
I can type it in manually, but then it's gone every time I run vagrant provision.
What would be the best way to add this code during or after the provisioning?
Update 1
I found this article: https://medium.com/#maxxscho/laravel-homestead-with-custom-nginx-configurations-2e0fda4ba7dc
which brings me a step closer but now I have to figure out a way to place the custom serve-###.sh file outside the vendor folder. If anyone has thoughts on this, they are welcome as well!
Update 2
After fiddling some more I came up with a fairly decent solution:
Copy the vendor/laravel/homestead/scripts/serve-laravel.sh to a folder outside the vendor folder (e.g. ./scripts).
Edit the file to your needs.
Place some additional code in the Vagrant file to copy the script to the vendor folder before provisioning.
Add this to the Vegrantfile:
....
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.trigger.before :provision do |trigger|
trigger.info = "Copy custom scripts to vendor folder"
trigger.run = {
inline: "cp -r /scripts/* vendor/laravel/homestead/scripts/"
}
end
....
Any other solutions are still welcome!

Related

Messed up file contents in vagrant smb shared file

I decided to throw up a quick OpenCart installation today using the Homestead vagrant box with Hyper-V on my Windows 10 machine, and I've got my opencart folder mapped like this in Homestead.yml:
- map: D:\web\www\oc\oc-3.0.2.0
to: /home/vagrant/code/opencart
type: "smb"
All's well. The folder is synced in the vagrant VM as expected. But on trying to install OpenCart, I get this parse error:
PHP Parse error: syntax error, unexpected ''] == '' (T_CONSTANT_ENCAPSED_STRING) in /home/vagrant/code/opencart/upload/system/startup.php on line 93
Looking at lines 92-94 of that file on my host machine:
require_once(modification(DIR_SYSTEM . 'engine/event.php'));
require_once(modification(DIR_SYSTEM . 'engine/router.php'));
require_once(modification(DIR_SYSTEM . 'engine/loader.php'));
Looking at the same lines on the synced file in vagrant:
require_once(modification(DIR_SYSTEM . 'engine/event.php'));
requirePROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
$_SERVER['HTTPS'] = true;
This code that's replaced the "require_once" code here appears earlier in this file. It's as if it's recopied over later code, making this file unusable. Here's the full file.
If I delete the lines that are copied or replace them with some other code, things appear find in the vagrant version. But if I only delete a couple of the lines, the rest still overwrite later code.
Does anyone have any idea what's causing this or how to resolve it?
So it turns out this had nothing to do with Homestead nor OpenCart. I pulled up another vagrant project I had and noticed similar problems with synced files.
I had recently installed Docker for Windows to play around with, so I decided to uninstall it and see if that would fix the problem. And it did.
So somewhere, somehow Vagrant and Docker aren't getting along on Windows 10. No idea where the exact problem is, though.

Scope of modules using Puppet in a Vagrant multi machine environment

I have a projekt with a setup for a multi machine environment for Vagrant. I had to fix some problems, which were initially caused by the redirect issue to https, but solving these lead into other errors, which I fixed in all projects except this one now, which uses the multi machine feature of Vagrant.
So I have this folder structure:
/Vagrantfile
/puppet/box_1/puppetfile
/puppet/box_1/manifests/site.pp
This is my code snippet, where I define my provision directories:
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/box_1/manifests"
puppet.manifest_file = "site.pp"
end
My puppetfile looks like this:
forge "https://forgeapi.puppetlabs.com"
mod 'tPl0ch/composer'
mod 'puppetlabs/apt'
mod 'puppetlabs/apache'
mod 'puppetlabs/firewall'
In my site.pp I try to include apt, but I get this error message:
Error: Evaluation Error: Error while evaluating a Function Call, Could not find class ::apt for project.local at /tmp/vagrant-puppet/manifests-f2b1fd0ac42b51938ed6ae7e6917367e/site.pp:1:1 on node project.local
When I rearange my puppet files like this:
/Vagrantfile
/puppet/puppetfile
/puppet/manifests/site.pp
like this is the common way of setting this up, it works without that problem, but as I mentioned, there are other boxes, which use different puppetfiles and site.pp files, so this folder structure makes some kinda sense. It seems, that it doesn'even matter, if I delete the config for the other boxes, and setup my Vagrantfile, as if it would be only one box, so I am just confused, how the location, of these files influence the scope of certain classes.
So my questions is here: Is there a way, to keep this folder structure and still have these modules defined in puppetfile available in my site.pp? Or is this generally some kinda bad practice to organize it this way? I was searching for some examples for this, but couldn't find any for some reason...
EDIT: It seems, on provision the puppetfile isnt even used anymore, when its not located in /puppet/ So maybe I just have to tell Vagrant how to use it?
define where librarian should find the puppet file
Vagrant.configure("2") do |config|
config.librarian_puppet.puppetfile_dir = "puppet/box1"
config.vm.provision :puppet do |puppet|
puppet.manifests_path = "puppet/box_1/manifests"
puppet.manifest_file = "site.pp"
end

Issue with SaltStack

Recently I started taking an interest in Salt and begun doing a tutorial on it.I am currently working on a Mac and I having a hard time trying to start the vm[the minion] from my laptop[I am using Vagrant as an application to start the process]
The vagrant file for the vm contains these lines:
# salt-vagrant config
config.vm.provision :salt do |salt|
salt.run_highstate = true
salt.minion_config = "/etc/salt/minion"
salt.minion_key = "./minion1.pem"
salt.minion_pub = "./minion1.pub"
end
even though I wrote this it gets stuck at:
Calling state.highstate... (this may take a while)
Any ideas why?
One more thing.I seem to need to modify the top.sls file at the next step which is located in /srv/salt.Unfortunately I can not find the /srv file anywhere,why is that?is there a way to tell the master that the top file is somewhere else?
If you don't have a top.sls created, then you won't be able to run a hightstate like you have configured with the salt.run_highstate = true line.
If you don't have a /srv/salt/ directory created, then you can just create it yourself. Just make sure the user the salt-master is running as can read it.
The /srv/salt/ directory is the default location of what is known as the file_root. You can modify its location in the master config file /etc/salt/master and modify the file_roots config option.

Creating a custom Vagrantfile from Packer

I've been trying to figure out how to create a custom vagrant file from packer, I understand that in the post-processor section you will define a directory from which to scrap from, what I do not understand is if there needs to be a specifically named file inside as to which to gather data from.
"post-processors": [{
"vagrantfile_template": "configs/vagrantfile_template",
"type": "vagrant"
}],
The above code to my knowledge would look under configs/vagrantfile_template, but what would need to be in here? Would I create a Vagrantfile and place it there, or would it need to be a specifically named Ruby file?
The vagrantfile_template option in a vagrant post-processor points directly to a file and not a directory [0]. The contents of this file should be structured like a normal Vagrantfile and contain any customizations for the box artifact that you are creating.
For example, if you wanted users of your custom Vagrant box to not have the /vagrant shared folder mounted by default, your Vagrantfile template might look like this...
Vagrant.configure("2") do |config|
config.vm.synced_folder \
".",
"/vagrant",
:disabled => true
end
Resources
[0]: https://github.com/mitchellh/packer/blob/53b1db16692c7bf2e654ac125b8676c02154d07a/post-processor/vagrant/post-processor.go#L113-L138

Vagrant not able to find home/vagrant/hiera.yaml

I have inherited a python app that uses Puppet, Vagrant and VirtualBox to provision a local Centos test machine.
This app was written on Mac and I'm developing on Windows.
When I run vagrant up I see a large list of command line errors, the most relevant of which is:
Running Puppet with site.pp..
Warning: Config file /home/vagrant/hiera.yaml not found, using Hiera defaults
WARN: Fri Apr 25 16:32:24 +0100 2014: Not using Hiera::Puppet_logger. It does not report itself to b
e suitable.
I know what Hiera is, and why it's important, but I'm not sure how to fix this.
The file hiera.yaml is present in the repo but it's not found at home/vagrant/hiera.yaml, instead it's found at ./puppet/manifests/hiera.yaml.... Similarly if I ssh into the box, there is absolutely nothing whatsoever inside home/vagrant but I can find this file when I look in /tmp
So I'm confused, is vagrant looking inside the Virtual box and expecting this file to be found at home/vagrant/hiera.yaml? Or have I inherited an app that did not work properly in the first place? I'm really stuck here and I can't get in touch with the original dev.
Here are some details from my Vagrantfile:
Vagrant.configure("2") do |config|
# Base box configuration ommitted
# Forwarded ports ommitted
# Statically set hostname and internal network to match puppet env ommitted
# Enable provisioning with Puppet standalone
config.vm.provision :puppet do |puppet|
# Tell Puppet where to find the hiera config
puppet.options = "--hiera_config hiera.yaml --manifestdir /tmp/vagrant-puppet/manifests"
# Boilerplate Vagrant/Puppet configuration
puppet.module_path = "puppet/modules"
puppet.manifests_path = "puppet/manifests"
puppet.manifest_file = "site.pp"
# Custom facts provided to Puppet
puppet.facter = {
# Tells Puppet that we're running in Vagrant
"is_vagrant" => true,
}
end
# Make the client accessible
config.vm.synced_folder "marflar_client/", "/opt/marflar_client"
end
It's really strange.
There's this puppet.options line that tells Puppet where to look for hiera.yaml and currently it simply specifies the file name. Now, when you run vagrant up, it mounts the current directory (the one that has the Vagrantfile in it) into /vagrant on the guest machine. Since you're saying hiera.yaml is actually found in ./puppet/manifests/hiera.yaml, I believe you want to change the puppet.options line as follows:
puppet.options = "--hiera_config /vagrant/puppet/manifests/hiera.yaml --manifestdir /tmp/vagrant-puppet/manifests"
To solve this warning, you may first try to check from where this file is referenced, i.e.:
$ grep -Rn hiera.yaml /etc/puppet
/etc/puppet/modules/stdlib/spec/spec_helper_acceptance.rb:13: on hosts, '/bin/touch /etc/puppet/hiera.yaml'
/etc/puppet/modules/mysql/spec/spec_helper_acceptance.rb:34: shell("/bin/touch #{default['puppetpath']}/hiera.yaml")
In this case it's your vagrant file.
You may also try to find the right path to it via:
sudo updatedb && locate hiera.yaml
And either follow the #EvgenyChernyavskiy suggestion, create a symbolic link if you found the file:
sudo ln -vs /etc/hiera.yaml /etc/puppet/hiera.yaml
or create the empty file (touch /etc/puppet/hiera.yaml).
In your case you should use the absolute path to your hiera.yaml file, instead of relative.
The warning should be gone.

Resources