1

Given Chef 12 support from OpsWorks was released very recently - all documents I can find are for the Chef 11. So, here is my current setup: flask + gunicorn + nginx on OpsWorks with Chef 12. I use Upstart to start Gunicorn using a template:

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid <%= node['conf-cookbook']['gunicorn_user'] %>
setgid <%= node['conf-cookbook']['gunicorn_group'] %>

env MAIL_SERVER="email-smtp.us-east-1.amazonaws.com"
env MAIL_USERNAME="[redcacted]"
env MAIL_PASSWORD="[redacted]"

chdir <%= node['conf-cookbook']['app_dir'] %>

exec gunicorn --workers 3 --bind unix:<%= node['conf-cookbook']['gunicorn_socket'] %> -m 007 --log-file <%= node['conf-cookbook']['gunicorn_logfile']%> manage:app

I have to include the sensitive environment variable information in the template. In OpsWorks Chef 12, environment variables can be specified using App Data Bag (aws_opsworks_app) and retrieved using something like in deploy recipe (never tried - is it correct):

app = search(:aws_opsworks_app).first
app['environment']['MAIL_SERVER']

I would like to use the app data bag environment variables to replace the ones I defined in the template file and don't know how. Any one can help?

Thanks!!

dami.max
  • 377
  • 3
  • 17
  • Just to confirm, those aren't your real SES credentials, right? – coderanger Feb 01 '16 at 22:47
  • thanks, I have changed the username and password :) – dami.max Feb 02 '16 at 00:52
  • Always be careful when copying sensitive data around. Also you don't need to edit my answer into your question. – coderanger Feb 02 '16 at 01:06
  • You can submit edit suggestions to answers using the link below them. – coderanger Feb 02 '16 at 20:49
  • Yes. I have deleted my UPDATE and move the edit suggestion to the comment of your answer. Thanks! I have another question: how to make Nginx work with application_python - I see Gunicorn helpers but no Nginx helper. I am really a beginner :) – dami.max Feb 03 '16 at 00:25

1 Answers1

1

You can pass arbitrary variables data to Chef template resources:

template '/etc/init/myapp.conf' do
  source 'myapp.conf.erb'
  variables node['conf-cookbook'].merge(app)
end

and then make your template look more like this:

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid <%= @gunicorn_user %>
setgid <%= @gunicorn_group %>

<%- @environment.each do |key, value| -%>
env <%= key %>="<%= value %>"
<%- end -%>

chdir <%= @app_dir %>

exec gunicorn --workers 3 --bind unix:<%= @gunicorn_socket %> -m 007 --log-file <%= @gunicorn_logfile %> manage:app

Also check out the poise-service and application_python cookbooks, which have helpers for both writing Upstart config files and Gunicorn services respectively.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • you are the man!!! thanks a lot. Amazon should pay you to help them with their documentations :) I will definitely use application framework when I gain more knowledge about Chef. Thanks again. – dami.max Feb 02 '16 at 00:52
  • I run into another issue - those environment variables are not available for other part of the recipe, for example, after `template '/etc/init/myapp.conf' do... end` I have `execute "setup database" do.. command "python manage.py deploy"..end` that wants to use environment variables - I don't know how. Thanks!!!! – dami.max Apr 18 '16 at 23:42
  • The `execute` resource has an `environment` property to set environment variables in the command being executed. – coderanger Apr 19 '16 at 02:10
  • got it, e.g., `environment ({'HOME' => '/home/myhome'})` thx!! – dami.max Apr 19 '16 at 12:41