Commit c61a5de8 authored by Steven Murray's avatar Steven Murray
Browse files

Added cta-catalogue-delete-all-data

parent 8a42481b
......@@ -78,7 +78,7 @@ add_library (ctainmemorycatalogueunittests SHARED
target_link_libraries (ctainmemorycatalogueunittests
ctacatalogue)
install(TARGETS ctainmemorycatalogueunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
install (TARGETS ctainmemorycatalogueunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
set (CATALOGUE_UNIT_TESTS_LIB_SRC_FILES
CatalogueTest.cpp
......@@ -90,7 +90,7 @@ add_library (ctacatalogueunittests SHARED
target_link_libraries (ctacatalogueunittests
ctacatalogue)
install(TARGETS ctacatalogueunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
install (TARGETS ctacatalogueunittests DESTINATION usr/${CMAKE_INSTALL_LIBDIR})
install (FILES cta_catalogue_db.conf.example
DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/cta
......@@ -104,7 +104,7 @@ add_executable(cta-catalogue-schema-lock
target_link_libraries (cta-catalogue-schema-lock
ctacatalogue)
install(TARGETS cta-catalogue-schema-lock DESTINATION /usr/bin)
install (TARGETS cta-catalogue-schema-lock DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-lock.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-schema-unlock
......@@ -115,7 +115,7 @@ add_executable(cta-catalogue-schema-unlock
target_link_libraries (cta-catalogue-schema-unlock
ctacatalogue)
install(TARGETS cta-catalogue-schema-unlock DESTINATION /usr/bin)
install (TARGETS cta-catalogue-schema-unlock DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-schema-unlock.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-schema-status
......@@ -126,5 +126,16 @@ add_executable(cta-catalogue-schema-status
target_link_libraries (cta-catalogue-schema-status
ctacatalogue)
install(TARGETS cta-catalogue-schema-status DESTINATION /usr/bin)
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)
/*
* 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);
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;
}
} // 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;
/**
* 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);
private:
/**
* 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/Exception.hpp"
#include <ostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
DeleteAllCatalogueDataCmdLineArgs::DeleteAllCatalogueDataCmdLineArgs(const int argc, const char *const *const argv) {
if(argc != 2) {
exception::Exception ex;
ex.getMessage() << "Wrong number of command-line arguments: excepted=1 actual=" << (argc - 1) << std::endl <<
std::endl;
printUsage(ex.getMessage());
throw ex;
}
dbConfigPath = argv[1];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void DeleteAllCatalogueDataCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-catalogue-delete-all-data databaseConnectionFile" << 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;
}
} // 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 <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 {
/**
* 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, const 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 "catalogue/DeleteAllCatalogueDataCmd.hpp"
#include "common/exception/Exception.hpp"
#include <iostream>
/**
* 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.
*/
static int exceptionThrowingMain(const int argc, char *const *const argv);
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
using namespace cta;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::Exception &ex) {
errorMessage = ex.getMessage().str();
} catch(std::exception &se) {
errorMessage = se.what();
} catch(...) {
errorMessage = "An unknown exception was thrown";
}
// Reaching this point means the command has failed, an exception was throw
// and errorMessage has been set accordingly
std::cerr << "Aborting: " << errorMessage << std::endl;
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::DeleteAllCatalogueDataCmd cmd(std::cin, std::cout, std::cerr);
return cmd.exceptionThrowingMain(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-LOCK 1CTA "November 2016" CTA CTA
.SH NAME
cta-catalogue-delete-all-data \- Delete all data in the CTA catalogue
.SH SYNOPSIS