在厨师中指定环境

Specifing environment in chef

我有一个 java 应用程序,其 spring 属性是通过如下所示的环境属性传递的。

name "dev"
description "The test environment"
    override_attributes({"dspring" => {"active_profile" => "dev",
                                       "jss_cert_location" => "/opt/mount1/certs/jssecacerts"},

我正在寻找类似的方法来为 Chef 中的 ruby 应用程序定义环境。用于执行此应用程序的命令是:

/usr/local/bin/python2.7 /opt/mount2/ozz/current/src/main.py --port=8080 --debug --log_file_prefix=/opt/mount2/ozz/logs/ozz.log --log_file_max_size=1000000 --env=test

如何在每个 Chef 环境(开发、测试、阶段和生产)中定义这个“--env”参数?

以下是食谱的摘录:

ark 'python' do
  url 'https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz'
  path "/opt/mount1/"
  owner 'python'
  action :put
end

execute "cd #{node[:base_dir]}/python && ./configure && make && make altinstall" do
  not_if { File.exists?('/usr/local/bin/python2.7') }
end

remote_file "#{node[:base_dir]}/get-pip.py" do
  source "https://bootstrap.pypa.io/get-pip.py"
  mode 00755
  not_if { File.exists?("#{node[:base_dir]}/get-pip.py")}
end

execute "/usr/local/bin/python2.7 #{node[:base_dir]}/get-pip.py"

cookbook_file "ua-parser-master.tar" do
  path "/tmp/ua-parser-master.tar"
  not_if { File.exists?('/tmp/ua-parser-master.tar') }
end

execute 'tar -C /tmp -xvf /tmp/ua-parser-master.tar && chown -R root.root /tmp/ua-parser-master/ && /usr/local/bin/pip install /tmp/ua-parser-master'

execute '/usr/local/bin/pip install tornado pyyaml user-agents'

template "/etc/init.d/#{node[:application_name]}" do
  source 'python_init'
  mode 00755
  owner 'root'
  group 'root'
  variables(
    :application_name => node[:application_name],
  )
end

deploy_revision "/opt/mount1/#{node[:application_name]}" do
  repo "#{node[:application_repo]}"
  user "python"
  keep_releases 10
  action :deploy
  migrate false
  symlink_before_migrate.clear
  create_dirs_before_symlink
  purge_before_symlink.clear
  symlinks.clear
  symlinks {}
  notifies :restart, "service[#{node[:application_name]}]"
end

service "#{node[:application_name]}" do
  supports :restart => true
  action :enable
end

假设 Chef 环境与您希望传递的参数相匹配,并且命令在此模板中:

template "/etc/init.d/#{node[:application_name]}" do
  source 'python_init'
  mode 00755
  owner 'root'
  group 'root'
  variables(
    :application_name => node[:application_name],
  )
end

执行的python_init模板行可以写成:

/usr/local/bin/python2.7 /opt/mount2/ozz/current/src/main.py --port=8080 --debug --log_file_prefix=/opt/mount2/ozz/logs/ozz.log --log_file_max_size=1000000 --env=<%= node.chef_environment %>

Chef 中的模板使用 erb 呈现最终文件。

  • <%= variable %>是erb中的占位符
  • node.chef_environment 是 Chef 节点对象的一种方法,它给出了节点的环境名称。