summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Doherty <cdoherty@ooyala.com>2014-09-02 09:14:04 -0700
committerChris Doherty <cdoherty@getchef.com>2014-09-10 16:34:14 -0700
commitda7dddf3d43fffbc38fd48296eb5d4d34467e22d (patch)
tree600920b617a4b9a46b541ab5bf150b7e53f4d9f5
parent6ef555bed751f2b8dac01391fdff2c527d947545 (diff)
downloadchef-da7dddf3d43fffbc38fd48296eb5d4d34467e22d.tar.gz
First-pass changes for a core reboot resource. Still uses run_state instead of run_context.
-rw-r--r--lib/chef/client.rb5
-rw-r--r--lib/chef/dsl/reboot_pending.rb8
-rw-r--r--lib/chef/provider/reboot.rb65
-rw-r--r--lib/chef/providers.rb1
-rw-r--r--lib/chef/rebooter.rb49
-rw-r--r--lib/chef/resource/reboot.rb49
-rw-r--r--lib/chef/resources.rb1
7 files changed, 175 insertions, 3 deletions
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index 2de3ca3e64..4aeecd03c2 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -44,6 +44,7 @@ require 'chef/resource_reporter'
require 'chef/run_lock'
require 'chef/policy_builder'
require 'chef/request_id'
+require 'chef/rebooter'
require 'ohai'
require 'rbconfig'
@@ -435,6 +436,10 @@ class Chef
Chef::Log.info("Chef Run complete in #{run_status.elapsed_time} seconds")
run_completed_successfully
@events.run_completed(node)
+
+ # rebooting has to be the last thing we do, no exceptions.
+ Chef::Rebooter.reboot_if_needed!(node)
+
true
rescue Exception => e
# CHEF-3336: Send the error first in case something goes wrong below and we don't know why
diff --git a/lib/chef/dsl/reboot_pending.rb b/lib/chef/dsl/reboot_pending.rb
index 9f80d38c61..31e57bf5e6 100644
--- a/lib/chef/dsl/reboot_pending.rb
+++ b/lib/chef/dsl/reboot_pending.rb
@@ -27,10 +27,12 @@ class Chef
include Chef::DSL::PlatformIntrospection
# Returns true if the system needs a reboot or is expected to reboot
- # Raises UnsupportedPlatform if this functionality isn't provided yet
+ # Note that we will silently miss any other platform-specific reboot notices besides Windows+Ubuntu.
def reboot_pending?
- if platform?("windows")
+ if node.run_state[:reboot_requested]
+ true
+ elsif platform?("windows")
# PendingFileRenameOperations contains pairs (REG_MULTI_SZ) of filenames that cannot be updated
# due to a file being in use (usually a temporary file and a system file)
# \??\c:\temp\test.sys!\??\c:\winnt\system32\test.sys
@@ -53,7 +55,7 @@ class Chef
# This should work for Debian as well if update-notifier-common happens to be installed. We need an API for that.
File.exists?('/var/run/reboot-required')
else
- raise Chef::Exceptions::UnsupportedPlatform.new(node[:platform])
+ false
end
end
end
diff --git a/lib/chef/provider/reboot.rb b/lib/chef/provider/reboot.rb
new file mode 100644
index 0000000000..f58a037347
--- /dev/null
+++ b/lib/chef/provider/reboot.rb
@@ -0,0 +1,65 @@
+#
+# Author:: Seth Chisamore (<schisamo@opscode.com>)
+# Author:: Chris Doherty <cdoherty@getchef.com>)
+# Copyright:: Copyright (c) 2014 Chef, 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 'chef/log'
+require 'chef/provider'
+
+if RUBY_PLATFORM =~ /mswin|mingw32|windows/
+ require 'win32/registry'
+end
+
+# require 'chef/mixin/shell_out'
+# require 'chef/mixin/command'
+
+class Chef
+ class Provider
+ class Reboot < Chef::Provider
+
+ # def whyrun_supported?
+ # true
+ # end
+
+ def load_current_resource
+ @current_resource ||= Chef::Resource::Reboot.new(@new_resource.name)
+ @current_resource.reason(@new_resource.reason)
+ @current_resource.timeout(@new_resource.timeout)
+ @current_resource.timestamp(@new_resource.timestamp)
+ @current_resource
+ end
+
+ def action_request
+ Chef::Log.warn "Reboot requested: #{@new_resource.name}"
+ node.run_state[:reboot_requested] = true
+ node.run_state[:reboot_timeout] = @new_resource.timeout
+ node.run_state[:reboot_reason] = @new_resource.reason
+ node.run_state[:timestamp] = Time.now
+ node.run_state[:requested_by] = @new_resource.name
+ end
+
+ def action_cancel
+ Chef::Log.warn "Reboot cancel: #{@new_resource.name}"
+ node.run_state.delete(:reboot_requested)
+ node.run_state.delete(:reboot_timeout)
+ node.run_state.delete(:reboot_reason)
+ node.run_state.delete(:timestamp)
+ node.run_state.delete(:requested_by)
+ end
+ end
+ end
+end
diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
index 3c9e94e6f7..1b0ff3ffff 100644
--- a/lib/chef/providers.rb
+++ b/lib/chef/providers.rb
@@ -39,6 +39,7 @@ require 'chef/provider/mdadm'
require 'chef/provider/mount'
require 'chef/provider/package'
require 'chef/provider/powershell_script'
+require 'chef/provider/reboot'
require 'chef/provider/remote_directory'
require 'chef/provider/remote_file'
require 'chef/provider/route'
diff --git a/lib/chef/rebooter.rb b/lib/chef/rebooter.rb
new file mode 100644
index 0000000000..7e880c1f71
--- /dev/null
+++ b/lib/chef/rebooter.rb
@@ -0,0 +1,49 @@
+#
+# Author:: Chris Doherty <cdoherty@getchef.com>)
+# Copyright:: Copyright (c) 2014 Chef, 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 'chef/dsl/reboot_pending'
+require 'chef/log'
+
+# this encapsulates any and all gnarly stuff needed to reboot the server.
+
+# where should this file go in the hierarchy?
+class Chef
+ class Rebooter
+ # below are awkward contortions to re-use the RebootPending code.
+ include Chef::DSL::RebootPending
+
+ attr_reader :node, :reboot_info
+
+ def initialize(node)
+ @node = node
+ @reboot_info = node.run_state[:reboot_info]
+ end
+
+ def reboot!
+ Chef::Log.warn "Totally would have rebooted here."
+ end
+
+ def self.reboot_if_needed!(node)
+ @@rebooter ||= self.new(node)
+ if @@rebooter.reboot_pending?
+ @@rebooter.reboot!
+ end
+ end
+
+ end
+end \ No newline at end of file
diff --git a/lib/chef/resource/reboot.rb b/lib/chef/resource/reboot.rb
new file mode 100644
index 0000000000..4c683337d5
--- /dev/null
+++ b/lib/chef/resource/reboot.rb
@@ -0,0 +1,49 @@
+#
+# Author:: Chris Doherty <cdoherty@getchef.com>)
+# Copyright:: Copyright (c) 2014 Chef, 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 'chef/resource'
+
+class Chef
+ class Resource
+ class Reboot < Chef::Resource
+ def initialize(name, run_context=nil)
+ super
+ @resource_name = :reboot
+ @provider = Chef::Provider::Reboot
+ @allowed_actions = [:request, :cancel]
+
+ @reason = "Reboot by Chef"
+ @timeout = 0
+
+ # no default action just now.
+ end
+
+ def reason(arg=nil)
+ set_or_return(:reason, arg, :kind_of => String)
+ end
+
+ def timeout(arg=nil)
+ set_or_return(:timeout, arg, :kind_of => Fixnum)
+ end
+
+ def timestamp(arg=nil)
+ set_or_return(:timestamp, arg, :kind_of => Time)
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index 93ff682288..8c2f71bd30 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -53,6 +53,7 @@ require 'chef/resource/perl'
require 'chef/resource/portage_package'
require 'chef/resource/powershell_script'
require 'chef/resource/python'
+require 'chef/resource/reboot'
require 'chef/resource/registry_key'
require 'chef/resource/remote_directory'
require 'chef/resource/remote_file'