Skip to content

Commit

Permalink
Refs #10055 - Initial model bindings for OSTREE CRUD
Browse files Browse the repository at this point in the history
  • Loading branch information
parthaa committed May 21, 2015
1 parent 2a10707 commit 1f3d9f0
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 30 deletions.
33 changes: 27 additions & 6 deletions app/controllers/katello/api/v2/repositories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Katello
class Api::V2::RepositoriesController < Api::V2::ApiController
wrap_parameters :include => (Repository.attribute_names + [:ostree_branches])

before_filter :find_organization, :only => [:index]
before_filter :find_product, :only => [:index]
before_filter :find_product_for_create, :only => [:create]
Expand All @@ -24,9 +26,10 @@ class Api::V2::RepositoriesController < Api::V2::ApiController
param :url, String, :desc => N_("repository source url")
param :gpg_key_id, :number, :desc => N_("id of the gpg key that will be assigned to the new repository")
param :unprotected, :bool, :desc => N_("true if this repository can be published via HTTP")
param :content_type, String, :required => true, :desc => N_("type of repo (either 'yum', 'puppet' or 'docker')")
param :content_type, String, :required => true, :desc => N_("type of repo (either 'yum', 'puppet', 'docker', or 'ostree')")
param :checksum_type, String, :desc => N_("checksum of the repository, currently 'sha1' & 'sha256' are supported.'")
param :docker_upstream_name, String, :desc => N_("name of the upstream docker repository")
param :ostree_branches, Array, :desc => N_("list of ostree branch refs associated to an rpm ostree repository")
end

api :GET, "/repositories", N_("List of enabled repositories")
Expand Down Expand Up @@ -93,8 +96,9 @@ def create
repository = @product.add_repo(repo_params[:label], repo_params[:name], repo_params[:url],
repo_params[:content_type], repo_params[:unprotected],
gpg_key, repository_params[:checksum_type])

repository.docker_upstream_name = repo_params[:docker_upstream_name] if repo_params[:docker_upstream_name]
sync_task(::Actions::Katello::Repository::Create, repository, false, true)
sync_task(::Actions::Katello::Repository::Create, repository, false, true, repo_params[:ostree_branches])
repository = Repository.find(repository.id)
respond_for_show(:resource => repository)
end
Expand All @@ -120,9 +124,10 @@ def sync
param :checksum_type, String, :desc => N_("checksum of the repository, currently 'sha1' & 'sha256' are supported.'")
param :url, String, :desc => N_("the feed url of the original repository ")
param :docker_upstream_name, String, :desc => N_("name of the upstream docker repository")
param :ostree_branches, Array, :desc => N_("list of ostree branch refs associated to an rpm ostree repository")
def update
repo_params = repository_params
repo_params[:url] = nil if repository_params[:url].blank?
repo_params[:url] = nil if repository_params[:url].blank? && !@repository.ostree?
sync_task(::Actions::Katello::Repository::Update, @repository, repo_params)
respond_for_show(:resource => @repository)
end
Expand Down Expand Up @@ -247,9 +252,25 @@ def find_gpg_key
end

def repository_params
keys = [:url, :gpg_key_id, :unprotected, :name, :checksum_type, :docker_upstream_name]
keys += [:label, :content_type] if params[:action] == "create"
params.require(:repository).permit(*keys)
if params[:action] == "create"
params.require(:repository).permit(:label,
:content_type,
:url,
:gpg_key_id,
:unprotected,
:name,
:checksum_type,
:docker_upstream_name,
:ostree_branches => [])
else
params.require(:repository).permit(:url,
:gpg_key_id,
:unprotected,
:name,
:checksum_type,
:docker_upstream_name,
:ostree_branches => [])
end
end

def error_on_rh_product
Expand Down
8 changes: 6 additions & 2 deletions app/lib/actions/katello/repository/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ module Katello
module Repository
class Create < Actions::EntryAction
# rubocop:disable MethodLength
def plan(repository, clone = false, plan_create = false)
def plan(repository, clone = false, plan_create = false, ostree_branches = [])
repository.disable_auto_reindex!
repository.save!
ostree_branches.each do |branch_name|
repository.ostree_branches.create!(:name => branch_name)
end if ostree_branches
action_subject(repository)

org = repository.organization
Expand All @@ -25,7 +28,8 @@ def plan(repository, clone = false, plan_create = false)
unprotected: repository.unprotected,
checksum_type: repository.checksum_type,
path: path,
with_importer: true)
with_importer: true,
ostree_branches: repository.ostree_branch_names)

return if create_action.error

Expand Down
2 changes: 2 additions & 0 deletions app/lib/actions/katello/repository/metadata_generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def distributor_class(repository, clone)
Runcible::Models::IsoDistributor
when ::Katello::Repository::DOCKER_TYPE
Runcible::Models::DockerDistributor
when ::Katello::Repository::OSTREE_TYPE
Runcible::Models::OstreeDistributor
end
end
end
Expand Down
14 changes: 14 additions & 0 deletions app/lib/actions/katello/repository/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ class Update < Actions::EntryAction
def plan(repository, repo_params)
repository.disable_auto_reindex!
action_subject repository
ostree_branches = repo_params.delete(:ostree_branches)
if ostree_branches
# remove the ostree_branches not in this list
repository.ostree_branches.keep_if do |branch|
ostree_branches.include?(branch.name)
end

# add the new ostree_branches
(ostree_branches - repository.ostree_branch_names).each do |ref|
repository.ostree_branches.create!(:name => ref)
end
end
repository.update_attributes!(repo_params)

if (::Katello.config.use_cp && ::Katello.config.use_pulp)
Expand Down Expand Up @@ -49,6 +61,8 @@ def distributor_type_id(content_type)
Runcible::Models::IsoDistributor
when ::Katello::Repository::DOCKER_TYPE
Runcible::Models::DockerDistributor
when ::Katello::Repository::OSTREE_TYPE
Runcible::Models::OstreeDistributor
end
distributor.type_id
end
Expand Down
68 changes: 49 additions & 19 deletions app/lib/actions/pulp/repository/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Create < Pulp::Abstract
param :path
param :with_importer
param :docker_upstream_name
param :ostree_branches
end

def run
Expand All @@ -28,32 +29,51 @@ def run
end

def importer
case input[:content_type]
when ::Katello::Repository::YUM_TYPE, ::Katello::Repository::FILE_TYPE
yum_or_iso_importer
when ::Katello::Repository::PUPPET_TYPE
puppet_importer
when ::Katello::Repository::DOCKER_TYPE
docker_importer
when ::Katello::Repository::OSTREE_TYPE
ostree_importer
else
fail _("Unexpected repo type %s") % input[:content_type]
end
end

def yum_or_iso_importer
importer = case input[:content_type]
when ::Katello::Repository::YUM_TYPE
Runcible::Models::YumImporter.new
when ::Katello::Repository::FILE_TYPE
Runcible::Models::IsoImporter.new
when ::Katello::Repository::PUPPET_TYPE
Runcible::Models::PuppetImporter.new
when ::Katello::Repository::DOCKER_TYPE
Runcible::Models::DockerImporter.new
end
importer.feed = input[:feed]
importer.ssl_ca_cert = input[:ssl_ca_cert]
importer.ssl_client_cert = input[:ssl_client_cert]
importer.ssl_client_key = input[:ssl_client_key]
importer
end

if input[:with_importer]
case input[:content_type]
when ::Katello::Repository::YUM_TYPE,
::Katello::Repository::FILE_TYPE
importer.feed = input[:feed]
importer.ssl_ca_cert = input[:ssl_ca_cert]
importer.ssl_client_cert = input[:ssl_client_cert]
importer.ssl_client_key = input[:ssl_client_key]
when ::Katello::Repository::PUPPET_TYPE
importer.feed = input[:feed]
when ::Katello::Repository::DOCKER_TYPE
importer.upstream_name = input[:docker_upstream_name] if input[:docker_upstream_name]
importer.feed = input[:feed]
end
end
def ostree_importer
importer = Runcible::Models::OstreeImporter.new
importer.feed = input[:feed]
importer.branches = input[:ostree_branches] unless input[:ostree_branches].blank?
importer
end

def puppet_importer
importer = Runcible::Models::PuppetImporter.new
importer.feed = input[:feed]
importer
end

def docker_importer
importer = Runcible::Models::DockerImporter.new
importer.upstream_name = input[:docker_upstream_name] if input[:docker_upstream_name]
importer.feed = input[:feed]
importer
end

Expand All @@ -67,6 +87,8 @@ def distributors
input[:path].blank? ? [] : [puppet_install_distributor, nodes_distributor]
when ::Katello::Repository::DOCKER_TYPE
[docker_distributor, nodes_distributor]
when ::Katello::Repository::OSTREE_TYPE
[ostree_distributor, nodes_distributor]
else
fail _("Unexpected repo type %s") % input[:content_type]
end
Expand Down Expand Up @@ -110,6 +132,14 @@ def docker_distributor
auto_publish: true }
Runcible::Models::DockerDistributor.new(options)
end

def ostree_distributor
ostree_path_url = URI(input[:feed])

options = { id: input[:pulp_id],
relative_path: ostree_path_url.path}
Runcible::Models::OstreeDistributor.new(options)
end
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/katello/glue/pulp/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def generate_importer
options[:upstream_name] = self.docker_upstream_name
options[:feed] = self.url if self.respond_to?(:url)
Runcible::Models::DockerImporter.new(options)
when Repository::OSTREE_TYPE
options = {}
options[:feed] = self.url if self.respond_to?(:url)
options[:ostree_branches] = self.ostree_branch_names
Runcible::Models::OstreeImporter.new(options)
else
fail _("Unexpected repo type %s") % self.content_type
end
Expand Down Expand Up @@ -166,6 +171,12 @@ def generate_distributors
:auto_publish => true }
docker_dist = Runcible::Models::DockerDistributor.new(options)
[docker_dist, nodes_distributor]
when Repository::OSTREE_TYPE
options = { :protected => !self.unprotected,
:id => self.pulp_id,
:auto_publish => true }
dist = Runcible::Models::OstreeDistributor.new(options)
[dist, nodes_distributor]
else
fail _("Unexpected repo type %s") % self.content_type
end
Expand All @@ -185,6 +196,8 @@ def importer_type
Runcible::Models::PuppetImporter::ID
when Repository::DOCKER_TYPE
Runcible::Models::DockerImporter::ID
when Repository::OSTREE_TYPE
Runcible::Models::OstreeImporter::ID
else
fail _("Unexpected repo type %s") % self.content_type
end
Expand Down Expand Up @@ -716,6 +729,8 @@ def unit_type_id
"puppet_module"
when Repository::DOCKER_TYPE
"docker_image"
when Repository::OSTREE_TYPE
"ostree"
end
end

Expand Down
14 changes: 14 additions & 0 deletions app/models/katello/ostree_branch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Katello
class OstreeBranch < Katello::Model
belongs_to :repository, :class_name => "Katello::Repository", :inverse_of => :ostree_branches
validates :name, presence: true, uniqueness: {scope: :repository_id}
validates :repository_id, :presence => true
validate :ensure_ostree_repository

def ensure_ostree_repository
unless self.repository.ostree?
errors.add(:base, _("Branch cannot be created since it does not belong to an RPM OSTree repository."))
end
end
end
end
21 changes: 19 additions & 2 deletions app/models/katello/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class Repository < Katello::Model
FILE_TYPE = 'file'
PUPPET_TYPE = 'puppet'
DOCKER_TYPE = 'docker'
TYPES = [YUM_TYPE, FILE_TYPE, PUPPET_TYPE, DOCKER_TYPE]
SELECTABLE_TYPES = [YUM_TYPE, PUPPET_TYPE, DOCKER_TYPE]
OSTREE_TYPE = 'ostree'
TYPES = [YUM_TYPE, FILE_TYPE, PUPPET_TYPE, DOCKER_TYPE, OSTREE_TYPE]
SELECTABLE_TYPES = [YUM_TYPE, PUPPET_TYPE, DOCKER_TYPE, OSTREE_TYPE]
CHECKSUM_TYPES = %w(sha1 sha256)

belongs_to :environment, :inverse_of => :repositories, :class_name => "Katello::KTEnvironment"
Expand All @@ -44,6 +45,8 @@ class Repository < Katello::Model
has_many :docker_images, :through => :repository_docker_images
has_many :docker_tags, :dependent => :destroy, :class_name => "Katello::DockerTag"

has_many :ostree_branches, :class_name => "Katello::OstreeBranch", :dependent => :destroy

has_many :system_repositories, :class_name => "Katello::SystemRepository", :dependent => :destroy
has_many :systems, :through => :system_repositories

Expand Down Expand Up @@ -75,6 +78,7 @@ class Repository < Katello::Model
:message => (_("must be one of the following: %s") % TYPES.join(', '))
}
validate :ensure_valid_docker_attributes, :if => :docker?
validate :ensure_has_url_for_ostree, :if => :ostree?

# TODO: remove this default scope
# rubocop:disable Rails/DefaultScope
Expand Down Expand Up @@ -130,6 +134,10 @@ def docker?
content_type == DOCKER_TYPE
end

def ostree?
content_type == OSTREE_TYPE
end

def archive?
self.environment.nil?
end
Expand Down Expand Up @@ -481,6 +489,10 @@ def import_system_applicability
end
end

def ostree_branch_names
self.ostree_branches.map(&:name)
end

protected

def assert_deletable
Expand Down Expand Up @@ -515,5 +527,10 @@ def ensure_valid_docker_attributes
errors.add(:base, N_("Repository URL or Upstream Name is empty. Both are required for syncing from the upstream."))
end
end

def ensure_has_url_for_ostree
return true if url.present? || library_instance_id
errors.add(:url, N_("cannot be blank. RPM OSTree Repository URL required for syncing from the upstream."))
end
end
end
1 change: 1 addition & 0 deletions app/views/katello/api/v2/repositories/show.json.rabl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ attributes :gpg_key_id
attributes :content_id, :content_view_version_id, :library_instance_id
attributes :product_type
attributes :promoted? => :promoted
attributes :ostree_branch_names => :ostree_branches

node :content_counts do |repo|
{
Expand Down
25 changes: 25 additions & 0 deletions db/migrate/20150513034751_add_ostree_branches.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class AddOstreeBranches < ActiveRecord::Migration
def up
create_table :katello_ostree_branches do |t|
t.string :name, :null => false
t.references :repository, :null => false
t.timestamps
end

add_index :katello_ostree_branches, [:repository_id],
:name => :index_branches_on_repository

add_foreign_key :katello_ostree_branches,
:katello_repositories,
:column => "repository_id",
:name => "katello_ostree_branches_repository_id_fk"

add_index :katello_ostree_branches, [:repository_id, :name],
:name => :katello_ostree_branches_repo_branch, :unique => true
end

def down
remove_foreign_key :katello_ostree_branches, :name => "katello_ostree_branches_repository_id_fk"
drop_table :katello_ostree_branches
end
end
Loading

0 comments on commit 1f3d9f0

Please sign in to comment.