summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam Jacob <adam@hjksolutions.com>2008-10-02 14:41:13 -0700
committerAdam Jacob <adam@hjksolutions.com>2008-10-02 14:41:13 -0700
commit28a6ace0566b93a538effa9ddb61e1bc20031534 (patch)
treeabb00321038f9e84beee4de355777a4f429ff71c
parentcdd43e77db271c2accda73f94efa59358b900dfe (diff)
parent77641960368293d4e33bfa4bf8ce1f594b7f72eb (diff)
downloadchef-28a6ace0566b93a538effa9ddb61e1bc20031534.tar.gz
merging in fujins service resource and provider
-rw-r--r--.gitignore1
-rw-r--r--Manifest.txt134
-rw-r--r--examples/config/cookbooks/servicetest/recipes/default.rb12
-rw-r--r--examples/config/nodes/junglist.gen.nz.rb2
-rw-r--r--lib/chef/client.rb32
-rw-r--r--lib/chef/exceptions.rb3
-rw-r--r--lib/chef/node.rb2
-rw-r--r--lib/chef/platform.rb4
-rw-r--r--lib/chef/provider/service.rb86
-rw-r--r--lib/chef/provider/service/base.rb32
-rw-r--r--lib/chef/provider/service/debian.rb62
-rw-r--r--lib/chef/provider/service/init.rb93
-rw-r--r--lib/chef/resource.rb5
-rw-r--r--lib/chef/resource/service.rb113
-rw-r--r--lib/chef_server/init.rb4
-rw-r--r--spec/unit/resource/service_spec.rb100
16 files changed, 624 insertions, 61 deletions
diff --git a/.gitignore b/.gitignore
index ad8fe4913b..7e2a22c7c6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,3 +6,4 @@ examples/search_index
examples/store
examples/openid-cstore
examples/openid-db
+*.swp
diff --git a/Manifest.txt b/Manifest.txt
index 38793f098b..79d961a738 100644
--- a/Manifest.txt
+++ b/Manifest.txt
@@ -1,85 +1,115 @@
-.gitignore
History.txt
+LICENSE
Manifest.txt
README.txt
Rakefile
+bin/chef-client
+bin/chef-indexer
+bin/chef-server
bin/chef-solo
+config/chef-server.rb
examples/config.rb
examples/config/chef-solo.rb
+examples/config/cookbooks/fakefile/attributes/first.rb
+examples/config/cookbooks/fakefile/files/default/remote_test/another/turn/the_page.txt
+examples/config/cookbooks/fakefile/files/default/remote_test/another/window.txt
+examples/config/cookbooks/fakefile/files/default/remote_test/mycat.txt
+examples/config/cookbooks/fakefile/files/default/the_park.txt
examples/config/cookbooks/fakefile/recipes/default.rb
+examples/config/cookbooks/fakefile/templates/default/monkey.erb
+examples/config/cookbooks/tempfile/attributes/second.rb
examples/config/cookbooks/tempfile/recipes/default.rb
+examples/config/nodes/adam.rb
+examples/config/nodes/default.rb
examples/config/nodes/latte.rb
+examples/log/stompserver.pid
examples/mrepo/Rakefile
examples/node.rb
examples/node.yml
examples/sample_definition.rb
examples/sample_recipe.rb
+examples/search_index/_19.cfs
+examples/search_index/_1k.cfs
+examples/search_index/_1k_0.del
+examples/search_index/_1l.cfs
+examples/search_index/_c.cfs
+examples/search_index/_c_0.del
+examples/search_index/_n.cfs
+examples/search_index/_y.cfs
+examples/search_index/segments
+examples/search_index/segments_1v
+examples/search_syntax.rb
+examples/user_index.pl
+examples/user_index.rb
lib/chef.rb
+lib/chef/client.rb
lib/chef/compile.rb
lib/chef/config.rb
lib/chef/cookbook.rb
lib/chef/cookbook_loader.rb
+lib/chef/couchdb.rb
+lib/chef/exceptions.rb
+lib/chef/file_store.rb
lib/chef/log.rb
lib/chef/log/formatter.rb
lib/chef/mixin/check_helper.rb
+lib/chef/mixin/checksum.rb
+lib/chef/mixin/command.rb
lib/chef/mixin/from_file.rb
lib/chef/mixin/params_validate.rb
+lib/chef/mixin/template.rb
lib/chef/node.rb
+lib/chef/openid_registration.rb
+lib/chef/platform.rb
lib/chef/provider.rb
+lib/chef/provider/apt.rb
+lib/chef/provider/directory.rb
+lib/chef/provider/execute.rb
lib/chef/provider/file.rb
+lib/chef/provider/link.rb
+lib/chef/provider/package.rb
+lib/chef/provider/remote_directory.rb
+lib/chef/provider/remote_file.rb
+lib/chef/provider/script.rb
+lib/chef/provider/sysctl.rb
+lib/chef/provider/template.rb
+lib/chef/queue.rb
lib/chef/recipe.rb
lib/chef/resource.rb
+lib/chef/resource/bash.rb
+lib/chef/resource/csh.rb
+lib/chef/resource/directory.rb
+lib/chef/resource/execute.rb
lib/chef/resource/file.rb
+lib/chef/resource/link.rb
+lib/chef/resource/package.rb
+lib/chef/resource/perl.rb
+lib/chef/resource/python.rb
+lib/chef/resource/remote_directory.rb
+lib/chef/resource/remote_file.rb
+lib/chef/resource/ruby.rb
+lib/chef/resource/script.rb
+lib/chef/resource/sysctl.rb
+lib/chef/resource/template.rb
lib/chef/resource_collection.rb
lib/chef/resource_definition.rb
-spec/data/bad-config.rb
-spec/data/compile/cookbooks/test/attributes/george.rb
-spec/data/compile/cookbooks/test/definitions/new_cat.rb
-spec/data/compile/cookbooks/test/recipes/default.rb
-spec/data/compile/cookbooks/test/recipes/one.rb
-spec/data/compile/cookbooks/test/recipes/two.rb
-spec/data/compile/nodes/compile.rb
-spec/data/config.rb
-spec/data/cookbooks/openldap/attributes/default.rb
-spec/data/cookbooks/openldap/attributes/smokey.rb
-spec/data/cookbooks/openldap/definitions/client.rb
-spec/data/cookbooks/openldap/definitions/server.rb
-spec/data/cookbooks/openldap/ignore
-spec/data/cookbooks/openldap/recipes/default.rb
-spec/data/cookbooks/openldap/recipes/gigantor.rb
-spec/data/cookbooks/openldap/recipes/one.rb
-spec/data/definitions/test.rb
-spec/data/kitchen/openldap/attributes/default.rb
-spec/data/kitchen/openldap/attributes/robinson.rb
-spec/data/kitchen/openldap/definitions/client.rb
-spec/data/kitchen/openldap/definitions/drewbarrymore.rb
-spec/data/kitchen/openldap/recipes/gigantor.rb
-spec/data/kitchen/openldap/recipes/ignoreme.rb
-spec/data/kitchen/openldap/recipes/woot.rb
-spec/data/nodes/default.rb
-spec/data/nodes/test.example.com.rb
-spec/data/nodes/test.rb
-spec/data/recipes/test.rb
-spec/data/seattle.txt
-spec/lib/chef/resource/cat.rb
-spec/lib/chef/resource/zen_master.rb
-spec/rcov.opts
-spec/spec.opts
-spec/spec_helper.rb
-spec/unit/chef_spec.rb
-spec/unit/compile_spec.rb
-spec/unit/config_spec.rb
-spec/unit/cookbook_loader_spec.rb
-spec/unit/cookbook_spec.rb
-spec/unit/log/formatter_spec.rb
-spec/unit/log_spec.rb
-spec/unit/mixin/params_validate_spec.rb
-spec/unit/node_spec.rb
-spec/unit/provider/file_spec.rb
-spec/unit/provider_spec.rb
-spec/unit/recipe_spec.rb
-spec/unit/resource/file_spec.rb
-spec/unit/resource_collection_spec.rb
-spec/unit/resource_definition_spec.rb
-spec/unit/resource_spec.rb
-tasks/rspec.rb
+lib/chef/rest.rb
+lib/chef/runner.rb
+lib/chef/search.rb
+lib/chef/search_index.rb
+lib/chef_server/controllers/application.rb
+lib/chef_server/controllers/cookbook_files.rb
+lib/chef_server/controllers/cookbook_templates.rb
+lib/chef_server/controllers/cookbooks.rb
+lib/chef_server/controllers/exceptions.rb
+lib/chef_server/controllers/nodes.rb
+lib/chef_server/controllers/openid_consumer.rb
+lib/chef_server/controllers/openid_register.rb
+lib/chef_server/controllers/openid_server.rb
+lib/chef_server/controllers/search.rb
+lib/chef_server/controllers/search_entries.rb
+lib/chef_server/helpers/global_helpers.rb
+lib/chef_server/helpers/nodes_helper.rb
+lib/chef_server/helpers/openid_server_helpers.rb
+lib/chef_server/init.rb
+
diff --git a/examples/config/cookbooks/servicetest/recipes/default.rb b/examples/config/cookbooks/servicetest/recipes/default.rb
new file mode 100644
index 0000000000..812b48e19d
--- /dev/null
+++ b/examples/config/cookbooks/servicetest/recipes/default.rb
@@ -0,0 +1,12 @@
+service "puppet-client" do
+ service_name "puppet"
+ pattern "puppetd"
+ action :enable
+end
+
+file "/tmp/foo" do
+ owner "aj"
+ mode 0644
+ action :create
+ notifies :start, resources(:service => "puppet-client"), :immediate
+end
diff --git a/examples/config/nodes/junglist.gen.nz.rb b/examples/config/nodes/junglist.gen.nz.rb
index 305a9fbbec..70ce7661b5 100644
--- a/examples/config/nodes/junglist.gen.nz.rb
+++ b/examples/config/nodes/junglist.gen.nz.rb
@@ -11,4 +11,4 @@ owner "AJ Christensen"
##
# Nodes should have recipes
##
-recipes "rubygems_server"
+recipes "servicetest"
diff --git a/lib/chef/client.rb b/lib/chef/client.rb
index c8ab31eed7..f6ee8c14c4 100644
--- a/lib/chef/client.rb
+++ b/lib/chef/client.rb
@@ -59,7 +59,9 @@ class Chef
sync_definitions
sync_recipes
do_attribute_files
+ # do_recipe_files
save_node
+ # compile
converge
save_node
true
@@ -70,6 +72,9 @@ class Chef
#
# === Parameters
# node_name<String>:: The name of the node to build - defaults to nil
+ #
+ # === Returns
+ # node:: Returns the created node object, also stored in @node
def build_node(node_name=nil)
node_name ||= Facter["fqdn"].value ? Facter["fqdn"].value : Facter["hostname"].value
@safe_name = node_name.gsub(/\./, '_')
@@ -94,6 +99,9 @@ class Chef
# data.
#
# If it has not, we register it by calling create_registration.
+ #
+ # === Returns
+ # true:: Always returns true
def register
@registration = nil
begin
@@ -110,17 +118,25 @@ class Chef
else
create_registration
end
+ true
end
# Generates a random secret, stores it in the Chef::Filestore with the "registration" key,
# and posts our nodes registration information to the server.
+ #
+ # === Returns
+ # true:: Always returns true
def create_registration
@secret = random_password(500)
Chef::FileStore.store("registration", @safe_name, { "secret" => @secret })
@rest.post_rest("registrations", { :id => @safe_name, :password => @secret })
+ true
end
# Authenticates the node via OpenID.
+ #
+ # === Returns
+ # true:: Always returns true
def authenticate
response = @rest.post_rest('openid/consumer/start', {
"openid_identifier" => "#{Chef::Config[:openid_url]}/openid/server/node/#{@safe_name}",
@@ -193,6 +209,9 @@ class Chef
# Gets all the attribute files included in all the cookbooks available on the server,
# and executes them.
+ #
+ # === Returns
+ # true:: Always returns true
def do_attribute_files
update_file_cache("attributes", @rest.get_rest('cookbooks/_attribute_files'))
Chef::FileCache.list.each do |cache_file|
@@ -213,12 +232,19 @@ class Chef
end
# Updates the current node configuration on the server.
+ #
+ # === Returns
+ # true:: Always returns true
def save_node
@node = @rest.put_rest("nodes/#{@safe_name}", @node)
+ true
end
- # Compiles the full list of recipes for the node from the local cache, and
- # passes it to an instance of Chef::Runner.converge.
+ # Compiles the full list of recipes for the server, and passes it to an instance of
+ # Chef::Runner.converge.
+ #
+ # === Returns
+ # true:: Always returns true
def converge
Chef::Config[:cookbook_path] = File.join(Chef::Config[:file_cache_path], "cookbooks")
compile = Chef::Compile.new()
@@ -241,4 +267,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index a2125e862f..a70692b001 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -20,8 +20,9 @@ class Chef
class Exec < RuntimeError; end
class FileNotFound < RuntimeError; end
class Package < RuntimeError; end
+ class Service < RuntimeError; end
class SearchIndex < RuntimeError; end
class Override < RuntimeError; end
class UnsupportedAction < RuntimeError; end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/node.rb b/lib/chef/node.rb
index 9ad20cddc8..4eb72e1856 100644
--- a/lib/chef/node.rb
+++ b/lib/chef/node.rb
@@ -249,4 +249,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/platform.rb b/lib/chef/platform.rb
index 0345445fbe..c6c4a7580c 100644
--- a/lib/chef/platform.rb
+++ b/lib/chef/platform.rb
@@ -27,6 +27,7 @@ class Chef
:ubuntu => {
:default => {
:package => Chef::Provider::Apt,
+ :service => Chef::Provider::Debian,
}
},
:centos => {},
@@ -43,6 +44,7 @@ class Chef
:sysctl => Chef::Provider::Sysctl,
:execute => Chef::Provider::Execute,
:script => Chef::Provider::Script,
+ :service => Chef::Provider::Init,
:perl => Chef::Provider::Script,
:python => Chef::Provider::Script,
:ruby => Chef::Provider::Script,
@@ -190,4 +192,4 @@ class Chef
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/provider/service.rb b/lib/chef/provider/service.rb
new file mode 100644
index 0000000000..5ac53478e3
--- /dev/null
+++ b/lib/chef/provider/service.rb
@@ -0,0 +1,86 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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 File.join(File.dirname(__FILE__), "..", "mixin", "command")
+
+class Chef
+ class Provider
+ class Service < Chef::Provider
+
+ include Chef::Mixin::Command
+
+ def initialize(node, new_resource)
+ super(node, new_resource)
+ @enabled = nil
+ end
+
+ def action_enable
+ if @current_resource.enabled == false
+ Chef::Log.debug("#{@new_resource}: attempting to enable")
+ status = enable_service(@new_resource.service_name)
+ if status
+ @new_resource.enabled == true
+ Chef::Log.info("#{@new_resource}: enabled succesfully")
+ end
+ else
+ Chef::Log.debug("#{@new_resource}: not enabling, already enabled")
+ end
+ end
+
+ def action_disable
+ if @current_resource.enabled == true
+ Chef::Log.debug("#{@new_resource}: attempting to disable")
+ status = disable_service(@new_resource.service_name)
+ if status
+ @new_resource.enabled == false
+ Chef::Log.info("#{@new_resource}: disabled succesfully")
+ end
+ else
+ Chef::Log.debug("#{@new_resource}: not disabling, already disabled")
+ end
+ end
+
+ def action_start
+ if @current_resource.running == false
+ Chef::Log.debug("#{@new_resource}: attempting to start")
+ status = start_service(@new_resource.service_name)
+ if status
+ @new_resource.running == true
+ Chef::Log.info("Started service #{@new_resource} succesfully")
+ end
+ else
+ Chef::Log.debug("#{@new_resource}: not starting, already running")
+ end
+ end
+
+ def action_stop
+ if @current_resource.running == true
+ Chef::Log.debug("#{@new_resource}: attempting to stop")
+ status = stop_service(@new_resource.service_name)
+ if status
+ @new_resource.running == false
+ Chef::Log.info("#{@new_resource}: stopped succesfully")
+ end
+ else
+ Chef::Log.debug("#{@new_resource}: not stopping, already stopped")
+ end
+ end
+
+ end
+ end
+end
diff --git a/lib/chef/provider/service/base.rb b/lib/chef/provider/service/base.rb
new file mode 100644
index 0000000000..178878296e
--- /dev/null
+++ b/lib/chef/provider/service/base.rb
@@ -0,0 +1,32 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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 File.join(File.dirname(__FILE__), "..", "service")
+require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command")
+
+class Chef
+ class Provider
+ class Base < Chef::Provider::Service
+
+ def load_current_resource
+ true
+ end
+
+ end
+ end
+end
diff --git a/lib/chef/provider/service/debian.rb b/lib/chef/provider/service/debian.rb
new file mode 100644
index 0000000000..51597ff703
--- /dev/null
+++ b/lib/chef/provider/service/debian.rb
@@ -0,0 +1,62 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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 File.join(File.dirname(__FILE__), "..", "service")
+require File.join(File.dirname(__FILE__), "init")
+require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command")
+
+class Chef
+ class Provider
+ class Debian < Chef::Provider::Init
+ def load_current_resource
+ super
+
+ status = popen4("update-rc.d -n -f #{@current_resource.service_name} remove") do |pid, stdin, stdout, stderr|
+ stdin.close
+ stdout.gets(nil) =~ /etc\/rc[\dS].d\/S|not installed/i ? @current_resource.enabled(true) : @current_resource.enabled(false)
+ end
+
+ unless status.exitstatus == 0
+ raise Chef::Exception::Service, "update-rc.d -n -f #{@current_resource.service_name} failed - #{status.inspect}"
+ end
+
+ @current_resource
+ end
+
+ def enable_service(name)
+ run_command(:command => "update-rc.d #{name} defaults")
+ end
+
+ def disable_service(name)
+ run_command(:command => "update-rc.d -f #{name} remove")
+ end
+
+ def start_service(name)
+ super
+ end
+
+ def stop_service(name)
+ super
+ end
+
+ def restart_service(name)
+ super
+ end
+ end
+ end
+end
diff --git a/lib/chef/provider/service/init.rb b/lib/chef/provider/service/init.rb
new file mode 100644
index 0000000000..3eb70e594e
--- /dev/null
+++ b/lib/chef/provider/service/init.rb
@@ -0,0 +1,93 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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 File.join(File.dirname(__FILE__), "..", "service")
+require File.join(File.dirname(__FILE__), "..", "..", "mixin", "command")
+
+class Chef
+ class Provider
+ class Init < Chef::Provider::Service
+
+ def load_current_resource
+ @current_resource = Chef::Resource::Service.new(@new_resource.name)
+ @current_resource.service_name(@new_resource.service_name)
+ process_running = false
+ if @new_resource.supports[:status]
+ run_command(:command => "/etc/init.d/#{@current_resource.service_name} status") == 0 ? process_running = true : process_running = false
+ elsif @new_resource.status_command
+ run_command(:command => @new_resource.status_command) == 0 ? process_running = true : process_running = false
+ else
+ Chef::Log.debug("#{@new_resource} does not support status and you have not specified a status command, falling back to process table inspection")
+ if @new_resource.pattern == @new_resource.service_name
+ Chef::Log.debug("#{@new_resource} defaulting pattern to #{Regex.new(@new_resource.pattern)}")
+ elsif Facter["ps"].value == ""
+ raise Chef::Exception::Service, "#{@new_resource}: Facter could not determine how to call `ps` on your system (#{Facter["ps"].value})"
+ end
+
+ process_pid = nil
+ status = popen4(Facter["ps"].value) do |pid, stdin, stdout, stderr|
+ stdin.close
+ r = Regexp.new(@new_resource.pattern)
+ Chef::Log.debug("#{@new_resource}: attempting to match #{@new_resource.pattern} (#{r}) against process table")
+ stdout.each_line do |line|
+ if r.match(line)
+ process_pid = line.sub(/^\s+/, '').split(/\s+/)[1]
+ end
+ end
+ end
+ unless status.exitstatus == 0
+ raise Chef::Exception::Service, "Command #{Facter["ps"].value} failed"
+ else
+ process_pid ? process_running = true : process_running = false
+ Chef::Log.debug("#{@new_resource}: #{Facter["ps"].value} exited succesfully, process_running: #{process_running}")
+ end
+ end
+ @current_resource.running process_running
+ @current_resource
+ end
+
+ def start_service(name)
+ if @new_resource.start_command
+ run_command(:command => @new_resource.start_command)
+ else
+ run_command(:command => "/etc/init.d/#{name} start")
+ end
+ end
+
+ def stop_service(name)
+ if @new_resource.stop_command
+ run_command(:command => @new_resource.stop_command)
+ else
+ run_command(:command => "/etc/init.d/#{name} stop")
+ end
+ end
+
+ def restart_service(name)
+ if @new_resource.supports[:restart]
+ run_command(:command => "/etc/init.d/#{name} restart")
+ elsif @new_resource.restart_command
+ run_command(:command => @new_resource.restart_command)
+ else
+ stop_service
+ start_service
+ end
+ end
+
+ end
+ end
+end
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index 866b77dc14..553a695379 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -25,7 +25,7 @@ class Chef
include Chef::Mixin::CheckHelper
include Chef::Mixin::ParamsValidate
- attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name
+ attr_accessor :actions, :params, :provider, :updated, :allowed_actions, :collection, :cookbook_name, :recipe_name, :supports
attr_reader :resource_name, :source_line, :node
def initialize(name, collection=nil, node=nil)
@@ -44,6 +44,7 @@ class Chef
@allowed_actions = [ :nothing ]
@action = :nothing
@updated = false
+ @supports = {}
@source_line = caller(4).shift.gsub!(/^(.+):(.+):.+$/, '\1 line \2')
@source_line = ::File.expand_path(@source_line) if @source_line
end
@@ -160,4 +161,4 @@ class Chef
timing
end
end
-end \ No newline at end of file
+end
diff --git a/lib/chef/resource/service.rb b/lib/chef/resource/service.rb
new file mode 100644
index 0000000000..1c9a784745
--- /dev/null
+++ b/lib/chef/resource/service.rb
@@ -0,0 +1,113 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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
+ class Resource
+ class Service < Chef::Resource
+
+ def initialize(name, collection=nil, node=nil)
+ super(name, collection, node)
+ @resource_name = :service
+ @service_name = name
+ @enabled = nil
+ @running = nil
+ @pattern = service_name
+ @start_command = nil
+ @stop_command = nil
+ @status_command = nil
+ @restart_command = nil
+ @action = "none"
+ @supports = { :restart => false, :status => false }
+ @allowed_actions.push(:enable, :disable, :start, :stop)
+ end
+
+ def service_name(arg=nil)
+ set_or_return(
+ :service_name,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # regex for match against ps -ef when !supports[:has_status] && status == nil
+ def pattern(arg=nil)
+ set_or_return(
+ :pattern,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # command to call to start service
+ def start_command(arg=nil)
+ set_or_return(
+ :start_command,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # command to call to stop service
+ def stop_command(arg=nil)
+ set_or_return(
+ :stop_command,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # command to call to get status of service
+ def status_command(arg=nil)
+ set_or_return(
+ :status_command,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # command to call to restart service
+ def restart_command(arg=nil)
+ set_or_return(
+ :restart_command,
+ arg,
+ :kind_of => [ String ]
+ )
+ end
+
+ # if the service is enabled or not
+ def enabled(arg=nil)
+ set_or_return(
+ :enabled,
+ arg,
+ :kind_of => [ TrueClass, FalseClass ]
+ )
+ end
+
+ # if the service is running or not
+ def running(arg=nil)
+ set_or_return(
+ :running,
+ arg,
+ :kind_of => [ TrueClass, FalseClass ]
+ )
+ end
+
+
+ end
+ end
+end
diff --git a/lib/chef_server/init.rb b/lib/chef_server/init.rb
index 11579e80f6..8b1b8e803b 100644
--- a/lib/chef_server/init.rb
+++ b/lib/chef_server/init.rb
@@ -63,6 +63,10 @@ Gem.path.unshift(Merb.root / "gems")
# Merb.push_path(:lib, Merb.root / "lib") # uses **/*.rb as path glob.
+
+# disable the ActiveSupport json annoying stuff, as it breaks our to_json
+Merb.disable :json
+
# ==== Dependencies
# These are some examples of how you might specify dependencies.
diff --git a/spec/unit/resource/service_spec.rb b/spec/unit/resource/service_spec.rb
new file mode 100644
index 0000000000..f5941f89ae
--- /dev/null
+++ b/spec/unit/resource/service_spec.rb
@@ -0,0 +1,100 @@
+#
+# Author:: AJ Christensen (<aj@hjksolutions.com>)
+# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
+# 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "spec_helper"))
+
+describe Chef::Resource::Service do
+
+ before(:each) do
+ @resource = Chef::Resource::Service.new("chef")
+ end
+
+ it "should create a new Chef::Resource::Service" do
+ @resource.should be_a_kind_of(Chef::Resource)
+ @resource.should be_a_kind_of(Chef::Resource::Service)
+ end
+
+ it "should set the service_name to the first argument to new" do
+ @resource.service_name.should eql("chef")
+ end
+
+ it "should set the pattern to be the service name by default" do
+ @resource.pattern.should eql("chef")
+ end
+
+ it "should accept a string for the service name" do
+ @resource.service_name "something"
+ @resource.service_name.should eql("something")
+ end
+
+ it "should accept a string for the service pattern" do
+ @resource.pattern ".*"
+ @resource.pattern.should eql(".*")
+ end
+
+ it "should not accept a regexp for the service pattern" do
+ lambda {
+ @resource.pattern /.*/
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should accept a string for the service start command" do
+ @resource.start_command "/etc/init.d/chef start"
+ @resource.start_command.should eql("/etc/init.d/chef start")
+ end
+
+ it "should not accept a regexp for the service start command" do
+ lambda {
+ @resource.start_command /.*/
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should accept a string for the service stop command" do
+ @resource.stop_command "/etc/init.d/chef stop"
+ @resource.stop_command.should eql("/etc/init.d/chef stop")
+ end
+
+ it "should not accept a regexp for the service stop command" do
+ lambda {
+ @resource.stop_command /.*/
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should accept a string for the service status command" do
+ @resource.status_command "/etc/init.d/chef status"
+ @resource.status_command.should eql("/etc/init.d/chef status")
+ end
+
+ it "should not accept a regexp for the service status command" do
+ lambda {
+ @resource.status_command /.*/
+ }.should raise_error(ArgumentError)
+ end
+
+ it "should accept a string for the service restart command" do
+ @resource.restart_command "/etc/init.d/chef restart"
+ @resource.restart_command.should eql("/etc/init.d/chef restart")
+ end
+
+ it "should not accept a regexp for the service restart command" do
+ lambda {
+ @resource.restart_command /.*/
+ }.should raise_error(ArgumentError)
+ end
+
+end