Class: Inspec::Resources::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/resources/service.rb

Overview

We detect the init system for each operating system, based on the operating system.

Fedora 15 : systemd RedHat 7 : systemd Ubuntu 15.04 : systemd Ubuntu < 15.04 : upstart

TODO: extend the logic to detect the running init system, independently of OS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_name, service_ctl = nil) ⇒ Service

Returns a new instance of Service.



93
94
95
96
97
98
99
100
101
# File 'lib/inspec/resources/service.rb', line 93

def initialize(service_name, service_ctl = nil)
  @service_name = service_name
  @service_mgmt = nil
  @service_ctl ||= service_ctl
  @cache = nil
  @service_mgmt = select_service_mgmt

  return skip_resource "The `service` resource is not supported on your OS yet." if @service_mgmt.nil?
end

Instance Attribute Details

#service_ctlObject (readonly)

Returns the value of attribute service_ctl.



91
92
93
# File 'lib/inspec/resources/service.rb', line 91

def service_ctl
  @service_ctl
end

Instance Method Details

#descriptionObject

returns the service description from info



256
257
258
259
260
# File 'lib/inspec/resources/service.rb', line 256

def description
  return nil if info.nil?

  info[:description]
end

#enabled?(_level = nil) ⇒ Boolean

verifies if the service is enabled

Returns:

  • (Boolean)


208
209
210
211
212
# File 'lib/inspec/resources/service.rb', line 208

def enabled?(_level = nil)
  return false if info.nil?

  info[:enabled]
end

#has_start_mode?(mode) ⇒ Boolean

matcher equivalent to startmode property; compares start-up mode supported only on windows.

Returns:

  • (Boolean)

Raises:



278
279
280
281
282
# File 'lib/inspec/resources/service.rb', line 278

def has_start_mode?(mode)
  raise Inspec::Exceptions::ResourceSkipped, "The `has_start_mode` matcher is not supported on your OS yet." unless inspec.os.windows?

  mode == startmode
end

#installed?(_name = nil, _version = nil) ⇒ Boolean

verifies the service is registered

Returns:

  • (Boolean)


221
222
223
224
225
# File 'lib/inspec/resources/service.rb', line 221

def installed?(_name = nil, _version = nil)
  return false if info.nil?

  info[:installed]
end

#monitored_by?(monitoring_tool) ⇒ Boolean

matcher to check if the service is monitored by the given monitoring tool/software

Returns:

  • (Boolean)


285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/inspec/resources/service.rb', line 285

def monitored_by?(monitoring_tool)
  # Currently supported monitoring tools are: monit & god
  # To add support for new monitoring tools, extend the case statement with additional monitoring tool and
  # add the definition and logic in a new class (inheriting the base class MonitoringTool: optional)
  case monitoring_tool
  when "monit"
    current_monitoring_tool = Monit.new(inspec, @service_name)
  when "god"
    current_monitoring_tool = God.new(inspec, @service_name)
  else
    puts "The monitoring tool #{monitoring_tool} is not yet supported by InSpec."
  end
  current_monitoring_tool.is_service_monitored?
end

#nameObject

returns the service name from info



249
250
251
252
253
# File 'lib/inspec/resources/service.rb', line 249

def name
  return @service_name if info.nil?

  info[:name]
end

#paramsObject



214
215
216
217
218
# File 'lib/inspec/resources/service.rb', line 214

def params
  return {} if info.nil?

  Hashie::Mash.new(info[:params] || {})
end

#resource_idObject



300
301
302
# File 'lib/inspec/resources/service.rb', line 300

def resource_id
  @service_name || "Service"
end

#runlevels(*args) ⇒ Object

get all runlevels that are available and their configuration



235
236
237
238
239
# File 'lib/inspec/resources/service.rb', line 235

def runlevels(*args)
  return Runlevels.new(self) if info.nil? || info[:runlevels].nil?

  Runlevels.from_hash(self, info[:runlevels], args)
end

#running?(_under = nil) ⇒ Boolean

verifies the service is currently running

Returns:

  • (Boolean)


228
229
230
231
232
# File 'lib/inspec/resources/service.rb', line 228

def running?(_under = nil)
  return false if info.nil?

  info[:running]
end

#select_service_mgmtObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/inspec/resources/service.rb', line 103

def select_service_mgmt # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/MethodLength
  os = inspec.os
  platform = os[:name]

  return WindowsSrv.new(inspec) if os.windows?

  # Ubuntu
  # @see: https://wiki.ubuntu.com/SystemdForUpstartUsers
  # Ubuntu 15.04 : Systemd
  #   Systemd runs with PID 1 as /sbin/init.
  #   Upstart runs with PID 1 as /sbin/upstart.
  # Ubuntu < 15.04 : Upstart
  # Upstart runs with PID 1 as /sbin/init.
  # Systemd runs with PID 1 as /lib/systemd/systemd.

  case platform
  when "ubuntu"
    version = os[:release].to_f
    if version < 15.04
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "linuxmint"
    version = os[:release].to_f
    if version < 18
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "debian"
    if os[:release] == "buster/sid"
      version = 10
    else
      version = os[:release].to_i
    end
    if version > 7
      Systemd.new(inspec, service_ctl)
    elsif version > 0
      SysV.new(inspec, service_ctl || "/usr/sbin/service")
    end
  when "redhat", "fedora", "centos", "oracle", "cloudlinux", "scientific", "rocky", "almalinux"
    version = os[:release].to_i

    systemd = ((platform != "fedora" && version >= 7) ||
               (platform == "fedora" && version >= 15))

    if systemd
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "alibaba"
    if os[:release].to_i >= 3
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "wrlinux"
    SysV.new(inspec, service_ctl)
  when "mac_os_x", "darwin"
    LaunchCtl.new(inspec, service_ctl)
  when "freebsd"
    version = os[:release].to_f
    if version < 10
      BSDInit.new(inspec, service_ctl)
    else
      FreeBSD10Init.new(inspec, service_ctl)
    end
  when "arch"
    Systemd.new(inspec, service_ctl)
  when "coreos"
    Systemd.new(inspec, service_ctl)
  when "suse", "opensuse"
    if os[:release].to_i >= 12
      Systemd.new(inspec, service_ctl)
    else
      SysV.new(inspec, service_ctl || "/sbin/service")
    end
  when "aix"
    SrcMstr.new(inspec)
  when "amazon"
    # If `initctl` exists on the system, use `Upstart`. Else use `Systemd` since all-new Amazon Linux supports `systemctl`.
    # This way, it is not dependent on the version of Amazon Linux.
    if inspec.command("initctl").exist? || inspec.command("/sbin/initctl").exist?
      Upstart.new(inspec, service_ctl)
    else
      Systemd.new(inspec, service_ctl)
    end
  when "solaris", "smartos", "omnios", "openindiana", "opensolaris", "nexentacore"
    Svcs.new(inspec)
  when "yocto"
    Systemd.new(inspec, service_ctl)
  when "alpine"
    SysV.new(inspec, service_ctl)
  end
end

#startmodeObject

returns the service start up mode from info



263
264
265
266
267
# File 'lib/inspec/resources/service.rb', line 263

def startmode
  return nil if info.nil?

  info[:startmode]
end

#startnameObject

returns the service’s user from info



270
271
272
273
274
# File 'lib/inspec/resources/service.rb', line 270

def startname
  return nil if info.nil?

  info[:startname]
end

#to_sObject



304
305
306
# File 'lib/inspec/resources/service.rb', line 304

def to_s
  "Service #{@service_name}"
end

#typeObject

returns the service type from info



242
243
244
245
246
# File 'lib/inspec/resources/service.rb', line 242

def type
  return nil if info.nil?

  info[:type]
end