Skip to content
Snippets Groups Projects
Commit 1328317a authored by Michael Davis's avatar Michael Davis
Browse files

[kill-bash] Deletes cta.deprecated command-line tool

parent b9cc81d5
No related branches found
No related tags found
No related merge requests found
......@@ -53,11 +53,3 @@ set_property (TARGET cta-wfe-test APPEND PROPERTY INSTALL_RPATH ${PROTOBUF3_RPAT
#target_compile_definitions(eoscta_stub PUBLIC XRDSSI_DEBUG)
install(TARGETS cta-wfe-test DESTINATION usr/bin)
#
# Old cta is scheduled for deletion
#
add_executable(cta.deprecated CTACmdMain.cpp Configuration.cpp)
target_link_libraries(cta.deprecated ${XROOTD_XRDCL_LIB} cryptopp)
install(TARGETS cta.deprecated DESTINATION usr/bin)
INSTALL(FILES cta-cli.conf DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/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 "cmdline/Configuration.hpp"
#include "common/Configuration.hpp"
#include "common/dataStructures/FrontendReturnCode.hpp"
#include "XrdCl/XrdClCopyProcess.hh"
#include "XrdCl/XrdClEnv.hh"
#include "XrdCl/XrdClDefaultEnv.hh"
#include <cryptopp/base64.h>
#include <cryptopp/osrng.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdexcept>
#include <xrootd/XrdCl/XrdClFile.hh>
/**
* Returns true if --stderr is on the command-line.
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
*/
static bool stderrIsOnTheCmdLine(const int argc, const char *const *const argv) {
for(int i = 1; i < argc; i++) {
const std::string arg = argv[i];
if(arg == "--stderr") {
return true;
}
}
return false;
}
/**
* Replaces all occurrences in a string "str" of a substring "from" with the string "to"
*
* @param str The original string
* @param from The substring to replace
* @param to The replacement string
*/
void replaceAll(std::string& str, const std::string& from, const std::string& to){
if(from.empty() || str.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length();
}
}
/**
* Encodes a string in base 64 and replaces slashes ('/') in the result
* with underscores ('_').
*
* @param msg string to encode
* @return encoded string
*/
std::string encode(const std::string msg) {
std::string ret;
const bool noNewLineInBase64Output = false;
CryptoPP::StringSource ss1(msg, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(ret), noNewLineInBase64Output));
// need to replace slashes ('/') with underscores ('_') because xroot removes
// consecutive slashes, and the cryptopp base64 algorithm may produce
// consecutive slashes. This is solved in cryptopp-5.6.3 (using
// Base64URLEncoder instead of Base64Encoder) but we currently have
// cryptopp-5.6.2. To be changed in the future...
replaceAll(ret, "/", "_");
return ret;
}
/**
* Formats the command path string
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
* @return the command string
*/
std::string formatCommandPath(const int argc, const char **argv) {
cta::cmdline::Configuration cliConf("/etc/cta/cta-cli.conf");
std::string cmdPath = "root://"+cliConf.getFrontendHostAndPort()+"//";
for(int i=0; i<argc; i++) {
if(i) cmdPath += "&";
cmdPath += encode(std::string(argv[i]));
}
return cmdPath;
}
/**
* Sends the command and waits for the reply
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
* @return the return code
*/
int sendCommand(const int argc, const char **argv) {
int rc = 0;
const bool writeToStderr = stderrIsOnTheCmdLine(argc, argv);
const std::string cmdPath = formatCommandPath(argc, argv);
XrdCl::File xrootFile;
// Open the xroot file reprsenting the execution of the command
{
const XrdCl::Access::Mode openMode = XrdCl::Access::None;
const uint16_t openTimeout = 15; // Timeout in seconds that is rounded up to the nearest 15 seconds
const XrdCl::XRootDStatus openStatus = xrootFile.Open(cmdPath, XrdCl::OpenFlags::Read, openMode, openTimeout);
if(!openStatus.IsOK()) {
throw std::runtime_error(std::string("Failed to open ") + cmdPath + ": " + openStatus.ToStr());
}
}
// The cta frontend return code is the first char of the answer
{
uint64_t readOffset = 0;
uint32_t bytesRead = 0;
char rc_char = '0';
const XrdCl::XRootDStatus readStatus = xrootFile.Read(readOffset, 1, &rc_char, bytesRead);
if(!readStatus.IsOK()) {
throw std::runtime_error(std::string("Failed to read first byte from ") + cmdPath + ": " +
readStatus.ToStr());
}
if(bytesRead != 1) {
throw std::runtime_error(std::string("Failed to read first byte from ") + cmdPath +
": Expected to read exactly 1 byte, actually read " +
std::to_string((long long unsigned int)bytesRead) + " bytes");
}
rc = rc_char - '0';
}
// Read and print the command result
{
uint64_t readOffset = 1; // The first character at offset 0 has already been read
uint32_t bytesRead = 0;
const size_t bufSize = 20480;
std::unique_ptr<char []> buf(new char[bufSize]);
do {
bytesRead = 0;
memset(buf.get(), 0, bufSize);
const XrdCl::XRootDStatus readStatus = xrootFile.Read(readOffset, bufSize - 1, buf.get(), bytesRead);
if(!readStatus.IsOK()) {
throw std::runtime_error(std::string("Failed to read ") + cmdPath + ": " + readStatus.ToStr());
}
if(bytesRead > 0) {
std::cout << buf.get();
if(writeToStderr) {
std::cerr << buf.get();
}
}
readOffset += bytesRead;
} while(bytesRead > 0);
}
// Close the xroot file reprsenting the execution of the command
{
const XrdCl::XRootDStatus closeStatus = xrootFile.Close();
if(!closeStatus.IsOK()) {
throw std::runtime_error(std::string("Failed to close ") + cmdPath + ": " + closeStatus.ToStr());
}
}
return rc;
}
/**
* The entry function of the command.
*
* @param argc The number of command-line arguments.
* @param argv The command-line arguments.
*/
int main(const int argc, const char **argv) {
try {
return sendCommand(argc, argv);
} catch (std::exception &ex) {
std::cerr << "Failed to execute the command. Reason: " << ex.what() << std::endl;
return cta::common::dataStructures::FrontendReturnCode::ctaErrorNoRetry;
} catch (...) {
std::cerr << "Failed to execute the command for an unknown reason" << std::endl;
return cta::common::dataStructures::FrontendReturnCode::ctaErrorNoRetry;
}
}
# The CTA frontend address in the form <FQDN>:<TCPPort>
<host>.cern.ch:10955
.\" 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 1CTA "August 2016" CTA CTA
.SH NAME
cta \- main command line interface to the CERN Tape Archive
.SH SYNOPSIS
.BI "cta"
.SH DESCRIPTION
Please refer to the help provided within the \fBcta\fP command. Just type \fBcta\fP for generic help or
\fBcta <command>\fP for help on a specific command.
.SH AUTHOR
\fBCTA\fP Team
......@@ -113,7 +113,6 @@ CERN Tape Archive:
The xroot plugin
%files -n cta-cli
%defattr(-,root,root)
%attr(0755,root,root) %{_bindir}/cta.deprecated
%attr(0755,root,root) %{_bindir}/cta-admin
%attr(0755,root,root) %{_bindir}/cta-wfe-test
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/cta/cta-cli.conf
......@@ -178,7 +177,6 @@ CERN Tape Archive:
The xroot plugin
%files -n cta-cli
%defattr(-,root,root)
%attr(0755,root,root) %{_bindir}/cta.deprecated
%attr(0755,root,root) %{_bindir}/cta-admin
%attr(0755,root,root) %{_bindir}/cta-wfe-test
%attr(0644,root,root) %config(noreplace) %{_sysconfdir}/cta/cta-cli.conf
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment