How to use the python LWRP with Opsworks/Chef 11
-
20-12-2019 - |
Question
I am new to Chef/OpsWorks and am attempting a simple recipe to install Django 1.6 on an Ubuntu 12.04 instance:
python_pip "Django" do
version "1.6"
action :install
end
My providers/pip.rb and providers/virtualenv.rb seem to override what, ostensibly, was already loaded:
DEBUG: Loading cookbook python's providers from /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/pip.rb
DEBUG: Loaded contents of /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/pip.rb into a provider named python_pip defined in Chef::Provider::PythonPip
DEBUG: Loading cookbook python's providers from /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/virtualenv.rb
DEBUG: Loaded contents of /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/virtualenv.rb into a provider named python_virtualenv defined in Chef::Provider::PythonVirtualenv
DEBUG: Loading cookbook python's providers from /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/pip.rb
INFO: PythonPip light-weight provider already initialized -- overriding!
DEBUG: Loaded contents of /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/pip.rb into a provider named python_pip defined in Chef::Provider::PythonPip
DEBUG: Loading cookbook python's providers from /opt/aws/opsworks/releases/20131107153140_213/site-cookbooks/python/providers/virtualenv.rb
INFO: PythonVirtualenv light-weight provider already initialized -- overriding!
But when I try to use python_pip I receive an error:
ERROR: Caught exception while compiling OpsWorks custom run list: NameError - Cannot find a resource for python_pip on ubuntu version 12.04
I've tried adding depends "python"
in my metadata.rb, but this did not help. Any suggestions are appreciated.
My directory structure of my cookbook 'python':
.
├── python
│ ├── attributes
│ │ └── default.rb
│ ├── metadata.rb
│ ├── providers
│ │ ├── pip.rb
│ │ └── virtualenv.rb
│ ├── recipes
│ │ ├── default.rb
│ │ ├── django.rb
│ │ ├── package.rb
│ │ ├── pip.rb
│ │ ├── source.rb
│ │ └── virtualenv.rb
│ ├── specs
│ │ └── configure_spec.rb
│ └── templates
│ └── default
└── README.md
Solution
Don't try to modify the python cookbook unless it doesn't already do what you need. Your cookbooks directory should at minimum look like:
cookbooks/
├─python/ <clone of https://github.com/poise/python>
|
└─application_cookbook/
├─recipes/
| └─default.rb <containing the resource declaration you have in the question>
└─metadata.rb <with a `depends "python"` statement in it>
That's it, no need to modify the python cookbook. Now just set your runlist to [ "python", "application_cookbook" ]
and you're good to go.
OTHER TIPS
Before you can use the python_pip
LWRP, you need to either have python in your runlist or include_recipe "python"
within your own recipe.
Here's an example that assumes you have a user named dj
with a home directory already set up.
# Note that the recipe will install Python, setuptools and pip
include_recipe "python"
# Create a virtual environment
python_virtualenv '/home/dj/virtenv' do
interpreter "python2.7"
owner 'dj'
group 'dj'
action :create
end
# Install Django to a virtualenv
python_pip "django" do
virtualenv '/home/dj/virtenv'
user 'dj'
group 'dj'
action :install
end