Skip to content
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

Merged
merged 1 commit into from
Mar 28, 2017

Conversation

ares
Copy link
Member

@ares ares commented Feb 23, 2017

The PR looks big but what it does is very simple

  • extracts names into constants and into separate seed files so they can be reused safely in migration without triggering the seed
  • adds a migration that sets locked flag for seeded templates to true
  • makes sure all newly seeded templates are locked

For more information and motivations, please see the discussion on foreman-dev list

@mention-bot
Copy link

@ares, thanks for your PR! By analyzing the history of the files in this pull request, we identified @dLobatog, @domcleal and @stbenjam to be potential reviewers.


# Template kinds
kinds = {}
TemplateKind.default_template_labels.keys.collect(&:to_sym).each do |type|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.


# Template kinds
kinds = {}
TemplateKind.default_template_labels.keys.collect(&:to_sym).each do |type|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer map over collect.

@timogoebel
Copy link
Member

@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?
Copy link
Member

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?

Copy link
Member Author

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

@ares
Copy link
Member Author

ares commented Feb 24, 2017

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.

@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?

@dLobatog
Copy link
Member

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

@timogoebel
Copy link
Member

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?

Actually, I think this is still bad design. I think, we should only lock seeded templates and not continue with this mess.
Let's ship locked default templates managed by us and users can "fork" them if they choose to do so.

@ares
Copy link
Member Author

ares commented Mar 14, 2017

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 rake db:seed.

@dLobatog
Copy link
Member

@timogoebel That's what this PR is for 😄 the locking functionality was there because Katello did this for a long time already.

@@ -57,6 +58,13 @@ def filename
name.downcase.delete('-').gsub(/\s+/, '_') + '.erb'
end

def ignore_locking(&block)
self.modify_locked = true
block.call(self)

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.

@ares ares force-pushed the fix/18639 branch 2 times, most recently from 3a3e947 to 56728d8 Compare March 17, 2017 15:56
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

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.

@@ -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

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.

@ares ares force-pushed the fix/18639 branch 2 times, most recently from 7cde2d2 to b1cccdb Compare March 23, 2017 09:27
@ares
Copy link
Member Author

ares commented Mar 23, 2017

Rebased to avoid compute resource test failures, should be green now.

@timogoebel
Copy link
Member

@dLobatog / @tbrisker : Does anyone of you have time to check this one out? We should get this in. :-)

@ares
Copy link
Member Author

ares commented Mar 23, 2017

Foreman issues seems unrelated

  • HostsControllerTest.test_0070_select multiple action with valid host_ids param should return a selection page (pg/ruby 2.4.0)
  • ComputeResourceJSIntegrationTest.test_0003_compute resource password isn't deleted when testing connection (pg/ruby 2.0.0)

@ares
Copy link
Member Author

ares commented Mar 23, 2017

[test katello]

@ares
Copy link
Member Author

ares commented Mar 24, 2017

Rebased again

@tbrisker
Copy link
Member

code-wise looks good, waiting for feedback from @dLobatog since he reviewed this more deeply.

Copy link
Member

@dLobatog dLobatog left a 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
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

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?

Copy link
Member Author

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.

@dLobatog dLobatog merged commit 4a98e18 into theforeman:develop Mar 28, 2017
@dLobatog
Copy link
Member

Thanks @ares & @tbrisker @timogoebel !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants