在安装Nginx时,我遇到了一些Chef相关的困难
想要创建本地开发环境,但不顺利,尝试了各种方法和措施,做了一些记录…
用Vagrant + Chef + CentOS6.6制作
为了安装Nginx,我们将创建一个Cookbook。
$ bundle exec knife cookbook create nginx -o ./site-cookbooks
我会参考《Chef実践入門》这本书来写食谱(同时也准备了模板)。
include_recipe "yum-epel"
package "nginx" do
action :install
end
service "nginx" do
action [ :enable, :start ]
supports :status => true, :restart => true, :reload => true
end
template 'nginx.conf' do
path '/etc/nginx/nginx.conf'
source "nginx.conf.erb"
owner 'root'
group 'root'
mode 0644
notifies :reload, "service[nginx]"
end
执行berks命令。
使用yum、yum-epel和社区Cookbook。
source "https://supermarket.getchef.com"
cookbook "yum"
cookbook "yum-epel"
cookbook "nginx", path: "./site-cookbooks/nginx"
$ bundle exec berks vendor ./cookbooks
Vagrantfile的内容大概是这样的。
…
# vagrant-omnibusの有効化
config.omnibus.chef_version = :latest
config.vm.provision :chef_solo do |chef|
chef.log_level = :debug
chef.cookbooks_path = ["./cookbooks", "./site-cookbooks"]
chef.json = {
nginx: {
port: 80
}
}
chef.run_list = %w[
recipe[yum]
recipe[yum-epel]
recipe[nginx]
]
end
尝试提供
$ vagrant up --provision
出现了错误。。。
…
==> default: [2015-06-16T14:55:35+00:00] INFO: Starting Chef Run for localhost
==> default: [2015-06-16T14:55:35+00:00] INFO: Running start handlers
==> default: [2015-06-16T14:55:35+00:00] INFO: Start handlers complete.
==> default: [2015-06-16T14:55:35+00:00] ERROR: Running exception handlers
==> default: [2015-06-16T14:55:35+00:00] ERROR: Exception handlers complete
==> default: [2015-06-16T14:55:35+00:00] FATAL: Stacktrace dumped to /var/chef/cache/chef-stacktrace.out
==> default: [2015-06-16T14:55:35+00:00] ERROR: Cookbook yum-epel not found. If you're loading yum-epel from another cookbook, make sure you configure the dependency in your metadata
==> default: [2015-06-16T14:55:35+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.
因为似乎被要求定义依赖关系,所以我会在metadata.rb中添加对yum-epel的依赖关系。
name 'nginx'
maintainer 'YOUR_COMPANY_NAME'
maintainer_email 'YOUR_EMAIL'
license 'All rights reserved'
description 'Installs/Configures nginx'
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version '0.1.0'
depends 'yum-epel' #追加
执行berks命令后再进行配置。
$ bundle exec berks vendor ./cookbooks
$ vagrant provision
虽然还有一些错误出现,但内容似乎已经改变了。
…
==> default: [2015-06-20T01:23:18+00:00] ERROR: yum_package[nginx] (nginx::default line 21) had an error: Chef::Exceptions::Exec: yum -d0 -e0 -y install nginx-0.8.55-6.el5 returned 1:
==> default: STDOUT: You could try using --skip-broken to work around the problem
==> default: You could try running: rpm -Va --nofiles --nodigest
==> default:
==> default: STDERR: Error: Package: nginx-0.8.55-6.el5.x86_64 (epel)
==> default: Requires: perl(:MODULE_COMPAT_5.8.8)
==> default: Error: Package: geoipupdate-2.2.1-2.el5.x86_64 (epel)
==> default: Requires: libcurl.so.3()(64bit)
==> default: [2015-06-20T01:23:18+00:00] FATAL: Chef::Exceptions::ChildConvergeError: Chef run process exited unsuccessfully (exit code 1)
Chef never successfully completed! Any errors should be visible in the
output above. Please fix your recipes so that they properly complete.
看起来Nginx的版本似乎是0.8,有点过时的样子…
通过vagrant ssh进入Guest OS,检查一下似乎有yum-epel,所以试着运行sudo yum install Nginx,结果出现了刚才的错误。经过一番搜索发现,似乎需要添加Nginx的yum仓库。
由于Nginx的wiki上详细介绍了安装方法,因此我按照其说明创建并放置了repo文件,然后尝试执行相同的sudo yum install Nginx命令,成功安装了1.8版本。
只要准备好repo文件,似乎就能够成功,所以我会在templates中创建repo文件的模板,并且修改食谱使其将该文件放置在指定的位置。
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
include_recipe "yum-epel"
# yumにNginxのリポジトリを追加
template 'nginx.repo' do
path '/etc/yum.repos.d/nginx.repo'
source 'nginx.repo.erb'
mode 0644
user 'root'
group 'root'
end
package "nginx" do
action :install
end
service "nginx" do
action [ :enable, :start ]
supports :status => true, :restart => true, :reload => true
end
template 'nginx.conf' do
path '/etc/nginx/nginx.conf'
source "nginx.conf.erb"
owner 'root'
group 'root'
mode 0644
notifies :reload, "service[nginx]"
end
我暫時刪除虛擬環境,然後重新嘗試一次。這是Chef的好處,可以輕鬆地這樣做。
$ vagrant destory
$ bundle exec berks vendor ./cookbooks
$ vagrant up --provision
这次似乎顺利了。