Class: Inspec::Resources::Cron

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

Instance Attribute Summary

Attributes inherited from Crontab

#params

Instance Method Summary collapse

Methods inherited from Crontab

#crontab_cmd, #parse_crontab_line, #read_crontab

Methods included from Utils::CommentParser

#parse_comment_line

Constructor Details

#initialize(opts = nil) ⇒ Cron

Returns a new instance of Cron.



18
19
20
21
# File 'lib/inspec/resources/cron.rb', line 18

def initialize(opts = nil)
  super
  @params = read_cron_contents
end

Instance Method Details

#has_entry?(rule) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/inspec/resources/cron.rb', line 37

def has_entry?(rule)
  @params.include?(rule)
end

#read_cron_contentsObject



23
24
25
26
27
28
29
30
31
# File 'lib/inspec/resources/cron.rb', line 23

def read_cron_contents
  result = inspec.command(crontab_cmd)
  if result.exit_status == 0
    result.stdout.lines.map { |l| parse_comment_line(l, comment_char: "#", standalone_comments: false)[0].strip }
  else
    error = result.stdout + "\n" + result.stderr
    raise Inspec::Exceptions::ResourceFailed, "Error while executing #{crontab_cmd} command: #{error}"
  end
end

#resource_idObject



41
42
43
44
# File 'lib/inspec/resources/cron.rb', line 41

def resource_id
  user = @user || "current user"
  "cron #{user}"
end

#tableObject



33
34
35
# File 'lib/inspec/resources/cron.rb', line 33

def table
  @params.reject(&:empty?).join("\n")
end

#to_sObject



46
47
48
49
50
51
52
# File 'lib/inspec/resources/cron.rb', line 46

def to_s
  if is_user_crontab?
    "cron for user #{@user}"
  else
    "cron for current user"
  end
end