From 578a69011eadda14e51c2ca83f9206f58b9a6243 Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Fri, 8 May 2020 17:56:56 +0530 Subject: Fixed cron resource weekday property. Implemented same weekday functionality for cron_d resource. Fixed rspec failure. Added cron helper for weekday functionality Added spec for cron helper. Signed-off-by: antima-gupta --- lib/chef/provider/cron.rb | 12 ------ lib/chef/resource/cron.rb | 30 ++++----------- lib/chef/resource/cron_d.rb | 8 ++-- lib/chef/resource/helpers/cron.rb | 19 ++++++++++ lib/chef/resource/helpers/cron_validations.rb | 7 +++- spec/functional/resource/cron_spec.rb | 10 +++++ spec/unit/provider/cron_spec.rb | 44 +--------------------- spec/unit/resource/cron_spec.rb | 4 +- spec/unit/resource/helpers/cron_spec.rb | 31 +++++++++++++++ .../unit/resource/helpers/cron_validations_spec.rb | 6 ++- 10 files changed, 85 insertions(+), 86 deletions(-) create mode 100644 lib/chef/resource/helpers/cron.rb create mode 100644 spec/unit/resource/helpers/cron_spec.rb diff --git a/lib/chef/provider/cron.rb b/lib/chef/provider/cron.rb index a5669b0a4a..8abecfa698 100644 --- a/lib/chef/provider/cron.rb +++ b/lib/chef/provider/cron.rb @@ -15,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - require_relative "../log" require_relative "../provider" @@ -27,8 +26,6 @@ class Chef 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 @@ -288,15 +285,6 @@ class Chef newcron.join("\n") end - - def weekday_in_crontab - weekday_in_crontab = WEEKDAY_SYMBOLS.index(new_resource.weekday) - if weekday_in_crontab.nil? - new_resource.weekday - else - weekday_in_crontab.to_s - end - end end end end diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb index 79cf5642af..2915463c2f 100644 --- a/lib/chef/resource/cron.rb +++ b/lib/chef/resource/cron.rb @@ -19,6 +19,7 @@ require_relative "../resource" require_relative "helpers/cron_validations" +require_relative "helpers/cron" require_relative "../provider/cron" # do not remove. we actually need this below class Chef @@ -64,29 +65,12 @@ class Chef "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, } - def weekday(arg = nil) - if arg.is_a?(Integer) - converted_arg = arg.to_s - else - converted_arg = arg - end - begin - error_message = "You provided '#{arg}' as a weekday, acceptable values are " - error_message << Provider::Cron::WEEKDAY_SYMBOLS.map { |sym| ":#{sym}" }.join(", ") - error_message << " and a string in crontab format" - if (arg.is_a?(Symbol) && !Provider::Cron::WEEKDAY_SYMBOLS.include?(arg)) || - (!arg.is_a?(Symbol) && integerize(arg) > 7) || - (!arg.is_a?(Symbol) && integerize(arg) < 0) - raise RangeError, error_message - end - rescue ArgumentError - end - set_or_return( - :weekday, - converted_arg, - kind_of: [String, Symbol] - ) - end + property :weekday, [Integer, String, Symbol], + description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, `monday-sunday`, or `*`), where Sunday is both `0` and `7`.", + default: "*", coerce: proc { |wday| Chef::ResourceHelpers::Cron.weekday_in_crontab(wday) }, + callbacks: { + "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, + } property :time, Symbol, description: "A time interval.", diff --git a/lib/chef/resource/cron_d.rb b/lib/chef/resource/cron_d.rb index c60240acc2..f9fdca2294 100644 --- a/lib/chef/resource/cron_d.rb +++ b/lib/chef/resource/cron_d.rb @@ -17,6 +17,7 @@ require_relative "../resource" require_relative "helpers/cron_validations" +require_relative "helpers/cron" require "shellwords" unless defined?(Shellwords) require_relative "../dist" @@ -122,9 +123,10 @@ class Chef "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, } - property :weekday, [Integer, String], - description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, or `*`), where Sunday is both `0` and `7`.", - default: "*", callbacks: { + property :weekday, [Integer, String, Symbol], + description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, `monday-sunday`, or `*`), where Sunday is both `0` and `7`.", + default: "*", coerce: proc { |wday| Chef::ResourceHelpers::Cron.weekday_in_crontab(wday) }, + callbacks: { "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, } diff --git a/lib/chef/resource/helpers/cron.rb b/lib/chef/resource/helpers/cron.rb new file mode 100644 index 0000000000..dcf50e0f94 --- /dev/null +++ b/lib/chef/resource/helpers/cron.rb @@ -0,0 +1,19 @@ +class Chef + module ResourceHelpers + module Cron + WEEKDAYS = { sunday: '0', monday: '1', tuesday: '2', wednesday: '3', thursday: '4', friday: '5', saturday: '6', + sun: '0', mon: '1', tue: '2', wed: '3', thu: '4', fri: '5', sat: '6' + } + + # Convert weekday input value into crontab format that + # could be written in the crontab + # @return [Integer, String] A weekday formed as per the user inputs. + def weekday_in_crontab(wday) + weekday = wday.to_s.downcase + weekday_in_crontab = WEEKDAYS[weekday.to_sym] || wday + end + + extend self + end + end +end \ No newline at end of file diff --git a/lib/chef/resource/helpers/cron_validations.rb b/lib/chef/resource/helpers/cron_validations.rb index 8e62fa8095..c0da71acce 100644 --- a/lib/chef/resource/helpers/cron_validations.rb +++ b/lib/chef/resource/helpers/cron_validations.rb @@ -62,13 +62,16 @@ class Chef end end - # validate the provided day of the week is sun-sat, 0-7, or * + # validate the provided day of the week is sun-sat, sunday-saturday, 0-7, or * + # Added crontab param to check cron resource # @param spec the value to validate # @return [Boolean] valid or not? def validate_dow(spec) + spec = spec.to_s spec == "*" || validate_numeric(spec, 0, 7) || - %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase) + %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase) || + %w{sunday monday tuesday wednesday thursday friday saturday}.include?(String(spec).downcase) end # validate the day of the month is 1-31 diff --git a/spec/functional/resource/cron_spec.rb b/spec/functional/resource/cron_spec.rb index 66f630018e..3a26226115 100644 --- a/spec/functional/resource/cron_spec.rb +++ b/spec/functional/resource/cron_spec.rb @@ -80,6 +80,16 @@ describe Chef::Resource::Cron, :requires_root, :unix_only do 5.times { new_resource.run_action(:create) } cron_should_exists(new_resource.name, new_resource.command) end + + # Test cron for day of week + weekdays = { Mon: 1, tuesday: 2, '3': 3, 'thursday': 4, 'Fri': 5, 6 => 6 } + weekdays.each do|key, value| + it "should create crontab entry and set #{value} for mon as weekday" do + new_resource.weekday key + expect{ new_resource.run_action(:create) }.not_to raise_error + cron_should_exists(new_resource.name, new_resource.command) + end + end end describe "delete action" do diff --git a/spec/unit/provider/cron_spec.rb b/spec/unit/provider/cron_spec.rb index bebde66a8b..da77e6698c 100644 --- a/spec/unit/provider/cron_spec.rb +++ b/spec/unit/provider/cron_spec.rb @@ -322,7 +322,7 @@ describe Chef::Provider::Cron do expect(cron.hour).to eq("5") expect(cron.day).to eq("*") expect(cron.month).to eq("Jan") - expect(cron.weekday).to eq("Mon") + expect(cron.weekday).to eq("1") expect(cron.command).to eq("/bin/true param1 param2") end @@ -1040,48 +1040,6 @@ describe Chef::Provider::Cron do end end - describe "weekday_in_crontab" do - context "when weekday is symbol" do - it "should return weekday in crontab format" do - @new_resource.weekday :wednesday - expect(@provider.send(:weekday_in_crontab)).to eq("3") - end - - it "should raise an error with an unknown weekday" do - expect { @new_resource.weekday :caturday }.to raise_error(RangeError) - end - end - - context "when weekday is a number in a string" do - it "should return the string" do - @new_resource.weekday "3" - expect(@provider.send(:weekday_in_crontab)).to eq("3") - end - - it "should raise an error with an out of range number" do - expect { @new_resource.weekday "-1" }.to raise_error(RangeError) - end - end - - context "when weekday is string with the name of the week" do - it "should return the string" do - @new_resource.weekday "mon" - expect(@provider.send(:weekday_in_crontab)).to eq("mon") - end - end - - context "when weekday is an integer" do - it "should return the integer" do - @new_resource.weekday 1 - expect(@provider.send(:weekday_in_crontab)).to eq("1") - end - - it "should raise an error with an out of range integer" do - expect { @new_resource.weekday 45 }.to raise_error(RangeError) - end - end - end - describe "#env_var_str" do context "when no env vars are set" do it "returns an empty string" do diff --git a/spec/unit/resource/cron_spec.rb b/spec/unit/resource/cron_spec.rb index 4322d6c24b..c9dbef06c6 100644 --- a/spec/unit/resource/cron_spec.rb +++ b/spec/unit/resource/cron_spec.rb @@ -132,10 +132,10 @@ describe Chef::Resource::Cron do describe "weekday" do it "rejects any weekday over 7" do - expect { resource.weekday "8" }.to raise_error(RangeError) + expect { resource.weekday "8" }.to raise_error(Chef::Exceptions::ValidationFailed) end it "rejects any symbols which don't represent day of week" do - expect { resource.weekday :foo }.to raise_error(RangeError) + expect { resource.weekday :foo }.to raise_error(Chef::Exceptions::ValidationFailed) end end diff --git a/spec/unit/resource/helpers/cron_spec.rb b/spec/unit/resource/helpers/cron_spec.rb new file mode 100644 index 0000000000..758827e4ca --- /dev/null +++ b/spec/unit/resource/helpers/cron_spec.rb @@ -0,0 +1,31 @@ +require "spec_helper" +require "chef/resource/helpers/cron" + +describe Chef::ResourceHelpers::Cron do + + describe "#weekday_in_crontab" do + context "when weekday is symbol with full name as a day of week" do + it "should return weekday in crontab standard format" do + expect(Chef::ResourceHelpers::Cron.weekday_in_crontab(:wednesday)).to eq("3") + end + end + + context "when weekday is a number in a string" do + it "should return the string" do + expect(Chef::ResourceHelpers::Cron.weekday_in_crontab("3")).to eq("3") + end + end + + context "when weekday is string with the short name as a day of week" do + it "should return the number string in crontab standard format" do + expect(Chef::ResourceHelpers::Cron.weekday_in_crontab("mon")).to eq("1") + end + end + + context "when weekday is an integer" do + it "should return the integer" do + expect(Chef::ResourceHelpers::Cron.weekday_in_crontab(1)).to eq(1) + end + end + end +end \ No newline at end of file diff --git a/spec/unit/resource/helpers/cron_validations_spec.rb b/spec/unit/resource/helpers/cron_validations_spec.rb index 6b7d8f592c..9ec58e8b5f 100644 --- a/spec/unit/resource/helpers/cron_validations_spec.rb +++ b/spec/unit/resource/helpers/cron_validations_spec.rb @@ -34,8 +34,12 @@ describe Chef::ResourceHelpers::CronValidations do expect(Chef::ResourceHelpers::CronValidations.validate_dow(8)).to be false end + it "it accepts the string day with full name" do + expect(Chef::ResourceHelpers::CronValidations.validate_dow("monday")).to be true + end + it "returns false for an invalid string" do - expect(Chef::ResourceHelpers::CronValidations.validate_dow("monday")).to be false + expect(Chef::ResourceHelpers::CronValidations.validate_dow("funday")).to be false end end -- cgit v1.2.1 From 33d608179372fed46c1c5a88f214677748b8de8c Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Mon, 11 May 2020 13:19:08 +0530 Subject: Added freeze for WEEKDAYS constant. Fixed chefstyle spec failure. Made changes as per review comment. Signed-off-by: antima-gupta --- lib/chef/resource/helpers/cron.rb | 14 ++++++++------ spec/functional/resource/cron_spec.rb | 8 ++++---- spec/unit/resource/helpers/cron_spec.rb | 2 +- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/chef/resource/helpers/cron.rb b/lib/chef/resource/helpers/cron.rb index dcf50e0f94..78c3e09b38 100644 --- a/lib/chef/resource/helpers/cron.rb +++ b/lib/chef/resource/helpers/cron.rb @@ -1,19 +1,21 @@ class Chef module ResourceHelpers module Cron - WEEKDAYS = { sunday: '0', monday: '1', tuesday: '2', wednesday: '3', thursday: '4', friday: '5', saturday: '6', - sun: '0', mon: '1', tue: '2', wed: '3', thu: '4', fri: '5', sat: '6' - } + + WEEKDAYS = { + sunday: "0", monday: "1", tuesday: "2", wednesday: "3", thursday: "4", friday: "5", saturday: "6", + sun: "0", mon: "1", tue: "2", wed: "3", thu: "4", fri: "5", sat: "6" + }.freeze # Convert weekday input value into crontab format that # could be written in the crontab # @return [Integer, String] A weekday formed as per the user inputs. def weekday_in_crontab(wday) - weekday = wday.to_s.downcase - weekday_in_crontab = WEEKDAYS[weekday.to_sym] || wday + weekday = wday.to_s.downcase.to_sym + weekday_in_crontab = WEEKDAYS[weekday] || wday end extend self end end -end \ No newline at end of file +end diff --git a/spec/functional/resource/cron_spec.rb b/spec/functional/resource/cron_spec.rb index 3a26226115..ed4905b980 100644 --- a/spec/functional/resource/cron_spec.rb +++ b/spec/functional/resource/cron_spec.rb @@ -80,13 +80,13 @@ describe Chef::Resource::Cron, :requires_root, :unix_only do 5.times { new_resource.run_action(:create) } cron_should_exists(new_resource.name, new_resource.command) end - + # Test cron for day of week weekdays = { Mon: 1, tuesday: 2, '3': 3, 'thursday': 4, 'Fri': 5, 6 => 6 } - weekdays.each do|key, value| - it "should create crontab entry and set #{value} for mon as weekday" do + weekdays.each do |key, value| + it "should create crontab entry and set #{value} for #{key} as weekday" do new_resource.weekday key - expect{ new_resource.run_action(:create) }.not_to raise_error + expect { new_resource.run_action(:create) }.not_to raise_error cron_should_exists(new_resource.name, new_resource.command) end end diff --git a/spec/unit/resource/helpers/cron_spec.rb b/spec/unit/resource/helpers/cron_spec.rb index 758827e4ca..f1e3457a2c 100644 --- a/spec/unit/resource/helpers/cron_spec.rb +++ b/spec/unit/resource/helpers/cron_spec.rb @@ -28,4 +28,4 @@ describe Chef::ResourceHelpers::Cron do end end end -end \ No newline at end of file +end -- cgit v1.2.1 From d119c901aa3aa79d91f9ade440066e85b0d8867d Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Wed, 13 May 2020 13:46:08 +0530 Subject: Added cron_shared partial. Fixed provider cron spec failure. Moved cron and cron resource into the lib/chef/resource/cron directory. Signed-off-by: antima-gupta --- lib/chef/resource/cron.rb | 141 ----------------- lib/chef/resource/cron/_cron_shared.rb | 102 +++++++++++++ lib/chef/resource/cron/cron.rb | 50 ++++++ lib/chef/resource/cron/cron_d.rb | 186 +++++++++++++++++++++++ lib/chef/resource/cron_d.rb | 270 --------------------------------- lib/chef/resources.rb | 4 +- spec/unit/provider/cron_spec.rb | 15 +- 7 files changed, 349 insertions(+), 419 deletions(-) delete mode 100644 lib/chef/resource/cron.rb create mode 100644 lib/chef/resource/cron/_cron_shared.rb create mode 100644 lib/chef/resource/cron/cron.rb create mode 100644 lib/chef/resource/cron/cron_d.rb delete mode 100644 lib/chef/resource/cron_d.rb diff --git a/lib/chef/resource/cron.rb b/lib/chef/resource/cron.rb deleted file mode 100644 index 2915463c2f..0000000000 --- a/lib/chef/resource/cron.rb +++ /dev/null @@ -1,141 +0,0 @@ -# -# Author:: Bryan McLellan (btm@loftninjas.org) -# Author:: Tyler Cloke () -# Copyright:: Copyright 2009-2016, Bryan McLellan -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require_relative "../resource" -require_relative "helpers/cron_validations" -require_relative "helpers/cron" -require_relative "../provider/cron" # do not remove. we actually need this below - -class Chef - class Resource - class Cron < Chef::Resource - unified_mode true - provides :cron - - description "Use the **cron** resource to manage cron entries for time-based job scheduling. Properties for a schedule will default to * if not provided. The cron resource requires access to a crontab program, typically cron." - - state_attrs :minute, :hour, :day, :month, :weekday, :user - - default_action :create - allowed_actions :create, :delete - - def initialize(name, run_context = nil) - super - @month = "*" - @weekday = "*" - end - - property :minute, [Integer, String], - description: "The minute at which the cron entry should run (`0 - 59`).", - default: "*", callbacks: { - "should be a valid minute spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 59) }, - } - - property :hour, [Integer, String], - description: "The hour at which the cron entry is to run (`0 - 23`).", - default: "*", callbacks: { - "should be a valid hour spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 23) }, - } - - property :day, [Integer, String], - description: "The day of month at which the cron entry should run (`1 - 31`).", - default: "*", callbacks: { - "should be a valid day spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 1, 31) }, - } - - property :month, [Integer, String], - description: "The month in the year on which a cron entry is to run (`1 - 12`, `jan-dec`, or `*`).", - default: "*", callbacks: { - "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, - } - - property :weekday, [Integer, String, Symbol], - description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, `monday-sunday`, or `*`), where Sunday is both `0` and `7`.", - default: "*", coerce: proc { |wday| Chef::ResourceHelpers::Cron.weekday_in_crontab(wday) }, - callbacks: { - "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, - } - - property :time, Symbol, - description: "A time interval.", - equal_to: Chef::Provider::Cron::SPECIAL_TIME_VALUES - - property :mailto, String, - description: "Set the `MAILTO` environment variable." - - property :path, String, - description: "Set the `PATH` environment variable." - - property :home, String, - description: "Set the `HOME` environment variable." - - property :shell, String, - description: "Set the `SHELL` environment variable." - - property :command, String, - description: "The command to be run, or the path to a file that contains the command to be run.", - identity: true - - property :user, String, - description: "The name of the user that runs the command. If the user property is changed, the original user for the crontab program continues to run until that crontab program is deleted. This property is not applicable on the AIX platform.", - default: "root" - - property :environment, Hash, - description: "A Hash containing additional arbitrary environment variables under which the cron job will be run in the form of `({'ENV_VARIABLE' => 'VALUE'})`.", - default: lazy { {} } - - TIMEOUT_OPTS = %w{duration preserve-status foreground kill-after signal}.freeze - TIMEOUT_REGEX = /\A\S+/.freeze - - property :time_out, Hash, - description: "A Hash of timeouts in the form of `({'OPTION' => 'VALUE'})`. - Accepted valid options are: - `preserve-status` (BOOL, default: 'false'), - `foreground` (BOOL, default: 'false'), - `kill-after` (in seconds), - `signal` (a name like 'HUP' or a number)", - default: lazy { {} }, - introduced: "15.7", - coerce: proc { |h| - if h.is_a?(Hash) - invalid_keys = h.keys - TIMEOUT_OPTS - unless invalid_keys.empty? - error_msg = "Key of option time_out must be equal to one of: \"#{TIMEOUT_OPTS.join('", "')}\"! You passed \"#{invalid_keys.join(", ")}\"." - raise Chef::Exceptions::ValidationFailed, error_msg - end - unless h.values.all? { |x| x =~ TIMEOUT_REGEX } - error_msg = "Values of option time_out should be non-empty string without any leading whitespace." - raise Chef::Exceptions::ValidationFailed, error_msg - end - h - elsif h.is_a?(Integer) || h.is_a?(String) - { "duration" => h } - end - } - - private - - def integerize(integerish) - Integer(integerish) - rescue TypeError - 0 - end - end - end -end diff --git a/lib/chef/resource/cron/_cron_shared.rb b/lib/chef/resource/cron/_cron_shared.rb new file mode 100644 index 0000000000..06fc072528 --- /dev/null +++ b/lib/chef/resource/cron/_cron_shared.rb @@ -0,0 +1,102 @@ +unified_mode true + +WEEKDAYS = { + sunday: "0", monday: "1", tuesday: "2", wednesday: "3", thursday: "4", friday: "5", saturday: "6", + sun: "0", mon: "1", tue: "2", wed: "3", thu: "4", fri: "5", sat: "6" +}.freeze + +property :minute, [Integer, String], + description: "The minute at which the cron entry should run (`0 - 59`).", + default: "*", callbacks: { + "should be a valid minute spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 59) }, + } + +property :hour, [Integer, String], + description: "The hour at which the cron entry is to run (`0 - 23`).", + default: "*", callbacks: { + "should be a valid hour spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 23) }, + } + +property :day, [Integer, String], + description: "The day of month at which the cron entry should run (`1 - 31`).", + default: "*", callbacks: { + "should be a valid day spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 1, 31) }, + } + +property :month, [Integer, String], + description: "The month in the year on which a cron entry is to run (`1 - 12`, `jan-dec`, or `*`).", + default: "*", callbacks: { + "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, + } + +property :weekday, [Integer, String, Symbol], + description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, `monday-sunday`, or `*`), where Sunday is both `0` and `7`.", + default: "*", coerce: proc { |wday| weekday_in_crontab(wday) }, + callbacks: { + "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, + } + +property :shell, String, + description: "Set the `SHELL` environment variable." + +property :path, String, + description: "Set the `PATH` environment variable." + +property :home, String, + description: "Set the `HOME` environment variable." + +property :mailto, String, + description: "Set the `MAILTO` environment variable." + +property :command, String, + description: "The command to be run, or the path to a file that contains the command to be run.", + identity: true, + required: [:create] + +property :user, String, + description: "The name of the user that runs the command.", + default: "root" + +property :environment, Hash, + description: "A Hash containing additional arbitrary environment variables under which the cron job will be run in the form of `({'ENV_VARIABLE' => 'VALUE'})`.", + default: lazy { {} } + + +TIMEOUT_OPTS = %w{duration preserve-status foreground kill-after signal}.freeze +TIMEOUT_REGEX = /\A\S+/.freeze + +property :time_out, Hash, + description: "A Hash of timeouts in the form of `({'OPTION' => 'VALUE'})`. + Accepted valid options are: + `preserve-status` (BOOL, default: 'false'), + `foreground` (BOOL, default: 'false'), + `kill-after` (in seconds), + `signal` (a name like 'HUP' or a number)", + default: lazy { {} }, + introduced: "15.7", + coerce: proc { |h| + if h.is_a?(Hash) + invalid_keys = h.keys - TIMEOUT_OPTS + unless invalid_keys.empty? + error_msg = "Key of option time_out must be equal to one of: \"#{TIMEOUT_OPTS.join('", "')}\"! You passed \"#{invalid_keys.join(", ")}\"." + raise Chef::Exceptions::ValidationFailed, error_msg + end + unless h.values.all? { |x| x =~ TIMEOUT_REGEX } + error_msg = "Values of option time_out should be non-empty string without any leading whitespaces." + raise Chef::Exceptions::ValidationFailed, error_msg + end + h + elsif h.is_a?(Integer) || h.is_a?(String) + { "duration" => h } + end + } + + +private +# Convert weekday input value into crontab format that +# could be written in the crontab +# @return [Integer, String] A weekday formed as per the user inputs. +def weekday_in_crontab(wday) + weekday = wday.to_s.downcase.to_sym + weekday_in_crontab = WEEKDAYS[weekday] || wday +end \ No newline at end of file diff --git a/lib/chef/resource/cron/cron.rb b/lib/chef/resource/cron/cron.rb new file mode 100644 index 0000000000..aeeef5624d --- /dev/null +++ b/lib/chef/resource/cron/cron.rb @@ -0,0 +1,50 @@ +# +# Author:: Bryan McLellan (btm@loftninjas.org) +# Author:: Tyler Cloke () +# Copyright:: Copyright 2009-2016, Bryan McLellan +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../../resource" +require_relative "../helpers/cron_validations" +require_relative "../../provider/cron" # do not remove. we actually need this below + +class Chef + class Resource + class Cron < Chef::Resource + use "cron_shared" + + provides :cron + + description "Use the **cron** resource to manage cron entries for time-based job scheduling. Properties for a schedule will default to * if not provided. The cron resource requires access to a crontab program, typically cron." + + state_attrs :minute, :hour, :day, :month, :weekday, :user + + default_action :create + allowed_actions :create, :delete + + def initialize(name, run_context = nil) + super + @month = "*" + @weekday = "*" + end + + property :time, Symbol, + description: "A time interval.", + equal_to: Chef::Provider::Cron::SPECIAL_TIME_VALUES + + end + end +end diff --git a/lib/chef/resource/cron/cron_d.rb b/lib/chef/resource/cron/cron_d.rb new file mode 100644 index 0000000000..39a811fdf8 --- /dev/null +++ b/lib/chef/resource/cron/cron_d.rb @@ -0,0 +1,186 @@ +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../../resource" +require_relative "../helpers/cron_validations" +require "shellwords" unless defined?(Shellwords) +require_relative "../../dist" + +class Chef + class Resource + class CronD < Chef::Resource + use "cron_shared" + + provides :cron_d + + introduced "14.4" + description "Use the **cron_d** resource to manage cron job files in the /etc/cron.d directory. This is similar to the 'cron' resource, but it does not use the monolithic /etc/crontab file." + examples <<~DOC + **Run a program on the fifth hour of the day** + + ```ruby + cron_d 'noop' do + hour '5' + minute '0' + command '/bin/true' + end + ``` + + **Run an entry if a folder exists** + + ```ruby + cron_d 'ganglia_tomcat_thread_max' do + command "/usr/bin/gmetric + -n 'tomcat threads max' + -t uint32 + -v '/usr/local/bin/tomcat-stat + --thread-max'" + only_if { ::File.exist?('/home/jboss') } + end + ``` + + **Run an entry every Saturday, 8:00 AM** + + ```ruby + cron_d 'name_of_cron_entry' do + minute '0' + hour '8' + weekday '6' + mailto 'admin@example.com' + command "/bin/true" + action :create + end + ``` + + **Run an entry at 8:00 PM, every weekday (Monday through Friday), but only in November** + + ```ruby + cron_d 'name_of_cron_entry' do + minute '0' + hour '20' + day '*' + month '11' + weekday '1-5' + command "/bin/true" + action :create + end + ``` + + **Remove a cron job by name**: + + ```ruby + cron_d 'job_to_remove' do + action :delete + end + ``` + DOC + + property :cron_name, String, + description: "An optional property to set the cron name if it differs from the resource block's name.", + name_property: true + + property :cookbook, String, desired_state: false, skip_docs: true + + property :predefined_value, String, + description: "Schedule your cron job with one of the special predefined value instead of ** * pattern.", + equal_to: %w{ @reboot @yearly @annually @monthly @weekly @daily @midnight @hourly } + + property :comment, String, + description: "A comment to place in the cron.d file." + + property :mode, [String, Integer], + description: "The octal mode of the generated crontab file.", + default: "0600" + + property :random_delay, Integer, + description: "Set the `RANDOM_DELAY` environment variable in the cron.d file." + + # warn if someone passes the deprecated cookbook property + def after_created + raise ArgumentError, "The 'cookbook' property for the cron_d resource is no longer supported now that it ships as a core resource." if cookbook + end + + action :create do + description "Add a cron definition file to /etc/cron.d." + + create_template(:create) + end + + action :create_if_missing do + description "Add a cron definition file to /etc/cron.d, but do not update an existing file." + + create_template(:create_if_missing) + end + + action :delete do + description "Remove a cron definition file from /etc/cron.d if it exists." + + # cleanup the legacy named job if it exists + file "legacy named cron.d file" do + path "/etc/cron.d/#{new_resource.cron_name}" + action :delete + end + + file "/etc/cron.d/#{sanitized_name}" do + action :delete + end + end + + action_class do + # @return [String] cron_name property with . replaced with - + def sanitized_name + new_resource.cron_name.tr(".", "-") + end + + def create_template(create_action) + # cleanup the legacy named job if it exists + file "#{new_resource.cron_name} legacy named cron.d file" do + path "/etc/cron.d/#{new_resource.cron_name}" + action :delete + only_if { new_resource.cron_name != sanitized_name } + end + + # @todo this is Chef 12 era cleanup. Someday we should remove it all + template "/etc/cron.d/#{sanitized_name}" do + source ::File.expand_path("../../support/cron.d.erb", __FILE__) + local true + mode new_resource.mode + variables( + name: sanitized_name, + predefined_value: new_resource.predefined_value, + minute: new_resource.minute, + hour: new_resource.hour, + day: new_resource.day, + month: new_resource.month, + weekday: new_resource.weekday, + command: new_resource.command, + user: new_resource.user, + mailto: new_resource.mailto, + path: new_resource.path, + home: new_resource.home, + shell: new_resource.shell, + comment: new_resource.comment, + random_delay: new_resource.random_delay, + environment: new_resource.environment + ) + action create_action + end + end + end + end + end +end diff --git a/lib/chef/resource/cron_d.rb b/lib/chef/resource/cron_d.rb deleted file mode 100644 index f9fdca2294..0000000000 --- a/lib/chef/resource/cron_d.rb +++ /dev/null @@ -1,270 +0,0 @@ -# -# Copyright:: Copyright (c) Chef Software Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require_relative "../resource" -require_relative "helpers/cron_validations" -require_relative "helpers/cron" -require "shellwords" unless defined?(Shellwords) -require_relative "../dist" - -class Chef - class Resource - class CronD < Chef::Resource - unified_mode true - provides :cron_d - - introduced "14.4" - description "Use the **cron_d** resource to manage cron job files in the /etc/cron.d directory. This is similar to the 'cron' resource, but it does not use the monolithic /etc/crontab file." - examples <<~DOC - **Run a program on the fifth hour of the day** - - ```ruby - cron_d 'noop' do - hour '5' - minute '0' - command '/bin/true' - end - ``` - - **Run an entry if a folder exists** - - ```ruby - cron_d 'ganglia_tomcat_thread_max' do - command "/usr/bin/gmetric - -n 'tomcat threads max' - -t uint32 - -v '/usr/local/bin/tomcat-stat - --thread-max'" - only_if { ::File.exist?('/home/jboss') } - end - ``` - - **Run an entry every Saturday, 8:00 AM** - - ```ruby - cron_d 'name_of_cron_entry' do - minute '0' - hour '8' - weekday '6' - mailto 'admin@example.com' - command "/bin/true" - action :create - end - ``` - - **Run an entry at 8:00 PM, every weekday (Monday through Friday), but only in November** - - ```ruby - cron_d 'name_of_cron_entry' do - minute '0' - hour '20' - day '*' - month '11' - weekday '1-5' - command "/bin/true" - action :create - end - ``` - - **Remove a cron job by name**: - - ```ruby - cron_d 'job_to_remove' do - action :delete - end - ``` - DOC - - property :cron_name, String, - description: "An optional property to set the cron name if it differs from the resource block's name.", - name_property: true - - property :cookbook, String, desired_state: false, skip_docs: true - - property :predefined_value, String, - description: "Schedule your cron job with one of the special predefined value instead of ** * pattern.", - equal_to: %w{ @reboot @yearly @annually @monthly @weekly @daily @midnight @hourly } - - property :minute, [Integer, String], - description: "The minute at which the cron entry should run (`0 - 59`).", - default: "*", callbacks: { - "should be a valid minute spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 59) }, - } - - property :hour, [Integer, String], - description: "The hour at which the cron entry is to run (`0 - 23`).", - default: "*", callbacks: { - "should be a valid hour spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 0, 23) }, - } - - property :day, [Integer, String], - description: "The day of month at which the cron entry should run (`1 - 31`).", - default: "*", callbacks: { - "should be a valid day spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_numeric(spec, 1, 31) }, - } - - property :month, [Integer, String], - description: "The month in the year on which a cron entry is to run (`1 - 12`, `jan-dec`, or `*`).", - default: "*", callbacks: { - "should be a valid month spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_month(spec) }, - } - - property :weekday, [Integer, String, Symbol], - description: "The day of the week on which this entry is to run (`0-7`, `mon-sun`, `monday-sunday`, or `*`), where Sunday is both `0` and `7`.", - default: "*", coerce: proc { |wday| Chef::ResourceHelpers::Cron.weekday_in_crontab(wday) }, - callbacks: { - "should be a valid weekday spec" => ->(spec) { Chef::ResourceHelpers::CronValidations.validate_dow(spec) }, - } - - property :command, String, - description: "The command to run.", - required: [:create] - - property :user, String, - description: "The name of the user that runs the command.", - default: "root" - - property :mailto, String, - description: "Set the `MAILTO` environment variable in the cron.d file." - - property :path, String, - description: "Set the `PATH` environment variable in the cron.d file." - - property :home, String, - description: "Set the `HOME` environment variable in the cron.d file." - - property :shell, String, - description: "Set the `SHELL` environment variable in the cron.d file." - - property :comment, String, - description: "A comment to place in the cron.d file." - - property :environment, Hash, - description: "A Hash containing additional arbitrary environment variables under which the cron job will be run in the form of `({'ENV_VARIABLE' => 'VALUE'})`.", - default: lazy { {} } - - TIMEOUT_OPTS = %w{duration preserve-status foreground kill-after signal}.freeze - TIMEOUT_REGEX = /\A\S+/.freeze - - property :time_out, Hash, - description: "A Hash of timeouts in the form of `({'OPTION' => 'VALUE'})`. - Accepted valid options are: - `preserve-status` (BOOL, default: 'false'), - `foreground` (BOOL, default: 'false'), - `kill-after` (in seconds), - `signal` (a name like 'HUP' or a number)", - default: lazy { {} }, - introduced: "15.7", - coerce: proc { |h| - if h.is_a?(Hash) - invalid_keys = h.keys - TIMEOUT_OPTS - unless invalid_keys.empty? - error_msg = "Key of option time_out must be equal to one of: \"#{TIMEOUT_OPTS.join('", "')}\"! You passed \"#{invalid_keys.join(", ")}\"." - raise Chef::Exceptions::ValidationFailed, error_msg - end - unless h.values.all? { |x| x =~ TIMEOUT_REGEX } - error_msg = "Values of option time_out should be non-empty string without any leading whitespace." - raise Chef::Exceptions::ValidationFailed, error_msg - end - h - elsif h.is_a?(Integer) || h.is_a?(String) - { "duration" => h } - end - } - - property :mode, [String, Integer], - description: "The octal mode of the generated crontab file.", - default: "0600" - - property :random_delay, Integer, - description: "Set the `RANDOM_DELAY` environment variable in the cron.d file." - - # warn if someone passes the deprecated cookbook property - def after_created - raise ArgumentError, "The 'cookbook' property for the cron_d resource is no longer supported now that it ships as a core resource." if cookbook - end - - action :create do - description "Add a cron definition file to /etc/cron.d." - - create_template(:create) - end - - action :create_if_missing do - description "Add a cron definition file to /etc/cron.d, but do not update an existing file." - - create_template(:create_if_missing) - end - - action :delete do - description "Remove a cron definition file from /etc/cron.d if it exists." - - # cleanup the legacy named job if it exists - file "legacy named cron.d file" do - path "/etc/cron.d/#{new_resource.cron_name}" - action :delete - end - - file "/etc/cron.d/#{sanitized_name}" do - action :delete - end - end - - action_class do - # @return [String] cron_name property with . replaced with - - def sanitized_name - new_resource.cron_name.tr(".", "-") - end - - def create_template(create_action) - # cleanup the legacy named job if it exists - file "#{new_resource.cron_name} legacy named cron.d file" do - path "/etc/cron.d/#{new_resource.cron_name}" - action :delete - only_if { new_resource.cron_name != sanitized_name } - end - - # @todo this is Chef 12 era cleanup. Someday we should remove it all - template "/etc/cron.d/#{sanitized_name}" do - source ::File.expand_path("../support/cron.d.erb", __FILE__) - local true - mode new_resource.mode - variables( - name: sanitized_name, - predefined_value: new_resource.predefined_value, - minute: new_resource.minute, - hour: new_resource.hour, - day: new_resource.day, - month: new_resource.month, - weekday: new_resource.weekday, - command: new_resource.command, - user: new_resource.user, - mailto: new_resource.mailto, - path: new_resource.path, - home: new_resource.home, - shell: new_resource.shell, - comment: new_resource.comment, - random_delay: new_resource.random_delay, - environment: new_resource.environment - ) - action create_action - end - end - end - end - end -end diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb index c786b2d78e..459130b88a 100644 --- a/lib/chef/resources.rb +++ b/lib/chef/resources.rb @@ -38,9 +38,9 @@ require_relative "resource/chocolatey_config" require_relative "resource/chocolatey_feature" require_relative "resource/chocolatey_package" require_relative "resource/chocolatey_source" -require_relative "resource/cron" +require_relative "resource/cron/cron" require_relative "resource/cron_access" -require_relative "resource/cron_d" +require_relative "resource/cron/cron_d" require_relative "resource/csh" require_relative "resource/directory" require_relative "resource/dmg_package" diff --git a/spec/unit/provider/cron_spec.rb b/spec/unit/provider/cron_spec.rb index da77e6698c..85060c07b7 100644 --- a/spec/unit/provider/cron_spec.rb +++ b/spec/unit/provider/cron_spec.rb @@ -15,7 +15,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # - +require 'pry' require "spec_helper" describe Chef::Provider::Cron do @@ -338,6 +338,7 @@ describe Chef::Provider::Cron do 0 2 * * * /some/other/command # Chef Name: cronhole some stuff + * * * * * /bin/true CRONTAB cron = @provider.load_current_resource expect(@provider.cron_exists).to eq(true) @@ -347,7 +348,7 @@ describe Chef::Provider::Cron do expect(cron.month).to eq("*") expect(cron.weekday).to eq("*") expect(cron.time).to eq(nil) - expect(cron.command).to eq(nil) + expect(cron.command).to eq("/bin/true") end it "should not pick up a commented out crontab line" do @@ -355,6 +356,7 @@ describe Chef::Provider::Cron do 0 2 * * * /some/other/command # Chef Name: cronhole some stuff + * * * * * /bin/true #* 5 * 1 * /bin/true param1 param2 CRONTAB cron = @provider.load_current_resource @@ -365,7 +367,7 @@ describe Chef::Provider::Cron do expect(cron.month).to eq("*") expect(cron.weekday).to eq("*") expect(cron.time).to eq(nil) - expect(cron.command).to eq(nil) + expect(cron.command).to eq("/bin/true") end it "should not pick up a later crontab entry" do @@ -373,6 +375,7 @@ describe Chef::Provider::Cron do 0 2 * * * /some/other/command # Chef Name: cronhole some stuff + * * * * * /bin/true #* 5 * 1 * /bin/true param1 param2 # Chef Name: something else 2 * 1 * * /bin/false @@ -387,7 +390,7 @@ describe Chef::Provider::Cron do expect(cron.month).to eq("*") expect(cron.weekday).to eq("*") expect(cron.time).to eq(nil) - expect(cron.command).to eq(nil) + expect(cron.command).to eq("/bin/true") end end end @@ -1154,8 +1157,8 @@ describe Chef::Provider::Cron do context "Without command, passed" do context "as nil" do it "returns an empty string with a next line" do - @new_resource.command nil - expect(@provider.send(:cmd_str)).to eq(" \n") + @new_resource.command "bin/true" + expect(@provider.send(:cmd_str)).to eq(" bin/true\n") end end context "as an empty string" do -- cgit v1.2.1 From 22d38547a8db55c1a579c1e939253ad526a158cd Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Wed, 13 May 2020 13:55:37 +0530 Subject: removed cron helper files Signed-off-by: antima-gupta --- lib/chef/resource/helpers/cron.rb | 21 --------------------- spec/unit/resource/helpers/cron_spec.rb | 31 ------------------------------- 2 files changed, 52 deletions(-) delete mode 100644 lib/chef/resource/helpers/cron.rb delete mode 100644 spec/unit/resource/helpers/cron_spec.rb diff --git a/lib/chef/resource/helpers/cron.rb b/lib/chef/resource/helpers/cron.rb deleted file mode 100644 index 78c3e09b38..0000000000 --- a/lib/chef/resource/helpers/cron.rb +++ /dev/null @@ -1,21 +0,0 @@ -class Chef - module ResourceHelpers - module Cron - - WEEKDAYS = { - sunday: "0", monday: "1", tuesday: "2", wednesday: "3", thursday: "4", friday: "5", saturday: "6", - sun: "0", mon: "1", tue: "2", wed: "3", thu: "4", fri: "5", sat: "6" - }.freeze - - # Convert weekday input value into crontab format that - # could be written in the crontab - # @return [Integer, String] A weekday formed as per the user inputs. - def weekday_in_crontab(wday) - weekday = wday.to_s.downcase.to_sym - weekday_in_crontab = WEEKDAYS[weekday] || wday - end - - extend self - end - end -end diff --git a/spec/unit/resource/helpers/cron_spec.rb b/spec/unit/resource/helpers/cron_spec.rb deleted file mode 100644 index f1e3457a2c..0000000000 --- a/spec/unit/resource/helpers/cron_spec.rb +++ /dev/null @@ -1,31 +0,0 @@ -require "spec_helper" -require "chef/resource/helpers/cron" - -describe Chef::ResourceHelpers::Cron do - - describe "#weekday_in_crontab" do - context "when weekday is symbol with full name as a day of week" do - it "should return weekday in crontab standard format" do - expect(Chef::ResourceHelpers::Cron.weekday_in_crontab(:wednesday)).to eq("3") - end - end - - context "when weekday is a number in a string" do - it "should return the string" do - expect(Chef::ResourceHelpers::Cron.weekday_in_crontab("3")).to eq("3") - end - end - - context "when weekday is string with the short name as a day of week" do - it "should return the number string in crontab standard format" do - expect(Chef::ResourceHelpers::Cron.weekday_in_crontab("mon")).to eq("1") - end - end - - context "when weekday is an integer" do - it "should return the integer" do - expect(Chef::ResourceHelpers::Cron.weekday_in_crontab(1)).to eq(1) - end - end - end -end -- cgit v1.2.1 From be3fd874f98c6f973a41a97ccd061b6493982833 Mon Sep 17 00:00:00 2001 From: antima-gupta Date: Thu, 14 May 2020 16:06:31 +0530 Subject: Moved all the constants together. Removed unnecessary assignment for weekday_in_crontab. Removed pry. Removed converted to a string function from weekday validation. Added unified_mode in cron and cron_d resources. Signed-off-by: antima-gupta --- lib/chef/resource/cron/_cron_shared.rb | 8 ++++---- lib/chef/resource/cron/cron.rb | 8 ++------ lib/chef/resource/cron/cron_d.rb | 2 ++ lib/chef/resource/helpers/cron_validations.rb | 4 ++-- spec/unit/provider/cron_spec.rb | 1 - 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/chef/resource/cron/_cron_shared.rb b/lib/chef/resource/cron/_cron_shared.rb index 06fc072528..4e2eb5428a 100644 --- a/lib/chef/resource/cron/_cron_shared.rb +++ b/lib/chef/resource/cron/_cron_shared.rb @@ -1,5 +1,7 @@ unified_mode true +TIMEOUT_OPTS = %w{duration preserve-status foreground kill-after signal}.freeze +TIMEOUT_REGEX = /\A\S+/.freeze WEEKDAYS = { sunday: "0", monday: "1", tuesday: "2", wednesday: "3", thursday: "4", friday: "5", saturday: "6", sun: "0", mon: "1", tue: "2", wed: "3", thu: "4", fri: "5", sat: "6" @@ -62,8 +64,6 @@ property :environment, Hash, default: lazy { {} } -TIMEOUT_OPTS = %w{duration preserve-status foreground kill-after signal}.freeze -TIMEOUT_REGEX = /\A\S+/.freeze property :time_out, Hash, description: "A Hash of timeouts in the form of `({'OPTION' => 'VALUE'})`. @@ -82,7 +82,7 @@ property :time_out, Hash, raise Chef::Exceptions::ValidationFailed, error_msg end unless h.values.all? { |x| x =~ TIMEOUT_REGEX } - error_msg = "Values of option time_out should be non-empty string without any leading whitespaces." + error_msg = "Values of option time_out should be non-empty strings without any leading whitespace." raise Chef::Exceptions::ValidationFailed, error_msg end h @@ -98,5 +98,5 @@ private # @return [Integer, String] A weekday formed as per the user inputs. def weekday_in_crontab(wday) weekday = wday.to_s.downcase.to_sym - weekday_in_crontab = WEEKDAYS[weekday] || wday + WEEKDAYS[weekday] || wday end \ No newline at end of file diff --git a/lib/chef/resource/cron/cron.rb b/lib/chef/resource/cron/cron.rb index aeeef5624d..31d6efcfde 100644 --- a/lib/chef/resource/cron/cron.rb +++ b/lib/chef/resource/cron/cron.rb @@ -24,6 +24,8 @@ require_relative "../../provider/cron" # do not remove. we actually need this be class Chef class Resource class Cron < Chef::Resource + unified_mode true + use "cron_shared" provides :cron @@ -35,12 +37,6 @@ class Chef default_action :create allowed_actions :create, :delete - def initialize(name, run_context = nil) - super - @month = "*" - @weekday = "*" - end - property :time, Symbol, description: "A time interval.", equal_to: Chef::Provider::Cron::SPECIAL_TIME_VALUES diff --git a/lib/chef/resource/cron/cron_d.rb b/lib/chef/resource/cron/cron_d.rb index 39a811fdf8..3a913b22c3 100644 --- a/lib/chef/resource/cron/cron_d.rb +++ b/lib/chef/resource/cron/cron_d.rb @@ -23,6 +23,8 @@ require_relative "../../dist" class Chef class Resource class CronD < Chef::Resource + unified_mode true + use "cron_shared" provides :cron_d diff --git a/lib/chef/resource/helpers/cron_validations.rb b/lib/chef/resource/helpers/cron_validations.rb index c0da71acce..b801b7bf11 100644 --- a/lib/chef/resource/helpers/cron_validations.rb +++ b/lib/chef/resource/helpers/cron_validations.rb @@ -70,8 +70,8 @@ class Chef spec = spec.to_s spec == "*" || validate_numeric(spec, 0, 7) || - %w{sun mon tue wed thu fri sat}.include?(String(spec).downcase) || - %w{sunday monday tuesday wednesday thursday friday saturday}.include?(String(spec).downcase) + %w{sun mon tue wed thu fri sat}.include?(spec.downcase) || + %w{sunday monday tuesday wednesday thursday friday saturday}.include?(spec.downcase) end # validate the day of the month is 1-31 diff --git a/spec/unit/provider/cron_spec.rb b/spec/unit/provider/cron_spec.rb index 85060c07b7..76f170312e 100644 --- a/spec/unit/provider/cron_spec.rb +++ b/spec/unit/provider/cron_spec.rb @@ -15,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # -require 'pry' require "spec_helper" describe Chef::Provider::Cron do -- cgit v1.2.1 From 6a28f8724d33eb5a02ed475dd883e697b0561a36 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Mon, 15 Jun 2020 22:16:06 -0700 Subject: Chefstyle fixes Signed-off-by: Tim Smith --- lib/chef/resource/cron/_cron_shared.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/chef/resource/cron/_cron_shared.rb b/lib/chef/resource/cron/_cron_shared.rb index 4e2eb5428a..b2e200d27a 100644 --- a/lib/chef/resource/cron/_cron_shared.rb +++ b/lib/chef/resource/cron/_cron_shared.rb @@ -63,8 +63,6 @@ property :environment, Hash, description: "A Hash containing additional arbitrary environment variables under which the cron job will be run in the form of `({'ENV_VARIABLE' => 'VALUE'})`.", default: lazy { {} } - - property :time_out, Hash, description: "A Hash of timeouts in the form of `({'OPTION' => 'VALUE'})`. Accepted valid options are: @@ -91,7 +89,6 @@ property :time_out, Hash, end } - private # Convert weekday input value into crontab format that # could be written in the crontab -- cgit v1.2.1