Class: Inspec::Resources::Crontab

Inherits:
Object
  • Object
show all
Includes:
CommentParser
Defined in:
lib/resources/crontab.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommentParser

#parse_comment_line

Constructor Details

#initialize(user = nil) ⇒ Crontab

Returns a new instance of Crontab.



30
31
32
33
34
35
# File 'lib/resources/crontab.rb', line 30

def initialize(user = nil)
  @user   = user
  @params = read_crontab

  return skip_resource 'The `crontab` resource is not supported on your OS.' unless inspec.os.unix?
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



26
27
28
# File 'lib/resources/crontab.rb', line 26

def params
  @params
end

Instance Method Details

#crontab_cmdObject



56
57
58
# File 'lib/resources/crontab.rb', line 56

def crontab_cmd
  @user.nil? ? 'crontab -l' : "crontab -l -u #{@user}"
end

#parse_crontab_line(l) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/resources/crontab.rb', line 41

def parse_crontab_line(l)
  data, = parse_comment_line(l, comment_char: '#', standalone_comments: false)
  return nil if data.nil? || data.empty?

  elements = data.split(/\s+/, 6)
  {
    'minute'  => elements.at(0),
    'hour'    => elements.at(1),
    'day'     => elements.at(2),
    'month'   => elements.at(3),
    'weekday' => elements.at(4),
    'command' => elements.at(5),
  }
end

#read_crontabObject



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

def read_crontab
  inspec.command(crontab_cmd).stdout.lines.map { |l| parse_crontab_line(l) }.compact
end

#to_sObject



79
80
81
# File 'lib/resources/crontab.rb', line 79

def to_s
  @user.nil? ? 'crontab for current user' : "crontab for user #{@user}"
end