Skip to content

Commit 211c2fd

Browse files
author
Bastian Schmidt
committed
Fixes #37897 - Separate autosign key generation and configuration
* Replace the update of salt_autosign_key with setting the field after_validation to guarantee that the host is not updated during deployment * Move the API calls, configuring the autosign key on the corresponding proxy, to an additional orchestration stage to make it visible to the user and reduce delay during validation/saving.
1 parent 382d1dc commit 211c2fd

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

app/models/foreman_salt/concerns/host_managed_extensions.rb

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def inherited_attributes
4242

4343
validate :salt_modules_in_host_environment
4444

45-
before_provision :ensure_salt_autosign, if: ->(host) { host.salt_proxy }
46-
before_destroy :remove_salt_minion, if: ->(host) { host.salt_proxy }
45+
after_validation :queue_ensure_salt_autosign, if: ->(host) { host.salt_proxy }
46+
before_destroy :queue_remove_salt_minion, if: ->(host) { host.salt_proxy }
4747
end
4848

4949
def salt_params
@@ -107,10 +107,33 @@ def derive_salt_grains(use_autosign: false)
107107

108108
private
109109

110+
def queue_ensure_salt_autosign
111+
unless build
112+
generate_salt_autosign_key
113+
queue.create(id: "ensure_salt_autosign_#{id}", name: _('Configure Salt Autosign key for %s') % self,
114+
priority: 101, action: [self, :ensure_salt_autosign])
115+
end
116+
end
117+
118+
def queue_remove_salt_minion
119+
queue.create(id: "queue_remove_salt_minion_#{id}", name: _('Remove Salt Minion for %s') % self,
120+
priority: 101, action: [self, :remove_salt_minion])
121+
end
122+
123+
def generate_salt_autosign_key
124+
if salt_autosign_key.nil?
125+
Rails.logger.info("Generate salt autosign key for #{fqdn}")
126+
key = generate_provisioning_key
127+
self.salt_autosign_key = key
128+
else
129+
Rails.logger.info("Use existing salt autosign key for #{fqdn}")
130+
end
131+
self.salt_status = ForemanSalt::SaltStatus.minion_auth_waiting
132+
end
133+
110134
def ensure_salt_autosign
111135
remove_salt_key
112-
remove_salt_autosign
113-
create_salt_autosign
136+
configure_salt_autosign
114137
end
115138

116139
def remove_salt_minion
@@ -140,13 +163,11 @@ def generate_provisioning_key
140163
SecureRandom.hex(10)
141164
end
142165

143-
def create_salt_autosign
144-
Rails.logger.info("Create salt autosign key for host #{fqdn}")
166+
def configure_salt_autosign
167+
Rails.logger.info("Configure salt autosign key for host #{fqdn} on #{salt_proxy.url}")
145168
api = ProxyAPI::Salt.new(url: salt_proxy.url)
146-
key = generate_provisioning_key
169+
key = salt_autosign_key
147170
api.autosign_create_key(key)
148-
update(salt_autosign_key: key)
149-
update(salt_status: ForemanSalt::SaltStatus.minion_auth_waiting)
150171
rescue Foreman::Exception => e
151172
Rails.logger.warn("Unable to create salt autosign for #{fqdn}: #{e}")
152173
end

0 commit comments

Comments
 (0)