Mailinglist Archive: yast-commit (725 mails)

< Previous Next >
[yast-commit] <rest-service> master : Added system REST service
  • From: Ladislav Slezak <lslezak@xxxxxxxxxx>
  • Date: Tue, 11 Aug 2009 09:49:31 +0200
  • Message-id: <E1ManuJ-0000XV-UG@xxxxxxxxxxxxxxxx>
ref: refs/heads/master
commit a536b362df354c1afab4f4b4cc41575a3af614ed
Author: Ladislav Slezak <lslezak@xxxxxxxxxx>
Date: Tue Aug 11 09:49:31 2009 +0200

Added system REST service

Initial commit
---
plugins/system/.gitignore | 3 +
plugins/system/MIT-LICENSE | 20 +++++
plugins/system/README | 6 ++
plugins/system/Rakefile | 11 +++
.../system/app/controllers/system_controller.rb | 46 ++++++++++
plugins/system/app/models/system.rb | 77 +++++++++++++++++
plugins/system/config/rails_parent.rb | 15 ++++
plugins/system/config/resources/system.yml | 3 +
plugins/system/init.rb | 1 +
plugins/system/install.rb | 1 +
.../system/package/yast2-webservice-system.changes | 5 +
.../system/package/yast2-webservice-system.spec | 87 ++++++++++++++++++++
plugins/system/test/test_helper.rb | 52 ++++++++++++
plugins/system/uninstall.rb | 1 +
14 files changed, 328 insertions(+), 0 deletions(-)

diff --git a/plugins/system/.gitignore b/plugins/system/.gitignore
new file mode 100644
index 0000000..945bd79
--- /dev/null
+++ b/plugins/system/.gitignore
@@ -0,0 +1,3 @@
+package/www/
+package/www.tar.bz2
+coverage
diff --git a/plugins/system/MIT-LICENSE b/plugins/system/MIT-LICENSE
new file mode 100644
index 0000000..658dd59
--- /dev/null
+++ b/plugins/system/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Novell, Inc.
+
+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/system/README b/plugins/system/README
new file mode 100644
index 0000000..12f8a12
--- /dev/null
+++ b/plugins/system/README
@@ -0,0 +1,6 @@
+System
+======
+
+REST interface for accessing low-level basic system components
+
+Copyright (c) 2009 Novell, released under the MIT license
diff --git a/plugins/system/Rakefile b/plugins/system/Rakefile
new file mode 100644
index 0000000..2c69676
--- /dev/null
+++ b/plugins/system/Rakefile
@@ -0,0 +1,11 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'rake/packagetask'
+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/system/app/controllers/system_controller.rb
b/plugins/system/app/controllers/system_controller.rb
new file mode 100644
index 0000000..8225706
--- /dev/null
+++ b/plugins/system/app/controllers/system_controller.rb
@@ -0,0 +1,46 @@
+
+class SystemController < ApplicationController
+
+ before_filter :login_required
+
+ def show
+ @actions = System.instance.actions
+
+ respond_to do |format|
+ format.html { render :xml => @actions.to_xml(:root => :actions),
:location => "none" } #return xml only
+ format.xml { render :xml => @actions.to_xml(:root => :actions),
:location => "none" }
+ format.json { render :json => @actions.to_json, :location => "none"
}
+ end
+ end
+
+ def update
+ root = params[:time]
+ if root == nil
+ render ErrorResult.error(404, 2, "format or internal error") and
return
+ end
+
+ @system = System.instance
+
+ # do the action
+ action = params.find { |k, v| v == true}
+
+ if not action.blank?
+ case action
+ when :reboot
+ @system.reboot
+ when :shutdown
+ @system.shutdown
+ else
+ render ErrorResult.error(404, 2, "format error") and return
+ end
+ end
+
+ render :show
+ end
+
+ # See update
+ def create
+ update
+ end
+
+end
diff --git a/plugins/system/app/models/system.rb
b/plugins/system/app/models/system.rb
new file mode 100644
index 0000000..e0f05d4
--- /dev/null
+++ b/plugins/system/app/models/system.rb
@@ -0,0 +1,77 @@
+
+require 'dbus'
+require 'singleton'
+
+
+class System
+
+ include Singleton
+
+ def initialize
+ @reboot = false
+ @shutdown = false
+ end
+
+ def actions
+ return {:reboot => @reboot, :shutdown => @shutdown}
+ end
+
+ def reboot
+ logger.debug "##### reboot"
+# if hal_power_management(:reboot)
+# @reboot = true
+# end
+ end
+
+ def shutdown
+ logger.debug "##### shutdown"
+# if hal_power_management(:shutdown)
+# @shutdown = true
+# end
+ end
+
+
+# private methods
+ private
+
+ def hal_power_management(action, params = {})
+
+ return false unless action == :reboot or action == :shutdown
+
+ begin
+ # connect to the system bus
+ system_bus = DBus::SystemBus.instance
+
+ # get the HAL service
+ hal_service = system_bus.service('org.freedesktop.Hal')
+
+ computer =
hal_service.object('/org/freedesktop/Hal/devices/computer')
+ computer.introspect
+ computer.default_iface =
'org.freedesktop.Hal.Device.SystemPowerManagement'
+
+
+ case action
+
+ when :reboot
+ logger.debug 'Rebooting the computer...'
+ return computer.Reboot == 0
+ when :shutdown
+ logger.debug 'Shutting down the computer...'
+ return computer.Shutdown == 0
+ else
+ logger.error "Unsupported HAL command: #{action}"
+ end
+
+ # handle DBus errors
+ rescue DBus::Error => dbe
+ logger.error "DBus error: #{dbe.dbus_message.error_name}"
+ return false
+ # handle generic errors
+ rescue Exception => e
+ logger.error "Caught exception: #{e.message}"
+ return false
+ end
+ end
+
+
+end
diff --git a/plugins/system/config/rails_parent.rb
b/plugins/system/config/rails_parent.rb
new file mode 100644
index 0000000..b869908
--- /dev/null
+++ b/plugins/system/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/system/config/resources/system.yml
b/plugins/system/config/resources/system.yml
new file mode 100644
index 0000000..32d64ba
--- /dev/null
+++ b/plugins/system/config/resources/system.yml
@@ -0,0 +1,3 @@
+interface: org.opensuse.yast.system.system
+controller: system
+singular: true
diff --git a/plugins/system/init.rb b/plugins/system/init.rb
new file mode 100644
index 0000000..3c19a74
--- /dev/null
+++ b/plugins/system/init.rb
@@ -0,0 +1 @@
+# Include hook code here
diff --git a/plugins/system/install.rb b/plugins/system/install.rb
new file mode 100644
index 0000000..f7732d3
--- /dev/null
+++ b/plugins/system/install.rb
@@ -0,0 +1 @@
+# Install hook code here
diff --git a/plugins/system/package/yast2-webservice-system.changes
b/plugins/system/package/yast2-webservice-system.changes
new file mode 100644
index 0000000..7c258fa
--- /dev/null
+++ b/plugins/system/package/yast2-webservice-system.changes
@@ -0,0 +1,5 @@
+-------------------------------------------------------------------
+Tue Aug 11 08:39:41 CEST 2009 - lslezak@xxxxxxx
+
+- initial version
+
diff --git a/plugins/system/package/yast2-webservice-system.spec
b/plugins/system/package/yast2-webservice-system.spec
new file mode 100644
index 0000000..9fbd492
--- /dev/null
+++ b/plugins/system/package/yast2-webservice-system.spec
@@ -0,0 +1,87 @@
+#
+# spec file for package yast2-webservice-patches (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-system
+PreReq: yast2-webservice
+# requires HAL for reboot/shutdown actions
+Requires: hal
+Provides:
yast2-webservice:/srv/www/yastws/app/controllers/system_controller.rb
+License: MIT
+Group: Productivity/Networking/Web/Utilities
+Autoreqprov: on
+Version: 0.0.1
+Release: 0
+Summary: YaST2 - Webservice - System
+Source: www.tar.bz2
+BuildRoot: %{_tmppath}/%{name}-%{version}-build
+BuildArch: noarch
+
+#
+%define pkg_user yastws
+%define plugin_name patches
+#
+
+
+%description
+YaST2 - Webservice - REST based interface for basic system access
+
+Authors:
+--------
+ Ladislav Slezak <lslezak@xxxxxxxxxx>
+
+%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}
+
+
+%clean
+rm -rf $RPM_BUILD_ROOT
+
+%post
+# granting all permissions for the web user
+polkit-auth --user %{pkg_user} --grant
org.freedesktop.hal.power-management.shutdown
+polkit-auth --user %{pkg_user} --grant
org.freedesktop.hal.power-management.shutdown-multiple-sessions
+polkit-auth --user %{pkg_user} --grant
org.freedesktop.hal.power-management.reboot
+polkit-auth --user %{pkg_user} --grant
org.freedesktop.hal.power-management.reboot-multiple-sessions
+
+%postun
+# discard all configured permissions for the web user
+polkit-auth --user %{pkg_user} --revoke
org.freedesktop.hal.power-management.shutdown
+polkit-auth --user %{pkg_user} --revoke
org.freedesktop.hal.power-management.shutdown-multiple-sessions
+polkit-auth --user %{pkg_user} --revoke
org.freedesktop.hal.power-management.reboot
+polkit-auth --user %{pkg_user} --revoke
org.freedesktop.hal.power-management.reboot-multiple-sessions
+
+%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}
+/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}/tasks
+
+%changelog
diff --git a/plugins/system/test/test_helper.rb
b/plugins/system/test/test_helper.rb
new file mode 100644
index 0000000..7d67910
--- /dev/null
+++ b/plugins/system/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/system/uninstall.rb b/plugins/system/uninstall.rb
new file mode 100644
index 0000000..9738333
--- /dev/null
+++ b/plugins/system/uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
--
To unsubscribe, e-mail: yast-commit+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: yast-commit+help@xxxxxxxxxxxx

< Previous Next >
This Thread
  • No further messages