From fc2f412c5360c2c1eafdf9d7f02f4076822de0b4 Mon Sep 17 00:00:00 2001 From: Arvin Schnell Date: Fri, 26 Jan 2018 14:40:09 +0100 Subject: [PATCH] - improved error handling for systemd services --- VERSION | 2 +- client/systemd-helper.cc | 97 +++++++++++++++++++++++++++++++--------- package/snapper.changes | 9 +++- 3 files changed, 86 insertions(+), 22 deletions(-) diff --git a/VERSION b/VERSION index be14282b..7d856835 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.5.3 +0.5.4 diff --git a/client/systemd-helper.cc b/client/systemd-helper.cc index b14bbb1c..3638168f 100644 --- a/client/systemd-helper.cc +++ b/client/systemd-helper.cc @@ -1,6 +1,6 @@ /* * Copyright (c) [2014-2015] Novell, Inc. - * Copyright (c) 2016 SUSE LLC + * Copyright (c) [2016,2018] SUSE LLC * * All Rights Reserved. * @@ -39,9 +39,36 @@ using namespace snapper; using namespace std; -void +// cout and cerr are visible with 'journalctl' and 'systemctl status +// snapper-.service'. + + +bool +call_with_error_check(std::function func) +{ + try + { + func(); + return true; + } + catch (const DBus::ErrorException& e) + { + cerr << error_description(e) << endl; + return false; + } + catch (const DBus::FatalException& e) + { + cerr << "failure (" << e.what() << ")." << endl; + return false; + } +} + + +bool timeline(ProxySnappers* snappers, const map& userdata) { + bool ok = true; + map configs = snappers->getConfigs(); for (const map::value_type value : configs) { @@ -57,15 +84,25 @@ timeline(ProxySnappers* snappers, const map& userdata) scd.cleanup = "timeline"; scd.userdata = userdata; - snapper->createSingleSnapshot(scd); + cout << "running timeline for '" << value.first << "'." << endl; + + if (!call_with_error_check([snapper, scd](){ snapper->createSingleSnapshot(scd); })) + { + cerr << "timeline for '" << value.first << "' failed." << endl; + ok = false; + } } } + + return ok; } -void +bool cleanup(ProxySnappers* snappers) { + bool ok = true; + map configs = snappers->getConfigs(); for (const map::value_type value : configs) { @@ -76,21 +113,41 @@ cleanup(ProxySnappers* snappers) map::const_iterator pos1 = raw.find("NUMBER_CLEANUP"); if (pos1 != raw.end() && pos1->second == "yes") { - do_cleanup_number(snapper, false); + cout << "running number cleanup for '" << value.first << "'." << endl; + + if (!call_with_error_check([snapper](){ do_cleanup_number(snapper, false); })) + { + cerr << "number cleanup for '" << value.first << "' failed." << endl; + ok = false; + } } map::const_iterator pos2 = raw.find("TIMELINE_CLEANUP"); if (pos2 != raw.end() && pos2->second == "yes") { - do_cleanup_timeline(snapper, false); + cout << "running timeline cleanup for '" << value.first << "'." << endl; + + if (!call_with_error_check([snapper](){ do_cleanup_timeline(snapper, false); })) + { + cerr << "timeline cleanup for '" << value.first << "' failed." << endl; + ok = false; + } } map::const_iterator pos3 = raw.find("EMPTY_PRE_POST_CLEANUP"); if (pos3 != raw.end() && pos3->second == "yes") { - do_cleanup_empty_pre_post(snapper, false); + cout << "running empty-pre-post cleanup for '" << value.first << "'." << endl; + + if (!call_with_error_check([snapper](){ do_cleanup_empty_pre_post(snapper, false); })) + { + cerr << "empty-pre-post cleanup for " << value.first << " failed." << endl; + ok = false; + } } } + + return ok; } @@ -128,24 +185,24 @@ main(int argc, char** argv) if ((opt = opts.find("userdata")) != opts.end()) userdata = read_userdata(opt->second); - try - { + bool ok = true; + + if (!call_with_error_check([do_timeline, do_cleanup, userdata, &ok]() { + ProxySnappers snappers(ProxySnappers::createDbus()); if (do_timeline) - timeline(&snappers, userdata); + if (!timeline(&snappers, userdata)) + ok = false; if (do_cleanup) - cleanup(&snappers); - } - catch (const DBus::ErrorException& e) - { - cerr << error_description(e) << endl; - exit(EXIT_FAILURE); - } - catch (const DBus::FatalException& e) + if (!cleanup(&snappers)) + ok = false; + + })) { - cerr << _("Failure") << " (" << e.what() << ")." << endl; - exit(EXIT_FAILURE); + ok = false; } + + exit(ok ? EXIT_SUCCESS : EXIT_FAILURE); } diff --git a/package/snapper.changes b/package/snapper.changes index 306c6625..ed7b2143 100644 --- a/package/snapper.changes +++ b/package/snapper.changes @@ -1,3 +1,10 @@ +------------------------------------------------------------------- +Fri Jan 26 14:36:20 CET 2018 - aschnell@suse.com + +- improved error handling for systemd services + (gh#openSUSE/snapper#382) +- version 0.5.4 + ------------------------------------------------------------------- Wed Jan 10 14:33:11 CET 2018 - aschnell@suse.com @@ -8,7 +15,7 @@ Wed Dec 6 16:49:06 CET 2017 - kukuk@suse.de - Switched from cron to systemd timers (spec file based distributions) (fate#324529) -- 0.5.3 +- version 0.5.3 ------------------------------------------------------------------- Thu Nov 23 13:51:45 UTC 2017 - rbrown@suse.com -- 2.47.3