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

Added cta-catalogue-admin-user-create and cta-catalogue-admin-host-create

parent f6452853
......@@ -192,3 +192,25 @@ target_link_libraries (cta-database-poll
install (TARGETS cta-database-poll DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-database-poll.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-admin-user-create
CreateAdminUserCmd.cpp
CreateAdminUserCmdLineArgs.cpp
CreateAdminUserCmdMain.cpp)
target_link_libraries (cta-catalogue-admin-user-create
ctacatalogue)
install (TARGETS cta-catalogue-admin-user-create DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-admin-user-create.1cta DESTINATION /usr/share/man/man1)
add_executable(cta-catalogue-admin-host-create
CreateAdminHostCmd.cpp
CreateAdminHostCmdLineArgs.cpp
CreateAdminHostCmdMain.cpp)
target_link_libraries (cta-catalogue-admin-host-create
ctacatalogue)
install (TARGETS cta-catalogue-admin-host-create DESTINATION /usr/bin)
install (FILES ${CMAKE_CURRENT_SOURCE_DIR}/cta-catalogue-admin-host-create.1cta DESTINATION /usr/share/man/man1)
......@@ -18,6 +18,8 @@
#include "catalogue/CmdLineTool.hpp"
#include <unistd.h>
namespace cta {
namespace catalogue {
......@@ -39,5 +41,32 @@ CmdLineTool::CmdLineTool(
CmdLineTool::~CmdLineTool() noexcept {
}
//------------------------------------------------------------------------------
// getUsername
//------------------------------------------------------------------------------
std::string CmdLineTool::getUsername() const {
char buf[256];
if(getlogin_r(buf, sizeof(buf))) {
return "UNKNOWN";
} else {
return buf;
}
}
//------------------------------------------------------------------------------
// getHostname
//------------------------------------------------------------------------------
std::string CmdLineTool::getHostname() const {
char buf[256];
if(gethostname(buf, sizeof(buf))) {
return "UNKNOWN";
} else {
buf[sizeof(buf) - 1] = '\0';
return buf;
}
}
} // namespace catalogue
} // namespace cta
......@@ -70,6 +70,20 @@ protected:
*/
std::ostream &m_err;
/**
* Returns the name of the user running the command-line tool.
*
* @return The name of the user running the command-line tool.
*/
std::string getUsername() const;
/**
* Returns the name of the host on which the command-line tool is running.
*
* @return The name of the host on which the command-line tool is running.
*/
std::string getHostname() const;
}; // class CmdLineTool
} // namespace catalogue
......
/*
* 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/CreateAdminHostCmd.hpp"
#include "catalogue/CreateAdminHostCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "rdbms/ConnFactoryFactory.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CreateAdminHostCmd::CreateAdminHostCmd(
std::istream &inStream,
std::ostream &outStream,
std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
CreateAdminHostCmd::~CreateAdminHostCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int CreateAdminHostCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const CreateAdminHostCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
CreateAdminHostCmdLineArgs::printUsage(m_out);
return 0;
}
const rdbms::Login dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
const uint64_t nbDbConns = 1;
auto catalogue = CatalogueFactory::create(dbLogin, nbDbConns);
const common::dataStructures::SecurityIdentity adminRunningCommand(getUsername(), getHostname());
catalogue->createAdminHost(adminRunningCommand, cmdLineArgs.adminHostname, cmdLineArgs.comment);
return 0;
}
} // 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 creates an admin host.
*/
class CreateAdminHostCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
CreateAdminHostCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~CreateAdminHostCmd() 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);
}; // class CreateAdminHostCmd
} // 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/CreateAdminHostCmdLineArgs.hpp"
#include "common/exception/UserError.hpp"
#include <getopt.h>
#include <ostream>
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CreateAdminHostCmdLineArgs::CreateAdminHostCmdLineArgs(const int argc, char *const *const argv):
help(false) {
static struct option longopts[] = {
{"comment" , 0, NULL, 'c'},
{"help" , 0, NULL, 'h'},
{"hostname" , 0, NULL, 'n'},
{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, ":c:hn:", longopts, NULL)) != -1) {
switch(opt) {
case 'c':
comment = optarg;
break;
case 'h':
help = true;
break;
case 'n':
adminHostname = optarg;
break;
case ':': // Missing parameter
{
exception::UserError ex;
ex.getMessage() << "The -" << (char)opt << " option requires a parameter";
throw ex;
}
case '?': // Unknown option
{
exception::UserError ex;
if(0 == optopt) {
ex.getMessage() << "Unknown command-line option";
} else {
ex.getMessage() << "Unknown command-line option: -" << (char)optopt;
}
throw ex;
}
default:
{
exception::UserError 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;
}
if(adminHostname.empty()) {
throw exception::Exception("The hostname option must be specified with a non-empty string");
}
if(comment.empty()) {
throw exception::Exception("The comment option must be specified with a non-empty string");
}
// Calculate the number of non-option ARGV-elements
const int nbArgs = argc - optind;
// Check the number of arguments
if(nbArgs != 1) {
exception::UserError ex;
ex.getMessage() << "Wrong number of command-line arguments: excepted=1 actual=" << (argc - 1);
throw ex;
}
dbConfigPath = argv[optind];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void CreateAdminHostCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-catalogue-admin-host-create databaseConnectionFile -n <hostname> -c <comment> [-h]" << 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 <<
" -n,--hostname <hostname>" << std::endl <<
" The name of the admin host to be created" << std::endl <<
" -c,--comment <comment>" << std::endl <<
" Comment to describe the creation of the admin host" << std::endl <<
" -h,--help" << std::endl <<
" Prints this usage message" << std::endl <<
"" << 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-admin-host-create
*/
struct CreateAdminHostCmdLineArgs {
/**
* 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;
/**
* The name of the admin host to be created in the catalogue database.
*/
std::string adminHostname;
/**
* The optional comment describing the creation of the admin host.
*/
std::string comment;
/**
* 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.
*/
CreateAdminHostCmdLineArgs(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 "catalogue/CreateAdminHostCmd.hpp"
#include "catalogue/CreateAdminHostCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "common/exception/UserError.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;
bool userError = false;
std::string errorMessage;
try {
return exceptionThrowingMain(argc, argv);
} catch(exception::UserError &ue) {
errorMessage = ue.getMessage().str();
userError = true;
} 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;
if(userError) {
std::cerr << std::endl;
catalogue::CreateAdminHostCmdLineArgs::printUsage(std::cerr);
}
return 1;
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
static int exceptionThrowingMain(const int argc, char *const *const argv) {
using namespace cta;
catalogue::CreateAdminHostCmd 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/>.
*/
#include "catalogue/CatalogueFactory.hpp"
#include "catalogue/CreateAdminUserCmd.hpp"
#include "catalogue/CreateAdminUserCmdLineArgs.hpp"
#include "common/exception/Exception.hpp"
#include "rdbms/ConnFactoryFactory.hpp"
namespace cta {
namespace catalogue {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
CreateAdminUserCmd::CreateAdminUserCmd(
std::istream &inStream,
std::ostream &outStream,
std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
CreateAdminUserCmd::~CreateAdminUserCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int CreateAdminUserCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const CreateAdminUserCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
CreateAdminUserCmdLineArgs::printUsage(m_out);
return 0;
}
const rdbms::Login dbLogin = rdbms::Login::parseFile(cmdLineArgs.dbConfigPath);
const uint64_t nbDbConns = 1;
auto catalogue = CatalogueFactory::create(dbLogin, nbDbConns);
const common::dataStructures::SecurityIdentity adminRunningCommand(getUsername(), getHostname());
catalogue->createAdminUser(adminRunningCommand, cmdLineArgs.adminUsername, cmdLineArgs.comment);
return 0;
}
} // 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 creates an admin user.
*/
class CreateAdminUserCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
CreateAdminUserCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~CreateAdminUserCmd() 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);
}; // class CreateAdminUserCmd