/
seeds.rb
165 lines (141 loc) · 6.46 KB
/
seeds.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# !!! PLEASE KEEP THIS SCRIPT IDEMPOTENT !!!
#
require 'util/password'
def format_errors(model = nil)
return '(nil found)' if model.nil?
model.errors.full_messages.join(';')
end
if ENV['SEED_ORGANIZATION']
Organization.without_auditing do
User.current = User.anonymous_admin
org = Organization.find_by_name!(ENV['SEED_ORGANIZATION'])
ForemanTasks.sync_task(::Actions::Katello::Organization::Create, org) unless org.library
User.current = nil
end
if Setting['db_pending_seed']
admin = User.where(:login => ENV['SEED_ADMIN_USER'].present? ? ENV['SEED_ADMIN_USER'] : 'admin').first
if admin && admin.default_organization.nil?
admin.default_organization = Organization.find_by_name!(ENV['SEED_ORGANIZATION'])
admin.save!
end
end
end
::User.current = ::User.anonymous_api_admin
if Katello.config.use_pulp
Katello::Repository.ensure_sync_notification
end
# Provisioning Templates
kinds = [:provision, :finish, :user_data].inject({}) do |hash, kind|
hash[kind] = TemplateKind.find_by_name(kind)
hash
end
defaults = {:vendor => "Katello", :default => true, :locked => true}
templates = [{:name => "Katello Kickstart Default", :source => "kickstart-katello.erb", :template_kind => kinds[:provision]},
{:name => "Katello Kickstart Default User Data", :source => "userdata-katello.erb", :template_kind => kinds[:user_data]},
{:name => "Katello Kickstart Default Finish", :source => "finish-katello.erb", :template_kind => kinds[:finish]},
{:name => "subscription_manager_registration", :source => "snippets/_subscription_manager_registration.erb", :snippet => true}]
templates.each do |template|
template[:template] = File.read(File.join(Katello::Engine.root, "app/views/foreman/unattended", template.delete(:source)))
ConfigTemplate.find_or_create_by_name(template).update_attributes(defaults.merge(template))
end
# Ensure all default templates are seeded into the first org and loc
ConfigTemplate.where(:default => true).each do |template|
template.organizations << Organization.first unless template.organizations.include?(Organization.first) || Organization.count.zero?
template.locations << Location.first unless template.locations.include?(Location.first) || Location.count.zero?
end
Katello::Util::Search.backend_search_classes.each { |c| c.create_index }
# Proxy features
feature = Feature.find_or_create_by_name('Pulp')
if feature.nil? || feature.errors.any?
fail "Unable to create proxy feature: #{format_errors feature}"
end
# Roles and Permissions
permissions = [
%w(Katello::ActivationKey view_activation_keys),
%w(Katello::ActivationKey create_activation_keys),
%w(Katello::ActivationKey edit_activation_keys),
%w(Katello::ActivationKey destroy_activation_keys),
%w(SmartProxy manage_capsule_content),
%w(Katello::System view_content_hosts),
%w(Katello::System create_content_hosts),
%w(Katello::System edit_content_hosts),
%w(Katello::System destroy_content_hosts),
%w(Katello::ContentView view_content_views),
%w(Katello::ContentView create_content_views),
%w(Katello::ContentView edit_content_views),
%w(Katello::ContentView destroy_content_views),
%w(Katello::ContentView publish_content_views),
%w(Katello::ContentView promote_or_remove_content_views),
%w(Katello::GpgKey view_gpg_keys),
%w(Katello::GpgKey create_gpg_keys),
%w(Katello::GpgKey edit_gpg_keys),
%w(Katello::GpgKey destroy_gpg_keys),
%w(Katello::HostCollection view_host_collections),
%w(Katello::HostCollection create_host_collections),
%w(Katello::HostCollection edit_host_collections),
%w(Katello::HostCollection destroy_host_collections),
%w(Katello::KTEnvironment view_lifecycle_environments),
%w(Katello::KTEnvironment create_lifecycle_environments),
%w(Katello::KTEnvironment edit_lifecycle_environments),
%w(Katello::KTEnvironment destroy_lifecycle_environments),
%w(Katello::KTEnvironment promote_or_remove_content_views_to_environments),
%w(Katello::Product view_products),
%w(Katello::Product create_products),
%w(Katello::Product edit_products),
%w(Katello::Product destroy_products),
%w(Katello::Product sync_products),
%w(Katello::SyncPlan view_sync_plans),
%w(Katello::SyncPlan create_sync_plans),
%w(Katello::SyncPlan edit_sync_plans),
%w(Katello::SyncPlan destroy_sync_plans),
%w(Organization view_subscriptions),
%w(Organization attach_subscriptions),
%w(Organization unattach_subscriptions),
%w(Organization import_manifest),
%w(Organization delete_manifest)
]
permissions.each do |resource, permission|
Permission.find_or_create_by_resource_type_and_name resource, permission
end
default_permissions = {
:Viewer => [:view_activation_keys, :view_content_hosts, :view_content_views, :view_gpg_keys, :view_host_collections,
:view_lifecycle_environments, :view_products, :view_subscriptions, :view_sync_plans]
}
Role.without_auditing do
default_permissions.each do |role_name, permission_names|
permissions = Permission.find_all_by_name permission_names
create_filters(Role.find_by_name(role_name), permissions)
end
end
Setting.find_by_name("dynflow_enable_console").update_attributes!(:value => true) if Rails.env.development?
["Pulp", "Pulp Node"].each do |input|
f = Feature.find_or_create_by_name(input)
fail "Unable to create proxy feature: #{format_errors f}" if f.nil? || f.errors.any?
end
# Mail Notifications
notifications = [
{:name => :katello_host_advisory,
:description => N_('A summary of available and applicable errata for your hosts'),
:mailer => 'Katello::ErrataMailer',
:method => 'host_errata',
:subscription_type => 'report'
},
{:name => :katello_sync_errata,
:description => N_('A summary of new errata after a repository is synchronized'),
:mailer => 'Katello::ErrataMailer',
:method => 'sync_errata',
:subscription_type => 'alert'
},
{:name => :katello_promote_errata,
:description => N_('A post-promotion summary of hosts with installable errata'),
:mailer => 'Katello::ErrataMailer',
:method => 'promote_errata',
:subscription_type => 'alert'
}
]
notifications.each do |notification|
::MailNotification.find_or_create_by_name(notification)
end