Skip to content
Snippets Groups Projects
Commit f71d3e0c authored by Steven Murray's avatar Steven Murray
Browse files

Factorized out the cta-xroot_plugins-write-notification-msg command

parent 72a3c3f5
Branches
Tags
No related merge requests found
......@@ -39,4 +39,14 @@ set (FAKE_EOS_CMD_SRC_FILES
add_executable (cta-xrootd_plugins-fakeeos ${FAKE_EOS_CMD_SRC_FILES})
add_dependencies (cta-xrootd_plugins-fakeeos generate_notification.pb.h)
target_link_libraries (cta-xrootd_plugins-fakeeos ctacommon ctafrontendmessages)
target_link_libraries (cta-xrootd_plugins-fakeeos ctacommon)
set (WRITE_NOTIFICATION_MSG_CMD_SRC_FILES
CmdLineTool.cpp
WriteNotificationMsgCmd.cpp
WriteNotificationMsgCmdLineArgs.cpp
WriteNotificationMsgCmdMain.cpp)
add_executable (cta-xrootd_plugins-write-notification-msg ${WRITE_NOTIFICATION_MSG_CMD_SRC_FILES})
add_dependencies (cta-xrootd_plugins-write-notification-msg generate_notification.pb.h)
target_link_libraries (cta-xrootd_plugins-write-notification-msg ctacommon ctafrontendmessages)
......@@ -27,6 +27,8 @@
#include <string>
#include <XrdCl/XrdClFileSystem.hh>
#include <google/protobuf/util/json_util.h>
namespace cta {
namespace xroot_plugins {
......@@ -67,61 +69,6 @@ int FakeEosCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const std::string fsUrl = protocol + ":" + "//" + cmdLineArgs.ctaHost + ":" + std::to_string(cmdLineArgs.ctaPort);
XrdCl::FileSystem fs(fsUrl, false);
eos::wfe::notification notification;
notification.mutable_wf()->set_event("notification_workflow_event");
notification.mutable_wf()->set_queue("notification_workflow_queue");
notification.mutable_wf()->set_wfname("notification_workflow_wfname");
notification.mutable_wf()->set_vpath("notification_workflow_vpath");
notification.mutable_wf()->mutable_instance()->set_name("notification_instance_name");
notification.mutable_wf()->mutable_instance()->set_url("notification_instance_url");
notification.mutable_wf()->set_timestamp(1100);
notification.set_turl("notification_turl");
notification.mutable_cli()->mutable_user()->set_n(1111);
notification.mutable_cli()->mutable_user()->set_name("notification_cli_user_name");
notification.mutable_cli()->mutable_sec()->set_host("notification_cli_sec_host");
notification.mutable_cli()->mutable_sec()->set_app("notification_cli_sec_app");
notification.mutable_cli()->mutable_sec()->set_name("notification_cli_sec_name");
notification.mutable_cli()->mutable_sec()->set_prot("notification_cli_sec_prot");
notification.mutable_cli()->mutable_sec()->set_grps("notification_cli_sec_grps");
notification.mutable_file()->set_fid(1122);
notification.mutable_file()->set_pid(1133);
notification.mutable_file()->mutable_ctime()->set_sec(1144);
notification.mutable_file()->mutable_ctime()->set_nsec(1155);
notification.mutable_file()->mutable_mtime()->set_sec(1166);
notification.mutable_file()->mutable_mtime()->set_nsec(1177);
notification.mutable_file()->mutable_btime()->set_sec(1188);
notification.mutable_file()->mutable_btime()->set_nsec(1199);
notification.mutable_file()->mutable_ttime()->set_sec(2200);
notification.mutable_file()->mutable_ttime()->set_nsec(2211);
notification.mutable_file()->mutable_owner()->set_n(2222);
notification.mutable_file()->mutable_owner()->set_name("notification_file_owner_name");
notification.mutable_file()->set_size(2233);
notification.mutable_file()->mutable_cks()->set_value("notification_file_cks_value");
notification.mutable_file()->mutable_cks()->set_name("notification_file_cks_name");
notification.mutable_file()->set_mode(244);
notification.mutable_file()->set_lpath("notification_file_lpath");
(*notification.mutable_file()->mutable_xattr())["notification_file_xattr1"] = "file_xattr1_value";
(*notification.mutable_file()->mutable_xattr())["notification_file_xattr2"] = "file_xattr2_value";
notification.mutable_directory()->set_fid(1122);
notification.mutable_directory()->set_pid(1133);
notification.mutable_directory()->mutable_ctime()->set_sec(1144);
notification.mutable_directory()->mutable_ctime()->set_nsec(1155);
notification.mutable_directory()->mutable_mtime()->set_sec(1166);
notification.mutable_directory()->mutable_mtime()->set_nsec(1177);
notification.mutable_directory()->mutable_btime()->set_sec(1188);
notification.mutable_directory()->mutable_btime()->set_nsec(1199);
notification.mutable_directory()->mutable_ttime()->set_sec(2200);
notification.mutable_directory()->mutable_ttime()->set_nsec(2211);
notification.mutable_directory()->mutable_owner()->set_n(2222);
notification.mutable_directory()->mutable_owner()->set_name("notification_directory");
notification.mutable_directory()->set_size(2233);
notification.mutable_directory()->mutable_cks()->set_value("notification_directory_cks_value");
notification.mutable_directory()->mutable_cks()->set_name("notification_directory_cks_name");
notification.mutable_directory()->set_mode(2244);
notification.mutable_directory()->set_lpath("notification_directory_lpath");
(*notification.mutable_directory()->mutable_xattr())["notification_directory_attr1"] = "directory_xattr1_value";
(*notification.mutable_directory()->mutable_xattr())["notification_directory_attr2"] = "directory_xattr2_value";
XrdCl::Buffer arg;
arg.Append(&queryFileContents[0], queryFileContents.size(), 0);
XrdCl::Buffer *response = nullptr;
......
......@@ -25,7 +25,7 @@ namespace cta {
namespace xroot_plugins {
/**
* Command-line tool for creating the catalogue schema.
* Command-line tool to emulate EOS sending CTA a message.
*/
class FakeEosCmd: public CmdLineTool {
public:
......@@ -62,16 +62,6 @@ private:
*/
void printUsage(std::ostream &os) override;
/**
* Returns true if the table with the specified name exists in the database
* schema of teh specified database connection.
*
* @param tableName The name of the database table.
* @param conn The database connection.
* @return True if the table exists.
*/
bool tableExists(const std::string tableName, rdbms::Conn &conn) const;
}; // class FakeEosCmd
} // namespace xroot_plugins
......
/*
* 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/make_unique.hpp"
#include "xroot_plugins/WriteNotificationMsgCmd.hpp"
#include "xroot_plugins/WriteNotificationMsgCmdLineArgs.hpp"
#include "xroot_plugins/messages/notification.pb.h"
#include <fstream>
#include <iostream>
#include <stdint.h>
#include <string>
#include <google/protobuf/util/json_util.h>
namespace cta {
namespace xroot_plugins {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
WriteNotificationMsgCmd::WriteNotificationMsgCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream):
CmdLineTool(inStream, outStream, errStream) {
}
//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
WriteNotificationMsgCmd::~WriteNotificationMsgCmd() noexcept {
}
//------------------------------------------------------------------------------
// exceptionThrowingMain
//------------------------------------------------------------------------------
int WriteNotificationMsgCmd::exceptionThrowingMain(const int argc, char *const *const argv) {
const WriteNotificationMsgCmdLineArgs cmdLineArgs(argc, argv);
if(cmdLineArgs.help) {
printUsage(m_out);
return 0;
}
eos::wfe::notification notification;
notification.mutable_wf()->set_event("notification_workflow_event");
notification.mutable_wf()->set_queue("notification_workflow_queue");
notification.mutable_wf()->set_wfname("notification_workflow_wfname");
notification.mutable_wf()->set_vpath("notification_workflow_vpath");
notification.mutable_wf()->mutable_instance()->set_name("notification_instance_name");
notification.mutable_wf()->mutable_instance()->set_url("notification_instance_url");
notification.mutable_wf()->set_timestamp(1100);
notification.set_turl("notification_turl");
notification.mutable_cli()->mutable_user()->set_n(1111);
notification.mutable_cli()->mutable_user()->set_name("notification_cli_user_name");
notification.mutable_cli()->mutable_sec()->set_host("notification_cli_sec_host");
notification.mutable_cli()->mutable_sec()->set_app("notification_cli_sec_app");
notification.mutable_cli()->mutable_sec()->set_name("notification_cli_sec_name");
notification.mutable_cli()->mutable_sec()->set_prot("notification_cli_sec_prot");
notification.mutable_cli()->mutable_sec()->set_grps("notification_cli_sec_grps");
notification.mutable_file()->set_fid(1122);
notification.mutable_file()->set_pid(1133);
notification.mutable_file()->mutable_ctime()->set_sec(1144);
notification.mutable_file()->mutable_ctime()->set_nsec(1155);
notification.mutable_file()->mutable_mtime()->set_sec(1166);
notification.mutable_file()->mutable_mtime()->set_nsec(1177);
notification.mutable_file()->mutable_btime()->set_sec(1188);
notification.mutable_file()->mutable_btime()->set_nsec(1199);
notification.mutable_file()->mutable_ttime()->set_sec(2200);
notification.mutable_file()->mutable_ttime()->set_nsec(2211);
notification.mutable_file()->mutable_owner()->set_n(2222);
notification.mutable_file()->mutable_owner()->set_name("notification_file_owner_name");
notification.mutable_file()->set_size(2233);
notification.mutable_file()->mutable_cks()->set_value("notification_file_cks_value");
notification.mutable_file()->mutable_cks()->set_name("notification_file_cks_name");
notification.mutable_file()->set_mode(244);
notification.mutable_file()->set_lpath("notification_file_lpath");
(*notification.mutable_file()->mutable_xattr())["notification_file_xattr1"] = "file_xattr1_value";
(*notification.mutable_file()->mutable_xattr())["notification_file_xattr2"] = "file_xattr2_value";
notification.mutable_directory()->set_fid(1122);
notification.mutable_directory()->set_pid(1133);
notification.mutable_directory()->mutable_ctime()->set_sec(1144);
notification.mutable_directory()->mutable_ctime()->set_nsec(1155);
notification.mutable_directory()->mutable_mtime()->set_sec(1166);
notification.mutable_directory()->mutable_mtime()->set_nsec(1177);
notification.mutable_directory()->mutable_btime()->set_sec(1188);
notification.mutable_directory()->mutable_btime()->set_nsec(1199);
notification.mutable_directory()->mutable_ttime()->set_sec(2200);
notification.mutable_directory()->mutable_ttime()->set_nsec(2211);
notification.mutable_directory()->mutable_owner()->set_n(2222);
notification.mutable_directory()->mutable_owner()->set_name("notification_directory");
notification.mutable_directory()->set_size(2233);
notification.mutable_directory()->mutable_cks()->set_value("notification_directory_cks_value");
notification.mutable_directory()->mutable_cks()->set_name("notification_directory_cks_name");
notification.mutable_directory()->set_mode(2244);
notification.mutable_directory()->set_lpath("notification_directory_lpath");
(*notification.mutable_directory()->mutable_xattr())["notification_directory_attr1"] = "directory_xattr1_value";
(*notification.mutable_directory()->mutable_xattr())["notification_directory_attr2"] = "directory_xattr2_value";
if(cmdLineArgs.writeJsonToStdOut) {
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = true;
options.always_print_primitive_fields = true;
std::string jsonNotification;
google::protobuf::util::MessageToJsonString(notification, &jsonNotification, options);
std::cout << jsonNotification;
return 0;
}
std::ofstream messageFileStream(cmdLineArgs.filename, std::ios_base::binary);
if(!messageFileStream) {
m_err << "Failed to open " << cmdLineArgs.filename << std::endl;
return 1;
}
notification.SerializeToOstream(&messageFileStream);
return 0;
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void WriteNotificationMsgCmd::printUsage(std::ostream &os) {
WriteNotificationMsgCmdLineArgs::printUsage(os);
}
} // namespace xroot_plugins
} // 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 "xroot_plugins/CmdLineTool.hpp"
#include "rdbms/Conn.hpp"
namespace cta {
namespace xroot_plugins {
/**
* Command-line tool for writing a notification message to a file.
*/
class WriteNotificationMsgCmd: public CmdLineTool {
public:
/**
* Constructor.
*
* @param inStream Standard input stream.
* @param outStream Standard output stream.
* @param errStream Standard error stream.
*/
WriteNotificationMsgCmd(std::istream &inStream, std::ostream &outStream, std::ostream &errStream);
/**
* Destructor.
*/
~WriteNotificationMsgCmd() 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;
}; // class WriteNotificationMsgCmd
} // namespace xroot_plugins
} // 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/CommandLineNotParsed.hpp"
#include "common/utils/utils.hpp"
#include "xroot_plugins/WriteNotificationMsgCmdLineArgs.hpp"
#include <getopt.h>
#include <ostream>
namespace cta {
namespace xroot_plugins {
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
WriteNotificationMsgCmdLineArgs::WriteNotificationMsgCmdLineArgs(const int argc, char *const *const argv):
writeJsonToStdOut(false),
help(false) {
static struct option longopts[] = {
{"help", no_argument, NULL, 'h'},
{"json", no_argument, NULL, 'j'},
{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, ":hj", longopts, NULL)) != -1) {
switch(opt) {
case 'h':
help = true;
break;
case 'j':
writeJsonToStdOut = 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()
if(help && writeJsonToStdOut) {
exception::CommandLineNotParsed ex;
ex.getMessage() << "Specifying both -h|--help and -j|--json is not permitted";
throw ex;
}
// Calculate the number of non-option ARGV-elements
const int actualNbArgs = argc - optind;
if(help && actualNbArgs > 0) {
exception::CommandLineNotParsed ex;
ex.getMessage() << "A command-line argument cannot be specified with the -h|--help option";
throw ex;
}
if(writeJsonToStdOut && actualNbArgs > 0) {
exception::CommandLineNotParsed ex;
ex.getMessage() << "A command-line argument cannot be specified with the -j|--json option";
throw ex;
}
// There is no need to continue parsing if either the help or json option is
// set
if(help || writeJsonToStdOut) {
return;
}
// Check the number of arguments
const int expectedNbArgs = 1;
if(actualNbArgs != expectedNbArgs) {
exception::CommandLineNotParsed ex;
ex.getMessage() << "Wrong number of command-line arguments: excepted=" << expectedNbArgs << " actual=" <<
actualNbArgs;
throw ex;
}
filename = argv[optind];
}
//------------------------------------------------------------------------------
// printUsage
//------------------------------------------------------------------------------
void WriteNotificationMsgCmdLineArgs::printUsage(std::ostream &os) {
os <<
"Usage:" << std::endl <<
" cta-xroot_plugins-write-notification-msg filename" << std::endl <<
" cta-xroot_plugins-write-notification-msg -h|--help" << std::endl <<
" cta-xroot_plugins-write-notification-msg -j|--json" << std::endl <<
"Where:" << std::endl <<
" filename" << std::endl <<
" The name of file to which the notification message will be written." << std::endl <<
" Please note that this file will be overwritten if it already exists." << std::endl <<
"Options:" << std::endl <<
" -h, --help" << std::endl <<
" Prints this usage message" << std::endl <<
" -j, --json" << std::endl <<
" Prints the JSON representation of the notification message to standard out" << std::endl <<
"Example:" << std::endl <<
" cta-xrootd_plugins-write-notification-msg notification.msg" << std::endl <<
" cat notification.msg | protoc --decode_raw" << std::endl;
}
} // namespace xroot_plugins
} // 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 <ostream>
#include <stdint.h>
#include <string>
namespace cta {
namespace xroot_plugins {
/**
* Structure to store the command-line arguments of the command-line tool
* named cta-write-notification-msg.
*/
struct WriteNotificationMsgCmdLineArgs {
/**
* The name of the file to which the notification message should be written.
*/
std::string filename;
/**
* True if the JSON representation of the notification message should be
* written to standard out.
*/
bool writeJsonToStdOut;
/**
* True if the usage message should be printed.
*/
bool help;
/**
* 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.
*/
WriteNotificationMsgCmdLineArgs(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 xroot_plugins
} // 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 "xroot_plugins/WriteNotificationMsgCmd.hpp"
#include <iostream>
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
int main(const int argc, char *const *const argv) {
cta::xroot_plugins::WriteNotificationMsgCmd cmd(std::cin, std::cout, std::cerr);
return cmd.main(argc, argv);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment