我正在为一些内置的OpsWorks烹饪书编写Chef包装器。我正在使用Berkshelf从他们的github repo克隆OpsWorks烹饪书。
这是我的Berksfile:
source 'https://supermarket.getchef.com'
metadata
def opsworks_cookbook(name)
cookbook name, github: 'aws/opsworks-cookbooks', branch: 'release-chef-11.10', rel: name
end
%w(dependencies scm_helper mod_php5_apache2 ssh_users opsworks_agent_monit
opsworks_java gem_support opsworks_commons opsworks_initial_setup
opsworks_nodejs opsworks_aws_flow_ruby
deploy mysql memcached).each do |cb|
opsworks_cookbook cb
end
我的metadata.rb:
depends 'deploy'
depends 'mysql'
depends 'memcached'
问题是,当我尝试覆盖依赖于opsworks
哈希中的node
键的属性时,我得到了:
NoMethodError
-------------
undefined method `[]=' for nil:NilClass
OpsWorks有一大堆预配方依赖项,可以创建这些键并进行大量设置。我想找到一种方法来拉入这些服务并在我的Kitchen实例上运行它们或者以我实际可以测试我的食谱的方式模拟它们。
有办法做到这一点吗?
答案 0 :(得分:3)
我强烈建议您查看Mike Greiling的博客文章Simplify OpsWorks Development With Packer和他的github repo opsworks-vm ,它们可以帮助您模拟整个opsworks堆栈,包括安装opsworks代理,以便您可以还可以同时测试应用部署配方,多个层,多个实例等。这真是令人印象深刻。
注意:这不能从ubuntu虚拟机完成,因为virtualbox不支持64位计算机的嵌套虚拟化。
mkdir /tmp/packages && cd /tmp/packages
wget https://opscode-omnibus-packages.s3.amazonaws.com/ubuntu/12.04/x86_64/chefdk_0.8.1-1_amd64.deb
sudo dpkg -i chefdk_0.8.0-1_amd64.deb
cd /opt/chefdk/
chef verify
which ruby
echo 'eval "$(chef shell-init bash)"' >> ~/.bash_profile && source ~/.bash_profile
echo 'deb http://download.virtualbox.org/virtualbox/debian vivid contrib' > /etc/apt/sources.list.d/virtualbox.list
wget -q https://www.virtualbox.org/download/oracle_vbox.asc -O- | sudo apt-key add -
sudo apt-get update -qqy
sudo apt-get install virtualbox-5.0 dkms
cd /tmp/packages
wget https://dl.bintray.com/mitchellh/vagrant/vagrant_1.7.4_x86_64.deb
sudo dpkg -i vagrant_1.7.4_x86_64.deb
vagrant plugin install vagrant-berkshelf
vagrant plugin install vagrant-omnibus
vagrant plugin list
mkdir /opt/packer && cd /opt/packer
wget https://dl.bintray.com/mitchellh/packer/packer_0.8.6_linux_amd64.zip
unzip packer_0.8.6_linux_amd64.zip
echo 'PATH=$PATH:/opt/packer' >> ~/.bash_profile && source ~/.bash_profile
mkdir ~/packer && cd ~/packer
git clone https://github.com/pixelcog/opsworks-vm.git
cd opsworks-vm
rake build install
要模拟单个opsworks实例,请创建一个新的Vagrantfile,如下所示:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu1404-opsworks"
config.vm.provision :opsworks, type: 'shell', args: 'path/to/dna.json'
end
dna.json
文件路径是相对于Vagrantfile设置的,应包含您希望发送给OpsWorks Chef的任何JSON数据。
例如:
{
"deploy": {
"my-app": {
"application_type": "php",
"scm": {
"scm_type": "git",
"repository": "path/to/my-app"
}
}
},
"opsworks_custom_cookbooks": {
"enabled": true,
"scm": {
"repository": "path/to/my-cookbooks"
},
"recipes": [
"recipe[opsworks_initial_setup]",
"recipe[dependencies]",
"recipe[mod_php5_apache2]",
"recipe[deploy::default]",
"recipe[deploy::php]",
"recipe[my_custom_cookbook::configure]"
]
}
}
要模拟多个opsworks实例并包含图层,请参阅下面包含AWS OpsWorks "Getting Started" Example的stack.json。
Vagrantfile(适用于多个实例)
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu1404-opsworks"
# Create the php-app layer
config.vm.define "app" do |layer|
layer.vm.provision "opsworks", type:"shell", args:[
'ops/dna/stack.json',
'ops/dna/php-app.json'
]
# Forward port 80 so we can see our work
layer.vm.network "forwarded_port", guest: 80, host: 8080
layer.vm.network "private_network", ip: "10.10.10.10"
end
# Create the db-master layer
config.vm.define "db" do |layer|
layer.vm.provision "opsworks", type:"shell", args:[
'ops/dna/stack.json',
'ops/dna/db-master.json'
]
layer.vm.network "private_network", ip: "10.10.10.20"
end
end
stack.json
{
"opsworks": {
"layers": {
"php-app": {
"instances": {
"php-app1": {"private-ip": "10.10.10.10"}
}
},
"db-master": {
"instances": {
"db-master1": {"private-ip": "10.10.10.20"}
}
}
}
},
"deploy": {
"simple-php": {
"application_type": "php",
"document_root": "web",
"scm": {
"scm_type": "git",
"repository": "dev/simple-php"
},
"memcached": {},
"database": {
"host": "10.10.10.20",
"database": "simple-php",
"username": "root",
"password": "correcthorsebatterystaple",
"reconnect": true
}
}
},
"mysql": {
"server_root_password": "correcthorsebatterystaple",
"tunable": {"innodb_buffer_pool_size": "256M"}
},
"opsworks_custom_cookbooks": {
"enabled": true,
"scm": {
"repository": "ops/cookbooks"
}
}
}
对于那些不熟悉流浪汉的人,你只需要vagrant up
来启动实例。然后,您可以在本地修改您的食谱,并通过使用vagrant provision.
对现有实例重新运行厨师来应用任何更改您可以执行vagrant destroy
和vagrant up
从头开始
答案 1 :(得分:0)
您必须手动执行此操作。您可以向.kitchen.yml
添加任意属性,只需使用OpsWorks计算机并记录您需要的值,直接使用它们或将其调整为可用的测试数据。