Skip to content

Commit

Permalink
Merge pull request #19 from yast/check_pkg_errors
Browse files Browse the repository at this point in the history
Check pkg errors
  • Loading branch information
lslezak committed Feb 27, 2014
2 parents 6347cb1 + 62fc64c commit f249243
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 120 deletions.
6 changes: 6 additions & 0 deletions package/yast2-registration.changes
@@ -1,3 +1,9 @@
-------------------------------------------------------------------
Thu Feb 27 08:35:31 UTC 2014 - lslezak@suse.cz

- Check for errors when adding/refreshing services
- 3.1.13

-------------------------------------------------------------------
Wed Feb 26 14:44:09 CET 2014 - locilka@suse.com

Expand Down
8 changes: 3 additions & 5 deletions package/yast2-registration.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-registration
Version: 3.1.12
Version: 3.1.13
Release: 0

BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand All @@ -28,13 +28,11 @@ License: GPL-2.0

Requires: yast2 >= 2.23.13
Requires: yast2-pkg-bindings >= 2.17.20
# Y2Logger
Requires: yast2-ruby-bindings >= 3.1.7
# N_() method
Requires: yast2-ruby-bindings >= 3.1.12
# SCC API library
Requires: rubygem-scc_api >= 0.2.4
Requires: yast2-slp >= 3.1.2
# Language module
Requires: yast2-country

BuildRequires: yast2 >= 2.23.13
BuildRequires: update-desktop-files
Expand Down
4 changes: 3 additions & 1 deletion src/Makefile.am
Expand Up @@ -9,7 +9,9 @@ desktop_DATA = \

ylibdir = @ylibdir@/registration
ylib_DATA = \
lib/registration/registration.rb
lib/registration/exceptions.rb \
lib/registration/sw_mgmt.rb \
lib/registration/helpers.rb

EXTRA_DIST = $(client_DATA) $(desktop_DATA) $(ylib_DATA)

Expand Down
109 changes: 15 additions & 94 deletions src/clients/inst_scc.rb
Expand Up @@ -25,18 +25,15 @@
# use external rubygem for SCC communication
require "scc_api"

require "tmpdir"
require "fileutils"
require "registration/exceptions"
require "registration/helpers"

module Yast
class InstSccClient < Client
include Yast::Logger

ZYPP_DIR = "/etc/zypp"

def main
Yast.import "UI"
Yast.import "Pkg"

textdomain "registration"

Expand All @@ -45,9 +42,6 @@ def main
Yast.import "Wizard"
Yast.import "Report"
Yast.import "Mode"
Yast.import "Progress"
Yast.import "PackageCallbacks"
Yast.import "Language"

# redirect the scc_api log to y2log
SccApi::GlobalLogger.instance.log = Y2Logger.instance
Expand Down Expand Up @@ -90,6 +84,12 @@ def main
else
Report.Error(_("Registration failed."))
end
rescue Registration::ServiceError => e
log.error("Service error: #{e.message % e.service}")
Report.Error(_(e.message) % e.service)
rescue Registration::PkgError => e
log.error("Pkg error: #{e.message}")
Report.Error(_(e.message))
rescue Exception => e
log.error("SCC registration failed: #{e}, #{e.backtrace}")
Report.Error(_("Registration failed."))
Expand All @@ -109,23 +109,23 @@ def register(email, reg_code)
scc = SccApi::Connection.new(email, reg_code)

# set the current language to receive translated error messages
scc.language = language
scc.language = Registration::Helpers.language

# announce (register the system) first
credentials = run_with_feedback(_("Registering the System..."), _("Contacting the SUSE Customer Center server")) do
scc.announce
end

# ensure the zypp config directories are writable in inst-sys
ensure_zypp_config_writable
Registration::SwMgmt.zypp_config_writable!

# write the global credentials
credentials.write

# then register the product(s)
product_services = run_with_feedback(_("Registering the Product..."), _("Contacting the SUSE Customer Center server")) do
# there will be just one base product, but theoretically there can be more...
selected_base_products.map do |base_product|
Registration::SwMgmt.products_to_register.map do |base_product|
log.info("Registering base product: #{base_product.inspect}")
scc.register(base_product)
end
Expand All @@ -144,38 +144,11 @@ def register(email, reg_code)
)

Progress.NextStage
add_services(product_services, credentials)
Progress.Finish
end
end

# add the services to libzypp and load (refresh) them
def add_services(product_services, credentials)
# save repositories before refreshing added services (otherwise
# pkg-bindings will treat them as removed by the service refresh and
# unload them)
Pkg.SourceSaveAll

# each registered product
product_services.each do |product_service|
# services for the each product
product_service.services.each do |service|
log.info "Adding service #{service.name.inspect} (#{service.url})"

# progress bar label
Progress.Title(_("Adding service %s...") % service.name)

# TODO FIXME: SCC currenly does not return credentials for the service,
# just reuse the global credentials and save to a different file
credentials.file = service.name + "_credentials"
credentials.write

Pkg.ServiceAdd(service.name, service.url.to_s)
# refresh works only for saved services
Pkg.ServiceSave(service.name)
Pkg.ServiceRefresh(service.name)

Progress.NextStep
begin
Registration::SwMgmt.add_services(product_services, credentials)
ensure
Progress.Finish
end
end
end
Expand Down Expand Up @@ -215,52 +188,13 @@ def show_scc_credentials_dialog
)
end

def selected_base_products
# just for debugging:
# return [{"name" => "SUSE_SLES", "arch" => "x86_64", "version" => "12-"}]

# source 0 is the base installation repo, the repos added later are considered as add-ons
# although they can also contain a different base product
#
# on a running system, products are :installed
selected_base_products = Pkg.ResolvableProperties("", :product, "").find_all do |p|
p["status"] == :selected || p["status"] == :installed
end

# filter out not needed data
product_info = selected_base_products.map{|p| { "name" => p["name"], "arch" => p["arch"], "version" => p["version"]}}

log.info("Found selected/installed base products: #{product_info}")

product_info
end

def run_with_feedback(header, label, &block)
Popup.ShowFeedback(header, label)
yield
ensure
Popup.ClearFeedback
end

# during installation /etc/zypp directory is not writable (mounted on
# a read-only file system), the workaround is to copy the whole directory
# structure into a writable temporary directory and override the original
# location by "mount -o bind"
def ensure_zypp_config_writable
if Mode.installation && !File.writable?(ZYPP_DIR)
log.info "Copying libzypp config to a writable place"

# create writable zypp directory structure in /tmp
tmpdir = Dir.mktmpdir

log.info "Copying #{ZYPP_DIR} to #{tmpdir} ..."
::FileUtils.cp_r ZYPP_DIR, tmpdir

log.info "Mounting #{tmpdir} to #{ZYPP_DIR}"
`mount -o bind #{tmpdir}/zypp #{ZYPP_DIR}`
end
end

def confirm_skipping
# Popup question: confirm skipping the registration
confirmation = _("If you do not register your system we will not be able\n" +
Expand All @@ -272,19 +206,6 @@ def confirm_skipping
Popup.YesNo(confirmation)
end

def language
lang = Language.language
log.info "Current language: #{lang}"

# remove the encoding (e.g. ".UTF-8")
lang.sub!(/\..*$/, "")
# replace lang/country separator "_" -> "-"
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
lang.tr!("_", "-")

log.info "Language for HTTP requests set to #{lang.inspect}"
lang
end
end
end

Expand Down
7 changes: 4 additions & 3 deletions src/clients/scc.rb
Expand Up @@ -23,14 +23,15 @@

# this is just a wrapper for running the SCC client in installed system

require "registration/registration"
require "registration/sw_mgmt"

module Yast
import "Wizard"

Wizard.CreateDialog
Yast::Registration.initialize_libzypp
Registration::SwMgmt.init

WFM.call("inst_scc")
Yast::Registration.save_libzypp

Wizard.CloseDialog
end
Expand Up @@ -20,25 +20,17 @@
# ------------------------------------------------------------------------------
#
#
module Registration
# Exception class for handling all Pkg errors
class PkgError < RuntimeError
end

require "yast"

module Yast
Yast.import "Pkg"
Yast.import "Installation"
Yast.import "PackageCallbacksInit"
class ServiceError < PkgError
attr_reader :service

class Registration
def self.initialize_libzypp
PackageCallbacksInit.InitPackageCallbacks
Pkg.TargetInitialize(Installation.destdir)
Pkg.TargetLoad
Pkg.SourceStartManager(true)
end

def self.save_libzypp
Pkg.SourceSaveAll
def initialize(msg, service)
super(msg)
@service = service
end
end
end

52 changes: 52 additions & 0 deletions src/lib/registration/helpers.rb
@@ -0,0 +1,52 @@
# encoding: utf-8

# ------------------------------------------------------------------------------
# Copyright (c) 2014 Novell, Inc. All Rights Reserved.
#
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of version 2 of the GNU General Public License as published by the
# Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may find
# current contact information at www.novell.com.
# ------------------------------------------------------------------------------
#
#

require "yast"

module Registration

class Helpers
include Yast::Logger

# Get the language for using in HTTP requests (in "Accept-Language" header)
def self.language
lang = Yast::WFM.GetLanguage
log.info "Current language: #{lang}"

if lang == "POSIX" || lang == "C"
log.warn "Ignoring #{lang.inspect} language for HTTP requests"
return nil
end

# remove the encoding (e.g. ".UTF-8")
lang.sub!(/\..*$/, "")
# replace lang/country separator "_" -> "-"
# see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
lang.tr!("_", "-")

log.info "Language for HTTP requests set to #{lang.inspect}"
lang
end

end
end

0 comments on commit f249243

Please sign in to comment.