[glom] Add the glom_test_connection command-line tool.



commit 1450ead414abb43505c1278b192977572138f2ae
Author: Murray Cumming <murrayc murrayc com>
Date:   Sun Dec 18 23:06:58 2011 +0100

    Add the glom_test_connection command-line tool.
    
    * Makefile_libglom.am:
    * glom/glom_test_connection.cc: This is useful for sanity checking
    of the configuration of central PostgreSQL servers.

 ChangeLog                    |    8 ++
 Makefile_libglom.am          |    5 +-
 glom/glom_test_connection.cc |  211 ++++++++++++++++++++++++++++++++++++++++++
 po/ChangeLog                 |    6 +
 po/POTFILES.in               |    1 +
 5 files changed, 230 insertions(+), 1 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0a3fa88..3e793e5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-12-18  Murray Cumming  <murrayc murrayc com>
+
+	Add the glom_test_connection command-line tool.
+
+	* Makefile_libglom.am:
+	* glom/glom_test_connection.cc: This is useful for sanity checking 
+	of the configuration of central PostgreSQL servers.
+
 2011-12-16  Murray Cumming  <murrayc murrayc com>
 
 	Film Manager example: Use English for Day/Night choices.
diff --git a/Makefile_libglom.am b/Makefile_libglom.am
index f72976b..f0a39c7 100644
--- a/Makefile_libglom.am
+++ b/Makefile_libglom.am
@@ -85,9 +85,12 @@ pkgconfig_DATA = glom/libglom/glom- GLOM_ABI_VERSION@.pc
 glom_commandline_ldadd = glom/libglom/libglom-$(GLOM_ABI_VERSION).la $(libglom_all_libs)
 glom_commandline_cppflags = $(glom_includes) $(LIBGLOM_CFLAGS) $(PYTHON_CPPFLAGS) $(BOOST_PYTHON_CFLAGS) $(glom_defines)
 
-bin_PROGRAMS = glom/glom_create_from_example
+bin_PROGRAMS = glom/glom_create_from_example glom/glom_test_connection
 
 glom_glom_create_from_example_SOURCES = glom/glom_create_from_example.cc
 glom_glom_create_from_example_LDADD = $(glom_commandline_ldadd)
 glom_glom_create_from_example_CPPFLAGS = $(glom_commandline_cppflags)
 
+glom_glom_test_connection_SOURCES = glom/glom_test_connection.cc
+glom_glom_test_connection_LDADD = $(glom_commandline_ldadd)
+glom_glom_test_connection_CPPFLAGS = $(glom_commandline_cppflags)
diff --git a/glom/glom_test_connection.cc b/glom/glom_test_connection.cc
new file mode 100644
index 0000000..1603427
--- /dev/null
+++ b/glom/glom_test_connection.cc
@@ -0,0 +1,211 @@
+/* Glom
+ *
+ * Copyright (C) 2011 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * 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, write to the
+71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+// For instance:
+// glom_test_connection --server-hostname=localhost --server-port=5433 --server-username=someuser
+
+#include "config.h"
+
+#include <libglom/connectionpool.h>
+#include <libglom/connectionpool_backends/postgres_central.h>
+#include <libglom/init.h>
+#include <libglom/privs.h>
+#include <libglom/utils.h>
+#include <giomm/file.h>
+#include <glibmm/optioncontext.h>
+#include <glibmm/convert.h>
+#include <iostream>
+
+#include <glibmm/i18n.h>
+
+class GlomCreateOptionGroup : public Glib::OptionGroup
+{
+public:
+  GlomCreateOptionGroup();
+
+  //These instances should live as long as the OptionGroup to which they are added,
+  //and as long as the OptionContext to which those OptionGroups are added.
+  bool m_arg_version;
+  Glib::ustring m_arg_server_hostname;
+  double m_arg_server_port;
+  Glib::ustring m_arg_server_username;
+  Glib::ustring m_arg_server_password;
+  Glib::ustring m_arg_server_database;
+};
+
+GlomCreateOptionGroup::GlomCreateOptionGroup()
+: Glib::OptionGroup("glom_create_from_example", _("Glom options"), _("Command-line options")),
+  m_arg_version(false),
+  m_arg_server_port(0)
+{
+  Glib::OptionEntry entry;
+
+  entry.set_long_name("version");
+  entry.set_short_name('V');
+  entry.set_description(_("The version of this application."));
+  add_entry(entry, m_arg_version);
+
+  entry.set_long_name("server-hostname");
+  entry.set_short_name('h');
+  entry.set_description(_("The hostname of the PostgreSQL server, such as localhost."));
+  add_entry(entry, m_arg_server_hostname);
+  
+  entry.set_long_name("server-port");
+  entry.set_short_name('p');
+  entry.set_description(_("The port of the PostgreSQL server, such as 5434."));
+  add_entry(entry, m_arg_server_port);
+  
+  entry.set_long_name("server-username");
+  entry.set_short_name('u');
+  entry.set_description(_("The username for the PostgreSQL server."));
+  add_entry(entry, m_arg_server_username);
+
+  //Optional:
+  entry.set_long_name("server-database");
+  entry.set_short_name('d');
+  entry.set_description(_("The specific database on the PostgreSQL server (Optional)."));
+  add_entry(entry, m_arg_server_database);
+}
+
+static void print_options_hint()
+{
+  //TODO: How can we just print them out?
+  std::cout << _("Use --help to see a list of available command-line options.") << std::endl;
+}
+
+
+int main(int argc, char* argv[])
+{
+  Glom::libglom_init();
+  
+  Glib::OptionContext context;
+  GlomCreateOptionGroup group;
+  context.set_main_group(group);
+  
+  try
+  {
+    context.parse(argc, argv);
+  }
+  catch(const Glib::OptionError& ex)
+  {
+    std::cout << _("Error while parsing command-line options: ") << std::endl << ex.what() << std::endl;
+    print_options_hint();
+    return EXIT_FAILURE;
+  }
+  catch(const Glib::Error& ex)
+  {
+    std::cerr << "Error: " << ex.what() << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  if(group.m_arg_version)
+  {
+    std::cout << PACKAGE_STRING << std::endl;
+    return EXIT_SUCCESS;
+  }
+
+  if(group.m_arg_server_hostname.empty())
+  {
+    std::cerr << "Please provide a database hostname." << std::endl;
+    print_options_hint();
+    return EXIT_FAILURE;
+  }
+
+  if(group.m_arg_server_username.empty())
+  {
+    std::cerr << _("Please provide a database username.") << std::endl;
+    print_options_hint();
+    return EXIT_FAILURE;
+  }
+
+  //Get the password from stdin.
+  //This is not a command-line option because then it would appear in logs.
+  //Other command-line utilities such as psql don't do this either.
+  //TODO: Support alternatives such as using a file.
+  const Glib::ustring prompt = Glib::ustring::compose(
+    _("Please enter the PostgreSQL server's password for the user %1: "), group.m_arg_server_username);
+  const char* password = ::getpass(prompt.c_str());
+
+
+  //Setup the connection, assuming that we are testing central hosting:
+  Glom::ConnectionPool* connection_pool = Glom::ConnectionPool::get_instance();
+
+  //Specify the backend and backend-specific details to be used by the connectionpool.
+  //This is usually done by ConnectionPool::setup_from_document():
+  Glom::ConnectionPoolBackends::PostgresCentralHosted* backend = 
+    new Glom::ConnectionPoolBackends::PostgresCentralHosted;
+  backend->set_host(group.m_arg_server_hostname);
+
+  //Use a specified port, or try all suitable ports:
+  if(group.m_arg_server_port)
+  {
+    backend->set_port(group.m_arg_server_port);
+    backend->set_try_other_ports(false);
+  }
+  else
+  {
+    backend->set_try_other_ports(true);
+  }
+
+  connection_pool->set_user(group.m_arg_server_username);
+  connection_pool->set_password(password);
+  connection_pool->set_backend(std::auto_ptr<Glom::ConnectionPool::Backend>(backend));
+
+  if(group.m_arg_server_database.empty())
+  {
+    //Prevent it from trying to connect to a database with the same name as the user,
+    //which is more likely to exist by chance than this silly name:
+    connection_pool->set_database("somenonexistantdatbasename");
+  }
+  else
+  {
+    connection_pool->set_database(group.m_arg_server_database);
+  }
+
+  connection_pool->set_ready_to_connect();
+
+  try
+  {
+    connection_pool->connect();
+  }
+  catch(const Glom::ExceptionConnection& ex)
+  {
+    if(ex.get_failure_type() == Glom::ExceptionConnection::FAILURE_NO_SERVER)
+    {
+      std::cerr << _("Error: Could not connect to the server even without specifying a database.") << std::endl;
+      return EXIT_FAILURE;
+    }
+    else if(ex.get_failure_type() == Glom::ExceptionConnection::FAILURE_NO_DATABASE)
+    {
+      //We expect this exception if we did not specify a database:
+      if(!(group.m_arg_server_database.empty()))
+      {  
+        std::cerr << _("Error: Could not connect to the specified database.") << std::endl;
+        return EXIT_FAILURE;
+      }
+    }
+  }
+
+  std::cout << _("Successful connection.") << std::endl;
+        
+  Glom::libglom_deinit();
+
+  return EXIT_SUCCESS;
+}
diff --git a/po/ChangeLog b/po/ChangeLog
index 3e97d88..abadec6 100644
--- a/po/ChangeLog
+++ b/po/ChangeLog
@@ -1,3 +1,9 @@
+2011-12-18  Murray Cumming  <murrayc localhost localdomain>
+
+	reviewed by: <delete if not using a buddy>
+
+	* POTFILES.in:
+
 2011-11-16  Murray Cumming  <murrayc murrayc com>
 
 	Correct the date format in the es_ES locale. 
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ce445e0..f056d39 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -57,6 +57,7 @@ glom/libglom/db_utils.cc
 glom/libglom/report_builder.cc
 glom/libglom/xsl_utils.cc
 glom/glom_create_from_example.cc
+glom/glom_test_connection.cc
 glom/main.cc
 glom/mode_data/box_data_calendar_related.cc
 glom/mode_data/box_data.cc



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]