Skip to content

Commit d0348c1

Browse files
committed
fixup! fixup! Make provider holdable
1 parent 332ce62 commit d0348c1

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

lib/puppet/provider/package/snap.rb

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,41 @@
1919

2020
mk_resource_methods
2121

22+
def self.prefetch(resources)
23+
Puppet.info('Called prefetch')
24+
# Build a hash of name => install_options from the catalog
25+
desired_options = resources.transform_values { |res| res[:install_options] }
26+
27+
installed_snaps.each do |snap|
28+
resource = resources[snap['name']]
29+
next unless resource
30+
31+
current_hold_time = snap['hold']
32+
desired_hold_time = parse_time_from_options(desired_options[snap['name']])
33+
34+
# Determine the appropriate mark
35+
mark = if should_change_hold?(desired_hold_time, current_hold_time)
36+
:none # force re-hold
37+
else
38+
:hold
39+
end
40+
41+
provider = new(
42+
name: snap['name'],
43+
ensure: snap['tracking-channel'],
44+
mark: mark,
45+
hold_time: current_hold_time,
46+
provider: 'snap'
47+
)
48+
49+
resource.provider = provider
50+
end
51+
end
52+
2253
def self.instances
2354
installed_snaps.map do |snap|
24-
mark = snap['hold'].nil? ? 'none' : 'hold'
25-
Puppet.info("refresh-inhibit = #{mark}")
55+
mark = snap['hold'].nil? ? :none : :hold
56+
Puppet.info("name = #{snap['name']}, refresh-inhibit = #{mark}")
2657
new(name: snap['name'], ensure: snap['tracking-channel'], mark: mark, hold_time: snap['hold'], provider: 'snap')
2758
end
2859
end
@@ -65,12 +96,13 @@ def hold
6596
Puppet.info('called hold')
6697
Puppet.info("@property_hash = #{@property_hash}")
6798
Puppet.info("install_options = #{@resource[:install_options]}")
68-
modify_snap('hold') if should_change_hold?(@resource[:install_options])
99+
modify_snap('hold')
100+
# @property_hash[:mark] = 'hold'
69101
end
70102

71103
def unhold
72104
Puppet.info('called unhold')
73-
modify_snap('unhold') unless @property_hash[:mark].equal?('none')
105+
modify_snap('unhold')
74106
end
75107

76108
def modify_snap(action, options = @resource[:install_options])
@@ -139,18 +171,20 @@ def self.parse_time_from_options(options)
139171
end
140172
end
141173

142-
def should_change_hold?(options)
174+
def self.should_change_hold?(options, current_hold_time)
143175
should_hold_time = self.class.parse_time_from_options(options)
144-
current_hold_time = @property_hash[:hold_time]
176+
# current_hold_time = @property_hash[:hold_time]
177+
178+
# if current hold time is nil we are fresh holding this snap
179+
return true if current_hold_time.nil?
145180

146181
parsed_hold_time = DateTime.parse(current_hold_time)
147182
# If the hold time is more than 100 years, assume "forever"
148183
current_hold_time = 'forever' if (parsed_hold_time - DateTime.now).to_i > 365 * 100
149184

150185
Puppet.info("should = #{should_hold_time}, current = #{current_hold_time}")
151-
152-
false if current_hold_time == should_hold_time
153-
true
186+
Puppet.info("equal = #{current_hold_time == should_hold_time}")
187+
current_hold_time != should_hold_time
154188
end
155189

156190
def self.channel_from_options(options)

spec/acceptance/01_snapd_spec.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@
110110

111111
it_behaves_like 'an idempotent resource'
112112

113-
describe command('snap list --unicode=never --color=never') do
113+
describe command('snap info --unicode=never --color=never --abs-time hello-world') do
114114
its(:stdout) do
115-
is_expected.to match(%r{hello-world})
116-
is_expected.to match(%r{beta})
117-
is_expected.to match(%r{held})
115+
is_expected.to match(%r{name:\s+hello-world})
116+
is_expected.to match(%r{tracking:\s+latest/beta})
117+
is_expected.to match(%r{hold:\s+forever})
118118
end
119119
end
120120
end
@@ -132,11 +132,11 @@
132132

133133
it_behaves_like 'an idempotent resource'
134134

135-
describe command('snap list --unicode=never --color=never') do
135+
describe command('snap info --unicode=never --color=never --abs-time hello-world') do
136136
its(:stdout) do
137-
is_expected.to match(%r{hello-world})
138-
is_expected.to match(%r{candidate})
139-
is_expected.to match(%r{held})
137+
is_expected.to match(%r{name:\s+hello-world})
138+
is_expected.to match(%r{tracking:\s+latest/candidate})
139+
is_expected.to match(%r{hold:\s+forever})
140140
end
141141
end
142142
end
@@ -155,11 +155,11 @@
155155

156156
it_behaves_like 'an idempotent resource'
157157

158-
describe command('snap list --unicode=never --color=never') do
158+
describe command('snap info --unicode=never --color=never --abs-time hello-world') do
159159
its(:stdout) do
160-
is_expected.to match(%r{hello-world})
161-
is_expected.to match(%r{candidate})
162-
is_expected.to match(%r{held})
160+
is_expected.to match(%r{name:\s+hello-world})
161+
is_expected.to match(%r{tracking:\s+latest/candidate})
162+
is_expected.to match(%r{hold:\s+2025-10-10T03:00:00})
163163
end
164164
end
165165
end
@@ -176,11 +176,11 @@
176176

177177
it_behaves_like 'an idempotent resource'
178178

179-
describe command('snap list --unicode=never --color=never') do
179+
describe command('snap info --unicode=never --color=never --abs-time hello-world') do
180180
its(:stdout) do
181-
is_expected.to match(%r{hello-world})
182-
is_expected.to match(%r{candidate})
183-
is_expected.not_to match(%r{held})
181+
is_expected.to match(%r{name:\s+hello-world})
182+
is_expected.to match(%r{tracking:\s+latest/candidate})
183+
is_expected.not_to match(%r{hold:.*})
184184
end
185185
end
186186
end

0 commit comments

Comments
 (0)