Class: ConfigOMat::Service

Inherits:
ConfigItem show all
Defined in:
lib/config_o_mat/shared/types.rb

Constant Summary collapse

RESTART_MODES =
%i[restart flip_flop restart_all none].freeze

Instance Attribute Summary collapse

Attributes inherited from ConfigItem

#errors

Instance Method Summary collapse

Methods inherited from ConfigItem

#==, #error, #errors?, #validate!

Constructor Details

#initialize(opts) ⇒ Service

Returns a new instance of Service.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/config_o_mat/shared/types.rb', line 96

def initialize(opts)
  @systemd_unit = (opts[:systemd_unit] || '')
  @restart_mode = opts[:restart_mode]&.to_sym
  @templates = opts[:templates]

  if (@restart_mode == :flip_flop || @restart_mode == :restart_all) && !@systemd_unit.include?('@')
    @systemd_unit = "#{@systemd_unit}@"
  end

  @restart_unit = @systemd_unit

  if @restart_mode == :restart_all
    @restart_unit = "#{@restart_unit}\\x2a"
  end
end

Instance Attribute Details

#restart_modeObject (readonly)

Returns the value of attribute restart_mode.



94
95
96
# File 'lib/config_o_mat/shared/types.rb', line 94

def restart_mode
  @restart_mode
end

#restart_unitObject (readonly)

Returns the value of attribute restart_unit.



94
95
96
# File 'lib/config_o_mat/shared/types.rb', line 94

def restart_unit
  @restart_unit
end

#systemd_unitObject (readonly)

Returns the value of attribute systemd_unit.



94
95
96
# File 'lib/config_o_mat/shared/types.rb', line 94

def systemd_unit
  @systemd_unit
end

#templatesObject (readonly)

Returns the value of attribute templates.



94
95
96
# File 'lib/config_o_mat/shared/types.rb', line 94

def templates
  @templates
end

Instance Method Details

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/config_o_mat/shared/types.rb', line 137

def eql?(other)
  return false if !super(other)
  return false if other.systemd_unit != systemd_unit || other.restart_mode != restart_mode || other.templates != templates
  true
end

#hashObject



133
134
135
# File 'lib/config_o_mat/shared/types.rb', line 133

def hash
  systemd_unit.hash ^ restart_mode.hash ^ templates.hash
end

#validateObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/config_o_mat/shared/types.rb', line 112

def validate
  error :templates, PRESENCE_ERROR_MSG if @templates.nil? || @templates.empty?
  unless @templates.is_a?(Array) && @templates.all? { |v| v.is_a?(String) }
    error :templates, 'must be an array of strings'
  end
  error :systemd_unit, PRESENCE_ERROR_MSG if @systemd_unit.nil? || @systemd_unit.empty? || @systemd_unit == '@'
  error :restart_mode, "must be one of #{RESTART_MODES}" unless RESTART_MODES.include?(@restart_mode)

  if @restart_mode == :flip_flop && !@systemd_unit.end_with?('@')
    error :systemd_unit, 'must not contain an instance (anything after a @)'
  end

  if restart_mode == :restart && @systemd_unit.end_with?('@')
    error :systemd_unit, 'must not be a naked instantiated unit when restart_mode=restart'
  end

  if restart_mode == :restart_all && !@systemd_unit.end_with?('@')
    error :systemd_unit, 'must not be an instantiated unit when restart_mode=restart_all'
  end
end