ref: refs/heads/master
commit 4fe7bc0cfc38df7ad9a15db2f10601ae3778638c
Author: Jiri Suchomel
Date: Tue Aug 18 15:20:38 2009 +0200
back to YaPI based version of services
---
.../app/controllers/commands_controller.rb | 33 -----
.../app/controllers/config_ntp_controller.rb | 131 ------------------
.../app/controllers/services_controller.rb | 46 +++++--
plugins/services/app/models/config_ntp.rb | 29 ----
plugins/services/app/models/service.rb | 68 ++++++++++
plugins/services/app/views/commands/show.html.erb | 1 -
plugins/services/app/views/commands/show.json.erb | 1 -
plugins/services/app/views/commands/show.xml.erb | 1 -
.../services/app/views/config_ntp/show.html.erb | 1 -
.../services/app/views/config_ntp/show.json.erb | 1 -
plugins/services/app/views/config_ntp/show.xml.erb | 1 -
plugins/services/config/resources/services.yml | 3 +-
plugins/services/lib/lsbservice.rb | 141 --------------------
.../org.opensuse.yast.modules.yapi.services.policy | 39 ++++++
.../org.opensuse.yast.system.services.policy | 39 ------
.../test/functional/services_controller_test.rb | 18 ++-
plugins/services/test/unit/lsbservice_test.rb | 14 --
17 files changed, 154 insertions(+), 413 deletions(-)
diff --git a/plugins/services/app/controllers/commands_controller.rb b/plugins/services/app/controllers/commands_controller.rb
deleted file mode 100644
index 0e6abf2..0000000
--- a/plugins/services/app/controllers/commands_controller.rb
+++ /dev/null
@@ -1,33 +0,0 @@
-require "scr"
-
-include ApplicationHelper
-
-class CommandsController < ApplicationController
-
- before_filter :login_required
-
- #FIXME: index requires parameter, but system_check does not expect that and breaks
- def index
- redirect_to service_path(params[:service_id])
- end
-
- def update
- id = params[:id]
- sid = params[:service_id]
- logger.debug "calling services/#{sid}/command #{id}"
-
- unless permission_check( "org.opensuse.yast.system.services.execute")
- render ErrorResult.error(403, 1, "no permission") and return
- end
-
- cmd = "/etc/init.d/#{sid}"
- logger.debug "Service cmd #{cmd} #{id}"
- ret = Scr.instance.execute([cmd, id])
- if !ret
- # or 503 service unavailable
- render ErrorResult.error(404, "2", "SCR call failed") and return
- end
- render :xml => ret
- end
-
-end
diff --git a/plugins/services/app/controllers/config_ntp_controller.rb b/plugins/services/app/controllers/config_ntp_controller.rb
deleted file mode 100644
index 3ef934f..0000000
--- a/plugins/services/app/controllers/config_ntp_controller.rb
+++ /dev/null
@@ -1,131 +0,0 @@
-include ApplicationHelper
-
-class ConfigNtpController < ApplicationController
- before_filter :login_required
-
- require "scr"
-
- def initialize
- @scr = Scr.instance
- end
-#
-#local functions
-#
- def get_manual_server
-
- manual_server = ""
- ret = @scr.execute(["/sbin/yast2", "ntp-client", "list"])
- if ret
- servers = ret[:stderr].split "\n"
- servers.each do |s|
- column = s.split(" ")
- if column.size == 2 && column[0] == "Server"
- if column[1] != "0.pool.ntp.org" && column[1] != "1.pool.ntp.org" && column[1] != "2.pool.ntp.org"
- if manual_server == ""
- # Thats one user defined ntp-server
- manual_server = column[1]
- else
- #There are more than one user defined server --> do not use it
- manual_server = "No single configured ntp server"
- end
- end
- end
- end
- end
- return manual_server
- end
-
- def enabled
-
- ret = @scr.execute(["/sbin/yast2", "ntp-client", "status"])
- return (ret[:stderr]=="NTP daemon is enabled.\n") if ret
- return false
- end
-
- def write_ntp_conf (requested_servers)
- update_required = false
- #remove evtl.old server if requested
- servers = []
- ret = @scr.execute(["/sbin/yast2", "ntp-client", "list"])
- servers_line = ret[:stderr].split "\n"
- servers_line.each do |s|
- column = s.split " "
- if column.size == 2 && column[0] == "Server"
- servers << column[1]
- end
- end
- update_required = true
- requested_servers.each do |req_server|
- servers.each do |server|
- if server==req_server
- update_required = false
- break
- end
- end
- end
-
- #update required
- if update_required
- servers.each do |server|
- @scr.execute(["/sbin/yast2", "ntp-client", "delete", server])
- end
- requested_servers.each do |req_server|
- @scr.execute(["/sbin/yast2", "ntp-client", "add", req_server])
- end
- end
- end
-
- def enable (enabled)
- @scr.execute(["/sbin/yast2", "ntp-client", enabled ? "enable" : "disable" ])
- end
-
-#
-#actions
-#
-
- def show
- unless permission_check( "org.opensuse.yast.system.services.read")
- render ErrorResult.error(403, 1, "no permission") and return
- end
-
- @ntp = ConfigNtp.new
- @ntp.manual_server = ""
- @ntp.use_random_server = true
- @ntp.manual_server = get_manual_server
- @ntp.enabled = enabled
- if @ntp.manual_server == ""
- @ntp.use_random_server = true
- else
- @ntp.use_random_server = false
- end
- end
-
- def update
-
- unless permission_check( "org.opensuse.yast.system.services.write")
- render ErrorResult.error(403, 1, "no permission") and return
- end
- @ntp = ConfigNtp.new
- if params[:config_ntp] != nil
- @ntp.use_random_server = params[:config_ntp][:use_random_server]
- @ntp.enabled = params[:config_ntp][:enabled]
- @ntp.manual_server = params[:config_ntp][:manual_server]
- logger.debug "UPDATED: #{@ntp.inspect}"
-
- requested_servers = []
- if @ntp.use_random_server == false
- requested_servers << @ntp.manual_server
- else
- requested_servers = ["0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org"]
- end
- write_ntp_conf(requested_servers)
- enable(@ntp.enabled)
- else
- render ErrorResult.error(404, 2, "format or internal error") and return
- end
- render :show
-
- end
-
-
-end
diff --git a/plugins/services/app/controllers/services_controller.rb b/plugins/services/app/controllers/services_controller.rb
index 54afc4f..05a6493 100644
--- a/plugins/services/app/controllers/services_controller.rb
+++ b/plugins/services/app/controllers/services_controller.rb
@@ -1,30 +1,54 @@
+require 'service'
+
class ServicesController < ApplicationController
before_filter :login_required
def index
- unless permission_check( "org.opensuse.yast.system.services.read")
+ unless permission_check("org.opensuse.yast.modules.yapi.services.read")
render ErrorResult.error(403, 1, "no permission") and return
end
-
- @services = Lsbservice.all
- @services = @services.sort unless params[:sort] == "0"
-
- @services = @services.map {|svc| {:link => svc} }
+ @services = Service.find_all
end
- # show the svc including the commands
+ # Shows service status. Requires read permission for services YaPI.
def show
- unless permission_check( "org.opensuse.yast.system.services.read")
- render ErrorResult.error(403, 1, "no permission") and return
+ unless permission_check("org.opensuse.yast.modules.yapi.services.get")
+ render ErrorResult.error( 403, 1, "no permission" ) and return
end
- id = params[:id]
+ id = params[:id]
begin
- @service = Lsbservice.new id
+ @service = Service.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 = Service.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/services/app/models/config_ntp.rb b/plugins/services/app/models/config_ntp.rb
deleted file mode 100644
index 901b82f..0000000
--- a/plugins/services/app/models/config_ntp.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-class ConfigNtp
-
- attr_accessor :enabled,
- :use_random_server,
- :manual_server
-
- def initialize
- @enabled = false
- @use_random_server = false
- @manual_server = ""
- end
-
- def to_xml( options = {} )
- xml = options[:builder] ||= Builder::XmlMarkup.new(options)
- xml.instruct! unless options[:skip_instruct]
-
- xml.config_ntp do
- xml.tag!(:manual_server, @manual_server )
- xml.tag!(:enabled, @enabled, {:type => "boolean"} )
- xml.tag!(:use_random_server, @use_random_server, {:type => "boolean"} )
- end
- end
-
- def to_json( options = {} )
- hash = Hash.from_xml(to_xml())
- return hash.to_json
- end
-
-end
diff --git a/plugins/services/app/models/service.rb b/plugins/services/app/models/service.rb
new file mode 100644
index 0000000..510e543
--- /dev/null
+++ b/plugins/services/app/models/service.rb
@@ -0,0 +1,68 @@
+require 'yast_service'
+
+class Service
+
+ attr_accessor :name
+ attr_accessor :status
+
+ def initialize
+ end
+
+ # services = Service.find_all
+ def self.find_all
+
+ services = []
+ yapi_ret = YastService.Call("YaPI::SERVICES::Read")
+
+ if yapi_ret.nil?
+ raise "Can't get services list"
+ else
+ yapi_ret.each do |s|
+ service = Service.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 = Service.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/services/app/views/commands/show.html.erb b/plugins/services/app/views/commands/show.html.erb
deleted file mode 100644
index d26d152..0000000
--- a/plugins/services/app/views/commands/show.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/app/views/commands/show.json.erb b/plugins/services/app/views/commands/show.json.erb
deleted file mode 100644
index d26d152..0000000
--- a/plugins/services/app/views/commands/show.json.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/app/views/commands/show.xml.erb b/plugins/services/app/views/commands/show.xml.erb
deleted file mode 100644
index d26d152..0000000
--- a/plugins/services/app/views/commands/show.xml.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @service.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/app/views/config_ntp/show.html.erb b/plugins/services/app/views/config_ntp/show.html.erb
deleted file mode 100644
index 1eb4136..0000000
--- a/plugins/services/app/views/config_ntp/show.html.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @ntp.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/app/views/config_ntp/show.json.erb b/plugins/services/app/views/config_ntp/show.json.erb
deleted file mode 100644
index 1eb4136..0000000
--- a/plugins/services/app/views/config_ntp/show.json.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @ntp.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/app/views/config_ntp/show.xml.erb b/plugins/services/app/views/config_ntp/show.xml.erb
deleted file mode 100644
index 1eb4136..0000000
--- a/plugins/services/app/views/config_ntp/show.xml.erb
+++ /dev/null
@@ -1 +0,0 @@
-<%= @ntp.to_xml -%>
\ No newline at end of file
diff --git a/plugins/services/config/resources/services.yml b/plugins/services/config/resources/services.yml
index 734dd0e..a3d3486 100644
--- a/plugins/services/config/resources/services.yml
+++ b/plugins/services/config/resources/services.yml
@@ -1,2 +1,3 @@
-interface: org.opensuse.yast.system.services
+interface: org.opensuse.yast.modules.yapi.services
controller: services
+nested: permissions
diff --git a/plugins/services/lib/lsbservice.rb b/plugins/services/lib/lsbservice.rb
deleted file mode 100644
index 7720b00..0000000
--- a/plugins/services/lib/lsbservice.rb
+++ /dev/null
@@ -1,141 +0,0 @@
-class Commands
-end
-
-#
-# This provides generic commands for
-# LSB compatible services
-#
-#
-#
-class Lsbservice
- PREFIX = '/etc/init.d/'
-
- # Iterates over all service links
- # Makes up some because the real ones are too many
- # to store in a session cookie (?!?!)
- def self.mock_each
- items = ["cron", "cups", "gpm", "ntp", "random", "smbfs", "sshd", "nfs",
- "java.binfmt_misc",
- "autofs", "apache2", "avahi-daemon", "SuSEfirewall2_setup", "pure-ftpd" ]
- items.each {|i| yield i}
- end
-
- #
- # iterates over all service links
- #
- def self.each
- Dir.foreach(PREFIX) do |d|
- next if d[0,1] == '.'
- # halt kills the X session
- next if %w(README boot halt rc reboot skeleton skeleton.compat).include? d
- next if File.directory?( PREFIX+d )
- yield d
- end
- end
-
- #
- # Lsbservice.all -> Array of string
- # returns array of all available services
- #
-
- def self.all
- result = []
- each do |d|
- result << d
- end
- result
- end
-
- # 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
- @@states = [ :success, :error, :badargs, :unimplemented, :noperm, :notinstalled, :notconfigured, :notrunning ]
-
- #
- # Lsbservice.new link
- # Creates a new instance of Lsbservice for service <link>
- #
- # Attributes
- # link: link of the service
- # path: path to init script
- # commands: available commands as array of strings, typically "start", "stop", ...
- #
-
- attr_reader :link, :path, :commands
-
- # scans the system to detect path and commands
- def read_system
- expected_path = PREFIX+link
- raise "Nonexistent service #{link}" unless File.exists?(expected_path)
- if File.executable?(expected_path)
- # run init script to get its 'Usage' line
- IO.popen(expected_path, 'r+') do |pipe|
- loop do
- break if pipe.eof?
- l = pipe.read
- case l
- when /Usage:\s*(\S*)\s*\{([^\}]*)\}/
-# STDERR.puts "USAGE: #{$1}, #{$2}"
- @path = $1
- @commands = $2.split("|")
- break
- end
- end
- end
- end
-
- #put at least run|stop|status|restart
- @commands = ["run", "stop", "status", "restart"] if @commands.blank?
- end
-
- # sorting operator
- def <=>(other_service)
- link <=> other_service.link
- end
-
- # returns the path of the service
- def path
- return @path if defined?(@path)
- read_system
- @path
- end
-
- def commands
- return @commands if defined?(@commands)
- read_system
- @commands
- end
-
- def initialize link
- link = link.to_s unless link.is_a? String
- @link = link
- end
-
-
- #
- # See 'The Rails Way', page 510
- #
-
- def to_xml( options = {} )
- xml = options[:builder] ||= Builder::XmlMarkup.new(options)
- xml.instruct! unless options[:skip_instruct]
-
- xml.service do
- xml.tag!(:link, link )
- xml.tag!(:path, path )
- xml.commands do
- commands.each do |c|
- xml.command do
- xml.tag!(:name, c)
- end
- end
- end
- end
- end
-
-end
diff --git a/plugins/services/package/org.opensuse.yast.modules.yapi.services.policy b/plugins/services/package/org.opensuse.yast.modules.yapi.services.policy
new file mode 100644
index 0000000..0f5d8ab
--- /dev/null
+++ b/plugins/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/services/package/org.opensuse.yast.system.services.policy b/plugins/services/package/org.opensuse.yast.system.services.policy
deleted file mode 100644
index 1db9ebd..0000000
--- a/plugins/services/package/org.opensuse.yast.system.services.policy
+++ /dev/null
@@ -1,39 +0,0 @@
-<?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>YaST Webservice Project</vendor>
- http://en.opensuse.org/YAST
-
-
-
- <action id="org.opensuse.yast.system.services.read">
- <description>Reading service settings</description>
- <message>Authentication is required to read service settings</message>
- <defaults>
- no
- no
- </defaults>
- </action>
- <action id="org.opensuse.yast.system.services.write">
- <description>Writing service settings; Starting/Stoping services</description>
- <message>Authentication is required to run services and set the parameters</message>
- <defaults>
- no
- no
- </defaults>
- </action>
- <action id="org.opensuse.yast.system.services.execute">
- <description>Starting/stopping/... services</description>
- <message>Authentication is required to handle services</message>
- <defaults>
- no
- no
- </defaults>
- </action>
-
-</policyconfig>
\ No newline at end of file
diff --git a/plugins/services/test/functional/services_controller_test.rb b/plugins/services/test/functional/services_controller_test.rb
index 73c0179..b9bb834 100644
--- a/plugins/services/test/functional/services_controller_test.rb
+++ b/plugins/services/test/functional/services_controller_test.rb
@@ -4,6 +4,7 @@ require 'rubygems'
require "scr"
require 'mocha'
+
class ServicesControllerTest < ActionController::TestCase
fixtures :accounts
def setup
@@ -12,15 +13,16 @@ class ServicesControllerTest < ActionController::TestCase
# http://railsforum.com/viewtopic.php?id=1719
@request.session[:account_id] = 1 # defined in fixtures
- s1 = Lsbservice.new("foo")
- s1.stubs(:path).returns("/foo")
- s1.stubs(:commands).returns(["start", "stop"])
-
- s2 = Lsbservice.new("cron")
- s2.stubs(:path).returns("/cron")
- s2.stubs(:commands).returns(["start", "stop", "kill"])
+ s1 = Service.new
+ s1.name = "foo"
+ s1.status = 0
- Lsbservice.stubs(:all).returns([s1, s2])
+ s2 = Service.new
+ s2.name = "cron"
+ s2.status = 1
+
+ Service.stubs(:find_all).returns([s1, s2])
+ Service.stubs(:find).with("cron").returns([s2])
end
test "access index" do
diff --git a/plugins/services/test/unit/lsbservice_test.rb b/plugins/services/test/unit/lsbservice_test.rb
deleted file mode 100644
index 2fd0a2d..0000000
--- a/plugins/services/test/unit/lsbservice_test.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + "/../test_helper")
-require 'test/unit'
-require 'rubygems'
-require "lsbservice"
-require 'mocha'
-require 'pp'
-
-class LsbserviceTest < Test::Unit::TestCase
-
- def setup
- @services = Lsbservice.all
- end
-
-end
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org