diff --git a/catalogue/CMakeLists.txt b/catalogue/CMakeLists.txt index fea9123a2716d25b4676b63ea0806da7212c8367..3c4188b4e63a1aa9093aba397961244f05970334 100644 --- a/catalogue/CMakeLists.txt +++ b/catalogue/CMakeLists.txt @@ -299,6 +299,19 @@ set_property(TARGET cta-catalogue-schema-drop APPEND PROPERTY INSTALL_RPATH ${OR install (TARGETS cta-catalogue-schema-drop DESTINATION /usr/bin) install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-drop.1cta DESTINATION /usr/share/man/man1) +add_executable(cta-catalogue-set-production + SetProductionCmd.cpp + SetProductionCmdMain.cpp + SetProductionCmdLineArgs.cpp +) + +target_link_libraries (cta-catalogue-set-production ctacatalogue ctaschemachecker) +set_property(TARGET cta-catalogue-set-production APPEND PROPERTY INSTALL_RPATH ${PROTOBUF3_RPATH}) +set_property(TARGET cta-catalogue-set-production APPEND PROPERTY INSTALL_RPATH ${ORACLE-INSTANTCLIENT_RPATH}) + +install (TARGETS cta-catalogue-set-production DESTINATION /usr/bin) +install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-set-production.1cta DESTINATION /usr/share/man/man1) + add_executable(cta-database-poll PollDatabaseCmd.cpp PollDatabaseCmdLineArgs.cpp diff --git a/catalogue/DropSchemaCmd.cpp b/catalogue/DropSchemaCmd.cpp index 187c096f5bf8f46b11eb74ae3d4eef574b1568c3..584cae2f90858824250d304d6c6661dcc1a8c359 100644 --- a/catalogue/DropSchemaCmd.cpp +++ b/catalogue/DropSchemaCmd.cpp @@ -360,10 +360,14 @@ void DropSchemaCmd::dropDatabaseSequences(rdbms::Conn &conn, const std::list<std bool DropSchemaCmd::isProductionSet(cta::rdbms::Conn & conn){ const char * const sql = "SELECT CTA_CATALOGUE.IS_PRODUCTION AS IS_PRODUCTION FROM CTA_CATALOGUE"; - auto stmt = conn.createStmt(sql); - auto rset = stmt.executeQuery(); - if(rset.next()){ - return rset.columnBool("IS_PRODUCTION"); + try { + auto stmt = conn.createStmt(sql); + auto rset = stmt.executeQuery(); + if(rset.next()){ + return rset.columnBool("IS_PRODUCTION"); + } + } catch(const exception::Exception & ex) { + throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str()); } //We should never arrive here throw cta::exception::Exception("Cannot check the IS_PRODUCTION bit because the CTA_CATALOGUE table is empty or does not exist."); diff --git a/catalogue/SetProductionCmd.cpp b/catalogue/SetProductionCmd.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dece2c4df7b00c3ef610554bd9c6539fc74f301f --- /dev/null +++ b/catalogue/SetProductionCmd.cpp @@ -0,0 +1,102 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#include "SetProductionCmd.hpp" +#include "rdbms/Login.hpp" +#include "SchemaChecker.hpp" +#include "SetProductionCmdLineArgs.hpp" + +namespace cta { +namespace catalogue { + +//------------------------------------------------------------------------------ +// constructor +//------------------------------------------------------------------------------ +SetProductionCmd::SetProductionCmd( + std::istream &inStream, + std::ostream &outStream, + std::ostream &errStream): + CmdLineTool(inStream, outStream, errStream) { +} + +//------------------------------------------------------------------------------ +// Destructor +//------------------------------------------------------------------------------ +SetProductionCmd::~SetProductionCmd() noexcept { + +} + +//------------------------------------------------------------------------------ +// exceptionThrowingMain +//------------------------------------------------------------------------------ +int SetProductionCmd::exceptionThrowingMain(const int argc, char *const *const argv) { + const SetProductionCmdLineArgs cmdLineArgs(argc, argv); + + if(cmdLineArgs.help) { + printUsage(m_out); + return 0; + } + + const rdbms::Login dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath); + const uint64_t maxNbConns = 1; + rdbms::ConnPool connPool(dbLogin, maxNbConns); + auto conn = connPool.getConn(); + + if(!isProductionSettable(dbLogin,conn)){ + throw cta::exception::Exception("Unable to set the catalogue as production because the column IS_PRODUCTION is missing"); + } + + m_out << "Setting the PRODUCTION flag..." << std::endl; + setProductionFlag(conn); + m_out << "PRODUCTION flag set." << std::endl; + + return 0; +} + +//------------------------------------------------------------------------------ +// printUsage +//------------------------------------------------------------------------------ +void SetProductionCmd::printUsage(std::ostream &os) { + SetProductionCmdLineArgs::printUsage(os); +} + +//------------------------------------------------------------------------------ +// isProductionSettable +//------------------------------------------------------------------------------ +bool SetProductionCmd::isProductionSettable(const cta::rdbms::Login & login, cta::rdbms::Conn & conn){ + //Check that the IS_PRODUCTION column is there + cta::catalogue::SchemaChecker::Builder builder("catalogue",login.dbType,conn); + auto schemaChecker = builder.build(); + cta::catalogue::SchemaCheckerResult res = schemaChecker->checkTableContainsColumns("CTA_CATALOGUE",{"IS_PRODUCTION"}); + return (res.getStatus() == cta::catalogue::SchemaCheckerResult::Status::SUCCESS); +} + +//------------------------------------------------------------------------------ +// setProductionFlag +//------------------------------------------------------------------------------ +void SetProductionCmd::setProductionFlag(cta::rdbms::Conn& conn) { + const char * const sql = "UPDATE CTA_CATALOGUE SET IS_PRODUCTION='1'"; + try { + conn.executeNonQuery(sql); + } catch(const exception::Exception & ex) { + throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str()); + } +} + + +}} \ No newline at end of file diff --git a/catalogue/SetProductionCmd.hpp b/catalogue/SetProductionCmd.hpp new file mode 100644 index 0000000000000000000000000000000000000000..6b1d8c2341aadeeee6f0bb364a4f2fa7824ccbd8 --- /dev/null +++ b/catalogue/SetProductionCmd.hpp @@ -0,0 +1,72 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include "CmdLineTool.hpp" +#include "rdbms/Conn.hpp" +#include "rdbms/Login.hpp" + +namespace cta { +namespace catalogue { + +class SetProductionCmd: public CmdLineTool { +public: + /** + * Constructor. + * + * @param inStream Standard input stream. + * @param outStream Standard output stream. + * @param errStream Standard error stream. + */ + SetProductionCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream); + ~SetProductionCmd() noexcept; +private: + + /** + * An exception throwing version of main(). + * + * @param argc The number of command-line arguments including the program name. + * @param argv The command-line arguments. + * @return The exit value of the program. + */ + int exceptionThrowingMain(const int argc, char *const *const argv) override; + + /** + * Prints the usage message of the command-line tool. + * + * @param os The output stream to which the usage message is to be printed. + */ + void printUsage(std::ostream &os) override; + + /** + * Returns true if the IS_PRODUCTION flag is settable, false otherwise + * @param login the database login informations + * @param conn the connection to the database + * @return true if the IS_PRODUCTION flag is settable, false otherwise + */ + bool isProductionSettable(const cta::rdbms::Login & login, cta::rdbms::Conn & conn); + + /** + * Set the IS_PRODUCTION flag to true on the CTA Catalogue + * @param conn the connection to the CTA Catalogue database + */ + void setProductionFlag(cta::rdbms::Conn & conn); +}; + +}} \ No newline at end of file diff --git a/catalogue/SetProductionCmdLineArgs.cpp b/catalogue/SetProductionCmdLineArgs.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3c92d7753dd4694c605a6ef46c4b6b1a247a08d9 --- /dev/null +++ b/catalogue/SetProductionCmdLineArgs.cpp @@ -0,0 +1,109 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#include "SetProductionCmdLineArgs.hpp" +#include "common/exception/CommandLineNotParsed.hpp" + +#include <getopt.h> +#include <ostream> + +namespace cta { +namespace catalogue { + +//------------------------------------------------------------------------------ +// constructor +//------------------------------------------------------------------------------ +SetProductionCmdLineArgs::SetProductionCmdLineArgs(const int argc, char *const *const argv): + help(false) +{ + static struct option longopts[] = { + {"help", no_argument, NULL, 'h'}, + {NULL , 0, NULL, 0} + }; + + // Prevent getopt() from printing an error message if it does not recognize + // an option character + opterr = 0; + + int opt = 0; + while((opt = getopt_long(argc, argv, ":h", longopts, NULL)) != -1) { + switch(opt) { + case 'h': + help = true; + break; + case ':': // Missing parameter + { + exception::CommandLineNotParsed ex; + ex.getMessage() << "The -" << (char)opt << " option requires a parameter"; + throw ex; + } + case '?': // Unknown option + { + exception::CommandLineNotParsed ex; + if(0 == optopt) { + ex.getMessage() << "Unknown command-line option"; + } else { + ex.getMessage() << "Unknown command-line option: -" << (char)optopt; + } + throw ex; + } + default: + { + exception::CommandLineNotParsed ex; + ex.getMessage() << + "getopt_long returned the following unknown value: 0x" << + std::hex << (int)opt; + throw ex; + } + } // switch(opt) + } // while getopt_long() + + // There is no need to continue parsing when the help option is set + if(help) { + return; + } + + // Calculate the number of non-option ARGV-elements + const int nbArgs = argc - optind; + + // Check the number of arguments + if(nbArgs != 1) { + exception::CommandLineNotParsed ex; + ex.getMessage() << "Wrong number of command-line arguments: expected=1 actual=" << nbArgs; + throw ex; + } + + dbConfigPath = argv[optind]; +} + +//------------------------------------------------------------------------------ +// printUsage +//------------------------------------------------------------------------------ +void SetProductionCmdLineArgs::printUsage(std::ostream &os) { + os << + "Usage:" << std::endl << + " cta-catalogue-set-production databaseConnectionFile [options]" << std::endl << + "Where:" << std::endl << + " databaseConnectionFile" << std::endl << + " The path to the file containing the connection details of the CTA" << std::endl << + " catalogue database" << std::endl << + "Options:" << std::endl << + " -h,--help" << std::endl << + " Prints this usage message" << std::endl; +} +}} diff --git a/catalogue/SetProductionCmdLineArgs.hpp b/catalogue/SetProductionCmdLineArgs.hpp new file mode 100644 index 0000000000000000000000000000000000000000..4cdc70652b3db83354e1767abb896acb6cf55dc6 --- /dev/null +++ b/catalogue/SetProductionCmdLineArgs.hpp @@ -0,0 +1,60 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#pragma once + +#include <string> + +namespace cta { +namespace catalogue { + + /** + * Structure to store the command-line arguments of the command-line tool + * named cta-catalogue-set-production + */ +struct SetProductionCmdLineArgs { + /** + * True if the usage message should be printed. + */ + bool help; + + /** + * Path to the file containing the connection details of the catalogue + * database. + */ + std::string dbConfigPath; + + /** + * Constructor that parses the specified command-line arguments. + * + * @param argc The number of command-line arguments including the name of the + * executable. + * @param argv The vector of command-line arguments. + */ + SetProductionCmdLineArgs(const int argc, char *const *const argv); + + /** + * Prints the usage message of the command-line tool. + * + * @param os The output stream to which the usage message is to be printed. + */ + static void printUsage(std::ostream &os); +}; + +}} + diff --git a/catalogue/SetProductionCmdMain.cpp b/catalogue/SetProductionCmdMain.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0550429d1d5300f2876a4960c9d2e0ca3d873c96 --- /dev/null +++ b/catalogue/SetProductionCmdMain.cpp @@ -0,0 +1,29 @@ +/* + * The CERN Tape Archive (CTA) project + * Copyright (C) 2019 CERN + * + * 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 3 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, see <http://www.gnu.org/licenses/>. + */ + +#include "catalogue/SetProductionCmd.hpp" + +#include <iostream> + +//------------------------------------------------------------------------------ +// main +//------------------------------------------------------------------------------ +int main(const int argc, char *const *const argv) { + cta::catalogue::SetProductionCmd cmd(std::cin, std::cout, std::cerr); + return cmd.main(argc, argv); +} diff --git a/catalogue/cta-catalogue-set-production.1cta b/catalogue/cta-catalogue-set-production.1cta new file mode 100644 index 0000000000000000000000000000000000000000..6ebfe7d18c9cb2bb9852de2fc6ceb120d04178bf --- /dev/null +++ b/catalogue/cta-catalogue-set-production.1cta @@ -0,0 +1,43 @@ +.\" The CERN Tape Archive (CTA) project +.\" Copyright (C) 2015 CERN +.\" +.\" 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 3 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, see <http://www.gnu.org/licenses/>. +.TH CTA-CATALOGUE-SCHEMA-DROP 1CTA "December 2016" CTA CTA +.SH NAME +cta-catalogue-set-production \- Set the IS_PRODUCTION flag on the CTA catalogue database. This will +prevent any dropping of the schema by the cta-catalogue-schema-drop tool. +.SH SYNOPSIS +.BI "cta-catalogue-set-production databaseConnectionFile [options]" + +.SH DESCRIPTION +\fBcta-catalogue-set-production\fP is a command-line tool that sets the IS_PRODUCTION flag +on the CTA_CATALOGUE table. +.P +This command-line tool will abort if the IS_PRODUCTION column is not in the CTA_CATALOGUE table. +.SH ARGUMENTS +.TP +\fBdatabaseConnectionFile +The path to the configuration file containing the connection details of the +CTA catalogue database. +.SH OPTIONS +.TP +\fB\-h, \-\-help +Prints the usage message. +.SH RETURN VALUE +Zero on success and non-zero on failure. +.SH EXAMPLES +cta-catalogue-set-production /etc/cta/cta-catalogue.conf + +.SH AUTHOR +\fBCTA\fP Team diff --git a/cta.spec.in b/cta.spec.in index ddf5ae3a3ae41a99e75447da3e723a5c2713fedb..51729050790997a8adb6781f7b0a8e45a518e9c4 100644 --- a/cta.spec.in +++ b/cta.spec.in @@ -319,6 +319,7 @@ Scripts and utilities to faciliate working with the CTA catalogue %attr(0755,root,root) %{_bindir}/cta-catalogue-admin-user-create %attr(0755,root,root) %{_bindir}/cta-catalogue-schema-create %attr(0755,root,root) %{_bindir}/cta-catalogue-schema-drop +%attr(0755,root,root) %{_bindir}/cta-catalogue-set-production %attr(0755,root,root) %{_bindir}/cta-catalogue-schema-verify %attr(0755,root,root) %{_bindir}/cta-database-poll %attr(0755,root,root) %{_bindir}/cta-statistics-save @@ -326,6 +327,7 @@ Scripts and utilities to faciliate working with the CTA catalogue %attr(0644,root,root) %doc /usr/share/man/man1/cta-catalogue-admin-user-create.1cta.gz %attr(0644,root,root) %doc /usr/share/man/man1/cta-catalogue-schema-create.1cta.gz %attr(0644,root,root) %doc /usr/share/man/man1/cta-catalogue-schema-drop.1cta.gz +%attr(0644,root,root) %doc /usr/share/man/man1/cta-catalogue-set-production.1cta.gz %attr(0644,root,root) %doc /usr/share/man/man1/cta-catalogue-schema-verify.1cta.gz %attr(0644,root,root) %doc /usr/share/man/man1/cta-database-poll.1cta.gz