Skip to content

Commit

Permalink
Fixes #22458 - Add katello-service functionality
Browse files Browse the repository at this point in the history
Katello-service is added as foreman-maintain service
  • Loading branch information
John Mitsch committed Mar 28, 2018
1 parent 4908493 commit 80a7757
Show file tree
Hide file tree
Showing 38 changed files with 810 additions and 195 deletions.
4 changes: 3 additions & 1 deletion definitions/checks/hammer_ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Checks::HammerPing < ForemanMaintain::Check

def run
result = feature(:hammer).hammer_ping_cmd
restart_procedure = Procedures::Service::Restart.new(:only => result[:data],
:wait_for_hammer_ping => true)
assert(result[:success],
result[:message],
:next_steps => Procedures::KatelloService::Restart.new(:only => result[:data]))
:next_steps => restart_procedure)
end
end
12 changes: 12 additions & 0 deletions definitions/checks/root_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Checks::RootUser < ForemanMaintain::Check
metadata do
label :root_user
tags :root_user
description 'Check if command is run as root user'
end

def run
is_root_user = Process.uid == 0
assert(is_root_user, 'This command needs to be run as the root user')
end
end
7 changes: 7 additions & 0 deletions definitions/features/candlepin_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ def env_content_ids_with_null_content
query(sql).map { |r| r['id'] }
end

def services
{
'tomcat' => 20,
'tomcat6' => 20
}
end

private

def load_configuration
Expand Down
8 changes: 8 additions & 0 deletions definitions/features/downstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ class Features::Downstream < ForemanMaintain::Feature
end
end

def less_than_version?(version)
Gem::Version.new(current_version) < Gem::Version.new(version)
end

def at_least_version?(version)
Gem::Version.new(current_version) >= Gem::Version.new(version)
end

def current_version
@current_version ||= rpm_version('satellite') || version_from_source
end
Expand Down
7 changes: 7 additions & 0 deletions definitions/features/foreman_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ def valid_dhcp_configs?
dhcp_req_pass? && !syntax_error_exists?
end

def services
{
'foreman-proxy' => 20,
'smart_proxy_dynflow_core' => 20
}
end

private

def dhcp_curl_cmd
Expand Down
7 changes: 7 additions & 0 deletions definitions/features/foreman_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class ForemanServer < ForemanMaintain::Feature
server?
end
end

def services
{
'postgresql' => 5,
'httpd' => 30
}
end
end
end
end
4 changes: 4 additions & 0 deletions definitions/features/foreman_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ def fetch_tasks_status(state, spinner)
puts "\nTimeout: #{e.message}. Try again."
end

def services
{ 'foreman-tasks' => 30 }
end

private

def check_task_count(state, spinner)
Expand Down
9 changes: 9 additions & 0 deletions definitions/features/katello.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ def data_dirs
def current_version
@current_version ||= rpm_version('katello')
end

def services
{
'qpidd' => 10,
'qdrouterd' => 10,
'goferd' => 30,
'elasticsearch' => 30
}
end
end
118 changes: 0 additions & 118 deletions definitions/features/katello_service.rb

This file was deleted.

11 changes: 11 additions & 0 deletions definitions/features/pulp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,15 @@ class Features::Pulp < ForemanMaintain::Feature
find_package('pulp-server')
end
end

def services
{
'mongod' => 5,
'squid' => 10,
'pulp_workers' => 20,
'pulp_celerybeat' => 20,
'pulp_resource_manager' => 20,
'pulp_streamer' => 20
}
end
end
13 changes: 13 additions & 0 deletions definitions/features/puppet_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Features::PuppetServer < ForemanMaintain::Feature
metadata do
label :puppet_server

confine do
find_package('puppetserver')
end
end

def services
{ 'puppetserver' => 30 }
end
end
125 changes: 125 additions & 0 deletions definitions/features/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
class Features::Service < ForemanMaintain::Feature
metadata do
label :service
end

def handle_services(spinner, action, options = {})
# options is used to handle "exclude" and "only" i.e.
# { :only => ["httpd"] }
# { :exclude => ["pulp-workers", "tomcat"] }
if feature(:downstream) && feature(:downstream).less_than_version?('6.3')
use_katello_service(action, options)
else
options[:reverse] = action == 'stop'

filtered_services(options).each do |service|
spinner.update("#{action_noun(action)} #{service}")
perform_action_on_service(action, service)
end

spinner.update("All services #{action_past_tense(action)}")
end
end

def list_services(service_list)
service_list = service_list.join(', ')
puts "#{service_list}\n"
end

def get_services_from_features(features)
features.map(&:services).
inject(&:merge).
sort_by { |_, value| value }.
map { |service| service[0] }
end

def existing_services
service_list = get_services_from_features(available_features)
service_list.select { |service| service_exists?(service) }
end

def filtered_services(options)
service_list = existing_services
service_list = filter_services(service_list, options)
raise 'No services found matching your parameters' unless service_list.any?
options[:reverse] ? service_list.reverse : service_list
end

def action_noun(action)
action_word_modified(action) + 'ing'
end

def action_past_tense(action)
action_word_modified(action) + 'ed'
end

private

def available_features
@available_features || ForemanMaintain.available_features
end

def filter_services(service_list, options)
service_list &= options[:only] if options[:only] && options[:only].any?
service_list -= options[:exclude] if options[:exclude] && options[:exclude].any?
service_list
end

def perform_action_on_service(action, service)
command = "systemctl #{action} #{service}"
if action == 'status'
status = execute(command)
puts "\n\n#{status}\n\n"
else
execute!(command)
end
end

def action_word_modified(action)
case action
when 'status'
'display'
when 'enable', 'disable'
action.chomp('e')
when 'stop'
action + 'p'
else
action
end
end

def use_katello_service(action, options)
if %w[enable disable].include?(action)
raise 'Service enable and disable are only supported in Satellite 6.3+'
end

command = "katello-service #{action} "

# katello-service in 6.1 does not support --only
if feature(:downstream).less_than_version?('6.2')
excluded_services = exclude_services_only(options)
command += "--exclude #{excluded_services.join(',')}" if excluded_services.any?
else
command += katello_service_filters(options)
end

run_katello_service(command)
end

def run_katello_service(command)
puts 'Services are handled by katello-service in Satellite versions 6.2 and earlier. ' \
"Redirecting to: \n#{command}\n"
puts execute(command)
end

def exclude_services_only(options)
existing_services - filtered_services(options)
end

def katello_service_filters(options)
filters = ''
filters += "--exclude #{options[:exclude]}" if options[:exclude] && options[:exclude].any?
filters += "--only #{options[:only]}" if options[:only] && options[:only].any?
filters
end
end
Loading

0 comments on commit 80a7757

Please sign in to comment.