Class: Chef::Provider::Cron
- Inherits:
-
Chef::Provider
- Object
- Chef::Provider
- Chef::Provider::Cron
- Includes:
- Mixin::Command
- Defined in:
- lib/chef/provider/cron.rb,
lib/chef/provider/cron/aix.rb,
lib/chef/provider/cron/unix.rb
Direct Known Subclasses
Defined Under Namespace
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
Constants included from Mixin::ShellOut
Mixin::ShellOut::DEPRECATED_OPTIONS
Instance Attribute Summary collapse
-
#cron_empty ⇒ Object
Returns the value of attribute cron_empty.
-
#cron_exists ⇒ Object
Returns the value of attribute cron_exists.
Attributes inherited from Chef::Provider
#action, #cookbook_name, #current_resource, #new_resource, #recipe_name, #run_context
Instance Method Summary collapse
- #action_create ⇒ Object
- #action_delete ⇒ Object
- #cron_different? ⇒ Boolean
-
#initialize(new_resource, run_context) ⇒ Cron
constructor
A new instance of Cron.
- #load_current_resource ⇒ Object
- #whyrun_supported? ⇒ Boolean
Methods included from Mixin::Command
#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_and_return_stdout_stderr, #run_command_with_systems_locale
Methods included from Mixin::Command::Windows
Methods included from Mixin::Command::Unix
Methods inherited from Chef::Provider
#action_nothing, #check_resource_semantics!, #cleanup_after_converge, #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?
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 DeprecatedLWRPClass
#const_missing, #deprecated_constants, #register_deprecated_lwrp_class
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
#run_command_compatible_options, #shell_out, #shell_out!, #shell_out_with_systems_locale, #shell_out_with_systems_locale!
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
Returns a new instance of Cron.
38 39 40 41 42 |
# File 'lib/chef/provider/cron.rb', line 38 def initialize(new_resource, run_context) super(new_resource, run_context) @cron_exists = false @cron_empty = false end |
Instance Attribute Details
#cron_empty ⇒ Object
Returns the value of attribute cron_empty.
43 44 45 |
# File 'lib/chef/provider/cron.rb', line 43 def cron_empty @cron_empty end |
#cron_exists ⇒ Object
Returns the value of attribute cron_exists.
43 44 45 |
# File 'lib/chef/provider/cron.rb', line 43 def cron_exists @cron_exists end |
Instance Method Details
#action_create ⇒ Object
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 156 157 158 159 160 161 |
# File 'lib/chef/provider/cron.rb', line 103 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_delete ⇒ Object
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 191 192 193 194 195 196 197 |
# File 'lib/chef/provider/cron.rb', line 163 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
97 98 99 100 101 |
# File 'lib/chef/provider/cron.rb', line 97 def cron_different? CRON_ATTRIBUTES.any? do |cron_var| @new_resource.send(cron_var) != @current_resource.send(cron_var) end end |
#load_current_resource ⇒ Object
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 90 91 92 93 94 95 |
# File 'lib/chef/provider/cron.rb', line 49 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 |
#whyrun_supported? ⇒ Boolean
45 46 47 |
# File 'lib/chef/provider/cron.rb', line 45 def whyrun_supported? true end |