Class: Chef::Provider::Cron

Inherits:
Chef::Provider show all
Defined in:
lib/chef/provider/cron.rb,
lib/chef/provider/cron/aix.rb,
lib/chef/provider/cron/unix.rb

Direct Known Subclasses

Unix

Defined Under Namespace

Classes: Aix, Unix

Constant Summary collapse

SPECIAL_TIME_VALUES =
[:reboot, :yearly, :annually, :monthly, :weekly, :daily, :midnight, :hourly]
CRON_ATTRIBUTES =
[:minute, :hour, :day, :month, :weekday, :time, :command, :mailto, :path, :shell, :home, :environment]
WEEKDAY_SYMBOLS =
[:sunday, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday]
CRON_PATTERN =
/\A([-0-9*,\/]+)\s([-0-9*,\/]+)\s([-0-9*,\/]+)\s([-0-9*,\/]+|[a-zA-Z]{3})\s([-0-9*,\/]+|[a-zA-Z]{3})\s(.*)/
SPECIAL_PATTERN =
/\A(@(#{SPECIAL_TIME_VALUES.join('|')}))\s(.*)/
ENV_PATTERN =
/\A(\S+)=(\S*)/
Solaris =

Just to create an alias so 'Chef::Provider::Cron::Solaris' is exposed and accessible to existing consumers of class.

Chef::Provider::Cron::Unix

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context

Instance Method Summary collapse

Methods inherited from Chef::Provider

action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #define_resource_requirements, #events, include_resource_dsl?, include_resource_dsl_module, #node, #process_resource_requirements, provides, provides?, #requirements, #resource_collection, #resource_updated?, #run_action, #set_updated_status, supports?, use_inline_resources, #whyrun_mode?, #whyrun_supported?

Methods included from Mixin::Provides

#provided_as, #provides, #provides?

Methods included from Mixin::DescendantsTracker

#descendants, descendants, #direct_descendants, direct_descendants, #find_descendants_by_name, find_descendants_by_name, #inherited, store_inherited

Methods included from Mixin::LazyModuleInclude

#descendants, #include, #included

Methods included from Mixin::NotifyingBlock

#notifying_block, #subcontext_block

Methods included from DSL::DeclareResource

#build_resource, #declare_resource, #delete_resource, #delete_resource!, #edit_resource, #edit_resource!, #find_resource, #find_resource!, #with_run_context

Methods included from Mixin::ShellOut

#a_to_s, #clean_array, #shell_out, #shell_out!, #shell_out_compact, #shell_out_compact!, #shell_out_compact_timeout, #shell_out_compact_timeout!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!

Methods included from Mixin::PathSanity

#enforce_path_sanity, #sanitized_path

Methods included from Mixin::PowershellOut

#powershell_out, #powershell_out!

Methods included from Mixin::WindowsArchitectureHelper

#assert_valid_windows_architecture!, #disable_wow64_file_redirection, #forced_32bit_override_required?, #is_i386_process_on_x86_64_windows?, #node_supports_windows_architecture?, #node_windows_architecture, #restore_wow64_file_redirection, #valid_windows_architecture?, #with_os_architecture, #wow64_architecture_override_required?, #wow64_directory

Methods included from DSL::PlatformIntrospection

#docker?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family

Constructor Details

#initialize(new_resource, run_context) ⇒ Cron


36
37
38
39
40
# File 'lib/chef/provider/cron.rb', line 36

def initialize(new_resource, run_context)
  super(new_resource, run_context)
  @cron_exists = false
  @cron_empty = false
end

Instance Attribute Details

#cron_emptyObject

Returns the value of attribute cron_empty


41
42
43
# File 'lib/chef/provider/cron.rb', line 41

def cron_empty
  @cron_empty
end

#cron_existsObject

Returns the value of attribute cron_exists


41
42
43
# File 'lib/chef/provider/cron.rb', line 41

def cron_exists
  @cron_exists
end

Instance Method Details

#action_createObject


97
98
99
100
101
102
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
# File 'lib/chef/provider/cron.rb', line 97

def action_create
  crontab = String.new
  newcron = String.new
  cron_found = false

  newcron = get_crontab_entry

  if @cron_exists
    unless cron_different?
      Chef::Log.debug("Skipping existing cron entry '#{new_resource.name}'")
      return
    end
    read_crontab.each_line do |line|
      case line.chomp
      when "# Chef Name: #{new_resource.name}"
        cron_found = true
        next
      when ENV_PATTERN
        crontab << line unless cron_found
        next
      when SPECIAL_PATTERN
        if cron_found
          cron_found = false
          crontab << newcron
          next
        end
      when CRON_PATTERN
        if cron_found
          cron_found = false
          crontab << newcron
          next
        end
      else
        if cron_found # We've got a Chef comment with no following crontab line
          crontab << newcron
          cron_found = false
        end
      end
      crontab << line
    end

    # Handle edge case where the Chef comment is the last line in the current crontab
    crontab << newcron if cron_found

    converge_by("update crontab entry for #{new_resource}") do
      write_crontab crontab
      Chef::Log.info("#{new_resource} updated crontab entry")
    end

  else
    crontab = read_crontab unless @cron_empty
    crontab << newcron

    converge_by("add crontab entry for #{new_resource}") do
      write_crontab crontab
      Chef::Log.info("#{new_resource} added crontab entry")
    end
  end
end

#action_deleteObject


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
# File 'lib/chef/provider/cron.rb', line 157

def action_delete
  if @cron_exists
    crontab = String.new
    cron_found = false
    read_crontab.each_line do |line|
      case line.chomp
      when "# Chef Name: #{new_resource.name}"
        cron_found = true
        next
      when ENV_PATTERN
        next if cron_found
      when SPECIAL_PATTERN
        if cron_found
          cron_found = false
          next
        end
      when CRON_PATTERN
        if cron_found
          cron_found = false
          next
        end
      else
        # We've got a Chef comment with no following crontab line
        cron_found = false
      end
      crontab << line
    end
    description = cron_found ? "remove #{new_resource.name} from crontab" : "save unmodified crontab"
    converge_by(description) do
      write_crontab crontab
      Chef::Log.info("#{new_resource} deleted crontab entry")
    end
  end
end

#cron_different?Boolean


91
92
93
94
95
# File 'lib/chef/provider/cron.rb', line 91

def cron_different?
  CRON_ATTRIBUTES.any? do |cron_var|
    new_resource.send(cron_var) != current_resource.send(cron_var)
  end
end

#load_current_resourceObject


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chef/provider/cron.rb', line 43

def load_current_resource
  crontab_lines = []
  @current_resource = Chef::Resource::Cron.new(new_resource.name)
  current_resource.user(new_resource.user)
  @cron_exists = false
  if crontab = read_crontab
    cron_found = false
    crontab.each_line do |line|
      case line.chomp
      when "# Chef Name: #{new_resource.name}"
        Chef::Log.debug("Found cron '#{new_resource.name}'")
        cron_found = true
        @cron_exists = true
        next
      when ENV_PATTERN
        set_environment_var($1, $2) if cron_found
        next
      when SPECIAL_PATTERN
        if cron_found
          current_resource.time($2.to_sym)
          current_resource.command($3)
          cron_found = false
        end
      when CRON_PATTERN
        if cron_found
          current_resource.minute($1)
          current_resource.hour($2)
          current_resource.day($3)
          current_resource.month($4)
          current_resource.weekday($5)
          current_resource.command($6)
          cron_found = false
        end
        next
      else
        cron_found = false # We've got a Chef comment with no following crontab line
        next
      end
    end
    Chef::Log.debug("Cron '#{new_resource.name}' not found") unless @cron_exists
  else
    Chef::Log.debug("Cron empty for '#{new_resource.user}'")
    @cron_empty = true
  end

  current_resource
end