Commit 28df04db authored by Steven Murray's avatar Steven Murray
Browse files

Removed unnecessary command-line tool cta-catalogue-delete-all-data

parent 6d379790
......@@ -146,17 +146,6 @@ target_link_libraries (cta-catalogue-schema-status
install (TARGETS cta-catalogue-schema-status DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-status.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-delete-all-data
DeleteAllCatalogueDataCmd.cpp
DeleteAllCatalogueDataCmdLineArgs.cpp
DeleteAllCatalogueDataCmdMain.cpp)
target_link_libraries (cta-catalogue-delete-all-data
ctacatalogue)
install (TARGETS cta-catalogue-delete-all-data DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-delete-all-data.1cta DESTINATION /usr/share/man/man1)
add_custom_command(OUTPUT oracle_catalogue_schema.cpp
COMMAND sed 's/^/\ \ \"/' oracle_catalogue_schema.sql | sed 's/$$/\"/' > oracle_catalogue_schema.cpp
DEPENDS oracle_catalogue_schema.sql)
......@@ -247,8 +236,6 @@ set (CATALOGUE_CMD_LINE_UNIT_TESTS_LIB_SRC_FILES
CreateAdminUserCmdLineArgsTest.cpp
CreateSchemaCmdLineArgs.cpp
CreateSchemaCmdLineArgsTest.cpp
DeleteAllCatalogueDataCmdLineArgs.cpp
DeleteAllCatalogueDataCmdLineArgsTest.cpp
DropSchemaCmdLineArgs.cpp
DropSchemaCmdLineArgsTest.cpp
LockSchemaCmdLineArgs.cpp
......
/*
* 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/>.
*/
#include "catalogue/CatalogueFactory.hpp"
#include "catalogue/DeleteAllCatalogueDataCmd.hpp"
#include "catalogue/DeleteAllCatalogueDataCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DeleteAllCatalogueDataCmd::DeleteAllCatalogueDataCmd(
std::istream &inStream,
std::ostream &outStream,
std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
DeleteAllCatalogueDataCmd::~DeleteAllCatalogueDataCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int DeleteAllCatalogueDataCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const DeleteAllCatalogueDataCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
printUsage(m_out);
return 0;
}
const auto dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
const uint64_t nbDbConns = 1;
auto catalogue = CatalogueFactory::create(dbLogin, nbDbConns);
if(catalogue->schemaIsLocked()) {
m_err <<
"Cannot delete the data in the catalogue because the schema is locked.\n"
"\n"
"Please see the following command-line tools:\n"
" cta-catalogue-schema-lock\n"
" cta-catalogue-schema-status\n"
" cta-catalogue-schema-unlock" << std::endl;
return 1;
}
deleteAllRowsExceptForCTA_CATALOGUE(*catalogue);
return 0;
}
//------------------------------------------------------------------------------
// deleteAllRowsExceptForCTA_CATALOGUE
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteAllRowsExceptForCTA_CATALOGUE(Catalogue &catalogue) {
deleteAdminUsers(catalogue);
deleteAdminHosts(catalogue);
deleteArchiveRoutes(catalogue);
deleteRequesterMountRules(catalogue);
deleteRequesterGroupMountRules(catalogue);
deleteArchiveAndTapeFiles(catalogue);
deleteTapes(catalogue);
deleteStorageClasses(catalogue);
deleteTapePools(catalogue);
deleteLogicalLibrares(catalogue);
deleteMountPolicies(catalogue);
}
//------------------------------------------------------------------------------
// deleteAdminUsers
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteAdminUsers(Catalogue &catalogue) {
const std::list<common::dataStructures::AdminUser> adminUsers = catalogue.getAdminUsers();
for(auto &adminUser: adminUsers) {
catalogue.deleteAdminUser(adminUser.name);
}
m_out << "Deleted " << adminUsers.size() << " admin users" << std::endl;
}
//------------------------------------------------------------------------------
// deleteAdminHosts
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteAdminHosts(Catalogue &catalogue) {
const std::list<common::dataStructures::AdminHost> adminHosts = catalogue.getAdminHosts();
for(auto &adminHost: adminHosts) {
catalogue.deleteAdminHost(adminHost.name);
}
m_out << "Deleted " << adminHosts.size() << " admin hosts" << std::endl;
}
//------------------------------------------------------------------------------
// deleteArchiveRoutes
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteArchiveRoutes(Catalogue &catalogue) {
const std::list<common::dataStructures::ArchiveRoute> archiveRoutes = catalogue.getArchiveRoutes();
for(auto &archiveRoute: archiveRoutes) {
catalogue.deleteArchiveRoute(archiveRoute.diskInstanceName, archiveRoute.storageClassName,
archiveRoute.copyNb);
}
m_out << "Deleted " << archiveRoutes.size() << " archive routes" << std::endl;
}
//------------------------------------------------------------------------------
// delelteAllRequesterMountRules
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteRequesterMountRules(Catalogue &catalogue) {
const std::list<common::dataStructures::RequesterMountRule> rules = catalogue.getRequesterMountRules();
for(auto &rule: rules) {
catalogue.deleteRequesterMountRule(rule.diskInstance, rule.name);
}
m_out << "Deleted " << rules.size() << " requester mount-rules" << std::endl;
}
//------------------------------------------------------------------------------
// deleteRequesterGroupMountRules
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteRequesterGroupMountRules(Catalogue &catalogue) {
const std::list<common::dataStructures::RequesterGroupMountRule> rules =
catalogue.getRequesterGroupMountRules();
for(auto &rule: rules) {
catalogue.deleteRequesterGroupMountRule(rule.diskInstance, rule.name);
}
m_out << "Deleted " << rules.size() << " requester-group mount-rules" << std::endl;
}
//------------------------------------------------------------------------------
// deleteArchiveAndTapeFiles
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteArchiveAndTapeFiles(Catalogue &catalogue) {
std::unique_ptr<ArchiveFileItor> itor = catalogue.getArchiveFileItor();
uint64_t nbArchiveFiles = 0;
while(itor->hasMore()) {
const auto archiveFile = itor->next();
catalogue.deleteArchiveFile(archiveFile.diskInstance, archiveFile.archiveFileID);
nbArchiveFiles++;
}
m_out << "Deleted " << nbArchiveFiles << " archive files and their associated tape copies" << std::endl;
}
//------------------------------------------------------------------------------
// deleteTapes
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteTapes(Catalogue &catalogue) {
const std::list<common::dataStructures::Tape> tapes = catalogue.getTapes();
for(auto &tape: tapes) {
catalogue.deleteTape(tape.vid);
}
m_out << "Deleted " << tapes.size() << " tapes" << std::endl;
}
//------------------------------------------------------------------------------
// deleteStorageClasses
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteStorageClasses(Catalogue &catalogue) {
const std::list<common::dataStructures::StorageClass> storageClasses = catalogue.getStorageClasses();
for(auto &storageClass: storageClasses) {
catalogue.deleteStorageClass(storageClass.diskInstance, storageClass.name);
}
m_out << "Deleted " << storageClasses.size() << " storage classes" << std::endl;
}
//------------------------------------------------------------------------------
// deleteTapePools
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteTapePools(Catalogue &catalogue) {
const std::list<common::dataStructures::TapePool> tapePools = catalogue.getTapePools();
for(auto &tapePool: tapePools) {
catalogue.deleteTapePool(tapePool.name);
}
m_out << "Deleted " << tapePools.size() << " tape pools" << std::endl;
}
//------------------------------------------------------------------------------
// deleteLogicalLibraries
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteLogicalLibrares(Catalogue &catalogue) {
const std::list<common::dataStructures::LogicalLibrary> logicalLibraries = catalogue.getLogicalLibraries();
for(auto &logicalLibrary: logicalLibraries) {
catalogue.deleteLogicalLibrary(logicalLibrary.name);
}
m_out << "Deleted " << logicalLibraries.size() << " logical libraries" << std::endl;
}
//------------------------------------------------------------------------------
// deleteMountPolicies
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::deleteMountPolicies(Catalogue &catalogue) {
const std::list<common::dataStructures::MountPolicy> mountPolicies = catalogue.getMountPolicies();
for(auto &mountPolicy: mountPolicies) {
catalogue.deleteMountPolicy(mountPolicy.name);
}
m_out << "Deleted " << mountPolicies.size() << " mount policies" << std::endl;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmd::printUsage(std::ostream &os) {
DeleteAllCatalogueDataCmdLineArgs::printUsage(os);
}
} // namespace catalogue
} // namespace cta
/*
* 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/>.
*/
#pragma once
#include "catalogue/Catalogue.hpp"
#include "catalogue/CmdLineTool.hpp"
namespace cta {
namespace catalogue {
/**
* Command-line tool that deletes all the rows of all the tables in the
* catalogue database except for the CTA_CATALOGUE table.
*/
class DeleteAllCatalogueDataCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
DeleteAllCatalogueDataCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~DeleteAllCatalogueDataCmd() 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;
/**
* Deletes all of the rows of all of the tables in the specified catalogue
* database except for the CTA_CATALOGUE table.
*
* @param catalogue The catalogue database.
*/
void deleteAllRowsExceptForCTA_CATALOGUE(Catalogue &catalogue);
/**
* Deletes all admin users from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteAdminUsers(Catalogue &catalogue);
/**
* Deletes all admin hosts from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteAdminHosts(Catalogue &catalogue);
/**
* Deletes all archive routes from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteArchiveRoutes(Catalogue &catalogue);
/**
* Deletes all requester mount-rules from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteRequesterMountRules(Catalogue &catalogue);
/**
* Deletes all requester-group mount-rules from the specified catalogue
* database.
*
* @param catalogue The catalogue database.
*/
void deleteRequesterGroupMountRules(Catalogue &catalogue);
/**
* Deletes all archive files and their associated tape files from the
* specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteArchiveAndTapeFiles(Catalogue &catalogue);
/**
* Deletes all tapes from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteTapes(Catalogue &catalogue);
/**
* Deletes all storage classes from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteStorageClasses(Catalogue &catalogue);
/**
* Deletes all tape pools from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteTapePools(Catalogue &catalogue);
/**
* Deletes all logical libraries from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteLogicalLibrares(Catalogue &catalogue);
/**
* Deletes all mount policies from the specified catalogue database.
*
* @param catalogue The catalogue database.
*/
void deleteMountPolicies(Catalogue &catalogue);
}; // class DeleteAllCatalogueDataCmd
} // namespace catalogue
} // namespace cta
/*
* 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/>.
*/
#include "catalogue/DeleteAllCatalogueDataCmdLineArgs.hpp"
#include "common/exception/CommandLineNotParsed.hpp"
#include <getopt.h>
#include <ostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DeleteAllCatalogueDataCmdLineArgs::DeleteAllCatalogueDataCmdLineArgs(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: excepted=1 actual=" << nbArgs;
throw ex;
}
dbConfigPath = argv[optind];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-catalogue-delete-all-data 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;
}
} // namespace catalogue
} // namespace cta
/*
* 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/>.
*/
#pragma once
#include <string>
namespace cta {
namespace catalogue {
/**
* Structure to store the command-line arguments of the command-line tool
* named cta-catalogue-delete-all-data.
*/
struct DeleteAllCatalogueDataCmdLineArgs {
/**
* 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.
*/
DeleteAllCatalogueDataCmdLineArgs(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);
};
} // namespace catalogue
} // namespace cta
/*
* 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/>.
*/
#include "common/exception/Exception.hpp"
#include "catalogue/DeleteAllCatalogueDataCmdLineArgs.hpp"
#include <gtest/gtest.h>
#include <list>
namespace unitTests {
class cta_catalogue_DeleteAllCatalogueDataCmdLineArgsTest : public ::testing::Test {