/
chef_provider_package_rbenvrubygems.rb
94 lines (81 loc) · 2.7 KB
/
chef_provider_package_rbenvrubygems.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#
# Cookbook:: ruby_rbenv
# Provider:: Chef::Provider::Package::RbenvRubygems
#
# Author:: Fletcher Nichol <fnichol@nichol.ca>
#
# Copyright:: 2011-2017, Fletcher Nichol
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'etc'
class Chef
module Rbenv
module Mixin
module ShellOut
# stub to satisfy RbenvRubygems (library load order not guarenteed)
end
end
module ScriptHelpers
# stub to satisfy RbenvRubygems (library load order not guarenteed)
end
end
class Provider
class Package
class RbenvRubygems < Chef::Provider::Package::Rubygems
class RbenvGemEnvironment < AlternateGemEnvironment
attr_reader :rbenv_version, :rbenv_user
include Chef::Rbenv::Mixin::ShellOut
def initialize(gem_binary_location, rbenv_version, rbenv_user = nil)
super(gem_binary_location)
@rbenv_version = rbenv_version
@rbenv_user = rbenv_user
end
end
attr_reader :rbenv_user
include Chef::Rbenv::Mixin::ShellOut
include Chef::Rbenv::ScriptHelpers
def initialize(new_resource, run_context = nil)
super
normalize_version
@new_resource.gem_binary(wrap_shim_cmd('gem'))
@rbenv_user = new_resource.respond_to?('user') ? new_resource.user : nil
@gem_env = RbenvGemEnvironment.new(
gem_binary_path, new_resource.rbenv_version, rbenv_user)
end
def install_package(name, version)
run_as_user(rbenv_user) { super }
rehash
true
end
def remove_package(name, version)
run_as_user(rbenv_user) { super }
rehash
true
end
private
# converts "global" to the current global ruby version
def normalize_version
@new_resource.rbenv_version(current_global_version) if @new_resource.rbenv_version == 'global'
end
def rehash
e = ::Chef::Resource.resource_for_node(:rbenv_rehash, node).new(new_resource.name, @run_context)
e.root_path rbenv_root
e.user rbenv_user if rbenv_user
e.action :nothing
e.run_action(:run)
end
end
end
end
end