ref: refs/heads/master
commit afc94befa4149ac4982f7cb67368b94dbfcdb0ac
Author: Jiri Suchomel
Date: Wed Nov 4 16:22:22 2009 +0100
- adapted to changes in SERVICES.pm: new API for Execute,
use YaPI for all services (bnc#551276)
- include YML.rb for parsing yml files from YaST
- require yast2-ruby-bindings
---
.../app/controllers/services_controller.rb | 5 +-
plugins/services/app/models/service.rb | 90 ++++++------------
plugins/services/package/YML.rb | 13 +++
.../package/yast2-webservice-services.changes | 8 ++
.../package/yast2-webservice-services.spec | 13 +++-
plugins/services/test/unit/services_test.rb | 100 +++++++++----------
.../unit/vendor_config/invalid/custom_services.yml | 3 -
.../unit/vendor_config/valid/custom_services.yml | 6 -
8 files changed, 112 insertions(+), 126 deletions(-)
diff --git a/plugins/services/app/controllers/services_controller.rb b/plugins/services/app/controllers/services_controller.rb
index e293071..f41ef23 100644
--- a/plugins/services/app/controllers/services_controller.rb
+++ b/plugins/services/app/controllers/services_controller.rb
@@ -25,7 +25,7 @@ class ServicesController < ApplicationController
@service = Service.new(params[:id])
begin
- @service.read_status
+ @service.read_status(params)
rescue Exception => e
render ErrorResult.error(404, 108, e.to_s) and return
end
@@ -40,6 +40,7 @@ class ServicesController < ApplicationController
# Execute service command (start or stop).
# Requires execute permission for services YaPI.
def update
+logger.debug "------------ update params: #{params.inspect}"
yapi_perm_check "services.execute"
begin
@service = Service.find params[:id]
@@ -49,7 +50,7 @@ class ServicesController < ApplicationController
end
begin
- ret = @service.save(params[:execute])
+ ret = @service.save(params)
rescue Exception => e
logger.debug e
render ErrorResult.error(404, @error_id, @error_string) and return
diff --git a/plugins/services/app/models/service.rb b/plugins/services/app/models/service.rb
index a3d7efb..c8d0c9e 100644
--- a/plugins/services/app/models/service.rb
+++ b/plugins/services/app/models/service.rb
@@ -1,11 +1,9 @@
-require "scr"
require 'yast_service'
-require 'yast/config_file'
# = Service model
# Proviceds access to configured services.
-# Uses YaPI for accessing standard system services (/etc/init.d) and
-# Scr library to access vendor specific services (defined in config file).
+# Uses YaPI for accessing standard system services (/etc/init.d)
+# and vendor specific services (defined in config file).
class Service
attr_accessor :name
@@ -47,42 +45,25 @@ class Service
services = []
read_status = params.has_key?("read_status")
- if params.has_key?("custom")
- begin
- cfg = YaST::ConfigFile.new(:custom_services)
- cfg.each do |name, s|
- service = Service.new(name)
- if read_status and s.has_key?("status")
- ret = Scr.instance.execute([s["status"]])
- service.status = ret[:exit]
- end
- if s.has_key?("description")
- service.description = s["description"]
- end
- Rails.logger.debug "custom service: #{service.inspect}"
- services << service
- end
- rescue Exception => e
- Rails.logger.error e
- end
- else
- rl = current_runlevel
- params = {
- "runlevel" => [ "i", rl ],
- "read_status" => [ "b", read_status]
- }
- yapi_ret = YastService.Call("YaPI::SERVICES::Read", params)
-
- if yapi_ret.nil?
+ rl = current_runlevel
+ args = {
+ "runlevel" => [ "i", rl ],
+ "read_status" => [ "b", read_status],
+ "custom" => [ "b", params.has_key?("custom")]
+ }
+
+ yapi_ret = YastService.Call("YaPI::SERVICES::Read", args)
+
+ if yapi_ret.nil?
raise "Can't get services list"
- else
- yapi_ret.each do |s|
+ else
+ yapi_ret.each do |s|
service = Service.new(s["name"])
service.status= s["status"] if s.has_key?("status")
+ service.description = s["description"] if s.has_key?("description")
Rails.logger.debug "service: #{service.inspect}"
services << service
end
- end
end
services
end
@@ -93,36 +74,23 @@ class Service
end
# load the status of the service
- def read_status
- @status = save('status')[:exit]
+ def read_status(params)
+ args = {
+ "execute" => 'status',
+ }
+ args["custom"] = 1 if params.has_key?("custom")
+ @status = save(args)[:exit]
end
# execute a service command (start, stop, ...)
- def save(cmd)
-
- begin
- cfg = YaST::ConfigFile.new(:custom_services)
- custom_service = cfg[self.name]
- rescue YaST::ConfigFile::NotFoundError
- Rails.logger.debug "No custom service defined"
- custom_service = nil
- rescue Exception => e
- Rails.logger.error "looking for service #{self.name}: #{e}"
- return { :stderr => e }
- end
-
- if custom_service.blank?
- Rails.logger.debug "no custom command found, calling YaPI..."
- ret = YastService.Call("YaPI::SERVICES::Execute", self.name, cmd)
- else
- if custom_service.has_key?(cmd) and !custom_service[cmd].blank?
- command = custom_service[cmd]
- Rails.logger.debug "Service commmand #{command}"
- ret = Scr.instance.execute([command])
- else
- raise Exception.new("Missing custom command to '#{cmd}' command")
- end
- end
+ def save(params)
+
+ args = {
+ "name" => [ "s", self.name ],
+ "action" => [ "s", params["execute"] ],
+ "custom" => [ "b", params.has_key?("custom") ]
+ }
+ ret = YastService.Call("YaPI::SERVICES::Execute", args)
Rails.logger.debug "Command returns: #{ret.inspect}"
ret.symbolize_keys!
diff --git a/plugins/services/package/YML.rb b/plugins/services/package/YML.rb
new file mode 100644
index 0000000..1c367d4
--- /dev/null
+++ b/plugins/services/package/YML.rb
@@ -0,0 +1,13 @@
+require 'yaml'
+
+# simple module for parsimg yaml files
+module YML
+ def self.parse(file_name)
+ ret = {}
+ if File.exists?(file_name)
+ ret = YAML::load(File.open(file_name));
+ ret = {} unless ret.is_a? Hash
+ end
+ return ret
+ end
+end
diff --git a/plugins/services/package/yast2-webservice-services.changes b/plugins/services/package/yast2-webservice-services.changes
index 2d8eee6..9c52dd1 100644
--- a/plugins/services/package/yast2-webservice-services.changes
+++ b/plugins/services/package/yast2-webservice-services.changes
@@ -1,4 +1,12 @@
-------------------------------------------------------------------
+Wed Nov 4 14:03:24 CET 2009 - jsuchome@suse.cz
+
+- adapted to changes in SERVICES.pm: new API for Execute,
+ use YaPI for all services (bnc#551276)
+- include YML.rb for parsing yml files from YaST
+- 0.0.11
+
+-------------------------------------------------------------------
Fri Oct 30 08:04:50 CET 2009 - lslezak@suse.cz
- fixed restdoc packaging
diff --git a/plugins/services/package/yast2-webservice-services.spec b/plugins/services/package/yast2-webservice-services.spec
index 534fea5..b88d6c5 100644
--- a/plugins/services/package/yast2-webservice-services.spec
+++ b/plugins/services/package/yast2-webservice-services.spec
@@ -15,15 +15,19 @@ Provides: yast2-webservice:/srv/www/yastws/app/controllers/services_contro
License: GPL v2 only
Group: Productivity/Networking/Web/Utilities
Autoreqprov: on
-Version: 0.0.10
+Version: 0.0.11
Release: 0
Summary: YaST2 - Webservice - Services
Source: www.tar.bz2
Source1: org.opensuse.yast.modules.yapi.services.policy
+Source2: YML.rb
BuildRoot: %{_tmppath}/%{name}-%{version}-build
BuildArch: noarch
BuildRequires: rubygem-yast2-webservice-tasks rubygem-restility
+# so SERVICES.pm is able to call YML.rb
+Requires: yast2-ruby-bindings >= 0.3.4
+
# YaPI/SERVICES.pm
%if 0%{?suse_version} == 0 || %suse_version > 1110
# 11.2 or newer
@@ -74,6 +78,10 @@ rm -f $RPM_BUILD_ROOT/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/COPYING
mkdir -p $RPM_BUILD_ROOT/usr/share/PolicyKit/policy
install -m 0644 %SOURCE1 $RPM_BUILD_ROOT/usr/share/PolicyKit/policy/
+# YML.rb
+mkdir -p $RPM_BUILD_ROOT/usr/share/YaST2/modules/
+cp %{SOURCE2} $RPM_BUILD_ROOT/usr/share/YaST2/modules/
+
%clean
rm -rf $RPM_BUILD_ROOT
@@ -91,6 +99,8 @@ rm -rf $RPM_BUILD_ROOT
%dir /srv/www/%{pkg_user}/vendor/plugins
%dir /srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}
%dir /srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/doc
+%dir /usr/share/YaST2/
+%dir /usr/share/YaST2/modules/
%dir /usr/share/PolicyKit
%dir /usr/share/PolicyKit/policy
/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/README
@@ -103,6 +113,7 @@ rm -rf $RPM_BUILD_ROOT
/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/tasks
/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/doc
/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/public
+/usr/share/YaST2/modules/YML.rb
%attr(644,root,root) %config /usr/share/PolicyKit/policy/org.opensuse.yast.modules.yapi.%{plugin_name}.policy
%doc COPYING
diff --git a/plugins/services/test/unit/services_test.rb b/plugins/services/test/unit/services_test.rb
index e959eeb..d1f8177 100644
--- a/plugins/services/test/unit/services_test.rb
+++ b/plugins/services/test/unit/services_test.rb
@@ -7,10 +7,6 @@ class ServiceTest < ActiveSupport::TestCase
def setup
end
- def vendor_config(name)
- File.join(File.dirname(__FILE__), 'vendor_config', name)
- end
-
test "read current runlevel" do
Service.stubs(:run_runlevel).returns("N 5")
@@ -33,7 +29,7 @@ class ServiceTest < ActiveSupport::TestCase
test "find_all nil parameter" do
Service.stubs(:run_runlevel).returns("N 5")
- YastService.stubs(:Call).with('YaPI::SERVICES::Read', {"runlevel" => [ "i", 5 ], "read_status" => [ "b", false]}).returns([])
+ YastService.stubs(:Call).with('YaPI::SERVICES::Read', {"runlevel" => [ "i", 5 ], "read_status" => [ "b", false], 'custom' => ['b', false]}).returns([])
ret = Service.find_all(nil)
assert ret == []
@@ -51,74 +47,72 @@ class ServiceTest < ActiveSupport::TestCase
srv = [{"name" => "acpid"}, {"name" => "dbus"}]
Service.stubs(:run_runlevel).returns("N 5")
- YastService.stubs(:Call).with('YaPI::SERVICES::Read', {"runlevel" => [ "i", 5 ], "read_status" => [ "b", false]}).returns(srv)
+ YastService.stubs(:Call).with('YaPI::SERVICES::Read', {"runlevel" => [ "i", 5 ], "read_status" => [ "b", false], 'custom' => ['b', false]}).returns(srv)
ret = Service.find_all(Hash.new)
assert ret.map {|s| s.name} == ['acpid', 'dbus']
end
- test "find custom service" do
- Service.stubs(:run_runlevel).returns("N 5")
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('valid'))
-
- ret = Service.find_all({"custom" => true})
- assert ret.map {|s| s.name} == ['vendor_service']
- end
-
- test "missing custom service" do
- Service.stubs(:run_runlevel).returns("N 5")
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('missing'))
- YastService.stubs(:Call).returns({})
-
- ret = Service.find_all({"custom" => true})
- assert ret.map {|s| s.name} == []
- end
-
- test "missing command in custom service" do
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('invalid'))
- YastService.stubs(:Call).returns({})
-
- s = Service.new('vendor_service')
- assert_raise Exception do
- s.save('status')
- end
- end
+# test "find custom service" do
+# Service.stubs(:run_runlevel).returns("N 5")
+# YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('valid'))
+#
+# ret = Service.find_all({"custom" => true})
+# assert ret.map {|s| s.name} == ['vendor_service']
+# end
+#
+# test "missing custom service" do
+# Service.stubs(:run_runlevel).returns("N 5")
+# YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('missing'))
+# YastService.stubs(:Call).returns({})
+#
+# ret = Service.find_all({"custom" => true})
+# assert ret.map {|s| s.name} == []
+# end
+#
+# test "missing command in custom service" do
+# YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('invalid'))
+# YastService.stubs(:Call).returns({})
+#
+# s = Service.new('vendor_service')
+# assert_raise Exception do
+# s.save('status')
+# end
+# end
test "check missing LSB service" do
ret = {"exit" => "127", "stderr" => "", "stdout" => "sh: line 1: /etc/init.d/non_existing_service: No such file or directory\n"}
- YastService.stubs(:Call).with('YaPI::SERVICES::Execute', 'non_existing_service', 'status').returns(ret)
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('missing'))
+ YastService.stubs(:Call).with('YaPI::SERVICES::Execute', {"name" => [ "s", "non_existing_service" ], "action" => [ "s", "status"], 'custom' => ['b', false]}).returns(ret)
s = Service.new('non_existing_service')
- assert s.save('status') == ret
+ assert s.save({"execute" => 'status'}) == ret
end
test "check LSB service status" do
ret = {'exit' => '0'}
- YastService.stubs(:Call).with('YaPI::SERVICES::Execute', 'ntp', 'status').returns(ret)
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('missing'))
+ YastService.stubs(:Call).with('YaPI::SERVICES::Execute', {"name" => [ "s", "ntp" ], "action" => [ "s", "status"], 'custom' => ['b', false]}).returns(ret)
s = Service.new('ntp')
- assert s.save('status') == ret
- s.read_status
+ assert s.save({"execute" => 'status'}) == ret
+ s.read_status({})
assert s.status == '0'
end
- test "check custom service status" do
- YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('valid'))
-
- # do not call introspection in the Scr constructor
- DBus::SystemBus.instance.stubs(:introspect).returns('<node><interface name="org.opensuse.yast.SCR.Methods"></interface></node>')
-
- ret = {'exit' => '0', 'stderr' => '', 'stdout' => "Checking for service collectd ..running\n"}
- Scr.instance.stubs(:execute).with(['/usr/sbin/rccollectd status']).returns(ret)
-
- s = Service.new('vendor_service')
- assert s.save("status") == ret
- s.read_status
- assert s.status == '0'
- end
+# test "check custom service status" do
+# YaST::ConfigFile.stubs(:config_default_location).returns(vendor_config('valid'))
+#
+# # do not call introspection in the Scr constructor
+# DBus::SystemBus.instance.stubs(:introspect).returns('<node><interface name="org.opensuse.yast.SCR.Methods"></interface></node>')
+#
+# ret = {'exit' => '0', 'stderr' => '', 'stdout' => "Checking for service collectd ..running\n"}
+# Scr.instance.stubs(:execute).with(['/usr/sbin/rccollectd status']).returns(ret)
+#
+# s = Service.new('vendor_service')
+# assert s.save("status") == ret
+# s.read_status
+# assert s.status == '0'
+# end
end
diff --git a/plugins/services/test/unit/vendor_config/invalid/custom_services.yml b/plugins/services/test/unit/vendor_config/invalid/custom_services.yml
deleted file mode 100644
index 755c63e..0000000
--- a/plugins/services/test/unit/vendor_config/invalid/custom_services.yml
+++ /dev/null
@@ -1,3 +0,0 @@
-# This an invalid example of vendor config (missing commands)
-vendor_service:
- description: Statistics Collection Daemon for filling RRD Files
diff --git a/plugins/services/test/unit/vendor_config/valid/custom_services.yml b/plugins/services/test/unit/vendor_config/valid/custom_services.yml
deleted file mode 100644
index 1b91373..0000000
--- a/plugins/services/test/unit/vendor_config/valid/custom_services.yml
+++ /dev/null
@@ -1,6 +0,0 @@
-# This a valid example of vendor config
-vendor_service:
- description: Statistics Collection Daemon for filling RRD Files
- start: "/usr/sbin/rccollectd start"
- stop: "/usr/sbin/rccollectd stop"
- status: "/usr/sbin/rccollectd status"
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org