Skip to content

Commit

Permalink
Refs #23794 - tweaks and refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
jturel committed Jun 18, 2019
1 parent 8fbd28a commit 809da1c
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ class Api::Rhsm::CandlepinDynflowProxyController < Api::V2::ApiController
include ForemanTasks::Triggers
include AbstractController::Callbacks

skip_before_action :authorize, :only => [:upload_package_profile, :upload_profiles]
before_action :find_host, :only => [:upload_package_profile, :upload_profiles]
before_action :authorize_client_or_user, :only => [:upload_package_profile, :upload_profiles]
before_action :deprecated, only: :deb_package_profile

skip_before_action :authorize, :only => [:upload_package_profile, :upload_profiles, :deb_package_profile]
before_action :find_host, :only => [:upload_package_profile, :upload_profiles, :deb_package_profile]
before_action :authorize_client_or_user, :only => [:upload_package_profile, :upload_profiles, :deb_package_profile]

skip_before_action :check_content_type

Expand All @@ -29,6 +31,19 @@ def upload_profiles
render :json => Resources::Candlepin::Consumer.get(@host.subscription_facet.uuid)
end

api :PUT, "/systems/:id/deb_package_profile", N_("Update installed deb packages")
param :deb_package_profile, Hash, :required => true do
param :deb_packages, Array, :required => true do
param :name, String, :required => true
param :architecture, String, :required => true
param :version, String, :required => true
end
end
param :id, String, :desc => N_("UUID of the system"), :required => true
def deb_package_profile
upload_profiles
end

def find_host(uuid = nil)
params = request.path_parameters
uuid ||= params[:id]
Expand All @@ -50,5 +65,9 @@ def client_authorized?
authorized = (User.current.uuid == @host.subscription_facet.uuid) if @host && User.consumer?
authorized
end

def deprecated
::Foreman::Deprecation.api_deprecation_warning("it will be removed in Katello 3.15 - please use /consumers/:id/profiles instead.")
end
end
end
28 changes: 4 additions & 24 deletions app/controllers/katello/api/rhsm/candlepin_proxies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class Api::Rhsm::CandlepinProxiesController < Api::V2::ApiController
around_action :repackage_message
before_action :find_host, :only => [:consumer_show, :consumer_destroy, :consumer_checkin, :enabled_repos,
:regenerate_identity_certificates, :facts,
:available_releases, :serials, :upload_tracer_profile, :deb_package_profile]
:available_releases, :serials, :upload_tracer_profile]
before_action :authorize, :only => [:consumer_create, :list_owners, :rhsm_index]
before_action :authorize_client_or_user, :only => [:consumer_show, :regenerate_identity_certificates, :upload_tracer_profile, :facts, :proxy_jobs_get_path]
before_action :authorize_client_or_admin, :only => [:hypervisors_update, :async_hypervisors_update]
before_action :authorize_proxy_routes, :only => [:get, :post, :put, :delete]
before_action :authorize_client, :only => [:consumer_destroy, :consumer_checkin,
:enabled_repos, :available_releases, :deb_package_profile]
:enabled_repos, :available_releases]

before_action :check_registration_services, :only => [:consumer_create, :consumer_destroy, :consumer_activate]

Expand Down Expand Up @@ -185,9 +185,8 @@ def regenerate_identity_certificates
end
param :id, String, :desc => N_("UUID of the system"), :required => true
def enabled_repos
fail(HttpErrors::BadRequest, _("Expected attribute is missing:") + " enabled_repos") if params['enabled_repos'].blank?
repos_params = params['enabled_repos']
repos_params = repos_params['repos'] || []
repos_params = params.dig('enabled_repos', 'repos')
fail(HttpErrors::BadRequest, _("The request did not contain any repository information.")) if repos_params.nil?

result = nil
User.as_anonymous_admin do
Expand All @@ -197,25 +196,6 @@ def enabled_repos
respond_for_show :resource => result
end

api :PUT, "/systems/:id/deb_package_profile", N_("Update installed deb packages")
param :deb_package_profile, Hash, :required => true do
param :deb_packages, Array, :required => true do
param :name, String, :required => true
param :architecture, String, :required => true
param :version, String, :required => true
end
end
param :id, String, :desc => N_("UUID of the system"), :required => true
def deb_package_profile
fail(HttpErrors::BadRequest, _("Expected attribute is missing:") + " deb_package_profile") if params['deb_package_profile'].blank?
deb_package_profile = params['deb_package_profile']
deb_packages = deb_package_profile['deb_packages'] || []
User.as_anonymous_admin do
task = async_task(::Actions::Katello::Host::UpdateDebPackageProfile, @host, deb_packages)
render :json => task
end
end

#api :POST, "/environments/:environment_id/consumers", N_("Register a consumer in environment")
def consumer_create
content_view_environment = find_content_view_environment
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/katello/api/v2/host_debs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Katello
class Api::V2::HostDebsController < Api::V2::ApiController
include Katello::Concerns::FilteredAutoCompleteSearch

before_action :find_host, :only => :index
before_action :find_host

resource_description do
api_version 'v2'
Expand Down
58 changes: 0 additions & 58 deletions app/lib/actions/katello/host/update_deb_package_profile.rb

This file was deleted.

18 changes: 16 additions & 2 deletions app/lib/actions/katello/host/upload_profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ def import_module_streams(payload, host)
Rails.logger.warn("Host with ID %s was not known to Pulp, continuing" % input[:host_id])
end

def import_deb_package_profile(host, profile)
installed_deb_ids = profile.map do |item|
::Katello::InstalledDeb.find_or_create_by(name: item['name'], architecture: item['architecture'], version: item['version']).id
end
host.installed_deb_ids = installed_deb_ids
host.save!
rescue ActiveRecord::InvalidForeignKey # this happens if the host gets deleted in between the "find_by" and "import_package_profile"
Rails.logger.warn("Host installed package list with ID %s was not able to be written to the DB (host likely is deleted), continuing" % host.id)
end

def run
profiles = JSON.parse(input[:profile_string])
#free the huge string from the memory
Expand All @@ -50,14 +60,18 @@ def run
Rails.logger.warn("Host with ID %s not found, continuing" % input[:host_id])
elsif host.content_facet.nil? || host.content_facet.uuid.nil?
Rails.logger.warn("Host with ID %s has no content facet, continuing" % input[:host_id])
elsif profiles.try(:has_key?, "deb_package_profile")
# remove this when deb_package_profile API is removed
payload = profiles.dig("deb_package_profile", "deb_packages") || []
import_deb_package_profile(host, payload)
else
profiles.each do |profile|
payload = profile["profile"]
case profile["content_type"]
when "deb"
UpdateDebPackageProfile.update(input[:host_id], payload)
when "rpm"
UploadPackageProfile.upload(input[:host_id], payload)
when "deb"
import_deb_package_profile(host, payload)
when "enabled_repos"
host.import_enabled_repositories(payload)
else
Expand Down
53 changes: 34 additions & 19 deletions app/models/katello/host/content_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,43 @@ class ContentFacet < Katello::Model
validates :host, :presence => true, :allow_blank => false
validates_with Validators::ContentViewEnvironmentValidator

def bindable_types
[
{
type: Repository::DEB_TYPE,
matcher: '/pulp/deb/',
paths: []
},
{
type: Repository::YUM_TYPE,
matcher: '/pulp/repos/',
paths: []
}
]
end

def update_repositories_by_paths(paths)
apt_paths = []
yum_paths = []
paths = paths.reject do |path|
if path.starts_with? '/pulp/deb/'
apt_paths << path.gsub('/pulp/deb/', '')
true
elsif path.starts_with? '/pulp/repos/'
yum_paths << path.gsub('/pulp/repos/', '')
true
else
false
bindable_paths = bindable_types
relative_paths = []

paths.each do |path|
bindable_paths.each do |supported|
relative_path = path.gsub(supported[:matcher], '')
relative_paths << relative_path
if path.starts_with?(supported[:matcher])
supported[:paths] << relative_path
break
end
end
end
apt_repos = Repository.joins(:root).where(RootRepository.table_name => {:content_type => Repository::DEB_TYPE}, :relative_path => apt_paths)
apt_missing = apt_paths - apt_repos.pluck(:relative_path)
yum_repos = Repository.joins(:root).where(RootRepository.table_name => {:content_type => Repository::YUM_TYPE}, :relative_path => yum_paths)
yum_missing = yum_paths - yum_repos.pluck(:relative_path)
repos = apt_repos + yum_repos

missing = paths + apt_missing + yum_missing
missing.each do |repo_path|

repos = bindable_paths.flat_map do |supported|
repos = Repository.joins(:root).where(RootRepository.table_name => {content_type: supported[:type]}, relative_path: supported[:paths])
relative_paths -= repos.pluck(:relative_path)
repos
end

relative_paths.each do |repo_path|
Rails.logger.warn("System #{self.host.name} (#{self.host.id}) requested binding to unknown repo #{repo_path}")
end

Expand Down
8 changes: 7 additions & 1 deletion app/models/katello/rhsm_fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,17 @@ def operatingsystem
os_name = ::Katello::Candlepin::Consumer.distribution_to_puppet_os(name)
major, minor = version.split('.')
if os_name && !invalid_centos_os?(os_name, minor)
os_attributes = {:major => major, :minor => minor || '', :name => os_name}
os_attributes = {:major => major, :minor => minor || '', :name => os_name, :release_name => os_release_name(os_name)}
::Operatingsystem.find_by(os_attributes) || ::Operatingsystem.create!(os_attributes)
end
end

def os_release_name(os_name)
if os_name.match(::Operatingsystem::FAMILIES['Debian'])
facts['distribution.id']
end
end

def invalid_centos_os?(name, minor_version)
name == 'CentOS' && minor_version.blank?
end
Expand Down
4 changes: 4 additions & 0 deletions app/services/katello/candlepin/consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ def self.distribution_to_puppet_os(name)
'Fedora'
elsif name =~ /sles/ || name =~ /suse.*enterprise.*/
'SLES'
elsif name =~ /debian/
'Debian'
elsif name =~ /ubuntu/
'Ubuntu'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes/api/rhsm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class ActionDispatch::Routing::Mapper
match '/consumers/:id/profiles/' => 'candlepin_dynflow_proxy#upload_profiles', :via => :put
match '/consumers/:id/profile/' => 'candlepin_dynflow_proxy#upload_package_profile', :via => :put
match '/consumers/:id/packages/' => 'candlepin_dynflow_proxy#upload_package_profile', :via => :put
match '/systems/:id/deb_package_profile' => 'candlepin_dynflow_proxy#deb_package_profile', :via => :put
match '/consumers/:id/tracer/' => 'candlepin_proxies#upload_tracer_profile', :via => :put
match '/consumers/:id/checkin/' => 'candlepin_proxies#checkin', :via => :put
match '/consumers/:id' => 'candlepin_proxies#facts', :via => :put
Expand All @@ -60,7 +61,6 @@ class ActionDispatch::Routing::Mapper
match '/consumers/:id/content_overrides/' => 'candlepin_proxies#delete', :via => :delete, :as => :proxy_consumer_content_overrides_delete_path
match '/consumers/:id/available_releases' => 'candlepin_proxies#available_releases', :via => :get
match '/systems/:id/enabled_repos' => 'candlepin_proxies#enabled_repos', :via => :put
match '/systems/:id/deb_package_profile' => 'candlepin_proxies#deb_package_profile', :via => :put
match '/jobs/:jobId' => 'candlepin_proxies#get', :via => :get, :as => :proxy_jobs_get_path
match '/status' => 'candlepin_proxies#server_status', :via => :get
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
Tasks
</a>
</li>
<li uib-dropdown
<li uib-dropdown ng-if="host.isRpmEnabled()"
ng-class="{active: stateIncludes('content-host.packages-actions') || stateIncludes('content-host.packages-installed') || stateIncludes('content-host.packages-upgradeable')}" >
<a uib-dropdown-toggle>
<span translate>Packages</span>
Expand All @@ -123,7 +123,7 @@
</li>
</ul>
</li>
<li uib-dropdown
<li uib-dropdown ng-if="host.isDebEnabled()"
ng-class="{active: stateIncludes('content-host.debs-actions') || stateIncludes('content-host.debs-installed') || stateIncludes('content-host.debs-upgradeable')}" >
<a uib-dropdown-toggle>
<span translate>Deb Packages</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ angular.module('Bastion.hosts').factory('Host',
resource.prototype.hasSubscription = function () {
return angular.isDefined(this.subscription_facet_attributes) && angular.isDefined(this.subscription_facet_attributes.uuid);
};

resource.prototype.isRpmEnabled = function() {
return !this.isDebEnabled();
};

resource.prototype.isDebEnabled = function() {
return angular.isDefined(this.operatingsystem_name) && (this.operatingsystem_name.includes("Debian") || this.operatingsystem_name.includes("Ubuntu"));
};

return resource;
}]
);
2 changes: 1 addition & 1 deletion lib/katello/permissions/host_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'katello/api/v2/hosts_bulk_actions/release_version',
'katello/api/rhsm/candlepin_dynflow_proxy/upload_package_profile',
'katello/api/rhsm/candlepin_dynflow_proxy/upload_profiles',
'katello/api/rhsm/candlepin_proxies/deb_package_profile',
'katello/api/rhsm/candlepin_dynflow_proxy/deb_package_profile',
'katello/api/rhsm/candlepin_proxies/regenerate_identity_certificates',
'katello/api/rhsm/candlepin_proxies/hypervisors_update',
'katello/api/rhsm/candlepin_proxies/async_hypervisors_update',
Expand Down
Loading

0 comments on commit 809da1c

Please sign in to comment.