Class: Chef::Provider::Cron
- Inherits:
-
Chef::Provider
- Object
- Chef::Provider
- Chef::Provider::Cron
- 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 =
%i{reboot yearly annually monthly weekly daily midnight hourly}.freeze
- CRON_ATTRIBUTES =
%i{minute hour day month weekday time command mailto path shell home environment}.freeze
- WEEKDAY_SYMBOLS =
%i{sunday monday tuesday wednesday thursday friday saturday}.freeze
- CRON_PATTERN =
%r{\A([-0-9*,/]+)\s([-0-9*,/]+)\s([-0-9*,/]+)\s([-0-9*,/]+|[a-zA-Z]{3})\s([-0-9*,/]+|[a-zA-Z]{3})\s(.*)}.freeze
- SPECIAL_PATTERN =
/\A(@(#{SPECIAL_TIME_VALUES.join('|')}))\s(.*)/.freeze
- ENV_PATTERN =
/\A(\S+)=(\S*)/.freeze
- ENVIRONMENT_PROPERTIES =
%w{MAILTO PATH SHELL HOME}.freeze
- 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
-
#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, #current_resource, #logger, #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
Methods inherited from Chef::Provider
action, #action_nothing, #check_resource_semantics!, #cleanup_after_converge, #compile_and_converge_action, #converge_by, #converge_if_changed, #cookbook_name, #define_resource_requirements, #description, #events, include_resource_dsl?, include_resource_dsl_module, #introduced, #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::ShellOut
apply_default_env, maybe_add_timeout, #shell_out, #shell_out!
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 Mixin::PowershellExec
Methods included from DSL::Powershell
Methods included from DSL::RegistryHelper
#registry_data_exists?, #registry_get_subkeys, #registry_get_values, #registry_has_subkeys?, #registry_key_exists?, #registry_value_exists?
Methods included from DSL::DataQuery
#data_bag, #data_bag_item, #search, #tagged?
Methods included from EncryptedDataBagItem::CheckEncrypted
Methods included from DSL::PlatformIntrospection
#older_than_win_2012_or_8?, #platform?, #platform_family?, #value_for_platform, #value_for_platform_family
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!, #resources, #with_run_context
Constructor Details
#initialize(new_resource, run_context) ⇒ Cron
Returns a new instance of Cron.
37 38 39 40 41 |
# File 'lib/chef/provider/cron.rb', line 37 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.
42 43 44 |
# File 'lib/chef/provider/cron.rb', line 42 def cron_empty @cron_empty end |
#cron_exists ⇒ Object
Returns the value of attribute cron_exists.
42 43 44 |
# File 'lib/chef/provider/cron.rb', line 42 def cron_exists @cron_exists end |
Instance Method Details
#action_create ⇒ Object
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 156 |
# File 'lib/chef/provider/cron.rb', line 98 def action_create crontab = "" newcron = "" cron_found = false newcron = get_crontab_entry if @cron_exists unless cron_different? logger.trace("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 logger.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 logger.info("#{new_resource} added crontab entry") end end end |
#action_delete ⇒ Object
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 191 |
# File 'lib/chef/provider/cron.rb', line 158 def action_delete if @cron_exists crontab = "" 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 logger.info("#{new_resource} deleted crontab entry") end end end |
#cron_different? ⇒ Boolean
92 93 94 95 96 |
# File 'lib/chef/provider/cron.rb', line 92 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
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 90 |
# File 'lib/chef/provider/cron.rb', line 44 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}" logger.trace("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 logger.trace("Cron '#{new_resource.name}' not found") unless @cron_exists else logger.trace("Cron empty for '#{new_resource.user}'") @cron_empty = true end current_resource end |