-
Notifications
You must be signed in to change notification settings - Fork 122
/
UploadRemoteFile.cpp
115 lines (98 loc) · 4.29 KB
/
UploadRemoteFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#include "MantidRemoteAlgorithms/UploadRemoteFile.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/RemoteJobManager.h"
#include "MantidKernel/SimpleJSON.h"
#include "boost/make_shared.hpp"
#include <fstream>
namespace Mantid {
namespace RemoteAlgorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(UploadRemoteFile)
using namespace Mantid::Kernel;
using namespace Mantid::API;
// A reference to the logger is provided by the base class, it is called g_log.
void UploadRemoteFile::init() {
// Unlike most algorithms, this one doesn't deal with workspaces....
auto requireValue = std::make_shared<MandatoryValidator<std::string>>();
// Compute Resources
std::vector<std::string> computes = Mantid::Kernel::ConfigService::Instance()
.getFacility()
.computeResources();
declareProperty("ComputeResource", "",
std::make_shared<StringListValidator>(computes),
"The name of the remote computer to upload the file to",
Direction::Input);
// The transaction ID comes from the StartRemoteTransaction algortithm
declareProperty("TransactionID", "", requireValue,
"The transaction the file will be associated with",
Direction::Input);
declareProperty("RemoteFileName", "", requireValue,
"The name to save the file as on the remote computer. "
"(Filename only; no path information)",
Direction::Input);
declareProperty(
"LocalFileName", "", requireValue,
"The full pathname (on the local machine) of the file to upload",
Direction::Input);
// Note: 'RemoteFileName' is just the name. The remote server figures out the
// full path
// from the transaction ID. 'LocalFileName' *IS* the full pathname (on the
// local machine)
}
void UploadRemoteFile::exec() {
std::shared_ptr<RemoteJobManager> jobManager =
Mantid::Kernel::ConfigService::Instance()
.getFacility()
.getRemoteJobManager(getPropertyValue("ComputeResource"));
// jobManager is a std::shared_ptr...
if (!jobManager) {
// Requested compute resource doesn't exist
// TODO: should we create our own exception class for this??
throw(std::runtime_error(
std::string("Unable to create a compute resource named " +
getPropertyValue("ComputeResource"))));
}
RemoteJobManager::PostDataMap postData;
postData["TransID"] = getPropertyValue("TransactionID");
std::string localFileName = getPropertyValue("LocalFileName");
std::ifstream infile(localFileName.c_str());
if (infile.good()) {
// Yes, we're reading the entire file into memory. Obviously, this is only
// feasible for fairly small files...
RemoteJobManager::PostDataMap fileData;
fileData[getPropertyValue("RemoteFileName")] =
std::string(std::istreambuf_iterator<char>(infile),
std::istreambuf_iterator<char>());
infile.close();
std::istream &respStream =
jobManager->httpPost("/upload", postData, fileData);
if (jobManager->lastStatus() ==
Poco::Net::HTTPResponse::HTTP_CREATED) // Upload returns a "201 -
// Created" code on success
{
g_log.information() << "Uploaded '" << getPropertyValue("RemoteFileName")
<< "' to '" << getPropertyValue("LocalFileName")
<< "'\n";
} else {
JSONObject resp;
initFromStream(resp, respStream);
std::string errMsg;
resp["Err_Msg"].getValue(errMsg);
throw(std::runtime_error(errMsg));
}
} else {
throw(std::runtime_error(
std::string("Failed to open " + getPropertyValue("LocalFileName"))));
}
}
} // end namespace RemoteAlgorithms
} // end namespace Mantid