foreman_maintain 0.2.9 → 0.2.10

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/bin/foreman-maintain +3 -0
  3. data/definitions/checks/backup/directory_ready.rb +1 -1
  4. data/definitions/checks/foreman_tasks/not_paused.rb +1 -0
  5. data/definitions/checks/hammer_ping.rb +1 -0
  6. data/definitions/checks/mongo/db_up.rb +1 -1
  7. data/definitions/checks/services_up.rb +18 -0
  8. data/definitions/features/candlepin.rb +4 -4
  9. data/definitions/features/candlepin_database.rb +7 -0
  10. data/definitions/features/foreman_database.rb +7 -0
  11. data/definitions/features/foreman_proxy.rb +4 -4
  12. data/definitions/features/foreman_server.rb +3 -4
  13. data/definitions/features/foreman_tasks.rb +1 -1
  14. data/definitions/features/gofer.rb +3 -3
  15. data/definitions/features/katello.rb +3 -3
  16. data/definitions/features/mongo.rb +3 -1
  17. data/definitions/features/pulp.rb +10 -10
  18. data/definitions/features/puppet_server.rb +1 -1
  19. data/definitions/features/service.rb +40 -79
  20. data/definitions/procedures/hammer_setup.rb +7 -2
  21. data/definitions/procedures/restore/mongo_dump.rb +1 -1
  22. data/definitions/procedures/service/base.rb +7 -1
  23. data/definitions/procedures/service/list.rb +3 -2
  24. data/definitions/procedures/service/restart.rb +1 -1
  25. data/lib/foreman_maintain/concerns/system_helpers.rb +0 -9
  26. data/lib/foreman_maintain/concerns/system_service.rb +9 -0
  27. data/lib/foreman_maintain/feature.rb +4 -3
  28. data/lib/foreman_maintain/utils/command_runner.rb +4 -2
  29. data/lib/foreman_maintain/utils/service/abstract.rb +64 -0
  30. data/lib/foreman_maintain/utils/service/remote_db.rb +70 -0
  31. data/lib/foreman_maintain/utils/service/systemd.rb +58 -0
  32. data/lib/foreman_maintain/utils/service.rb +14 -0
  33. data/lib/foreman_maintain/utils/system_helpers.rb +7 -0
  34. data/lib/foreman_maintain/utils.rb +2 -0
  35. data/lib/foreman_maintain/version.rb +1 -1
  36. data/lib/foreman_maintain.rb +1 -0
  37. metadata +9 -4
  38. data/config/foreman-maintain-hammer.yml +0 -4
  39. data/config/foreman_maintain.yml +0 -16
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe76d1f70ac466bed07284b762f9a30b852efb55
4
- data.tar.gz: 73a74651ba0e6bc1fb2c702c2c8e77e652caaed9
3
+ metadata.gz: 01b94d288066a77ce5e9203b9dd97258ce49c1ce
4
+ data.tar.gz: ff691d5547890b714d5f19ce5eadf059ee97bff1
5
5
  SHA512:
6
- metadata.gz: 01dd6c5e2568e26e806e04fcd004bb9ba26fce1e024f6801b13ad4c1a74d97bc04fe59a1bfcbf657e46d05c36dde616215f56ba69f7d68c876f0db78647daf5d
7
- data.tar.gz: 37b5e13bcfbd1d35a987977e7c7ccf93c3b995ce8eed9d3a6716aa0fb27dd6e014edf20b01c45d90b9349f8d1a4d34390d3fc7241d6ce355aced7db30221853e
6
+ metadata.gz: 93a26603996d1d3c0cf5f34357a9db6e55c92b14347b2039b74ed07bb975f9d62c7c0437a95c8743ae183d7baeb227075f97a1fb31e420a859298663f8834a66
7
+ data.tar.gz: afe06f39673cf070e0eb1a039b97f006036907c53ea6fa54d07d2012ee3c2af25a21bb637571a87dcab6d3b6975900d63b3adfc80f27a5ae03289a87a4fd24a6
data/bin/foreman-maintain CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ # Ensure we use UTF-8 by defualt, even when run with LC_ALL=C
4
+ Encoding.default_internal = Encoding.default_external = Encoding::UTF_8
5
+
3
6
  $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
4
7
 
5
8
  require 'foreman_maintain'
@@ -9,7 +9,7 @@ module Checks::Backup
9
9
  end
10
10
 
11
11
  def run
12
- assert(File.directory?(@backup_dir), "Backup directory (#{@backup_dir}) does not exit.")
12
+ assert(File.directory?(@backup_dir), "Backup directory (#{@backup_dir}) does not exist.")
13
13
  if feature(:instance).postgresql_local?
14
14
  result = system("runuser - postgres -c 'test -w #{@backup_dir}'")
15
15
  assert(result, "Postgres user needs write access to the backup directory \n" \
@@ -5,6 +5,7 @@ module Checks::ForemanTasks
5
5
  for_feature :foreman_tasks
6
6
  description 'Check for paused tasks'
7
7
  tags :default
8
+ after :services_up, :hammer_ping
8
9
  end
9
10
 
10
11
  def run
@@ -5,6 +5,7 @@ class Checks::HammerPing < ForemanMaintain::Check
5
5
  for_feature :hammer
6
6
  description 'Check whether all services are running using hammer ping'
7
7
  tags :default
8
+ after :services_up
8
9
 
9
10
  confine do
10
11
  feature(:katello)
@@ -19,7 +19,7 @@ module Checks
19
19
 
20
20
  def next_steps
21
21
  if feature(:mongo).local?
22
- [Procedures::Service::Start.new(:only => feature(:mongo).services.keys)]
22
+ [Procedures::Service::Start.new(:only => feature(:mongo).services)]
23
23
  else
24
24
  [] # there is nothing we can do for remote db
25
25
  end
@@ -0,0 +1,18 @@
1
+ class Checks::ServicesUp < ForemanMaintain::Check
2
+ metadata do
3
+ label :services_up
4
+ description 'Check whether all services are running'
5
+ tags :default
6
+ end
7
+
8
+ def run
9
+ failed_services = feature(:service).existing_services.reject(&:running?)
10
+ restart_procedure = Procedures::Service::Restart.new(
11
+ :only => failed_services,
12
+ :wait_for_hammer_ping => !!feature(:katello)
13
+ )
14
+ assert(failed_services.empty?,
15
+ 'Following services are not running: ' + failed_services.map(&:to_s).join(', '),
16
+ :next_steps => restart_procedure)
17
+ end
18
+ end
@@ -8,9 +8,9 @@ class Features::Candlepin < ForemanMaintain::Feature
8
8
  end
9
9
 
10
10
  def services
11
- {
12
- 'tomcat' => 20,
13
- 'tomcat6' => 20
14
- }
11
+ [
12
+ system_service('tomcat', 20),
13
+ system_service('tomcat6', 20)
14
+ ]
15
15
  end
16
16
  end
@@ -11,6 +11,13 @@ class Features::CandlepinDatabase < ForemanMaintain::Feature
11
11
  end
12
12
  end
13
13
 
14
+ def services
15
+ [
16
+ system_service('postgresql', 10, :component => 'candlepin',
17
+ :db_feature => feature(:candlepin_database))
18
+ ]
19
+ end
20
+
14
21
  def configuration
15
22
  @configuration || load_configuration
16
23
  end
@@ -15,6 +15,13 @@ class Features::ForemanDatabase < ForemanMaintain::Feature
15
15
  @configuration || load_configuration
16
16
  end
17
17
 
18
+ def services
19
+ [
20
+ system_service('postgresql', 10, :component => 'foreman',
21
+ :db_feature => feature(:foreman_database))
22
+ ]
23
+ end
24
+
18
25
  private
19
26
 
20
27
  def load_configuration
@@ -25,10 +25,10 @@ class Features::ForemanProxy < ForemanMaintain::Feature
25
25
  end
26
26
 
27
27
  def services
28
- {
29
- 'foreman-proxy' => 20,
30
- 'smart_proxy_dynflow_core' => 20
31
- }
28
+ [
29
+ system_service('smart_proxy_dynflow_core', 20),
30
+ system_service('foreman-proxy', 40)
31
+ ]
32
32
  end
33
33
 
34
34
  def features
@@ -9,10 +9,9 @@ module ForemanMaintain
9
9
  end
10
10
 
11
11
  def services
12
- {
13
- 'postgresql' => 5,
14
- 'httpd' => 30
15
- }
12
+ [
13
+ system_service('httpd', 30)
14
+ ]
16
15
  end
17
16
 
18
17
  def plugins
@@ -108,7 +108,7 @@ class Features::ForemanTasks < ForemanMaintain::Feature
108
108
  end
109
109
 
110
110
  def services
111
- { service_name => 30 }
111
+ [system_service(service_name, 30)]
112
112
  end
113
113
 
114
114
  def service_name
@@ -8,8 +8,8 @@ class Features::Gofer < ForemanMaintain::Feature
8
8
  end
9
9
 
10
10
  def services
11
- {
12
- 'goferd' => 30
13
- }
11
+ [
12
+ system_service('goferd', 30)
13
+ ]
14
14
  end
15
15
  end
@@ -16,9 +16,9 @@ class Features::Katello < ForemanMaintain::Feature
16
16
  end
17
17
 
18
18
  def services
19
- {
20
- 'elasticsearch' => 30
21
- }
19
+ [
20
+ system_service('elasticsearch', 30)
21
+ ]
22
22
  end
23
23
 
24
24
  # rubocop:disable Metrics/MethodLength
@@ -12,7 +12,9 @@ class Features::Mongo < ForemanMaintain::Feature
12
12
  end
13
13
 
14
14
  def services
15
- core.services
15
+ core.services.map do |service, priority|
16
+ system_service(service, priority, :db_feature => self)
17
+ end
16
18
  end
17
19
 
18
20
  def data_dir
@@ -8,16 +8,16 @@ class Features::Pulp < ForemanMaintain::Feature
8
8
  end
9
9
 
10
10
  def services
11
- {
12
- 'squid' => 10,
13
- 'qpidd' => 10,
14
- 'qdrouterd' => 10,
15
- 'pulp_workers' => 20,
16
- 'pulp_celerybeat' => 20,
17
- 'pulp_resource_manager' => 20,
18
- 'pulp_streamer' => 20,
19
- 'httpd' => 30
20
- }
11
+ [
12
+ system_service('squid', 10),
13
+ system_service('qpidd', 10),
14
+ system_service('qdrouterd', 10),
15
+ system_service('pulp_workers', 20),
16
+ system_service('pulp_celerybeat', 20),
17
+ system_service('pulp_resource_manager', 20),
18
+ system_service('pulp_streamer', 20),
19
+ system_service('httpd', 30)
20
+ ]
21
21
  end
22
22
 
23
23
  def data_dir
@@ -11,7 +11,7 @@ class Features::PuppetServer < ForemanMaintain::Feature
11
11
  end
12
12
 
13
13
  def services
14
- find_package('puppetserver') ? { 'puppetserver' => 30 } : {}
14
+ find_package('puppetserver') ? [system_service('puppetserver', 30)] : []
15
15
  end
16
16
 
17
17
  def puppet_version
@@ -10,37 +10,18 @@ class Features::Service < ForemanMaintain::Feature
10
10
  if feature(:downstream) && feature(:downstream).less_than_version?('6.3')
11
11
  use_katello_service(action, options)
12
12
  else
13
- options[:reverse] = action == 'stop'
14
-
15
- filtered_services(options).each do |service|
16
- spinner.update("#{action_noun(action)} #{service}")
17
- perform_action_on_service(action, service)
18
- end
19
-
20
- spinner.update("All services #{action_past_tense(action)}")
13
+ use_system_service(action, options, spinner)
21
14
  end
22
15
  end
23
16
 
24
- def list_services(service_list)
25
- service_list = service_list.join(', ')
26
- puts "#{service_list}\n"
27
- end
28
-
29
- def get_services_from_features(features)
30
- features.map(&:services).
31
- inject(&:merge).
32
- sort_by { |_, value| value }.
33
- map { |service| service[0] }
34
- end
35
-
36
17
  def existing_services
37
- service_list = get_services_from_features(available_features)
38
- service_list.select { |service| service_remote?(service) || service_exists?(service) }
39
- end
40
-
41
- def service_remote?(service)
42
- (service == 'postgresql' && !feature(:instance).postgresql_local?) ||
43
- (service == 'mongod' && feature(:instance).database_remote?(:mongo))
18
+ ForemanMaintain.available_features.map(&:services).
19
+ flatten(1).
20
+ sort.
21
+ inject([]) do |pool, service| # uniq(&:to_s) for ruby 1.8.7
22
+ pool.last.nil? || !pool.last.matches?(service) ? pool << service : pool
23
+ end.
24
+ select(&:exist?)
44
25
  end
45
26
 
46
27
  def filtered_services(options)
@@ -60,69 +41,49 @@ class Features::Service < ForemanMaintain::Feature
60
41
 
61
42
  private
62
43
 
63
- def available_features
64
- @available_features || ForemanMaintain.available_features
65
- end
44
+ def use_system_service(action, options, spinner)
45
+ options[:reverse] = action == 'stop'
46
+ raise 'Unsupported action detected' unless allowed_action?(action)
66
47
 
67
- def filter_services(service_list, options)
68
- service_list &= options[:only] if options[:only] && options[:only].any?
69
- service_list -= options[:exclude] if options[:exclude] && options[:exclude].any?
70
- service_list
71
- end
48
+ status = 0
49
+ failed_services = []
72
50
 
73
- def perform_action_on_service(action, service)
74
- if service == 'postgresql'
75
- if feature(:instance).postgresql_local?
76
- perform_action_on_local_service(action, service)
77
- end
78
- if feature(:instance).database_remote?(:candlepin_database)
79
- remote_db_message('Candlepin', :candlepin_database, action)
80
- end
81
- if feature(:instance).database_remote?(:foreman_database)
82
- remote_db_message('Foreman', :foreman_database, action)
51
+ filtered_services(options).each do |service|
52
+ spinner.update("#{action_noun(action)} #{service}")
53
+ item_status, output = service.send(action.to_sym)
54
+
55
+ if item_status > 0
56
+ status = item_status
57
+ failed_services << service
83
58
  end
84
- elsif service =~ /^.*mongod$/ && feature(:instance).database_remote?(:mongo)
85
- remote_db_message('Pulp', :mongo, action)
86
- else
87
- perform_action_on_local_service(action, service)
59
+
60
+ puts format_status(output)
88
61
  end
62
+
63
+ spinner.update("All services #{action_past_tense(action)}")
64
+ raise "Some services are not running (#{failed_services.join(', ')})" if status > 0
89
65
  end
90
66
 
91
- def remote_db_message(app, db, action)
92
- ping = !!feature(db).ping
93
- message = if %w[enable disable].include?(action)
94
- " - #{app} DB is remote. Can not #{action} the service."
95
- else
96
- 'the service is on remote host. The DB is ' + (ping ? 'UP.' : 'DOWN.')
97
- end
98
- if action == 'status'
99
- puts "\nFor #{app} DB #{message}\n"
100
- else
101
- print " - #{message}"
102
- end
103
- logger.info(message)
104
- if action == 'start' && !ping
105
- raise ForemanMaintain::Error::Fail, "The remote #{app} databse is down."
106
- end
67
+ def format_status(output)
68
+ status = "\n"
69
+ status += output if !output.nil? && !output.empty?
70
+ status
107
71
  end
108
72
 
109
- def perform_action_on_local_service(action, service)
110
- command = service_command(action, service)
111
- if action == 'status'
112
- status = execute(command)
113
- puts "\n\n#{status}\n\n"
114
- else
115
- execute!(command)
116
- end
73
+ def allowed_action?(action)
74
+ %w[start stop restart status enable disable].include?(action)
117
75
  end
118
76
 
119
- def service_command(action, service)
120
- if File.exist?('/usr/sbin/service-wait') &&
121
- !%w[enable disable].include?(action)
122
- "service-wait #{service} #{action}"
123
- else
124
- "systemctl #{action} #{service}"
77
+ def filter_services(service_list, options)
78
+ if options[:only] && options[:only].any?
79
+ service_list = service_list.select do |service|
80
+ options[:only].any? { |opt| service.matches?(opt) }
81
+ end
82
+ end
83
+ if options[:exclude] && options[:exclude].any?
84
+ service_list = service_list.reject { |service| options[:exclude].include?(service.name) }
125
85
  end
86
+ service_list
126
87
  end
127
88
 
128
89
  def action_word_modified(action)
@@ -5,8 +5,13 @@ class Procedures::HammerSetup < ForemanMaintain::Procedure
5
5
  end
6
6
 
7
7
  def run
8
- result = feature(:hammer).setup_admin_access
9
- logger.info 'Hammer was configured successfully.' if result
8
+ if feature(:foreman_server) && ForemanMaintain::Utils.system_service('httpd', 30).running?
9
+ puts 'Configuring Hammer CLI...'
10
+ result = feature(:hammer).setup_admin_access
11
+ logger.info 'Hammer was configured successfully.' if result
12
+ else
13
+ skip("#{feature(:instance).product_name} server is not running. Hammer can't be setup now.")
14
+ end
10
15
  end
11
16
 
12
17
  def necessary?
@@ -26,7 +26,7 @@ module Procedures::Restore
26
26
  def handle_mongo_service(action, spinner)
27
27
  if feature(:instance).database_local?(:mongo)
28
28
  feature(:service).handle_services(spinner, action,
29
- :only => feature(:mongo).services.keys)
29
+ :only => feature(:mongo).services)
30
30
  end
31
31
  end
32
32
 
@@ -16,7 +16,7 @@ module Procedures
16
16
  action_noun = feature(:service).action_noun(action).capitalize
17
17
  puts "#{action_noun} the following service(s):\n"
18
18
  services = feature(:service).filtered_services(options)
19
- feature(:service).list_services(services)
19
+ print_services(services)
20
20
 
21
21
  with_spinner('') do |spinner|
22
22
  feature(:service).handle_services(spinner, action, options)
@@ -26,6 +26,12 @@ module Procedures
26
26
  def common_options
27
27
  { :only => @only, :exclude => @exclude }
28
28
  end
29
+
30
+ private
31
+
32
+ def print_services(services)
33
+ puts services.map(&:to_s).join(', ')
34
+ end
29
35
  end
30
36
  end
31
37
  end
@@ -11,14 +11,15 @@ module Procedures::Service
11
11
  services = feature(:service).filtered_services(common_options)
12
12
  unit_files = unit_files_list(services)
13
13
  puts unit_files + "\n"
14
+ puts 'All services listed'
14
15
  end
15
16
 
16
17
  def unit_files_list(services)
17
18
  if systemd_installed?
18
- regex = services.map { |service| "^#{service}.service" }.join('\|')
19
+ regex = services.map { |service| "^#{service.name}.service" }.join('\|')
19
20
  execute("systemctl list-unit-files | grep '#{regex}'")
20
21
  else
21
- regex = services.map { |service| "^#{service} " }.join('\|')
22
+ regex = services.map { |service| "^#{service.name} " }.join('\|')
22
23
  execute("chkconfig --list 2>&1 | grep '#{regex}'")
23
24
  end
24
25
  end
@@ -2,12 +2,12 @@ require 'procedures/service/base'
2
2
 
3
3
  module Procedures::Service
4
4
  class Restart < Base
5
- include ForemanMaintain::Concerns::Hammer
6
5
  metadata do
7
6
  description 'Restart applicable services'
8
7
  Base.common_params(self)
9
8
  param :wait_for_hammer_ping,
10
9
  'Wait for hammer ping to return successfully before terminating'
10
+ preparation_steps { Procedures::HammerSetup.new if feature(:katello) }
11
11
  end
12
12
 
13
13
  RETRIES_FOR_SERVICES_RESTART = 5
@@ -32,15 +32,6 @@ module ForemanMaintain
32
32
  File.exist?('/usr/bin/systemctl')
33
33
  end
34
34
 
35
- def service_exists?(service)
36
- if systemd_installed?
37
- systemd = execute("systemctl is-enabled #{service} 2>&1 | tail -1").strip
38
- systemd == 'enabled' || systemd == 'disabled'
39
- else
40
- File.exist?("/etc/init.d/#{service}")
41
- end
42
- end
43
-
44
35
  def check_min_version(name, minimal_version)
45
36
  current_version = package_version(name)
46
37
  if current_version
@@ -0,0 +1,9 @@
1
+ module ForemanMaintain
2
+ module Concerns
3
+ module SystemService
4
+ def system_service(name, priority, options = {})
5
+ ForemanMaintain::Utils.system_service(name, priority, options)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -3,6 +3,7 @@ module ForemanMaintain
3
3
  include Concerns::Logger
4
4
  include Concerns::Reporter
5
5
  include Concerns::SystemHelpers
6
+ include Concerns::SystemService
6
7
  include Concerns::Metadata
7
8
  include Concerns::Finders
8
9
  include ForemanMaintain::Concerns::Hammer
@@ -15,13 +16,13 @@ module ForemanMaintain
15
16
  "#{self.class.metadata[:label]}<#{self.class.name}>"
16
17
  end
17
18
 
18
- # Override method with hash of applicable services for feature.
19
+ # Override method with list of applicable services for feature.
19
20
  # Services have a number for priority in order to ensure
20
21
  # they are started and stopped in the correct order.
21
22
  # example:
22
- # { :foo_service => 10, :bar_service => 20 }
23
+ # [ system_service('foo', 10), system_service('bar', 20) ]
23
24
  def services
24
- {}
25
+ []
25
26
  end
26
27
 
27
28
  # Override to generate additional feature instances that can't be
@@ -21,13 +21,15 @@ module ForemanMaintain
21
21
  end
22
22
 
23
23
  def run
24
- logger.debug(hide_strings("Running command #{@command} with stdin #{@stdin.inspect}"))
24
+ if logger
25
+ logger.debug(hide_strings("Running command #{@command} with stdin #{@stdin.inspect}"))
26
+ end
25
27
  if @interactive
26
28
  run_interactively
27
29
  else
28
30
  run_non_interactively
29
31
  end
30
- logger.debug("output of the command:\n #{hide_strings(output)}")
32
+ logger.debug("output of the command:\n #{hide_strings(output)}") if logger
31
33
  end
32
34
 
33
35
  def interactive?
@@ -0,0 +1,64 @@
1
+ module ForemanMaintain::Utils
2
+ module Service
3
+ class Abstract
4
+ include Comparable
5
+ attr_reader :name, :priority
6
+
7
+ def initialize(name, priority, _options = {})
8
+ @name = name
9
+ @priority = priority
10
+ end
11
+
12
+ def <=>(other)
13
+ prio_cmp = @priority <=> other.priority
14
+ prio_cmp == 0 ? @name <=> other.name : prio_cmp
15
+ end
16
+
17
+ def to_s
18
+ @name
19
+ end
20
+
21
+ def inspect
22
+ "#{self.class.name.split('::').last}(#{@name} [#{@priority}])"
23
+ end
24
+
25
+ def matches?(service)
26
+ if service.is_a? String
27
+ service == @name
28
+ elsif service.instance_of?(self.class)
29
+ service.name == @name
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ def exist?
36
+ raise NotImplementedError
37
+ end
38
+
39
+ def status
40
+ raise NotImplementedError
41
+ end
42
+
43
+ def start
44
+ raise NotImplementedError
45
+ end
46
+
47
+ def stop
48
+ raise NotImplementedError
49
+ end
50
+
51
+ def enable
52
+ raise NotImplementedError
53
+ end
54
+
55
+ def disable
56
+ raise NotImplementedError
57
+ end
58
+
59
+ def running?
60
+ raise NotImplementedError
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,70 @@
1
+ module ForemanMaintain::Utils
2
+ module Service
3
+ class RemoteDB < Abstract
4
+ attr_reader :component, :db_feature
5
+ def initialize(name, priority, options = {})
6
+ super
7
+ @db_feature = options.fetch(:db_feature)
8
+ @component = options.fetch(:component, nil)
9
+ end
10
+
11
+ def to_s
12
+ @name + (@component ? " (#{@component})" : '')
13
+ end
14
+
15
+ def inspect
16
+ component = @component ? ':' + @component : ''
17
+ "#{self.class.name.split('::').last}(#{@name}#{component} [#{@priority}])"
18
+ end
19
+
20
+ def status
21
+ db_status
22
+ end
23
+
24
+ def disable
25
+ [0, db_status("It can't be disabled.").last]
26
+ end
27
+
28
+ def enable
29
+ [0, db_status("It can't be enabled.").last]
30
+ end
31
+
32
+ def start
33
+ db_status
34
+ end
35
+
36
+ def stop
37
+ [0, db_status.last]
38
+ end
39
+
40
+ def running?
41
+ status.first == 0
42
+ end
43
+
44
+ def matches?(service)
45
+ if service.instance_of?(self.class)
46
+ service.name == @name && service.component == @component
47
+ elsif service.is_a? String
48
+ service == @name
49
+ else
50
+ false
51
+ end
52
+ end
53
+
54
+ def exist?
55
+ true
56
+ end
57
+
58
+ private
59
+
60
+ def db_status(msg = nil)
61
+ msg = " #{msg}" if msg
62
+ if @db_feature.ping
63
+ [0, "#{self} is remote and is UP.#{msg}"]
64
+ else
65
+ [1, "#{self} is remote and is DOWN.#{msg}"]
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,58 @@
1
+ module ForemanMaintain::Utils
2
+ module Service
3
+ class Systemd < Abstract
4
+ def initialize(name, priority, _options = {})
5
+ super
6
+ @sys = SystemHelpers.new
7
+ end
8
+
9
+ def command(action, options = {})
10
+ do_wait = options.fetch(:wait, true) # wait for service to start
11
+ if do_wait && File.exist?('/usr/sbin/service-wait')
12
+ "service-wait #{@name} #{action}"
13
+ else
14
+ "systemctl #{action} #{@name}"
15
+ end
16
+ end
17
+
18
+ def status
19
+ execute('status')
20
+ end
21
+
22
+ def start
23
+ execute('start')
24
+ end
25
+
26
+ def stop
27
+ execute('stop')
28
+ end
29
+
30
+ def enable
31
+ execute('enable', :wait => false)
32
+ end
33
+
34
+ def disable
35
+ execute('disable', :wait => false)
36
+ end
37
+
38
+ def running?
39
+ status.first == 0
40
+ end
41
+
42
+ def exist?
43
+ if @sys.systemd_installed?
44
+ systemd = @sys.execute("systemctl is-enabled #{@name} 2>&1 | tail -1").strip
45
+ systemd == 'enabled' || systemd == 'disabled'
46
+ else
47
+ File.exist?("/etc/init.d/#{service}")
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def execute(action, options = {})
54
+ @sys.execute_with_status(command(action, options))
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,14 @@
1
+ require 'foreman_maintain/utils/service/abstract'
2
+ require 'foreman_maintain/utils/service/systemd'
3
+ require 'foreman_maintain/utils/service/remote_db'
4
+
5
+ module ForemanMaintain::Utils
6
+ def self.system_service(name, priority, options = {})
7
+ db_feature = options.fetch(:db_feature, nil)
8
+ if db_feature && db_feature.respond_to?(:local?) && !db_feature.local?
9
+ Service::RemoteDB.new(name, priority, options)
10
+ else
11
+ Service::Systemd.new(name, priority, options)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ module ForemanMaintain
2
+ module Utils
3
+ class SystemHelpers
4
+ include Concerns::SystemHelpers
5
+ end
6
+ end
7
+ end
@@ -4,4 +4,6 @@ require 'foreman_maintain/utils/bash'
4
4
  require 'foreman_maintain/utils/hash_tools'
5
5
  require 'foreman_maintain/utils/curl_response'
6
6
  require 'foreman_maintain/utils/mongo_core'
7
+ require 'foreman_maintain/utils/service'
8
+ require 'foreman_maintain/utils/system_helpers'
7
9
  require 'foreman_maintain/utils/facter'
@@ -1,3 +1,3 @@
1
1
  module ForemanMaintain
2
- VERSION = '0.2.9'.freeze
2
+ VERSION = '0.2.10'.freeze
3
3
  end
@@ -16,6 +16,7 @@ module ForemanMaintain
16
16
  require 'foreman_maintain/concerns/metadata'
17
17
  require 'foreman_maintain/concerns/scenario_metadata'
18
18
  require 'foreman_maintain/concerns/system_helpers'
19
+ require 'foreman_maintain/concerns/system_service'
19
20
  require 'foreman_maintain/concerns/hammer'
20
21
  require 'foreman_maintain/concerns/base_database'
21
22
  require 'foreman_maintain/top_level_modules'
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.2.9
4
+ version: 0.2.10
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: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -113,9 +113,7 @@ files:
113
113
  - bin/foreman-maintain-complete
114
114
  - bin/foreman-maintain-rotate-tar
115
115
  - bin/passenger-recycler
116
- - config/foreman-maintain-hammer.yml
117
116
  - config/foreman-maintain.completion
118
- - config/foreman_maintain.yml
119
117
  - config/foreman_maintain.yml.example
120
118
  - config/foreman_maintain.yml.packaging
121
119
  - config/hammer.yml.example
@@ -143,6 +141,7 @@ files:
143
141
  - definitions/checks/restore/validate_backup.rb
144
142
  - definitions/checks/restore/validate_hostname.rb
145
143
  - definitions/checks/root_user.rb
144
+ - definitions/checks/services_up.rb
146
145
  - definitions/checks/system_registration.rb
147
146
  - definitions/features/candlepin.rb
148
147
  - definitions/features/candlepin_database.rb
@@ -263,6 +262,7 @@ files:
263
262
  - lib/foreman_maintain/concerns/reporter.rb
264
263
  - lib/foreman_maintain/concerns/scenario_metadata.rb
265
264
  - lib/foreman_maintain/concerns/system_helpers.rb
265
+ - lib/foreman_maintain/concerns/system_service.rb
266
266
  - lib/foreman_maintain/config.rb
267
267
  - lib/foreman_maintain/context.rb
268
268
  - lib/foreman_maintain/core_ext.rb
@@ -295,6 +295,11 @@ files:
295
295
  - lib/foreman_maintain/utils/facter.rb
296
296
  - lib/foreman_maintain/utils/hash_tools.rb
297
297
  - lib/foreman_maintain/utils/mongo_core.rb
298
+ - lib/foreman_maintain/utils/service.rb
299
+ - lib/foreman_maintain/utils/service/abstract.rb
300
+ - lib/foreman_maintain/utils/service/remote_db.rb
301
+ - lib/foreman_maintain/utils/service/systemd.rb
302
+ - lib/foreman_maintain/utils/system_helpers.rb
298
303
  - lib/foreman_maintain/version.rb
299
304
  - lib/foreman_maintain/yaml_storage.rb
300
305
  homepage: https://github.com/theforeman/foreman_maintain
@@ -1,4 +0,0 @@
1
- ---
2
- :foreman:
3
- :username: admin
4
- :password: changeme
@@ -1,16 +0,0 @@
1
- # Directory where the logs are stored.
2
- # The default file is named as /log/foreman_maintain.log
3
- :log_dir: 'log'
4
-
5
- # Logger levels: mention one of debug, info, warning, error, fatal
6
- # :log_level: 'debug'
7
-
8
- # Mention definitions directories. Default
9
- # :definitions_dirs:
10
-
11
- # Mention file path to store data
12
- :storage_file: 'data.yml'
13
-
14
- # Mention directory to store whole backup data
15
- :backup_dir: '/var/lib/foreman-maintain'
16
-