From 7322eb80de81930ed15b22d58733432525f619fc Mon Sep 17 00:00:00 2001 From: Mario Giammarco Date: Wed, 7 Oct 2009 16:40:06 +0200 Subject: [PATCH 1/4] Implemented suse zypper package support. --- chef/lib/chef/platform.rb | 7 ++ chef/lib/chef/provider/package/zypper.rb | 132 +++++++++++++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 chef/lib/chef/provider/package/zypper.rb diff --git a/chef/lib/chef/platform.rb b/chef/lib/chef/platform.rb index b1997b60087..20f2b04a4d3 100644 --- a/chef/lib/chef/platform.rb +++ b/chef/lib/chef/platform.rb @@ -60,6 +60,13 @@ class Platform :cron => Chef::Provider::Cron, :package => Chef::Provider::Package::Yum } + }, + :suse => { + :default => { +# :service => Chef::Provider::Service::Zypper, + :cron => Chef::Provider::Cron, + :package => Chef::Provider::Package::Zypper + } }, :redhat => { :default => { diff --git a/chef/lib/chef/provider/package/zypper.rb b/chef/lib/chef/provider/package/zypper.rb new file mode 100644 index 00000000000..7bebb5192de --- /dev/null +++ b/chef/lib/chef/provider/package/zypper.rb @@ -0,0 +1,132 @@ +# +# Author:: Adam Jacob () +# Copyright:: Copyright (c) 2008 Opscode, 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/provider/package' +require 'chef/mixin/command' +require 'chef/resource/package' +require 'singleton' + +class Chef + class Provider + class Package + class Zypper < Chef::Provider::Package + + + def load_current_resource + @current_resource = Chef::Resource::Package.new(@new_resource.name) + @current_resource.package_name(@new_resource.package_name) + + is_installed=false + version='' + is_out_of_date=false + Chef::Log.debug("Checking zypper for #{@new_resource.package_name}") + status = popen4("zypper info #{@new_resource.package_name}") do |pid, stdin, stdout, stderr| + stdout.each do |line| + case line + when /^Version: (.+)$/ + version = $1 + Chef::Log.debug("zypper version=#{$1}") + when /^Installed: Yes$/ + is_installed=true + Chef::Log.debug("zypper installed true") + + when /^Installed: No$/ + is_installed=false + Chef::Log.debug("zypper installed false") + when /^Status: out-of-date \(version (.+) installed\)$/ + is_out_of_date=true + oud_version=$1 + Chef::Log.debug("zypper out of date version=#{$1}") + end + end + end + + if is_installed==false + @candidate_version=version + @current_resource.version(nil) + Chef::Log.debug("dentro installed false"); + end + + if is_installed==true + if is_out_of_date==true + @current_resource.version(oud_version) + @candidate_version=version + Chef::Log.debug("dentro installed outofdate"); + else + @current_resource.version(version) + @candidate_version=version + Chef::Log.debug("dentro installed"); + end + end + + + unless status.exitstatus == 0 + raise Chef::Exceptions::Package, "zypper failed - #{status.inspect}!" + end + + + Chef::Log.debug("zypper current resource #{@current_resource}") + @current_resource + end + + def install_package(name, version) + if version + run_command( + :command => "zypper -n --no-gpg-checks install -l #{name}=#{version}" + ) + else + run_command( + :command => "zypper -n --no-gpg-checks install -l #{name}" + ) + end + end + + def upgrade_package(name, version) + if version + run_command( + :command => "zypper -n --no-gpg-checks upgrade -l #{name}=#{version}" + ) + else + run_command( + :command => "zypper -n --no-gpg-checks upgrade -l #{name}" + ) + end + end + + def remove_package(name, version) + if version + run_command( + :command => "zypper -n --no-gpg-checks remove -l #{name}=#{version}" + ) + else + run_command( + :command => "zypper -n --no-gpg-checks remove -l #{name}" + ) + end + + + end + + def purge_package(name, version) + remove_package(name, version) + end + + end + end + end +end From 82a64d93150f0229d93984b7b4570979eb453cde Mon Sep 17 00:00:00 2001 From: root Date: Fri, 9 Oct 2009 18:20:35 +0200 Subject: [PATCH 2/4] Added tests and corrected relative bugs. --- chef/lib/chef/provider/package/zypper.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/chef/lib/chef/provider/package/zypper.rb b/chef/lib/chef/provider/package/zypper.rb index 7bebb5192de..96d41f426b3 100644 --- a/chef/lib/chef/provider/package/zypper.rb +++ b/chef/lib/chef/provider/package/zypper.rb @@ -61,7 +61,7 @@ def load_current_resource @current_resource.version(nil) Chef::Log.debug("dentro installed false"); end - + if is_installed==true if is_out_of_date==true @current_resource.version(oud_version) @@ -99,11 +99,11 @@ def install_package(name, version) def upgrade_package(name, version) if version run_command( - :command => "zypper -n --no-gpg-checks upgrade -l #{name}=#{version}" + :command => "zypper -n --no-gpg-checks update -l #{name}=#{version}" ) else run_command( - :command => "zypper -n --no-gpg-checks upgrade -l #{name}" + :command => "zypper -n --no-gpg-checks update -l #{name}" ) end end @@ -111,11 +111,11 @@ def upgrade_package(name, version) def remove_package(name, version) if version run_command( - :command => "zypper -n --no-gpg-checks remove -l #{name}=#{version}" + :command => "zypper -n --no-gpg-checks remove #{name}=#{version}" ) else run_command( - :command => "zypper -n --no-gpg-checks remove -l #{name}" + :command => "zypper -n --no-gpg-checks remove #{name}" ) end From e0c997315ccbe6b1ef1c14b8feec526d9c375eb2 Mon Sep 17 00:00:00 2001 From: Mario Giammarco Date: Mon, 12 Oct 2009 12:16:47 +0200 Subject: [PATCH 3/4] Tests implemented. All tests pass. --- .../spec/unit/provider/package/zypper_spec.rb | 286 ++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 chef/spec/unit/provider/package/zypper_spec.rb diff --git a/chef/spec/unit/provider/package/zypper_spec.rb b/chef/spec/unit/provider/package/zypper_spec.rb new file mode 100644 index 00000000000..9f53184bbd4 --- /dev/null +++ b/chef/spec/unit/provider/package/zypper_spec.rb @@ -0,0 +1,286 @@ +# +# Author:: Adam Jacob () +# Copyright:: Copyright (c) 2008 Opscode, 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 File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "..", "spec_helper")) + +describe Chef::Provider::Package::Zypper, "load_current_resource" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "cups", + :version => nil, + :package_name => "cups", + :updated => nil + ) + @current_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "cups", + :version => nil, + :package_name => nil, + :updated => nil + ) + @status = mock("Status", :exitstatus => 0) + + + @provider = Chef::Provider::Package::Zypper.new(@node, @new_resource) + Chef::Resource::Package.stub!(:new).and_return(@current_resource) + @provider.stub!(:popen4).and_return(@status) + @stderr = mock("STDERR", :null_object => true) + @stdout = mock("STDERR", :null_object => true) + @pid = mock("PID", :null_object => true) + end + + it "should create a current resource with the name of the new_resource" do + Chef::Resource::Package.should_receive(:new).and_return(@current_resource) + @provider.load_current_resource + end + + it "should set the current resources package name to the new resources package name" do + @current_resource.should_receive(:package_name).with(@new_resource.package_name) + @provider.load_current_resource + end + + it "should run zypper info with the package name" do + @provider.should_receive(:popen4).with("zypper info #{@new_resource.package_name}").and_return(@status) + @provider.load_current_resource + end + + it "should read stdout on zypper info" do + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @stdout.should_receive(:each).and_return(true) + @provider.load_current_resource + end + + it "should set the installed version to nil on the current resource if zypper info installed version is (none)" do + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @current_resource.should_receive(:version).with(nil).and_return(true) + @provider.load_current_resource + end + + it "should set the installed version if zypper info has one" do + @stdout.stub!(:each).and_yield("Version: 1.0"). + and_yield("Installed: Yes") + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @current_resource.should_receive(:version).with("1.0").and_return(true) + @provider.load_current_resource + end + + it "should set the candidate version if zypper info has one" do + @stdout.stub!(:each).and_yield("Version: 1.0"). + and_yield("Installed: No"). + and_yield("Status: out-of-date (version 0.9 installed)") + + @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) + @provider.load_current_resource + @provider.candidate_version.should eql("1.0") + end + + it "should raise an exception if zypper info fails" do + @status.should_receive(:exitstatus).and_return(1) + lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Package) + end + + it "should not raise an exception if zypper info succeeds" do + @status.should_receive(:exitstatus).and_return(0) + lambda { @provider.load_current_resource }.should_not raise_error(Chef::Exceptions::Package) + end + + + # In my implementation it is not possible to not have a candidate version +# it "should raise an exception if zypper info does not return a candidate version" do +# @stdout.stub!(:each).and_yield("Version: 1.0"). +# and_yield("Installed: No"). +# and_yield("Status: out-of-date (version 0.9 installed)") +# +# @provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status) +# lambda { @provider.load_current_resource }.should raise_error(Chef::Exceptions::Package) +# end + + it "should return the current resouce" do + @provider.load_current_resource.should eql(@current_resource) + end +end + +describe Chef::Provider::Package::Zypper, "install_package" do + + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "emacs", + :version => nil, + :package_name => "emacs", + :updated => nil, + :options => nil + ) + @provider = Chef::Provider::Package::Zypper.new(@node, @new_resource) + + @status = mock("Status", :exitstatus => 0) + @provider.stub!(:popen4).and_return(@status) + end + + it "should run zypper install with the package name and version" do + @provider.should_receive(:run_command).with({ + :command => "zypper -n --no-gpg-checks install -l emacs=1.0", + }) + @provider.install_package("emacs", "1.0") + end + + # it "should run apt-get install with the package name and version and options if specified" do + # @provider.should_receive(:run_command_with_systems_locale).with({ + # :command => "apt-get -q -y --force-yes install emacs=1.0", + # :environment => { + # "DEBIAN_FRONTEND" => "noninteractive" + # } + # }) + # @new_resource.stub!(:options).and_return("--force-yes") + # + # @provider.install_package("emacs", "1.0") + # end +end + +describe Chef::Provider::Package::Zypper, "upgrade_package" do + + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "emacs", + :version => nil, + :package_name => "emacs", + :updated => nil, + :options => nil + ) + @provider = Chef::Provider::Package::Zypper.new(@node, @new_resource) + @status = mock("Status", :exitstatus => 0) + @provider.stub!(:popen4).and_return(@status) + end + it "should run zypper update with the package name and version" do + @provider.should_receive(:run_command).with({ + :command => "zypper -n --no-gpg-checks update -l emacs=1.0", + }) + @provider.upgrade_package("emacs", "1.0") + end + +end + +describe Chef::Provider::Package::Zypper, "remove_package" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "emacs", + :version => nil, + :package_name => "emacs", + :updated => nil, + :options => nil + ) + @provider = Chef::Provider::Package::Zypper.new(@node, @new_resource) + @status = mock("Status", :exitstatus => 0) + @provider.stub!(:popen4).and_return(@status) + end + + it "should run zypper remove with the package name" do + @provider.should_receive(:run_command).with({ + :command => "zypper -n --no-gpg-checks remove emacs=1.0", + }) + @provider.remove_package("emacs", "1.0") + end + + # it "should run apt-get remove with the package name and options if specified" do + # @provider.should_receive(:run_command_with_systems_locale).with({ + # :command => "apt-get -q -y --force-yes remove emacs", + # :environment => { + # "DEBIAN_FRONTEND" => "noninteractive" + # } + # }) + # @new_resource.stub!(:options).and_return("--force-yes") + # + # @provider.remove_package("emacs", "1.0") + # end +end + +describe Chef::Provider::Package::Zypper, "purge_package" do + before(:each) do + @node = mock("Chef::Node", :null_object => true) + @new_resource = mock("Chef::Resource::Package", + :null_object => true, + :name => "emacs", + :version => nil, + :package_name => "emacs", + :updated => nil, + :options => nil + ) + @provider = Chef::Provider::Package::Zypper.new(@node, @new_resource) + @status = mock("Status", :exitstatus => 0) + @provider.stub!(:popen4).and_return(@status) + end + it "should run remove_package with the name and version" do + @provider.should_receive(:remove_package).with("emacs", "1.0") + @provider.purge_package("emacs", "1.0") + end + + + # it "should run apt-get purge with the package name and options if specified" do + # @provider.should_receive(:run_command_with_systems_locale).with({ + # :command => "apt-get -q -y --force-yes purge emacs", + # }) + # @new_resource.stub!(:options).and_return("--force-yes") + # + # @provider.purge_package("emacs", "1.0") + # end +end + +#describe Chef::Provider::Package::Zypper, "preseed_package" do +# before(:each) do +# @node = mock("Chef::Node", :null_object => true) +# @new_resource = mock("Chef::Resource::Package", +# :null_object => true, +# :name => "emacs", +# :version => nil, +# :package_name => "emacs", +# :updated => nil, +# :response_file => "emacs-10.seed" +# ) +# @provider = Chef::Provider::Package::Apt.new(@node, @new_resource) +# @provider.stub!(:get_preseed_file).and_return("/tmp/emacs-10.seed") +# @provider.stub!(:run_command_with_systems_locale).and_return(true) +# end +# +# it "should get the full path to the preseed response file" do +# @provider.should_receive(:get_preseed_file).with("emacs", "10").and_return("/tmp/emacs-10.seed") +# @provider.preseed_package("emacs", "10") +# end +# +# it "should run debconf-set-selections on the preseed file if it has changed" do +# @provider.should_receive(:run_command_with_systems_locale).with({ +# :command => "debconf-set-selections /tmp/emacs-10.seed", +# :environment => { +# "DEBIAN_FRONTEND" => "noninteractive" +# } +# }).and_return(true) +# @provider.preseed_package("emacs", "10") +# end +# +# it "should not run debconf-set-selections if the preseed file has not changed" do +# @provider.stub!(:get_preseed_file).and_return(false) +# @provider.should_not_receive(:run_command_with_systems_locale) +# @provider.preseed_package("emacs", "10") +# end +#end From ed0e3653953160026b155030f858d57a25f63266 Mon Sep 17 00:00:00 2001 From: Mario Giammarco Date: Thu, 22 Oct 2009 12:14:47 +0200 Subject: [PATCH 4/4] Added init.d script example for suse --- chef/distro/suse/etc/init.d/chef-client | 121 ++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 chef/distro/suse/etc/init.d/chef-client diff --git a/chef/distro/suse/etc/init.d/chef-client b/chef/distro/suse/etc/init.d/chef-client new file mode 100755 index 00000000000..5c42230c120 --- /dev/null +++ b/chef/distro/suse/etc/init.d/chef-client @@ -0,0 +1,121 @@ +#! /bin/sh +# Copyright (c) 1995-2003 SuSE Linux AG, Nuernberg, Germany. +# All rights reserved. +# +# Author: Mario Giammarco +# +### BEGIN INIT INFO +# Provides: chef-client +# Required-Start: $remote_fs $syslog $named +# Required-Stop: $remote_fs $syslog +# Default-Start: 3 5 +# Default-Stop: 0 1 2 6 +# Short-Description: Chef client +# Description: Starts chef client +### END INIT INFO + +# First reset status of this service +. /etc/rc.status +rc_reset + +# Return values acc. to LSB for all commands but status: +# 0 - success +# 1 - generic or unspecified error +# 2 - invalid or excess argument(s) +# 3 - unimplemented feature (e.g. "reload") +# 4 - insufficient privilege +# 5 - program is not installed +# 6 - program is not configured +# 7 - program is not running + +# set default options +CHEF_CONF="/etc/chef/client.rb" +if [ ! -f ${CHEF_CONF} ]; then + echo -n "Chef client configuration file, ${CHEF_CONF} does not exist." + # Tell the user this has skipped + rc_status -s + exit 6 +fi + +CHEF_BIN="/usr/bin/chef-client" +if [ ! -x ${CHEF_BIN} ]; then + echo -n "Chef client, ${CHEF_BIN} not installed!" + rc_status -s + exit 5 +fi + +CHEF_LOGFILE=/var/log/chef/chef-client.log +CHEF_OPTIONS="" + +# set default PID variables +CHEF_PID="/var/run/chef/chef-client.pid" +CHEF_PID_NOPREFIX="/var/run/chef/chef-client.pid" + +function chef_is_running() { + $0 status >/dev/null +} + +case "$1" in + start) + + echo -n "Starting chef-client" + + startproc $CHEF_BIN -d -c "$CHEF_CONF" -L "$CHEF_LOGFILE" "$CHEF_OPTIONS" ">/dev/null" + + rc_status -v + ;; + stop) + echo -n "Shutting down chef client" + killproc -p ${CHEF_PID} -TERM $CHEF_BIN + rc_status -v + rm -f ${CHEF_PID} 2>/dev/null + ;; + try-restart) + $0 status + if test $? = 0; then + $0 restart $2 + else + rc_reset # Not running is not a failure. + fi + # Remember status and be quiet + rc_status + ;; + restart) + $0 stop + $0 start $2 + rc_status + ;; + try-restart-iburst) + $0 status + if test $? = 0; then + $0 stop + $0 start iburst + else + rc_reset # Not running is not a failure. + fi + # Remember status and be quiet + rc_status + ;; + force-reload) + # Does not support signalling to reload + $0 try-restart + rc_status + ;; + reload) + echo -n "Reload chef-client" + # Does not support signalling to reload + rc_failed 3 + rc_status -v + ;; + status) + checkproc -p ${CHEF_PID} $CHEF_BIN + echo -n "Checking for chef-client " + checkproc -p ${CHEF_PID} $CHEF_BIN + rc_status -v + ;; + *) + echo "Usage: $0 {start|stop|status|try-restart|restart|try-restart-iburst|force-reload|reload|addserver|ntptimeset}" + exit 1 + ;; +esac +rc_exit