-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
dpkg.rb
102 lines (81 loc) · 2.12 KB
/
dpkg.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
95
96
97
98
99
100
101
102
Puppet::Type.type(:alternative_entry).provide(:dpkg) do
confine osfamily: 'Debian'
defaultfor operatingsystem: [:debian, :ubuntu]
commands update: 'update-alternatives'
mk_resource_methods
def create
update('--install',
@resource.value(:altlink),
@resource.value(:altname),
@resource.value(:name),
@resource.value(:priority))
end
def exists?
# we cannot fetch @resource.value(:altname) if running 'puppet resource alternative_entry'
begin
output = update('--list', @resource.value(:altname) || altname)
rescue
return false
end
output.split(%r{\n}).map(&:strip).any? do |line|
line == @resource.value(:name)
end
end
def destroy
update('--remove', @resource.value(:altname), @resource.value(:name))
end
def self.instances
output = update('--get-selections')
entries = []
output.each_line do |line|
altname = line.split(%r{\s+}).first
query_alternative(altname).each do |alt|
entries << new(alt)
end
end
entries
end
def self.prefetch(resources)
instances.each do |prov|
# rubocop:disable Lint/AssignmentInCondition
if resource = resources[prov.name]
# rubocop:enable Lint/AssignmentInCondition
resource.provider = prov
end
end
end
ALT_QUERY_REGEX = %r{Alternative: (.*?)$.Priority: (.*?)$}m
def self.query_alternative(altname)
output = update('--query', altname)
altlink = output.match(%r{Link: (.*)$})[1]
output.scan(ALT_QUERY_REGEX).map do |(path, priority)|
{ altname: altname, altlink: altlink, name: path, priority: priority }
end
end
def name=(new_name)
rebuild do
@property_hash[:name] = new_name
end
end
def altname=(new_altname)
rebuild do
@property_hash[:altname] = new_altname
end
end
def altlink=(new_altlink)
rebuild do
@property_hash[:altlink] = new_altlink
end
end
def priority=(new_priority)
rebuild do
@property_hash[:priority] = new_priority
end
end
private
def rebuild(&_block)
destroy
yield
create
end
end