Skip to content

Instantly share code, notes, and snippets.

@jacobian
Created October 5, 2010 21:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacobian/612395 to your computer and use it in GitHub Desktop.
Save jacobian/612395 to your computer and use it in GitHub Desktop.
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
@skehlet
Copy link

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