Created
May 16, 2013 19:41
-
-
Save parthaa/5594470 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HeadpinMigration < ActiveRecord::Migration | |
if Katello.config.headpin? | |
class KTEnvironment < ActiveRecord::Base | |
self.table_name = "environments" | |
belongs_to :organization, :inverse_of => :environments | |
has_many :systems, :inverse_of => :environment, :dependent => :destroy, | |
:foreign_key => :environment_id | |
before_destroy :remove_from_candlepin | |
def remove_from_candlepin | |
Rails.logger.info _("Deleting environment in candlepin: %s") % self.label | |
Resources::Candlepin::Environment.destroy(self.cp_id) | |
rescue => e | |
Rails.logger.error _("Failed to delete candlepin environment %s") % | |
"#{self.label}: #{e}, #{e.backtrace.join("\n")}" | |
raise e | |
end | |
end | |
class SystemGroup < ActiveRecord::Base | |
has_many :system_system_groups, :dependent => :destroy | |
has_many :systems, {:through => :system_system_groups} | |
belongs_to :organization | |
before_save :set_pulp_id | |
def set_pulp_id | |
self.pulp_id = "boooo#{rand 10000000}" | |
end | |
end | |
class System < ActiveRecord::Base | |
has_many :system_system_groups, :dependent => :destroy | |
has_many :system_groups, {:through => :system_system_groups} | |
belongs_to :environment, :class_name => "KTEnvironment", :inverse_of => :systems | |
has_many :system_activation_keys, :dependent => :destroy | |
has_many :activation_keys, :through => :system_activation_keys | |
before_save :update_consumer_env_in_cp | |
def update_consumer_env_in_cp | |
attrs = {} | |
attrs[:environment] = self.cp_environment_id if self.cp_environment_id | |
unless attrs.empty? | |
consumer_class = Resources::Candlepin::Consumer | |
consumer_class.put(consumer_class.path(self.uuid), | |
attrs.to_json, | |
consumer_class.default_headers).body | |
end | |
end | |
end | |
class ActivationKey < ActiveRecord::Base | |
belongs_to :environment, :class_name => "KTEnvironment" | |
belongs_to :content_view, :inverse_of => :activation_keys | |
has_many :key_system_groups, :dependent => :destroy | |
has_many :system_groups, :through => :key_system_groups | |
end | |
class User < ActiveRecord::Base | |
scope :hidden, where(:hidden => true) | |
def cp_oauth_header | |
{ 'cp-user' => self.username } | |
end | |
end | |
class Organization < ActiveRecord::Base | |
has_many :environments, :class_name => "KTEnvironment", :dependent => :destroy, :inverse_of => :organization | |
has_one :library, :class_name =>"KTEnvironment", :conditions => {:library => true}, :dependent => :destroy | |
end | |
end | |
def up | |
if Katello.config.headpin? | |
KTEnvironment.reset_column_information | |
SystemGroup.reset_column_information | |
System.reset_column_information | |
ActivationKey.reset_column_information | |
Organization.reset_column_information | |
User.reset_column_information | |
# * A system group will be made for each existing env | |
# * Systems will be added to appropriate system group | |
# * All systems moved to Library env | |
#User.current = User.hidden.first | |
non_library_envs = KTEnvironment.where(:library => false) | |
non_library_envs.each do |env| | |
# create a system group with the same name as env | |
group = SystemGroup.create!(:name => env.name, :description => "Group systems in #{env.name}", | |
:organization => env.organization) | |
system_ids = env.system_ids | |
system_ids.each do |system_id| | |
s = System.find(system_id) | |
s.system_groups << group | |
s.environment = env.organization.library | |
s.content_view = nil | |
s.save! | |
end | |
end | |
# All users with a default system registration env will have that changed to Library | |
User.all.each do |user| | |
if user.default_environment && !user.default_environment.library? | |
user.default_environment = user.default_environment.organization.library | |
user.save! | |
end | |
end | |
# All activation keys will be switched to Library | |
ActivationKey.all.each do |ak| | |
if ak.environment && !ak.environment.library? | |
ak.content_view = nil | |
ak.environment = ak.environment.organization.library | |
ak.system_groups << ActivationKey.find_by_name(ak.environment.name) | |
ak.save! | |
end | |
end | |
# Delete the non library envs | |
Organization.all.each do |org| | |
org.promotion_paths.each do |path| | |
path.reverse.each do |env| | |
env.reload.destroy unless env.library? | |
end | |
end | |
end | |
end | |
end | |
def down | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment