diff --git a/modules/libs/lib/facter/ceph_services.rb b/modules/libs/lib/facter/ceph_services.rb new file mode 100644 index 0000000..541ef46 --- /dev/null +++ b/modules/libs/lib/facter/ceph_services.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'facter' + +# Detects active ceph service instances via systemctl and exposes facts +# for use in ceph service management profiles. +module Unkin::Ceph::Utils + TYPES = %w[mon mgr mds osd].freeze + + def self.services + output = Facter::Core::Execution.execute( + 'systemctl list-units "ceph*" --no-legend --plain --all 2>/dev/null', + on_fail: '' + ) + parse_units(output) + end + + def self.parse_units(output) + result = TYPES.each_with_object({}) { |type, hash| hash[type] = [] } + output.each_line do |line| + unit = line.split.first + next unless unit + + match_unit(result, unit) + end + result + end + + def self.match_unit(result, unit) + TYPES.each do |type| + match = unit.match(/\Aceph-#{type}@(.+)\.service\z/) + result[type] << "ceph-#{type}@#{match[1]}" if match + end + end + + TYPES.each do |type| + define_singleton_method(:"#{type}?") { !services[type].empty? } + end +end + +Facter.add('ceph_services') do + setcode { Unkin::Ceph::Utils.services } +end + +Unkin::Ceph::Utils::TYPES.each do |type| + Facter.add("is_ceph_#{type}") do + setcode { Unkin::Ceph::Utils.public_send(:"#{type}?") } + end +end diff --git a/site/profiles/manifests/ceph/mds.pp b/site/profiles/manifests/ceph/mds.pp new file mode 100644 index 0000000..1745c75 --- /dev/null +++ b/site/profiles/manifests/ceph/mds.pp @@ -0,0 +1,13 @@ +class profiles::ceph::mds ( + Boolean $ensure_running = true, +) { + + if $ensure_running and $facts['is_ceph_mds'] { + $facts['ceph_services']['mds'].each |String $svc| { + service { $svc: + ensure => running, + enable => true, + } + } + } +} diff --git a/site/profiles/manifests/ceph/mgr.pp b/site/profiles/manifests/ceph/mgr.pp new file mode 100644 index 0000000..34b3b4b --- /dev/null +++ b/site/profiles/manifests/ceph/mgr.pp @@ -0,0 +1,13 @@ +class profiles::ceph::mgr ( + Boolean $ensure_running = true, +) { + + if $ensure_running and $facts['is_ceph_mgr'] { + $facts['ceph_services']['mgr'].each |String $svc| { + service { $svc: + ensure => running, + enable => true, + } + } + } +} diff --git a/site/profiles/manifests/ceph/mon.pp b/site/profiles/manifests/ceph/mon.pp new file mode 100644 index 0000000..9be1864 --- /dev/null +++ b/site/profiles/manifests/ceph/mon.pp @@ -0,0 +1,13 @@ +class profiles::ceph::mon ( + Boolean $ensure_running = true, +) { + + if $ensure_running and $facts['is_ceph_mon'] { + $facts['ceph_services']['mon'].each |String $svc| { + service { $svc: + ensure => running, + enable => true, + } + } + } +} diff --git a/site/profiles/manifests/ceph/osd.pp b/site/profiles/manifests/ceph/osd.pp new file mode 100644 index 0000000..d56ff1d --- /dev/null +++ b/site/profiles/manifests/ceph/osd.pp @@ -0,0 +1,13 @@ +class profiles::ceph::osd ( + Boolean $ensure_running = true, +) { + + if $ensure_running and $facts['is_ceph_osd'] { + $facts['ceph_services']['osd'].each |String $svc| { + service { $svc: + ensure => running, + enable => true, + } + } + } +}