summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantima-gupta <agupta@msystechnologies.com>2020-05-13 13:46:08 +0530
committerantima-gupta <agupta@msystechnologies.com>2020-06-10 10:51:40 +0530
commitd119c901aa3aa79d91f9ade440066e85b0d8867d (patch)
tree8a2fc0439c97c0b67314547961b4465e3980d51d
parent33d608179372fed46c1c5a88f214677748b8de8c (diff)
downloadchef-d119c901aa3aa79d91f9ade440066e85b0d8867d.tar.gz
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 <agupta@msystechnologies.com>
-rw-r--r--lib/chef/resource/cron.rb141
-rw-r--r--lib/chef/resource/cron/_cron_shared.rb102
-rw-r--r--lib/chef/resource/cron/cron.rb50
-rw-r--r--lib/chef/resource/cron/cron_d.rb (renamed from lib/chef/resource/cron_d.rb)96
-rw-r--r--lib/chef/resources.rb4
-rw-r--r--spec/unit/provider/cron_spec.rb15
6 files changed, 169 insertions, 239 deletions
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 (<tyler@chef.io>)
-# 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 (<tyler@chef.io>)
+# 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_d.rb b/lib/chef/resource/cron/cron_d.rb
index f9fdca2294..39a811fdf8 100644
--- a/lib/chef/resource/cron_d.rb
+++ b/lib/chef/resource/cron/cron_d.rb
@@ -15,16 +15,16 @@
# limitations under the License.
#
-require_relative "../resource"
-require_relative "helpers/cron_validations"
-require_relative "helpers/cron"
+require_relative "../../resource"
+require_relative "../helpers/cron_validations"
require "shellwords" unless defined?(Shellwords)
-require_relative "../dist"
+require_relative "../../dist"
class Chef
class Resource
class CronD < Chef::Resource
- unified_mode true
+ use "cron_shared"
+
provides :cron_d
introduced "14.4"
@@ -99,93 +99,9 @@ class Chef
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"
@@ -240,7 +156,7 @@ class Chef
# @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__)
+ source ::File.expand_path("../../support/cron.d.erb", __FILE__)
local true
mode new_resource.mode
variables(
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