]> git.openstreetmap.org Git - chef.git/blob - cookbooks/python/resources/package.rb
allow a different source URL for python packages
[chef.git] / cookbooks / python / resources / package.rb
1 #
2 # Cookbook:: python
3 # Resource:: package
4 #
5 # Copyright:: 2017, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 unified_mode true
21
22 default_action :install
23
24 property :package_name, :kind_of => String, :name_property => true
25 property :version, :kind_of => String
26 property :python_version, :kind_of => String
27 property :python_virtualenv, :kind_of => String
28 property :extra_index_url, :kind_of => String
29
30 action :install do
31   if new_resource.version.nil?
32     execute "pip-install-#{new_resource.package_name}" do
33       command "#{pip_command} install #{pip_extra_index} #{new_resource.package_name}"
34       not_if "#{pip_command} show #{new_resource.package_name}"
35     end
36   else
37     execute "pip-install-#{new_resource.package_name}" do
38       command "#{pip_command} install #{pip_extra_index} #{new_resource.package_name}==#{new_resource.version}"
39       not_if "#{pip_command} show #{new_resource.package_name} | fgrep -q #{new_resource.version}"
40     end
41   end
42 end
43
44 action :upgrade do
45   if new_resource.version.nil?
46     execute "pip-upgrade-#{new_resource.package_name}" do
47       command "#{pip_command} install #{pip_extra_index} --upgrade #{new_resource.package_name}"
48       only_if "#{pip_command} list --outdated | fgrep -q #{new_resource.package_name}"
49     end
50   else
51     execute "pip-upgrade-#{new_resource.package_name}" do
52       command "#{pip_command} install #{pip_extra_index} --upgrade #{new_resource.package_name}==#{new_resource.version}"
53       not_if "#{pip_command} show #{new_resource.package_name} | fgrep -q #{new_resource.version}"
54     end
55   end
56 end
57
58 action :remove do
59   execute "pip-uninstall-#{new_resource.package_name}" do
60     command "#{pip_command} uninstall #{new_resource.package_name}"
61     only_if "#{pip_command} show #{new_resource.package_name}"
62   end
63 end
64
65 action_class do
66   def pip_extra_index
67     if new_resource.extra_index_url
68       "--extra-index-url '#{new_resource.extra_index_url}'"
69     end
70   end
71
72   def pip_command
73     if new_resource.python_virtualenv
74       "#{new_resource.python_virtualenv}/bin/pip"
75     else
76       "pip#{new_resource.python_version}"
77     end
78   end
79 end