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.



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

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.



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

def params
  @params
end

Instance Method Details

#crontab_cmdObject



60
61
62
# File 'lib/resources/crontab.rb', line 60

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

#parse_crontab_line(l) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/resources/crontab.rb', line 45

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



41
42
43
# File 'lib/resources/crontab.rb', line 41

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

#to_sObject



83
84
85
# File 'lib/resources/crontab.rb', line 83

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