使用 Vagrant 管理开发和生产环境? [英] Using Vagrant to manage development and production environments?
问题描述
人们如何使用 vagrant(最好来自同一个 vagrantfile)为 dev/prod 环境处理简单的自动化(使用 puppet)?
How are people handling simple automation (with puppet) for dev / prod environments with vagrant (ideally from the same vagrantfile)?
我正在尝试解决的用例
- 如果没有创建,我很乐意使用 vagrant 启动生产机器.
- 如果在我的开发环境的 puppet 文件中调整了 nginx 或 apache confs,我很乐意使用 vagrant 在生产环境中重新加载它们.
问题
当您通过 AWS 或 Digital Ocean 等提供商调用 vagrant 时,它会成为活动提供商,您无法切换.您收到此错误:
When you call vagrant up with a provider like AWS or Digital Ocean, it becomes the active provider and you can't switch. You get this 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.
似乎是要销毁的答案,但我只需要切换即可.我不想破坏.
It seems the answer it to destroy, but I just need to switch. I don't want to destroy.
我很想说
vagrant up prod
或
vagrant reload prod
然后一个简单的 vagrant up 将回退到默认机器.
and then a simple vagrant up would fall back to the default machine.
此语法类似于多台机器的工作方式,但是我不想在调用 vagrant up 时启动开发和生产环境(这是默认行为).
This syntax is similar to how multiple machines work, but I don't want to spin up a dev and production environment when I just call vagrant up (which is the default behavior).
我是否应该将打包程序视为工作流程的一部分?我在 puppetconf 2013 上观看了 Mitchell 关于 Multi-Provider http://puppetlabs.com/presentations/multi-provider-vagrant-aws-vmware-and-more
Should I be looking at packer as part of the workflow? I watched the whole talk at puppetconf 2013 on Mitchell's talk on Multi-Provider http://puppetlabs.com/presentations/multi-provider-vagrant-aws-vmware-and-more
我仍然没有看到解决我的问题的方法.
I'm still not seeing a solution for my problem.
更新 9/27/13
如果其他人反对这个想法,这篇文章解决了我的很多问题.http://pretengineer.com/post/packer-vagrant-infra
In case anybody else is fighting this idea, this article cleared up a lot of questions I had. http://pretengineer.com/post/packer-vagrant-infra
推荐答案
至于变通方法,您应该定义 config.vm.define
(如建议的 此处),以支持多个提供商.
As for workaround, you should define config.vm.define
(as suggested here), in order to support multiple providers.
请以@kzap 发布的以下配置为例:
Please find the following configuration posted by @kzap as example:
Vagrant.configure("2") do |config|
# Store the current version of Vagrant for use in conditionals when dealing
# with possible backward compatible issues.
vagrant_version = Vagrant::VERSION.sub(/^v/, '')
# Configuration options for the VirtualBox provider.
def configure_vbox_provider(config, name, ip, memory = 2048, cpus = 1)
config.vm.provider :virtualbox do |v, override|
# override box url
override.vm.box = "ubuntu/trusty64"
# configure host-only network
override.vm.hostname = "#{name}.dev"
override.vm.network :private_network, id: "vvv_primary", ip: ip
v.customize ["modifyvm", :id,
"--memory", memory,
"--cpus", cpus,
"--name", name,
"--natdnshostresolver1", "on",
"--natdnsproxy1", "on"
]
end
end
default_provider = "virtualbox"
supported_providers = %w(virtualbox rackspace aws managed)
active_provider = ENV['VAGRANT_ACTIVE_PROVIDER'] # it'd be better to get this from the CLI --provider option
supported_providers.each do |provider|
next unless (active_provider.nil? && provider == default_provider) || active_provider == provider
#
# VM per provider
#
config.vm.define :"sample-#{provider}" do | sample_web_config |
case provider
when "virtualbox"
configure_vbox_provider(sample_web_config, "examine-web", "192.168.50.1")
when "aws"
configure_aws_provider(sample_web_config)
when "managed"
configure_managed_provider(sample_web_config, "1.2.3.4")
when "rackspace"
configure_rackspace_provider(sample_web_config)
end
end
end
或者以下示例发布在 @maxlinc 的要点:
Or the following example posted at gist by @maxlinc:
# -*- 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 = "dummy"
config.vm.provider :rackspace do |rs|
rs.username = ENV['RAX_USERNAME']
rs.api_key = ENV['RAX_API_KEY']
rs.rackspace_region = :ord
end
supported_providers = %w(virtualbox rackspace)
active_provider = ENV['VAGRANT_ACTIVE_PROVIDER'] # it'd be better to get this from the CLI --provider option
supported_providers.each do |provider|
next unless active_provider.nil? || active_provider == provider
config.vm.define "exact_name_#{provider}" do |box|
box.vm.provider :rackspace do |rs|
rs.flavor = '1 GB Performance'
rs.image = 'Ubuntu 14.04 LTS (Trusty Tahr) (PVHVM)'
end
end
config.vm.define "regex_#{provider}" do |box|
box.vm.provider :rackspace do |rs|
rs.flavor = /1\s+GB\s+Performance/
rs.image = /Ubuntu.*Trusty Tahr.*(PVHVM)/
end
end
config.vm.define "id_#{provider}" do |box|
box.vm.provider :rackspace do |rs|
rs.flavor = 'performance1-1'
rs.image = 'bb02b1a3-bc77-4d17-ab5b-421d89850fca'
end
end
config.vm.define "unlisted_#{provider}" do |box|
box.vm.provider :rackspace do |rs|
rs.flavor = 'performance1-1'
rs.image = '547a46bd-d913-4bf7-ac35-2f24f25f1b7a'
end
end
end
end
这篇关于使用 Vagrant 管理开发和生产环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!