问题
I have a few services, each one living in its own Git repository.
Each service is self-contained and runs independently from each other.
I want them to run in the same VM.
How do you use Vagrant to set up a shared development environment containing these services?
(Please note that I want to avoid creating a Vagrantfile per repo.)
回答1:
Create a custom cookbook/puppet manifest or shell provisioner that will checkout individual git repository and run the service.
More details/configuration options for provisioners in Vagrant can be found here
https://www.vagrantup.com/docs/provisioning/basic_usage.html
回答2:
The easiest thing to do is just set up Beaker. Find a mature module on the Puppet Forge for guidance. You'll need spec/spec_helper_acceptance.rb
, spec/acceptance/nodesets/default.yml
, and you'll need the system tests selection of Gems from Gemfile
.
Now create a file spec/acceptance/yourclass_spec.rb
, and have it just apply your class to configure the VM in the way you want it.
To address the requirement of multiple projects in multiple git repos, you can install them all from within spec/spec_helper_acceptance.rb
.
Alternatively, you can set up .fixtures.yml
and puppetlabs_spec_helper
, and run bundle exec rake spec_prep
from within your spec helper.
To get your head around it clone some Forge module like puppetlabs/apache
.
bundle install
export BEAKER_destroy=no
bundle exec rspec spec/acceptance/some_spec.rb
After the test finishes you can log in to your VM.
It's a bit of a learning curve, but it this way means you've got a way of quickly provisioning VMs, and you can test them properly too.
Beaker can even spin up multiple VMs so you could have all your services running together if you need to.
回答3:
maybe I miss something but sounds like you can run a VM and just use shared folder with the different folders with the services you are working on.
shared folder can take absolute path (not just relative in the current project where you define your Vagrantfile) so you can have a mixture of folders like
Vagrant.configure(2) do |config|
config.vm.box = xxx
blablabla
config.vm.synced_folder "/Users/fhenri/project/service1", "/project/service1"
config.vm.synced_folder "/Users/fhenri/project/examples/service2", "/project/service2", type: "nfs"
config.vm.synced_folder "/Users/fhenri/project/ruby/service3", "/opt/rubyservice"
so when you provision this VM you can provision each of your service. as the folders are shared with the source of your project, any change you made in the original folder will be synced on the VM
you can also define multiple machines within this single Vagrantfile and have a shared folder in each of the machine
drawback: you cannot really share this Vagrantfile as the absolute path is mainly aligned with your own setting (you can use user_variable and so on but its not ideal)
回答4:
One approach involves using Vagrant and Git subtrees to reference and checkout the external components from a single repo.
Let’s suppose we have three different repos, service-auth, service-notifier and core, each one containing the components we want to configure.
We will create a repo, e.g. dev-env, containing the Vagrantfile and one subtree per external repo. The Vagrantfile will look like this:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(2) do |config|
config.vm.box = “ubuntu/trusty64”
config.vm.network :forwarded_port, host: 8080, guest: 8080 # core
config.vm.network :forwarded_port, host: 8081, guest: 8081 # service-auth
config.vm.network :forwarded_port, host: 8181, guest: 8181 # service-notifier
end
(We are assuming that our components will listen to different TCP ports.)
Then, we will use git subtree
to add, update and push our components.
Depending on your project's characteristics and the team's organization, this solution may be a good fit or not.
I've written an article giving more details on how to configure and use subtrees for this purposes.
来源:https://stackoverflow.com/questions/36364652/shared-vagrant-configuration-across-multiple-projects