foreman_maintain 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0fce23af3f0916264faaf404caa0164321bcb17d
4
- data.tar.gz: f65633719449de4778bdc7d5eb6e328ad4b130d3
3
+ metadata.gz: c13cb8b4593007b12938ca47f1c7f921b5fd1d8d
4
+ data.tar.gz: af861bcb2ad7672b579529f30a64a155630729b4
5
5
  SHA512:
6
- metadata.gz: 11b1ad761ebc5e73f2c4d211246f68825b1cf698b3d2ab7c8f60603d63005e57c0044b38b16ed9489b3ec8ae93bc5f79a4ece069e73760d600debe4ee37c8481
7
- data.tar.gz: 7fb92a17ea9e84cbc480844887580128887f915bdee78e88543581bc611c7fe5253694d26abed0363f082314e5e6686e7d7d079fbe3d5344a24ec2b9a3aedf2a
6
+ metadata.gz: 9f2552e5bc5ceea333091a3235debfb27e22c24980ea215b7a51bf63f0c01667d573a86f99eb0bebcdc6451f453bb82931b24d0d39a68248b4f03fa2eb5024f8
7
+ data.tar.gz: f601e479e2ea8ffa74f61e8e7b194ec1bd962ea5b171d1fb78cf3da6e05c45fafedbf5460c173fe3df053861845c8ec3d112918bf5184ab7cc497e9e752e2793
@@ -0,0 +1,59 @@
1
+ module Checks
2
+ module Foreman
3
+ class CheckCorruptedRoles < ForemanMaintain::Check
4
+ metadata do
5
+ label :corrupted_roles
6
+ for_feature :foreman_database
7
+ description 'Check for roles that have filters with multiple resources attached'
8
+ tags :pre_upgrade
9
+ confine do
10
+ check_min_version('foreman', '1.15')
11
+ end
12
+ end
13
+
14
+ def run
15
+ items = find_filter_permissions
16
+ assert(items.empty?,
17
+ 'There are user roles with inconsistent filters',
18
+ :next_steps => Procedures::Foreman::FixCorruptedRoles.new)
19
+ end
20
+
21
+ def find_filter_permissions
22
+ feature(:foreman_database).query(self.class.inconsistent_filter_perms)
23
+ end
24
+
25
+ def self.inconsistent_filter_perms
26
+ subquery = <<-SQL
27
+ SELECT filters.id AS filter_id,
28
+ filters.role_id,
29
+ filters.search,
30
+ filters.taxonomy_search,
31
+ filters.override,
32
+ filterings.id AS filtering_id,
33
+ permissions.id AS permission_id,
34
+ permissions.name AS permission_name,
35
+ permissions.resource_type
36
+ FROM filters INNER JOIN filterings ON filters.id = filterings.filter_id
37
+ INNER JOIN permissions ON permissions.id = filterings.permission_id
38
+ SQL
39
+
40
+ <<-SQL
41
+ SELECT DISTINCT first.filter_id,
42
+ first.role_id,
43
+ first.filtering_id,
44
+ first.permission_id,
45
+ first.permission_name,
46
+ first.resource_type,
47
+ first.search,
48
+ first.taxonomy_search,
49
+ first.override
50
+ FROM (#{subquery}) first JOIN (#{subquery}) second
51
+ ON first.filter_id = second.filter_id AND
52
+ ((first.resource_type IS NOT NULL AND second.resource_type IS NULL)
53
+ OR (first.resource_type IS NULL AND second.resource_type IS NOT NULL)
54
+ OR (first.resource_type != second.resource_type))
55
+ SQL
56
+ end
57
+ end
58
+ end
59
+ end
@@ -64,7 +64,7 @@ class Features::Downstream < ForemanMaintain::Feature
64
64
 
65
65
  rh_repos.concat(sat_and_tools_repos(rh_version_major, sat_version))
66
66
 
67
- rh_repos << 'rhel-7-server-ansible-2.6-rpms' if sat_version.to_s == '6.4'
67
+ rh_repos << 'rhel-7-server-ansible-2.6-rpms' if sat_version >= version('6.4')
68
68
 
69
69
  if current_minor_version == '6.3' && sat_version.to_s != '6.4' && (
70
70
  feature(:puppet_server) && feature(:puppet_server).puppet_version.major == 4)
@@ -70,19 +70,24 @@ class Features::Mongo < ForemanMaintain::Feature
70
70
  ['localhost', '127.0.0.1', hostname].include?(configuration['host'])
71
71
  end
72
72
 
73
+ # rubocop:disable Metrics/AbcSize
73
74
  def base_command(command, config = configuration, args = '')
74
75
  if config['ssl']
75
76
  ssl = ' --ssl'
76
- if config['ca_path']
77
+ if config['ca_path'] && !config['ca_path'].empty?
77
78
  ca_cert = " --sslCAFile #{config['ca_path']}"
78
- client_cert = " --sslPEMKeyFile #{config['ssl_certfile']}" if config['ssl_certfile']
79
79
  end
80
+ if config['ssl_certfile'] && !config['ssl_certfile'].empty?
81
+ client_cert = " --sslPEMKeyFile #{config['ssl_certfile']}"
82
+ end
83
+ verify_ssl = ' --sslAllowInvalidCertificates' if config['verify_ssl'] == false
80
84
  end
81
85
  username = " -u #{config['username']}" if config['username']
82
86
  password = " -p #{config['password']}" if config['password']
83
87
  host = "--host #{config['host']} --port #{config['port']}"
84
- "#{command}#{username}#{password} #{host}#{ssl}#{ca_cert}#{client_cert} #{args}"
88
+ "#{command}#{username}#{password} #{host}#{ssl}#{verify_ssl}#{ca_cert}#{client_cert} #{args}"
85
89
  end
90
+ # rubocop:enable Metrics/AbcSize
86
91
 
87
92
  def mongo_command(args, config = configuration)
88
93
  base_command(core.client_command, config, "#{args} #{config['name']}")
@@ -0,0 +1,106 @@
1
+ module Procedures::Foreman
2
+ class FixCorruptedRoles < ForemanMaintain::Procedure
3
+ metadata do
4
+ for_feature :foreman_database
5
+ tags :pre_migration
6
+ desc = 'Create additional filters so that each filter has only permissions of one resource'
7
+ description desc
8
+ confine do
9
+ check_min_version('foreman', '1.15')
10
+ end
11
+ end
12
+
13
+ def run
14
+ items = feature(:foreman_database).query(
15
+ Checks::Foreman::CheckCorruptedRoles.inconsistent_filter_perms
16
+ )
17
+ items.group_by { |item| item['filter_id'] }.each_value do |filter_perm_data|
18
+ inconsistent_sets = filter_perm_data.group_by { |perm_data| perm_data['resource_type'] }.
19
+ values
20
+ find_records_to_update(inconsistent_sets).each do |set|
21
+ update_records set
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def find_records_to_update(inconsistent_sets)
29
+ largest_set = inconsistent_sets.reduce([]) do |memo, set|
30
+ set.count > memo.count ? set : memo
31
+ end
32
+
33
+ inconsistent_sets.reject do |set|
34
+ set == largest_set
35
+ end
36
+ end
37
+
38
+ def update_records(set)
39
+ new_filter = create_filter set.first['role_id'],
40
+ set.first['search'],
41
+ set.first['taxonomy_search'],
42
+ set.first['override']
43
+ set.each do |item|
44
+ destroy_filtering item
45
+ next if !new_filter || new_filter.empty?
46
+ create_filtering item, new_filter
47
+ end
48
+ end
49
+
50
+ def create_filter(role_id, search, taxonomy_search, override)
51
+ feature(:foreman_database).query(
52
+ create_filter_query(search, role_id, taxonomy_search, override)
53
+ ).first
54
+ end
55
+
56
+ def escape_val(value)
57
+ value ? "'#{value}'" : 'NULL'
58
+ end
59
+
60
+ def create_filter_query(search, role_id, taxonomy_search, override)
61
+ <<-SQL
62
+ WITH rows AS (
63
+ INSERT INTO filters (search, role_id, taxonomy_search, override, created_at, updated_at)
64
+ VALUES (#{escape_val(search)}, #{role_id}, #{escape_val(taxonomy_search)}, '#{override}', '#{Time.now}', '#{Time.now}')
65
+ RETURNING id
66
+ )
67
+ SELECT id
68
+ FROM rows
69
+ SQL
70
+ end
71
+
72
+ def create_filtering(data, new_filter)
73
+ feature(:foreman_database).query(
74
+ create_filtering_query(data['permission_id'], new_filter['id'])
75
+ )
76
+ end
77
+
78
+ def destroy_filtering(data)
79
+ feature(:foreman_database).query(destroy_filtering_query(data['filtering_id']))
80
+ end
81
+
82
+ def destroy_filtering_query(filtering_id)
83
+ <<-SQL
84
+ WITH rows AS (
85
+ DELETE FROM filterings
86
+ WHERE id = #{filtering_id}
87
+ RETURNING id
88
+ )
89
+ SELECT id
90
+ FROM rows
91
+ SQL
92
+ end
93
+
94
+ def create_filtering_query(permission_id, filter_id)
95
+ <<-SQL
96
+ WITH rows AS (
97
+ INSERT INTO filterings (filter_id, permission_id, created_at, updated_at)
98
+ VALUES (#{filter_id}, #{permission_id}, '#{Time.now}', '#{Time.now}')
99
+ RETURNING id
100
+ )
101
+ SELECT id
102
+ FROM rows
103
+ SQL
104
+ end
105
+ end
106
+ end
@@ -116,12 +116,9 @@ module ForemanMaintain
116
116
  end
117
117
 
118
118
  def init_logger
119
- # Note - If timestamp added to filename then number of log files i.e second
120
- # argument to Logger.new will not work as expected
121
- filename = File.expand_path("#{config.log_dir}/foreman-maintain.log")
122
119
  # convert size in KB to Bytes
123
120
  log_fsize = config.log_file_size.to_i * 1024
124
- @logger = Logger.new(filename, 10, log_fsize).tap do |logger|
121
+ @logger = Logger.new(config.log_filename, 10, log_fsize).tap do |logger|
125
122
  logger.level = LOGGER_LEVEL_MAPPING[config.log_level] || Logger::DEBUG
126
123
  logger.datetime_format = '%Y-%m-%d %H:%M:%S%z '
127
124
  end
@@ -3,7 +3,7 @@ module ForemanMaintain
3
3
  class Config
4
4
  attr_accessor :pre_setup_log_messages,
5
5
  :config_file, :definitions_dirs, :log_level, :log_dir, :log_file_size,
6
- :storage_file, :backup_dir, :foreman_proxy_cert_path,
6
+ :log_filename, :storage_file, :backup_dir, :foreman_proxy_cert_path,
7
7
  :db_backup_dir, :completion_cache_file
8
8
 
9
9
  def initialize(options)
@@ -26,6 +26,9 @@ module ForemanMaintain
26
26
  @log_level = @options.fetch(:log_level, ::Logger::DEBUG)
27
27
  @log_dir = find_dir_path(@options.fetch(:log_dir, 'log'))
28
28
  @log_file_size = @options.fetch(:log_file_size, 10_000)
29
+ # Note - If timestamp added to filename then number of log files i.e second
30
+ # argument to Logger.new will not work as expected
31
+ @log_filename = File.expand_path("#{@log_dir}/foreman-maintain.log")
29
32
  end
30
33
 
31
34
  def load_backup_dir_paths
@@ -62,7 +62,9 @@ module ForemanMaintain::Utils
62
62
  if @db_feature.ping
63
63
  [0, "#{self} is remote and is UP.#{msg}"]
64
64
  else
65
- [1, "#{self} is remote and is DOWN.#{msg}"]
65
+ [1, "#{self} is remote and is DOWN.#{msg}" \
66
+ "\n Unable to connect to the remote database." \
67
+ "\n See the log (#{ForemanMaintain.config.log_filename}) for more details.#{msg}"]
66
68
  end
67
69
  end
68
70
  end
@@ -1,3 +1,3 @@
1
1
  module ForemanMaintain
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.3.2'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foreman_maintain
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Nečas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-31 00:00:00.000000000 Z
11
+ date: 2019-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -125,6 +125,7 @@ files:
125
125
  - definitions/checks/check_epel_repository.rb
126
126
  - definitions/checks/disk/available_space.rb
127
127
  - definitions/checks/disk/performance.rb
128
+ - definitions/checks/foreman/check_corrupted_roles.rb
128
129
  - definitions/checks/foreman/db_up.rb
129
130
  - definitions/checks/foreman/puppet_class_duplicates.rb
130
131
  - definitions/checks/foreman_openscap/invalid_report_associations.rb
@@ -192,6 +193,7 @@ files:
192
193
  - definitions/procedures/backup/snapshot/mount_pulp.rb
193
194
  - definitions/procedures/backup/snapshot/prepare_mount.rb
194
195
  - definitions/procedures/candlepin/delete_orphaned_records_from_env_content.rb
196
+ - definitions/procedures/foreman/fix_corrupted_roles.rb
195
197
  - definitions/procedures/foreman_openscap/invalid_report_associations.rb
196
198
  - definitions/procedures/foreman_tasks/delete.rb
197
199
  - definitions/procedures/foreman_tasks/fetch_tasks_status.rb
@@ -327,7 +329,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
327
329
  version: '0'
328
330
  requirements: []
329
331
  rubyforge_project:
330
- rubygems_version: 2.6.12
332
+ rubygems_version: 2.6.14.1
331
333
  signing_key:
332
334
  specification_version: 4
333
335
  summary: Foreman maintenance tool belt