summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--kitchen-tests/cookbooks/end_to_end/recipes/default.rb2
-rw-r--r--lib/chef/resource/timezone.rb95
-rw-r--r--lib/chef/resources.rb1
-rw-r--r--spec/unit/resource/timezone.rb39
4 files changed, 137 insertions, 0 deletions
diff --git a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
index 3985cefd5a..2779ed92fc 100644
--- a/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
+++ b/kitchen-tests/cookbooks/end_to_end/recipes/default.rb
@@ -9,6 +9,8 @@ hostname "chef-travis-ci.chef.io"
apt_update
+timezone "UTC"
+
include_recipe "ubuntu" if platform?("ubuntu")
if platform_family?("rhel", "fedora", "amazon")
diff --git a/lib/chef/resource/timezone.rb b/lib/chef/resource/timezone.rb
new file mode 100644
index 0000000000..0f12a82617
--- /dev/null
+++ b/lib/chef/resource/timezone.rb
@@ -0,0 +1,95 @@
+#
+# Author:: Kirill Kouznetsov <agon.smith@gmail.com>
+#
+# Copyright 2018, Kirill Kouznetsov.
+# Copyright 2018, Chef Software, Inc.
+#
+# 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 "chef/resource"
+
+class Chef
+ class Resource
+ class Timezone < Chef::Resource
+ preview_resource true
+ resource_name :timezone
+
+ description "Use the timezone resource to change the system timezone."
+ introduced "14.6"
+
+ property :timezone, String,
+ description: "The timezone value to set.",
+ name_property: true
+
+ action :set do
+ description "Set the timezone."
+
+ package "tzdata" do
+ package_name platform_family?("suse") ? "timezone" : "tzdata"
+ end
+
+ if node["init_package"] == "systemd"
+ # Modern Amazon, Fedora, CentOS, RHEL, Ubuntu & Debian
+ cmd_set_tz = "/usr/bin/timedatectl"
+ cmd_set_tz += " "
+ cmd_set_tz += "--no-ask-password"
+ cmd_set_tz += " "
+ cmd_set_tz += "set-timezone #{new_resource.timezone}"
+
+ cmd_check_if_set = "/usr/bin/timedatectl status"
+ cmd_check_if_set += " | /usr/bin/awk '/Time.*zone/{print}'"
+ cmd_check_if_set += " | grep -q #{new_resource.timezone}"
+
+ execute cmd_set_tz do
+ action :run
+ not_if cmd_check_if_set
+ end
+ elsif platform_family?("rhel", "amazon")
+ # Old version of RHEL & CentOS
+ file "/etc/sysconfig/clock" do
+ owner "root"
+ group "root"
+ mode "0644"
+ action :create
+ content %{ZONE="#{new_resource.timezone}"\nUTC="true"\n}
+ end
+
+ execute "tzdata-update" do
+ command "/usr/sbin/tzdata-update"
+ action :nothing
+ only_if { ::File.executable?("/usr/sbin/tzdata-update") }
+ subscribes :run, "file[/etc/sysconfig/clock]", :immediately
+ end
+
+ link "/etc/localtime" do
+ to "/usr/share/zoneinfo/#{new_resource.timezone}"
+ not_if { ::File.executable?("/usr/sbin/tzdata-update") }
+ end
+ elsif platform_family?("debian")
+ file "/etc/timezone" do
+ action :create
+ content "#{new_resource.timezone}\n"
+ end
+
+ bash "dpkg-reconfigure tzdata" do
+ user "root"
+ code "/usr/sbin/dpkg-reconfigure -f noninteractive tzdata"
+ action :nothing
+ subscribes :run, "file[/etc/timezone]", :immediately
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index d99aeed3ad..6324ce5b66 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -141,3 +141,4 @@ require "chef/resource/windows_printer_port"
require "chef/resource/windows_shortcut"
require "chef/resource/windows_task"
require "chef/resource/windows_workgroup"
+require "chef/resource/timezone"
diff --git a/spec/unit/resource/timezone.rb b/spec/unit/resource/timezone.rb
new file mode 100644
index 0000000000..d91a5dd49b
--- /dev/null
+++ b/spec/unit/resource/timezone.rb
@@ -0,0 +1,39 @@
+#
+# Copyright:: Copyright 2018, 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 "spec_helper"
+
+describe Chef::Resource::Timezone do
+ let(:resource) { Chef::Resource::Timezone.new("fakey_fakerton") }
+
+ it "sets resource name as :timezone" do
+ expect(resource.resource_name).to eql(:timezone)
+ end
+
+ it "the timezone property is the name_property" do
+ expect(resource.timezone).to eql("fakey_fakerton")
+ end
+
+ it "sets the default action as :set" do
+ expect(resource.action).to eql([:set])
+ end
+
+ it "supports the :set action only" do
+ expect { resource.action :set }.not_to raise_error
+ expect { resource.action :unset }.to raise_error
+ end
+end