Skip to content

Commit 1aa3f50

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

File tree

2 files changed

+58
-25
lines changed

2 files changed

+58
-25
lines changed

lib/puppet/provider/package/snap.rb

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

2020
mk_resource_methods
2121

22+
def self.prefetch(resources)
23+
# Build a hash of name => install_options from the catalog
24+
desired_options = resources.transform_values { |res| res[:install_options] }
25+
26+
installed_snaps.each do |snap|
27+
resource = resources[snap['name']]
28+
next unless resource
29+
30+
current_hold_time = snap['hold']
31+
desired_hold_time = parse_time_from_options(desired_options[snap['name']])
32+
33+
# Determine the appropriate mark
34+
mark = if should_change_hold?(desired_hold_time, current_hold_time)
35+
:none # force re-hold
36+
else
37+
:hold
38+
end
39+
40+
provider = new(
41+
name: snap['name'],
42+
ensure: snap['tracking-channel'],
43+
mark: mark,
44+
hold_time: current_hold_time,
45+
provider: 'snap'
46+
)
47+
48+
resource.provider = provider
49+
end
50+
end
51+
2252
def self.instances
2353
installed_snaps.map do |snap|
24-
mark = snap['hold'].nil? ? 'none' : 'hold'
25-
Puppet.info("refresh-inhibit = #{mark}")
54+
mark = snap['hold'].nil? ? :none : :hold
55+
Puppet.info("name = #{snap['name']}, refresh-inhibit = #{mark}")
2656
new(name: snap['name'], ensure: snap['tracking-channel'], mark: mark, hold_time: snap['hold'], provider: 'snap')
2757
end
2858
end
@@ -65,12 +95,13 @@ def hold
6595
Puppet.info('called hold')
6696
Puppet.info("@property_hash = #{@property_hash}")
6797
Puppet.info("install_options = #{@resource[:install_options]}")
68-
modify_snap('hold') if should_change_hold?(@resource[:install_options])
98+
modify_snap('hold')
99+
# @property_hash[:mark] = 'hold'
69100
end
70101

71102
def unhold
72103
Puppet.info('called unhold')
73-
modify_snap('unhold') unless @property_hash[:mark].equal?('none')
104+
modify_snap('unhold')
74105
end
75106

76107
def modify_snap(action, options = @resource[:install_options])
@@ -139,18 +170,20 @@ def self.parse_time_from_options(options)
139170
end
140171
end
141172

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

146180
parsed_hold_time = DateTime.parse(current_hold_time)
147181
# If the hold time is more than 100 years, assume "forever"
148182
current_hold_time = 'forever' if (parsed_hold_time - DateTime.now).to_i > 365 * 100
149183

150184
Puppet.info("should = #{should_hold_time}, current = #{current_hold_time}")
151-
152-
false if current_hold_time == should_hold_time
153-
true
185+
Puppet.info("equal = #{current_hold_time == should_hold_time}")
186+
current_hold_time != should_hold_time
154187
end
155188

156189
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)