summaryrefslogtreecommitdiff
path: root/lib/chef
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-03-23 11:35:27 -0700
committerTim Smith <tsmith84@gmail.com>2020-03-23 11:36:46 -0700
commite7e8781a5092cf67ef3900aa0b31e64971838a1b (patch)
tree5917a96733e8148d1bfd027240737d2fc29cde4e /lib/chef
parentb3509511cde8822693e86c98f9834e0508630f20 (diff)
downloadchef-e7e8781a5092cf67ef3900aa0b31e64971838a1b.tar.gz
Move cron validatioin methods into a mixin
This will allow us to use them in the other cron resource eventually, but definitely in the chef_client_cron resource today. Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef')
-rw-r--r--lib/chef/mixin/cron_validations.rb90
-rw-r--r--lib/chef/resource/cron_d.rb78
2 files changed, 96 insertions, 72 deletions
diff --git a/lib/chef/mixin/cron_validations.rb b/lib/chef/mixin/cron_validations.rb
new file mode 100644
index 0000000000..e8bb279a5a
--- /dev/null
+++ b/lib/chef/mixin/cron_validations.rb
@@ -0,0 +1,90 @@
+#
+# Copyright:: Copyright 2020, 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.
+#
+
+class Chef
+ module Mixin
+ # a collection of methods for validating cron times. Used in the various cron-like resources
+ module CronValidations
+ # validate a provided value is between two other provided values
+ # we also allow * as a valid input
+ # @param spec the value to validate
+ # @param min the lowest value allowed
+ # @param max the highest value allowed
+ # @return [Boolean] valid or not?
+ def self.validate_numeric(spec, min, max)
+ return true if spec == "*"
+
+ # binding.pry
+ if spec.respond_to? :to_int
+ return false unless spec >= min && spec <= max
+
+ return true
+ end
+
+ # Lists of invidual values, ranges, and step values all share the validity range for type
+ spec.split(%r{\/|-|,}).each do |x|
+ next if x == "*"
+ return false unless x =~ /^\d+$/
+
+ x = x.to_i
+ return false unless x >= min && x <= max
+ end
+ true
+ end
+
+ # validate the provided month value to be jan - dec, 1 - 12, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_month(spec)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ validate_numeric(spec, 1, 12)
+ elsif spec.respond_to? :to_str
+ return true if spec == "*"
+ # Named abbreviations are permitted but not as part of a range or with stepping
+ return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase
+
+ # 1-12 are legal for months
+ validate_numeric(spec, 1, 12)
+ else
+ false
+ end
+ end
+
+ # validate the provided day of the week is sun-sat, 0-7, or *
+ # @param spec the value to validate
+ # @return [Boolean] valid or not?
+ def self.validate_dow(spec)
+ return true if spec == "*"
+
+ if spec.respond_to? :to_int
+ validate_numeric(spec, 0, 7)
+ elsif spec.respond_to? :to_str
+ return true if spec == "*"
+ # Named abbreviations are permitted but not as part of a range or with stepping
+ return true if %w{sun mon tue wed thu fri sat}.include? spec.downcase
+
+ # 0-7 are legal for days of week
+ validate_numeric(spec, 0, 7)
+ else
+ false
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resource/cron_d.rb b/lib/chef/resource/cron_d.rb
index befe951a35..98e962ac66 100644
--- a/lib/chef/resource/cron_d.rb
+++ b/lib/chef/resource/cron_d.rb
@@ -16,6 +16,7 @@
#
require_relative "../resource"
+require_relative "../mixin/cron_validations"
require "shellwords" unless defined?(Shellwords)
require_relative "../dist"
@@ -73,73 +74,6 @@ class Chef
```
DOC
- # validate a provided value is between two other provided values
- # we also allow * as a valid input
- # @param spec the value to validate
- # @param min the lowest value allowed
- # @param max the highest value allowed
- # @return [Boolean] valid or not?
- def self.validate_numeric(spec, min, max)
- return true if spec == "*"
-
- # binding.pry
- if spec.respond_to? :to_int
- return false unless spec >= min && spec <= max
-
- return true
- end
-
- # Lists of invidual values, ranges, and step values all share the validity range for type
- spec.split(%r{\/|-|,}).each do |x|
- next if x == "*"
- return false unless x =~ /^\d+$/
-
- x = x.to_i
- return false unless x >= min && x <= max
- end
- true
- end
-
- # validate the provided month value to be jan - dec, 1 - 12, or *
- # @param spec the value to validate
- # @return [Boolean] valid or not?
- def self.validate_month(spec)
- return true if spec == "*"
-
- if spec.respond_to? :to_int
- validate_numeric(spec, 1, 12)
- elsif spec.respond_to? :to_str
- return true if spec == "*"
- # Named abbreviations are permitted but not as part of a range or with stepping
- return true if %w{jan feb mar apr may jun jul aug sep oct nov dec}.include? spec.downcase
-
- # 1-12 are legal for months
- validate_numeric(spec, 1, 12)
- else
- false
- end
- end
-
- # validate the provided day of the week is sun-sat, 0-7, or *
- # @param spec the value to validate
- # @return [Boolean] valid or not?
- def self.validate_dow(spec)
- return true if spec == "*"
-
- if spec.respond_to? :to_int
- validate_numeric(spec, 0, 7)
- elsif spec.respond_to? :to_str
- return true if spec == "*"
- # Named abbreviations are permitted but not as part of a range or with stepping
- return true if %w{sun mon tue wed thu fri sat}.include? spec.downcase
-
- # 0-7 are legal for days of week
- validate_numeric(spec, 0, 7)
- else
- false
- end
- end
-
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
@@ -153,31 +87,31 @@ class Chef
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) { validate_numeric(spec, 0, 59) },
+ "should be a valid minute spec" => ->(spec) { Chef::Mixin::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) { validate_numeric(spec, 0, 23) },
+ "should be a valid hour spec" => ->(spec) { Chef::Mixin::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) { validate_numeric(spec, 1, 31) },
+ "should be a valid day spec" => ->(spec) { Chef::Mixin::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) { validate_month(spec) },
+ "should be a valid month spec" => ->(spec) { Chef::Mixin::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: {
- "should be a valid weekday spec" => ->(spec) { validate_dow(spec) },
+ "should be a valid weekday spec" => ->(spec) { Chef::Mixin::CronValidations.validate_dow(spec) },
}
property :command, String,