ref: refs/heads/master
commit 51c4d88d32d55c56e38a96fd782310e8890e41ea
Author: Jiri Suchomel
Date: Mon Aug 17 10:01:21 2009 +0200
custom-services rest plugin (originaly services plugin for fate 306696)
---
plugins/custom_services/.gitignore | 3 +
plugins/custom_services/MIT-LICENSE | 20 +++++
plugins/custom_services/README | 7 ++
plugins/custom_services/Rakefile | 10 +++
.../app/controllers/custom_services_controller.rb | 54 +++++++++++++
.../custom_services/app/models/custom_service.rb | 70 ++++++++++++++++
.../app/views/custom_services/index.html.erb | 1 +
.../app/views/custom_services/index.json.erb | 1 +
.../app/views/custom_services/index.xml.erb | 1 +
.../app/views/custom_services/show.html.erb | 1 +
.../app/views/custom_services/show.json.erb | 1 +
.../app/views/custom_services/show.xml.erb | 1 +
plugins/custom_services/config/rails_parent.rb | 15 ++++
.../config/resources/custom_services.yml | 2 +
plugins/custom_services/init.rb | 1 +
plugins/custom_services/install.rb | 1 +
.../org.opensuse.yast.modules.yapi.services.policy | 39 +++++++++
.../package/yast2-webservice-services.changes | 16 ++++
.../package/yast2-webservice-services.spec | 83 ++++++++++++++++++++
.../tasks/custom_services_tasks.rake | 6 ++
.../functional/custom_services_controller_test.rb | 51 ++++++++++++
plugins/custom_services/test/test_helper.rb | 52 ++++++++++++
plugins/custom_services/uninstall.rb | 1 +
23 files changed, 437 insertions(+), 0 deletions(-)
diff --git a/plugins/custom_services/.gitignore b/plugins/custom_services/.gitignore
new file mode 100644
index 0000000..945bd79
--- /dev/null
+++ b/plugins/custom_services/.gitignore
@@ -0,0 +1,3 @@
+package/www/
+package/www.tar.bz2
+coverage
diff --git a/plugins/custom_services/MIT-LICENSE b/plugins/custom_services/MIT-LICENSE
new file mode 100644
index 0000000..9376605
--- /dev/null
+++ b/plugins/custom_services/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 [name of plugin creator]
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
diff --git a/plugins/custom_services/README b/plugins/custom_services/README
new file mode 100644
index 0000000..d8a5fa3
--- /dev/null
+++ b/plugins/custom_services/README
@@ -0,0 +1,7 @@
+Services
+=========
+
+REST interface for handling services.
+
+
+Copyright (c) 2009 Novell, released under the MIT license
diff --git a/plugins/custom_services/Rakefile b/plugins/custom_services/Rakefile
new file mode 100644
index 0000000..ca63e76
--- /dev/null
+++ b/plugins/custom_services/Rakefile
@@ -0,0 +1,10 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require File.join(File.dirname(__FILE__), 'config', 'rails_parent')
+require File.join(RailsParent.parent, 'config', 'boot')
+require 'tasks/rails'
+
+desc 'Default: run unit tests.'
+task :default => :test
+
diff --git a/plugins/custom_services/app/controllers/custom_services_controller.rb b/plugins/custom_services/app/controllers/custom_services_controller.rb
new file mode 100644
index 0000000..69e76f7
--- /dev/null
+++ b/plugins/custom_services/app/controllers/custom_services_controller.rb
@@ -0,0 +1,54 @@
+require 'custom_service'
+
+class CustomServicesController < ApplicationController
+ before_filter :login_required
+
+ def index
+ unless permission_check("org.opensuse.yast.modules.yapi.services.read")
+ render ErrorResult.error(403, 1, "no permission") and return
+ end
+ @services = CustomService.find_all
+ end
+
+ # Shows service status. Requires read permission for services YaPI.
+ def show
+ unless permission_check("org.opensuse.yast.modules.yapi.services.get")
+ render ErrorResult.error( 403, 1, "no permission" ) and return
+ end
+
+ id = params[:id]
+ begin
+ @service = CustomService.find id
+ rescue Exception => e # Don't fail on non-existing service. Should be more specific.
+ logger.debug e
+ render ErrorResult.error(404, 106, "no such service") and return
+ end
+ logger.debug "show@service #{@service.inspect}"
+ end
+
+ # PUT /services/1.xml
+ def update
+
+ unless permission_check( "org.opensuse.yast.modules.yapi.services.execute")
+ render ErrorResult.error(403, 1, "no permission") and return
+ end
+
+ begin
+ @service = CustomService.find params[:id]
+ rescue Exception => e
+ logger.debug e
+ render ErrorResult.error(404, 106, "no such service") and return
+ end
+
+ ret = {}
+ begin
+ ret = @service.save(params[:execute])
+ rescue Exception => e
+ logger.debug e
+ render ErrorResult.error(404, @error_id, @error_string) and return
+ end
+
+ render :xml => ret
+ end
+
+end
diff --git a/plugins/custom_services/app/models/custom_service.rb b/plugins/custom_services/app/models/custom_service.rb
new file mode 100644
index 0000000..26ce7ef
--- /dev/null
+++ b/plugins/custom_services/app/models/custom_service.rb
@@ -0,0 +1,70 @@
+require 'yast_service'
+
+class CustomService
+
+ attr_accessor :name
+ attr_accessor :status
+
+ def initialize
+ end
+
+ # services = CustomService.find_all
+ def self.find_all
+
+ services = []
+ yapi_ret = YastService.Call("YaPI::SERVICES::Read")
+
+Rails.logger.debug "reading YAPI ----------------------------"
+
+ if yapi_ret.nil?
+ raise "Can't get services list"
+ else
+ yapi_ret.each do |s|
+ service = CustomService.new
+ service.name = s["name"]
+ service.status = s["status"]
+ Rails.logger.debug "service: #{service.inspect}"
+ services << service
+ end
+ end
+ services
+ end
+
+ # load the status of the service
+ def self.find(id)
+ yapi_ret = YastService.Call("YaPI::SERVICES::Get", id)
+
+ raise "Got no data while loading service" if yapi_ret.empty?
+
+ service = CustomService.new
+ service.name = yapi_ret["name"]
+ service.status = yapi_ret["status"]
+
+ Rails.logger.debug service.inspect
+ service
+ end
+
+
+ def save(cmd)
+ ret = YastService.Call("YaPI::SERVICES::Execute", self.name, cmd)
+
+ Rails.logger.debug "Command returns: #{ret.inspect}"
+ ret
+ end
+
+ def to_xml( options = {} )
+ xml = options[:builder] ||= Builder::XmlMarkup.new(options)
+ xml.instruct! unless options[:skip_instruct]
+
+ xml.service do
+ xml.tag!(:name, name )
+ xml.tag!(:status, status, {:type => "integer"} )
+ end
+ end
+
+ def to_json( options = {} )
+ hash = Hash.from_xml(to_xml())
+ return hash.to_json
+ end
+
+end
diff --git a/plugins/custom_services/app/views/custom_services/index.html.erb b/plugins/custom_services/app/views/custom_services/index.html.erb
new file mode 100644
index 0000000..70578e8
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/index.html.erb
@@ -0,0 +1 @@
+<%= @services.to_xml( :root => "services", :dasherize => false ) -%>
\ No newline at end of file
diff --git a/plugins/custom_services/app/views/custom_services/index.json.erb b/plugins/custom_services/app/views/custom_services/index.json.erb
new file mode 100644
index 0000000..70578e8
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/index.json.erb
@@ -0,0 +1 @@
+<%= @services.to_xml( :root => "services", :dasherize => false ) -%>
\ No newline at end of file
diff --git a/plugins/custom_services/app/views/custom_services/index.xml.erb b/plugins/custom_services/app/views/custom_services/index.xml.erb
new file mode 100644
index 0000000..70578e8
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/index.xml.erb
@@ -0,0 +1 @@
+<%= @services.to_xml( :root => "services", :dasherize => false ) -%>
\ No newline at end of file
diff --git a/plugins/custom_services/app/views/custom_services/show.html.erb b/plugins/custom_services/app/views/custom_services/show.html.erb
new file mode 100644
index 0000000..d26d152
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/show.html.erb
@@ -0,0 +1 @@
+<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/custom_services/app/views/custom_services/show.json.erb b/plugins/custom_services/app/views/custom_services/show.json.erb
new file mode 100644
index 0000000..d26d152
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/show.json.erb
@@ -0,0 +1 @@
+<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/custom_services/app/views/custom_services/show.xml.erb b/plugins/custom_services/app/views/custom_services/show.xml.erb
new file mode 100644
index 0000000..d26d152
--- /dev/null
+++ b/plugins/custom_services/app/views/custom_services/show.xml.erb
@@ -0,0 +1 @@
+<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/custom_services/config/rails_parent.rb b/plugins/custom_services/config/rails_parent.rb
new file mode 100644
index 0000000..b869908
--- /dev/null
+++ b/plugins/custom_services/config/rails_parent.rb
@@ -0,0 +1,15 @@
+class RailsParent
+
+ def RailsParent.parent
+ parent = ENV["RAILS_PARENT"]
+ unless parent
+ parent = File.expand_path(File.join('..','..','..', 'webservice'), File.dirname(__FILE__))
+ unless File.directory?( parent || "" )
+ $stderr.puts "Nope: #{parent}\nPlease set RAILS_PARENT environment"
+ exit 1
+ end
+ end
+ parent
+ end
+
+end
diff --git a/plugins/custom_services/config/resources/custom_services.yml b/plugins/custom_services/config/resources/custom_services.yml
new file mode 100644
index 0000000..bc89d56
--- /dev/null
+++ b/plugins/custom_services/config/resources/custom_services.yml
@@ -0,0 +1,2 @@
+interface: org.opensuse.yast.modules.yapi.services
+controller: custom_services
diff --git a/plugins/custom_services/init.rb b/plugins/custom_services/init.rb
new file mode 100644
index 0000000..3c19a74
--- /dev/null
+++ b/plugins/custom_services/init.rb
@@ -0,0 +1 @@
+# Include hook code here
diff --git a/plugins/custom_services/install.rb b/plugins/custom_services/install.rb
new file mode 100644
index 0000000..f7732d3
--- /dev/null
+++ b/plugins/custom_services/install.rb
@@ -0,0 +1 @@
+# Install hook code here
diff --git a/plugins/custom_services/package/org.opensuse.yast.modules.yapi.services.policy b/plugins/custom_services/package/org.opensuse.yast.modules.yapi.services.policy
new file mode 100644
index 0000000..0f5d8ab
--- /dev/null
+++ b/plugins/custom_services/package/org.opensuse.yast.modules.yapi.services.policy
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE policyconfig PUBLIC "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
+
+<policyconfig>
+ <vendor>Novell, Inc.</vendor>
+ http://www.novell.com
+
+ <action id="org.opensuse.yast.modules.yapi.services.read">
+ <description>Get list of services.</description>
+ <message>System policy prevents the Yast DBus service from reading service list.</message>
+ <defaults>
+ no
+ no
+ auth_admin_keep_session
+ </defaults>
+ </action>
+
+ <action id="org.opensuse.yast.modules.yapi.services.get">
+ <description>Get the information about given service.</description>
+ <message>System policy prevents the Yast DBus service from reading service info.</message>
+ <defaults>
+ no
+ no
+ auth_admin_keep_session
+ </defaults>
+ </action>
+
+ <action id="org.opensuse.yast.modules.yapi.services.execute">
+ <description>Starting/stopping services.</description>
+ <message>System policy prevents the Yast DBus service from executing service script.</message>
+ <defaults>
+ no
+ no
+ auth_admin_keep_session
+ </defaults>
+ </action>
+</policyconfig>
+
diff --git a/plugins/custom_services/package/yast2-webservice-services.changes b/plugins/custom_services/package/yast2-webservice-services.changes
new file mode 100644
index 0000000..c1709a4
--- /dev/null
+++ b/plugins/custom_services/package/yast2-webservice-services.changes
@@ -0,0 +1,16 @@
+-------------------------------------------------------------------
+Wed Jun 24 10:40:44 CEST 2009 - mvidner@suse.cz
+
+- /services shows all services, not just fakes (but avoids "halt")
+- /services no longer includes command info, /services/FOO does.
+- /services is now sorted, disable by ?sort=0
+- /commands/update:
+ * call /etc/init.d/FOO, /usr/sbin/rcFOO is nonstandard
+ * always return exit+stdout+stderr
+- 0.0.3
+
+-------------------------------------------------------------------
+Fri Apr 17 11:50:12 CEST 2009 - schubi@suse.de
+
+- splitted off from yast2-webservice
+
diff --git a/plugins/custom_services/package/yast2-webservice-services.spec b/plugins/custom_services/package/yast2-webservice-services.spec
new file mode 100644
index 0000000..512f2a8
--- /dev/null
+++ b/plugins/custom_services/package/yast2-webservice-services.spec
@@ -0,0 +1,83 @@
+#
+# spec file for package yast2-webservice-services (Version 0.1)
+#
+# Copyright (c) 2008 SUSE LINUX Products GmbH, Nuernberg, Germany.
+# This file and all modifications and additions to the pristine
+# package are under the same license as the package itself.
+#
+# Please submit bugfixes or comments via http://bugs.opensuse.org/
+#
+
+
+Name: yast2-webservice-services
+PreReq: yast2-webservice
+Provides: yast2-webservice:/srv/www/yastws/app/controllers/services_controller.rb
+License: MIT
+Group: Productivity/Networking/Web/Utilities
+Autoreqprov: on
+Version: 0.0.3
+Release: 0
+Summary: YaST2 - Webservice - Services
+Source: www.tar.bz2
+Source1: org.opensuse.yast.system.services.policy
+BuildRoot: %{_tmppath}/%{name}-%{version}-build
+BuildArch: noarch
+
+#
+%define pkg_user yastws
+%define plugin_name services
+#
+
+
+%description
+YaST2 - Webservice - REST based interface of YaST in order to handle services.
+Authors:
+--------
+ Stefan Schubert
+
+%prep
+%setup -q -n www
+
+%build
+
+%install
+
+#
+# Install all web and frontend parts.
+#
+mkdir -p $RPM_BUILD_ROOT/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}
+cp -a * $RPM_BUILD_ROOT/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}
+
+# Policies
+mkdir -p $RPM_BUILD_ROOT/usr/share/PolicyKit/policy
+install -m 0644 %SOURCE1 $RPM_BUILD_ROOT/usr/share/PolicyKit/policy/
+
+%clean
+rm -rf $RPM_BUILD_ROOT
+
+%post
+#
+# granting all permissions for root
+#
+/etc/yastws/tools/policyKit-rights.rb --user root --action grant >& /dev/null || :
+
+%files
+%defattr(-,root,root)
+%dir /srv/www/%{pkg_user}
+%dir /srv/www/%{pkg_user}/vendor
+%dir /srv/www/%{pkg_user}/vendor/plugins
+%dir /srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}
+%dir /usr/share/PolicyKit
+%dir /usr/share/PolicyKit/policy
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/MIT-LICENSE
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/README
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/Rakefile
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/init.rb
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/install.rb
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/uninstall.rb
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/app
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/config
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/lib
+/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/tasks
+#/srv/www/%{pkg_user}/vendor/plugins/%{plugin_name}/test
+%attr(644,root,root) /usr/share/PolicyKit/policy/org.opensuse.yast.system.%{plugin_name}.policy
diff --git a/plugins/custom_services/tasks/custom_services_tasks.rake b/plugins/custom_services/tasks/custom_services_tasks.rake
new file mode 100644
index 0000000..aaab452
--- /dev/null
+++ b/plugins/custom_services/tasks/custom_services_tasks.rake
@@ -0,0 +1,6 @@
+begin
+ require 'tasks/webservice'
+rescue LoadError => e
+ $stderr.puts "Install rubygem-yast2-webservice-tasks.rpm"
+end
+
diff --git a/plugins/custom_services/test/functional/custom_services_controller_test.rb b/plugins/custom_services/test/functional/custom_services_controller_test.rb
new file mode 100644
index 0000000..740d1aa
--- /dev/null
+++ b/plugins/custom_services/test/functional/custom_services_controller_test.rb
@@ -0,0 +1,51 @@
+require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
+require 'test/unit'
+require 'rubygems'
+require 'mocha'
+
+
+class CustomServicesControllerTest < ActionController::TestCase
+ fixtures :accounts
+ def setup
+ @controller = CustomServicesController.new
+ @request = ActionController::TestRequest.new
+ # http://railsforum.com/viewtopic.php?id=1719
+ @request.session[:account_id] = 1 # defined in fixtures
+
+ s1 = CustomService.new
+ s1.name = "foo"
+ s1.status = 0
+
+ s2 = CustomService.new
+ s2.name = "cron"
+ s2.status = 1
+
+ CustomService.stubs(:find_all).returns([s1, s2])
+ CustomService.stubs(:find).with("cron").returns([s2])
+ end
+
+ test "access index" do
+ get :index
+ assert_response :success
+ end
+
+ test "access index xml" do
+ mime = Mime::XML
+ @request.accept = mime.to_s
+ get :index, :format => :xml
+ assert_equal mime.to_s, @response.content_type
+ end
+
+ test "access index json" do
+ mime = Mime::JSON
+ @request.accept = mime.to_s
+ get :index, :format => :json
+ assert_equal mime.to_s, @response.content_type
+ end
+
+ test "access show" do
+ get :show, :id =>"cron"
+ assert_response :success
+ end
+
+end
diff --git a/plugins/custom_services/test/test_helper.rb b/plugins/custom_services/test/test_helper.rb
new file mode 100644
index 0000000..7d67910
--- /dev/null
+++ b/plugins/custom_services/test/test_helper.rb
@@ -0,0 +1,52 @@
+# find the rails parent
+require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent')
+# first config rails
+require File.expand_path( File.join("config","environment"), RailsParent.parent )
+# then enable testing, this will get the routing right
+ENV["RAILS_ENV"] = "test"
+require 'test_help'
+
+class ActiveSupport::TestCase
+ # Transactional fixtures accelerate your tests by wrapping each test method
+ # in a transaction that's rolled back on completion. This ensures that the
+ # test database remains unchanged so your fixtures don't have to be reloaded
+ # between every test method. Fewer database queries means faster tests.
+ #
+ # Read Mike Clark's excellent walkthrough at
+ # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
+ #
+ # Every Active Record database supports transactions except MyISAM tables
+ # in MySQL. Turn off transactional fixtures in this case; however, if you
+ # don't care one way or the other, switching from MyISAM to InnoDB tables
+ # is recommended.
+ #
+ # The only drawback to using transactional fixtures is when you actually
+ # need to test transactions. Since your test is bracketed by a transaction,
+ # any transactions started in your code will be automatically rolled back.
+ self.use_transactional_fixtures = true
+
+ # Instantiated fixtures are slow, but give you @david where otherwise you
+ # would need people(:david). If you don't want to migrate your existing
+ # test cases which use the @david style and don't mind the speed hit (each
+ # instantiated fixtures translates to a database query per test method),
+ # then set this back to true.
+ self.use_instantiated_fixtures = false
+
+ # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
+ #
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
+ # -- they do not yet inherit this setting
+ fixtures :all
+
+ # Add more helper methods to be used by all tests here...
+
+ # See http://pennysmalls.com/2009/03/04/rails-23-breakage-and-fixage/
+ def clean_backtrace(&block)
+ yield
+ rescue ActiveSupport::TestCase::Assertion => error
+ framework_path = Regexp.new(File.expand_path("#{File.dirname(__FILE__)}/assertions"))
+ error.backtrace.reject! { |line| File.expand_path(line) =~ framework_path }
+ raise
+ end
+
+end
diff --git a/plugins/custom_services/uninstall.rb b/plugins/custom_services/uninstall.rb
new file mode 100644
index 0000000..9738333
--- /dev/null
+++ b/plugins/custom_services/uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org