Skip to content
Snippets Groups Projects
Commit de078733 authored by Cedric Caffy's avatar Cedric Caffy
Browse files

Created the cta-catalogue-set-production tool

parent cecfee05
Branches
Tags
No related merge requests found
......@@ -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
......
......@@ -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.");
......
/*
* 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
/*
* 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
/*
* 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;
}
}}
/*
* 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);
};
}}
/*
* 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);
}
.\" 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
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment