-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #18639 - lock all templates we seed #4320
Conversation
db/seeds.d/07-data.rb
Outdated
|
||
# Template kinds | ||
kinds = {} | ||
TemplateKind.default_template_labels.keys.collect(&:to_sym).each do |type| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer map over collect.
db/seeds.d/07-data.rb
Outdated
|
||
# Template kinds | ||
kinds = {} | ||
TemplateKind.default_template_labels.keys.collect(&:to_sym).each do |type| |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer map over collect.
@ares: How about taking a more radical approach and rename all seeded templates that have previously been modified so we can make sure all original templates are present and locked. I think, we should go all the way with this. |
@@ -105,7 +17,8 @@ | |||
|
|||
t = ProvisioningTemplate.create({ | |||
:snippet => false, | |||
:template => contents | |||
:template => contents, | |||
:locked => true | |||
}.merge(input)) | |||
|
|||
if t.default? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know, this is unrelated to this PR, but isn't t.default?
always true here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, it's always set like that few lines above, I'll remove the if condition while I'm at it
@timogoebel I'm afraid this won't work reliably for snippets, their names are hardcoded in templates. I'd like to avoid updating their names in existing templates. Since we override default templates with every seed (not sure for how many versions) I hope there won't be custom changes in seeded templates. If renaming is preferred, maybe we could do it for all templates except snippets? |
Tested and 👍 to the change once Katello tests are green. I was a bit confused by one thing: If I modified the "PXElinux default template" because I wanted to add custom stuff, then update Foreman to a version with this, my change remains in the locked template, but if I want to keep it i should clone it or rename it, correct? If that's the case, still 👍 but let's warn about it in the release and upgrade notes |
Actually, I think this is still bad design. I think, we should only lock seeded templates and not continue with this mess. |
I think that's what the PR does. @dLobatog if you want to keep such changes to survive the update, you need to rename it anyway because we currently override any custom changes on |
@timogoebel That's what this PR is for 😄 the locking functionality was there because Katello did this for a long time already. |
app/models/template.rb
Outdated
@@ -57,6 +58,13 @@ def filename | |||
name.downcase.delete('-').gsub(/\s+/, '_') + '.erb' | |||
end | |||
|
|||
def ignore_locking(&block) | |||
self.modify_locked = true | |||
block.call(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use yield instead of block.call.
3a3e947
to
56728d8
Compare
app/models/template.rb
Outdated
errors.add(:base, _("You are not authorized to lock templates.")) | ||
end | ||
end | ||
|
||
if actual_changes.include? 'default' | ||
unless User.current.can?(:create_organizations) || User.current.can?(:create_locations) | ||
if actual_changes.include? 'default' && !self.modify_default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use parentheses in the method call to avoid confusion about precedence.
app/models/template.rb
Outdated
@@ -71,19 +86,19 @@ def template_changes | |||
actual_changes = changes | |||
|
|||
# Locked & Default are Special | |||
if actual_changes.include? 'locked' | |||
unless User.current.can?("lock_#{self.class.to_s.underscore.pluralize}", self) | |||
if actual_changes.include? 'locked' && !self.modify_locked |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use parentheses in the method call to avoid confusion about precedence.
7cde2d2
to
b1cccdb
Compare
Rebased to avoid compute resource test failures, should be green now. |
Foreman issues seems unrelated
|
[test katello] |
Rebased again |
code-wise looks good, waiting for feedback from @dLobatog since he reviewed this more deeply. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ares 👍 from me again, I just have 2 questions you can reply to inline. Thanks for this!
@@ -1,5 +1,6 @@ | |||
class Template < ActiveRecord::Base | |||
include Exportable | |||
attr_accessor :modify_locked, :modify_default |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about making this an attr_reader
in order to prevent modifications outside of this class (using the ignore_locking/ignore_default helpers)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I could do that but I though people might find it useful e.g. in tests when they just disable locking in setup and re-enable in teardown even though the ignore_locking
is preferable elsewhere
class LockSeededTemplates < ActiveRecord::Migration | ||
def up | ||
names = (SEEDED_TEMPLATES + SEEDED_PARTITION_TABLES).map { |attrs| attrs[:name] } | ||
Template.where(:name => names).update_all(:locked => true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed if :locked => true
is set in the seed https://github.com/theforeman/foreman/pull/4320/files#diff-151633105c29141fe3aac7603de185a5R25 ? Is this only relevant for partition tables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On existing db we need to lock all templates, they were seeded unlocked in previous versions. So we find all templates by name that would be locked in new installation and lock them.
Thanks @ares & @tbrisker @timogoebel ! |
The PR looks big but what it does is very simple
For more information and motivations, please see the discussion on foreman-dev list