[buildd-tools-devel] [PATCH 6/6] Add new schroot action '--list-session-ages'
Jan-Marek Glogowski
glogow at fbihome.de
Tue Jun 28 10:27:58 UTC 2011
This action list all sessions sorted by age. Format is
“<session id> <creation age> <last run age> <user>”.
---
bin/schroot/schroot-main-base.cc | 11 ++++
bin/schroot/schroot-main-base.h | 6 ++
bin/schroot/schroot-options-base.cc | 7 +++
bin/schroot/schroot-options-base.h | 2 +
bin/schroot/schroot-options.cc | 7 ++-
debian/changelog | 4 +-
man/schroot.1.in | 10 +++-
sbuild/sbuild-chroot-config.cc | 97 +++++++++++++++++++++++++++++++++++
sbuild/sbuild-chroot-config.h | 8 +++
9 files changed, 149 insertions(+), 3 deletions(-)
diff --git a/bin/schroot/schroot-main-base.cc b/bin/schroot/schroot-main-base.cc
index e49aab7..3e53985 100644
--- a/bin/schroot/schroot-main-base.cc
+++ b/bin/schroot/schroot-main-base.cc
@@ -307,6 +307,11 @@ main_base::run_impl ()
action_config();
return EXIT_SUCCESS;
}
+ if (this->options->action == options_base::ACTION_LIST_SESSION_AGES)
+ {
+ action_list_session_ages();
+ return EXIT_SUCCESS;
+ }
/* Create a session. */
sbuild::session::operation sess_op(sbuild::session::OPERATION_AUTOMATIC);
@@ -376,3 +381,9 @@ main_base::add_session_auth ()
this->session->set_auth(auth);
#endif // SBUILD_FEATURE_PAM
}
+
+void
+main_base::action_list_session_ages ()
+{
+ this->config->print_chroot_session_list_ages(std::cout);
+}
diff --git a/bin/schroot/schroot-main-base.h b/bin/schroot/schroot-main-base.h
index 8b880c6..b304db4 100644
--- a/bin/schroot/schroot-main-base.h
+++ b/bin/schroot/schroot-main-base.h
@@ -94,6 +94,12 @@ namespace schroot
virtual void
action_config () = 0;
+ /**
+ * List chroot session sorted by age.
+ */
+ virtual void
+ action_list_session_ages ();
+
protected:
/**
* Run the program. This is the program-specific run method which
diff --git a/bin/schroot/schroot-options-base.cc b/bin/schroot/schroot-options-base.cc
index 6d17af3..52e4352 100644
--- a/bin/schroot/schroot-options-base.cc
+++ b/bin/schroot/schroot-options-base.cc
@@ -43,6 +43,7 @@ const options_base::action_type options_base::ACTION_LIST ("list");
const options_base::action_type options_base::ACTION_INFO ("info");
const options_base::action_type options_base::ACTION_LOCATION ("location");
const options_base::action_type options_base::ACTION_CONFIG ("config");
+const options_base::action_type options_base::ACTION_LIST_SESSION_AGES ("list_session_ages");
options_base::options_base ():
schroot_base::options (),
@@ -83,6 +84,7 @@ options_base::add_options ()
action.add(ACTION_INFO);
action.add(ACTION_LOCATION);
action.add(ACTION_CONFIG);
+ action.add(ACTION_LIST_SESSION_AGES);
actions.add_options()
("list,l",
@@ -255,6 +257,11 @@ options_base::check_actions ()
if (this->all_sessions)
this->load_chroots = this->load_sessions = true;
}
+ else if (this->action == ACTION_LIST_SESSION_AGES)
+ {
+ this->load_sessions = true;
+ this->all_sessions = true;
+ }
else
{
// Something went wrong
diff --git a/bin/schroot/schroot-options-base.h b/bin/schroot/schroot-options-base.h
index c4af6c3..49531fe 100644
--- a/bin/schroot/schroot-options-base.h
+++ b/bin/schroot/schroot-options-base.h
@@ -67,6 +67,8 @@ namespace schroot
static const action_type ACTION_LOCATION;
/// Display chroot configuration.
static const action_type ACTION_CONFIG;
+ /// Display the list of sessions and ages, sorted by age.
+ static const action_type ACTION_LIST_SESSION_AGES;
/// A shared_ptr to an options_base object.
typedef std::tr1::shared_ptr<options_base> ptr;
diff --git a/bin/schroot/schroot-options.cc b/bin/schroot/schroot-options.cc
index 0814f14..e5e187f 100644
--- a/bin/schroot/schroot-options.cc
+++ b/bin/schroot/schroot-options.cc
@@ -49,7 +49,9 @@ options::add_options ()
actions.add_options()
("location",
- _("Print location of selected chroots"));
+ _("Print location of selected chroots"))
+ ("list-session-ages",
+ _("Print an age-sorted session list"));
chroot.add_options()
("all,a",
@@ -98,6 +100,9 @@ options::check_options ()
if (vm.count("location"))
this->action = ACTION_LOCATION;
+ if (vm.count("list-session-ages"))
+ this->action = ACTION_LIST_SESSION_AGES;
+
if (vm.count("all"))
this->all = true;
if (vm.count("all-chroots"))
diff --git a/debian/changelog b/debian/changelog
index c8eafa6..c061637 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -10,8 +10,10 @@ schroot (1.4.24-1) unstable; urgency=low
* Don't include facet-source-clonable data in sessions.
* Include session creation and last-run date in session keyfile:
- Update session keyfile before and after session run.
+ * Add new schroot action '--list-session-ages' to list all
+ sessions sorted by age.
- -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de> Thu, 23 Jun 2011 16:48:27 +0200
+ -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de> Mon, 27 Jun 2011 14:49:54 +0200
schroot (1.4.23-1) unstable; urgency=low
diff --git a/man/schroot.1.in b/man/schroot.1.in
index 3277bca..dc15ca3 100644
--- a/man/schroot.1.in
+++ b/man/schroot.1.in
@@ -21,7 +21,9 @@ schroot \- securely enter a chroot environment
.SH SYNOPSIS
.B schroot
.RB [ \-h \[or] \-\-help " \[or] " \-V \[or] \-\-version
-.RB " \[or] " \-l \[or] \-\-list " \[or] " \-i \[or] \-\-info
+.RB " \[or] " \-l \[or] \-\-list
+.RB " \[or] " \-\-list\-session\-ages
+.RB " \[or] " \-i \[or] \-\-info
.RB " \[or] " \-\-config " \[or] " \-\-location
.RB " \[or] " \-\-automatic\-session
.RB " \[or] " \-b \[or] \-\-begin\-session " \[or] " \-\-recover\-session
@@ -109,6 +111,12 @@ Print version information.
.BR \-l ", " \-\-list
List all available chroots.
.TP
+.BR \-\-list\-session\-ages
+Prints a list of all sessions with their age and owners. This includes both
+creation and last run age (format = days:hours). Broken or missing ages are
+indicated by a hyphen (-). Values are double space separated. The line
+format is \[lq]<session id> <creation age> <last run age> <user>\[rq].
+.TP
.BR \-i ", " \-\-info
Print detailed information about the specified chroots.
.TP
diff --git a/sbuild/sbuild-chroot-config.cc b/sbuild/sbuild-chroot-config.cc
index 39619ff..58564b1 100644
--- a/sbuild/sbuild-chroot-config.cc
+++ b/sbuild/sbuild-chroot-config.cc
@@ -33,6 +33,7 @@
#include <ext/stdio_filebuf.h>
#include <boost/filesystem/operations.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
#include <sys/types.h>
#include <sys/stat.h>
@@ -531,6 +532,102 @@ chroot_config::print_chroot_list (string_list const& chroots,
}
void
+chroot_config::print_chroot_session_list_ages (std::ostream& stream) const
+{
+ using namespace boost::posix_time;
+ typedef std::map<long, chroot_map> chroot_age_map;
+
+ chroot_map const& chroots = find_namespace("session");
+ ptime now(second_clock::local_time());
+ chroot_age_map age_map;
+
+ /**
+ * Build a map of <last_run|creation hours, <session_id, chroot> >
+ *
+ * hours -1 = no date (old session)
+ * hours -2 = invalid date
+ */
+ for (chroot_map::const_iterator pos = chroots.begin();
+ pos != chroots.end();
+ ++pos)
+ {
+ long hours = -1;
+ chroot_facet_session::ptr psess =
+ pos->second->get_facet<chroot_facet_session>();
+
+ /* If last_run date is empty, use creation date. */
+ std::string date_str = psess->get_last_run_date();
+ if (date_str.empty())
+ date_str = psess->get_creation_date();
+ if (!date_str.empty())
+ try
+ {
+ hours = time_period(time_from_string(date_str), now).length().hours();
+ }
+ catch (std::exception& e)
+ {
+ hours = -2;
+ }
+
+ chroot_map& cmap = age_map[hours];
+ cmap[pos->first] = pos->second;
+ }
+
+ format age_fmt(" %1$d:%2$02d");
+
+ for (chroot_age_map::iterator age_pos = age_map.begin();
+ age_pos != age_map.end();
+ ++age_pos)
+ {
+ for (chroot_map::iterator pos = age_pos->second.begin();
+ pos != age_pos->second.end();
+ ++pos)
+ {
+ long hours_run = -1, hours_created = -1;
+
+ if (age_pos->first >= 0)
+ {
+ chroot_facet_session::ptr psess =
+ pos->second->get_facet<chroot_facet_session>();
+ std::string date_str = psess->get_last_run_date();
+ if (date_str.empty())
+ hours_created = age_pos->first;
+ else
+ try
+ {
+ hours_run = age_pos->first;
+ date_str = psess->get_creation_date();
+ hours_created = time_period(time_from_string(date_str), now)
+ .length().hours();
+ }
+ catch (std::exception& e)
+ {
+ }
+ }
+
+ /* format: "<session id> <creation age> <last run age> <user>" */
+ stream << pos->first;
+ if (hours_created < 0)
+ stream << " -";
+ else
+ stream << str(age_fmt % (hours_created / 24)
+ % (hours_created % 24));
+ if (hours_run < 0)
+ stream << " -";
+ else
+ stream << str(age_fmt % (hours_run / 24)
+ % (hours_run % 24));
+ if (pos->second->get_root_users().size() > 0)
+ stream << " "
+ << string_list_to_string(pos->second->get_root_users(), ",");
+ else
+ stream << " -";
+ stream << '\n';
+ }
+ }
+}
+
+void
chroot_config::print_chroot_list_simple (std::ostream& stream) const
{
stream << _("Available chroots: ");
diff --git a/sbuild/sbuild-chroot-config.h b/sbuild/sbuild-chroot-config.h
index 8238ec9..db36436 100644
--- a/sbuild/sbuild-chroot-config.h
+++ b/sbuild/sbuild-chroot-config.h
@@ -271,6 +271,14 @@ namespace sbuild
std::ostream& stream) const;
/**
+ * Print all available sessions to the specified stream.
+ *
+ * @param stream the stream to output to.
+ */
+ void
+ print_chroot_session_list_ages (std::ostream& stream) const;
+
+ /**
* Print a single line of all the available chroots to the
* specified stream.
*
--
1.7.2.5
More information about the Buildd-tools-devel
mailing list