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(opts = nil) ⇒ Crontab

Returns a new instance of Crontab.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/resources/crontab.rb', line 38

def initialize(opts = nil)
  if opts.respond_to?(:fetch)
    Hash[opts.map { |k, v| [k.to_sym, v] }]
    @user = opts.fetch(:user, nil)
    @path = opts.fetch(:path, nil)
    raise Inspec::Exceptions::ResourceFailed, 'A user or path must be supplied.' if @user.nil? && @path.nil?
  else
    @user = opts
    @path = nil
  end
  raise Inspec::Exceptions::ResourceSkipped, 'The `crontab` resource is not supported on your OS.' unless inspec.os.unix?
  @params = read_crontab
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#crontab_cmdObject



64
65
66
# File 'lib/resources/crontab.rb', line 64

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

#parse_crontab_line(l) ⇒ Object



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

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

  is_system_crontab? ? parse_system_crontab(data) : parse_user_crontab(data)
end

#read_crontabObject



52
53
54
55
# File 'lib/resources/crontab.rb', line 52

def read_crontab
  ct = is_system_crontab? ? inspec.file(@path).content : inspec.command(crontab_cmd).stdout
  ct.lines.map { |l| parse_crontab_line(l) }.compact
end

#to_sObject



88
89
90
91
92
93
94
95
96
# File 'lib/resources/crontab.rb', line 88

def to_s
  if is_system_crontab?
    "crontab for path #{@path}"
  elsif is_user_crontab?
    "crontab for user #{@user}"
  else
    'crontab for current user'
  end
end