Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
My first Chef definition: create a virtualenv. Be nice.
# An example of the below
virtualenv "/home/dvcsmirrors/hg" do
owner "root"
group "dvcsmirrors"
mode 0775
packages "Mercurial" => "1.6.3",
"hgsubversion" => "1.1.2"
end
#
# Definition to create virtualenvs
#
# For example::
#
# virtualenv "/home/me/myenv" do
# packages "Django" => "1.2.3"
# end
#
# This would create a new virtualenv in /home/me/myenv and install
# Django 1.2.3. "packages" is a hash, so you can include multiple
# packages there. Right now there's nothing to say "latest version"
# because I don't know Ruby that well!
#
# The definition also accepts path, owner, group, and mode arguments, just
# like the directory resource.
#
# TODO: support a requirements file.
#
define :virtualenv, :action => :create, :owner => "root", :group => "root", :mode => 0755, :packages => {} do
path = params[:path] ? params[:path] : params[:name]
if params[:action] == :create
# Manage the directory.
directory path do
owner params[:owner]
group params[:group]
mode params[:mode]
end
execute "create-virtualenv-#{path}" do
command "virtualenv #{path}"
not_if "test -f #{path}/bin/python"
end
params[:packages].each_pair do |package, version|
pip = "#{path}/bin/pip"
execute "install-#{package}-#{path}" do
command "#{pip} install #{package}==#{version}"
not_if "[ `#{pip} freeze | grep #{package} | cut -d'=' -f3` = '#{version}' ]"
end
end
elsif params[:action] == :delete
directory path do
action :delete
recursive true
end
end
end

JREAM commented Feb 19, 2015

nice!

skehlet commented Apr 24, 2015

Helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment