foreman_kernel_care 1.0.0.pre.alfa → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b69d029637d5bee05bb0c109c33c6c132f78eea786ffe869f65ca71b46b1094
4
- data.tar.gz: e42b57600e3633a14238ac046a9782128f50d37002fbf2a806904c91f34a820c
3
+ metadata.gz: c0348921f3d8a92fad7474e5e6256d1f766b016f827f4e16b75afe825230f511
4
+ data.tar.gz: b5811ad734c679a9341b8691f21e29ead36cfc8fac6b2f568c536732a1987ee9
5
5
  SHA512:
6
- metadata.gz: 2033bc0576354e02c2b56bfd8dea0b966702c538ae3fbb967284b9a3a07afb659957c20baa0e619d82c29355dfbb505989188ef00fdf3dad8d0e75a8ab424e0b
7
- data.tar.gz: 0b0a1f5475398e2340b22d6953cd785a5f911499ad8ba8b59d621d4469cfbb642b029e9ce401c6a060db115a535da43288c783b75c8198fb3acd305b969f709b
6
+ metadata.gz: 89fb27d78851ea1e38eb8add5398ecc04e9e970bcd69f5e501193ccb4f6ac6f86395e64c531b3cc92a794a0819c4aecd25f975de2d51f3f592ab99efac3462fa
7
+ data.tar.gz: 522e387d5dac01c5f49302eb5837132c89fe9daea5e53d8a8fd335d8a2aecfae066c9091febfde3e3eefc5f0a8e830a0d3afc6495bcbba33333d208bd1ca2283
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # ForemanKernelCare
2
2
 
3
- This plugin removes kernel [trace](https://theforeman.org/plugins/katello/3.18/user_guide/tracer/index.html) if [KernelCare](https://www.kernelcare.com/product/) package is installed on host
3
+ This plugin removes kernel [trace](https://theforeman.org/plugins/katello/nightly/user_guide/tracer/index.html) and update the kernel package version if [KernelCare](https://www.kernelcare.com/product/) package is installed on host
4
4
 
5
5
  ## Installation
6
6
 
7
- See [How_to_Install_a_Plugin](http://projects.theforeman.org/projects/foreman/wiki/How_to_Install_a_Plugin)
7
+ See [How_to_Install_a_Plugin](https://www.theforeman.org/plugins/#2.Installation)
8
8
  for how to install Foreman plugins
9
9
 
10
10
  ## Usage
@@ -17,7 +17,7 @@ Fork and send a Pull Request. Thanks!
17
17
 
18
18
  ## Copyright
19
19
 
20
- Copyright (c) 2021 maccelf
20
+ Copyright (c) 2022 maccelf
21
21
 
22
22
  This program is free software: you can redistribute it and/or modify
23
23
  it under the terms of the GNU General Public License as published by
@@ -0,0 +1,22 @@
1
+ module ForemanKernelCare
2
+ module ForemanTasks
3
+ def callback
4
+ callbacks = params.key?(:callback) ? Array(params) : params[:callbacks]
5
+ ids = callbacks.map { |payload| payload[:callback][:task_id] }
6
+ foreman_tasks = ::ForemanTasks::Task.where(:id => ids)
7
+ external_map = Hash[*foreman_tasks.pluck(:id, :external_id).flatten]
8
+ callbacks.each do |payload|
9
+ # We need to call .to_unsafe_h to unwrap the hash from ActionController::Parameters
10
+ callback = payload[:callback]
11
+ foreman_task = foreman_tasks.find { |task| task.id == callback[:task_id] }
12
+ if foreman_task.action.include?('Get patched kernel version')
13
+ version, release = payload[:data].to_unsafe_h['result'].first['output'].strip.split('-')
14
+ job_invocation = ::JobInvocation.where(:task_id => foreman_task.parent_task_id).first
15
+ job_invocation.targeting.hosts.each { |host| host.update_kernel_version(version, release) }
16
+ end
17
+ process_callback(external_map[callback[:task_id]], callback[:step_id].to_i, payload[:data].to_unsafe_h, :request_id => ::Logging.mdc['request'])
18
+ end
19
+ render :json => { :message => 'processing' }.to_json
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,16 @@
1
1
  module ForemanKernelCare
2
2
  module HostManagedExtensions
3
+ def import_package_profile(simple_packages)
4
+ if kernelcare?
5
+ composer = ::JobInvocationComposer.for_feature(:kernel_version, self)
6
+ composer.triggering.mode = :future
7
+ composer.trigger!
8
+ end
9
+
10
+ found = import_package_profile_in_bulk(simple_packages)
11
+ sync_package_associations(found.map(&:id).uniq)
12
+ end
13
+
3
14
  def import_tracer_profile(tracer_profile)
4
15
  traces = []
5
16
  tracer_profile.each do |trace, attributes|
@@ -20,6 +31,44 @@ module ForemanKernelCare
20
31
  update_trace_status
21
32
  end
22
33
 
34
+ def update_kernel_version(version, release)
35
+ packages = ::Katello::InstalledPackage.where('name LIKE ?', '%kernel%').where.not(name: 'kernelcare').to_a
36
+ delete_ids = []
37
+ simple_packages = packages.map do |p|
38
+ delete_ids << p.id
39
+ ::Katello::Pulp::SimplePackage.new({
40
+ arch: p.arch,
41
+ epoch: p.epoch,
42
+ version: version,
43
+ release: release,
44
+ name: p.name
45
+ })
46
+ end
47
+ found = import_package_profile_in_bulk(simple_packages)
48
+ sync_kernel_associations(found.map(&:id).uniq, delete_ids)
49
+ end
50
+
51
+ def sync_kernel_associations(new_patched_kernel_ids, delete_ids)
52
+ ::Katello::Util::Support.active_record_retry do
53
+ table_name = host_installed_packages.table_name
54
+
55
+ queries = []
56
+
57
+ if delete_ids.any?
58
+ queries << "DELETE FROM #{table_name} WHERE host_id=#{id} AND installed_package_id IN (#{delete_ids.join(', ')})"
59
+ end
60
+
61
+ unless new_patched_kernel_ids.empty?
62
+ inserts = new_patched_kernel_ids.map { |unit_id| "(#{unit_id.to_i}, #{id.to_i})" }
63
+ queries << "INSERT INTO #{table_name} (installed_package_id, host_id) VALUES #{inserts.join(', ')}"
64
+ end
65
+
66
+ queries.each do |query|
67
+ ::ActiveRecord::Base.connection.execute(query)
68
+ end
69
+ end
70
+ end
71
+
23
72
  protected
24
73
 
25
74
  def kernelcare?
@@ -0,0 +1,15 @@
1
+ <%#
2
+ kind: job_template
3
+ name: Kernel version
4
+ model: JobTemplate
5
+ job_category: Commands
6
+ description_format: "Get patched kernel version"
7
+ provider_type: SSH
8
+ feature: kernel_version
9
+ %>
10
+ <%
11
+ unless @host.installed_packages.select { |package| package.name == 'kernelcare' }.empty?
12
+ render_error(N_('Unsupported host.'))
13
+ end
14
+ %>
15
+ /usr/bin/kcarectl --uname
@@ -16,6 +16,13 @@ User.as_anonymous_admin do
16
16
  module_template.organizations << Organization.unscoped.all if module_template.organizations.empty?
17
17
  module_template.locations << Location.unscoped.all if module_template.locations.empty?
18
18
  end
19
+
20
+ module_template = JobTemplate.find_by(name: 'Kernel version')
21
+ if module_template && !Rails.env.test? && Setting[:remote_execution_sync_templates]
22
+ module_template.sync_feature('kernel_version')
23
+ module_template.organizations << Organization.unscoped.all if module_template.organizations.empty?
24
+ module_template.locations << Location.unscoped.all if module_template.locations.empty?
25
+ end
19
26
  end
20
27
  end
21
28
  end
@@ -19,6 +19,7 @@ module ForemanKernelCare
19
19
  # Include concerns in this config.to_prepare block
20
20
  config.to_prepare do
21
21
  Katello::Concerns::HostManagedExtensions.prepend ForemanKernelCare::HostManagedExtensions
22
+ ForemanTasks::Api::TasksController.prepend ForemanKernelCare::ForemanTasks
22
23
  rescue StandardError => e
23
24
  Rails.logger.warn "ForemanKernelCare: skipping engine hook (#{e})"
24
25
  end
@@ -11,6 +11,12 @@ module ForemanKernelCare
11
11
  N_('Run Update kernel'),
12
12
  :description => N_('Runs Update kernel')
13
13
  )
14
+
15
+ RemoteExecutionFeature.register(
16
+ :kernel_version,
17
+ N_('Get patched kernel version'),
18
+ :description => N_('Get patched kernel version')
19
+ )
14
20
  end
15
21
  end
16
22
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanKernelCare
2
- VERSION = '1.0.0-alfa'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
@@ -2,15 +2,40 @@ require 'test_plugin_helper'
2
2
 
3
3
  module ForemanKernelCare
4
4
  class HostManagedExtensionsTestBase < ActiveSupport::TestCase
5
+ end
6
+
7
+ class HostInstalledPackagesTest < HostManagedExtensionsTestBase
5
8
  def setup
6
- template = FactoryBot.create(:job_template, name: 'Update kernel')
7
- template.sync_feature('update_kernel')
9
+ feature = RemoteExecutionFeature.register('kernel_version', 'Kernel version')
10
+ template = FactoryBot.create(:job_template, name: 'Kernel versionl')
11
+ template.sync_feature(feature.label)
12
+
13
+ @host = FactoryBot.create(:host, :with_kernelcare)
14
+ package_json = {:name => "kernel", :version => "1", :release => "1.el7", :arch => "x86_64", :epoch => "1",
15
+ :nvra => "kernel-1-1.el7.x86_64"}
16
+ @host.import_package_profile([::Katello::Pulp::SimplePackage.new(package_json)])
17
+ @nvra = 'kernel-1-1.el7.x86_64'
18
+ @host.reload
19
+ end
20
+
21
+ def test_update_kernel_version
22
+ version = '2'
23
+ release = '2.el7'
24
+ @host.update_kernel_version(version, release)
25
+ kernel_package = @host.installed_packages.where(name: 'kernel').first
26
+
27
+ assert_not_equal kernel_package.nvra, @nvra
28
+ assert_equal kernel_package.version, version
29
+ assert_equal kernel_package.release, release
8
30
  end
9
31
  end
10
32
 
11
33
  class HostTracerTest < HostManagedExtensionsTestBase
12
34
  def setup
13
- super
35
+ feature = RemoteExecutionFeature.register('update_kernel', 'Update Kernel')
36
+ template = FactoryBot.create(:job_template, name: 'Update kernel')
37
+ template.sync_feature(feature.label)
38
+
14
39
  @tracer_json = {
15
40
  "kernel": {
16
41
  "type": 'static',
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_kernel_care
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.alfa
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxim Petukhov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-19 00:00:00.000000000 Z
11
+ date: 2022-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: katello
14
+ name: foreman_remote_execution
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.8.0
19
+ version: 1.5.6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.8.0
26
+ version: 1.5.6
27
27
  - !ruby/object:Gem::Dependency
28
- name: foreman_remote_execution
28
+ name: katello
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.5.6
33
+ version: 3.8.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.5.6
40
+ version: 3.8.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rdoc
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,8 +66,8 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: This plugin removes kernel trace if KernelCare package is installed on
70
- host
69
+ description: This plugin removes kernel trace and update the kernel package version
70
+ if KernelCare package is installed on host
71
71
  email:
72
72
  - maxmol27@gmail.com
73
73
  executables: []
@@ -77,7 +77,9 @@ files:
77
77
  - LICENSE
78
78
  - README.md
79
79
  - Rakefile
80
+ - app/controllers/concerns/foreman_kernel_care/foreman_tasks.rb
80
81
  - app/models/concerns/foreman_kernel_care/host_managed_extensions.rb
82
+ - app/views/foreman_kernel_care/job_templates/kernel_version.erb
81
83
  - app/views/foreman_kernel_care/job_templates/update_kernel.erb
82
84
  - db/seeds.d/100_assign_features_with_templates.rb
83
85
  - db/seeds.d/70_job_templates.rb
@@ -104,11 +106,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
104
106
  version: '0'
105
107
  required_rubygems_version: !ruby/object:Gem::Requirement
106
108
  requirements:
107
- - - ">"
109
+ - - ">="
108
110
  - !ruby/object:Gem::Version
109
- version: 1.3.1
111
+ version: '0'
110
112
  requirements: []
111
- rubygems_version: 3.1.6
113
+ rubygems_version: 3.1.2
112
114
  signing_key:
113
115
  specification_version: 4
114
116
  summary: Plugin for KernelCare