Added a helper for HDFS client to shell out commands. Code is copied from https://reviews.apache.org/r/40559/.
Review: https://reviews.apache.org/r/40941/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/b420b396 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/b420b396 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/b420b396 Branch: refs/heads/master Commit: b420b396d1fa1e0adac4ecde2e72788b81bf5ad5 Parents: 925e99e Author: Jie Yu <[email protected]> Authored: Mon Dec 14 11:42:02 2015 -0800 Committer: Jie Yu <[email protected]> Committed: Tue Dec 15 10:55:33 2015 -0800 ---------------------------------------------------------------------- src/hdfs/hdfs.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/b420b396/src/hdfs/hdfs.cpp ---------------------------------------------------------------------- diff --git a/src/hdfs/hdfs.cpp b/src/hdfs/hdfs.cpp index c32c2ae..e5e7097 100644 --- a/src/hdfs/hdfs.cpp +++ b/src/hdfs/hdfs.cpp @@ -15,8 +15,13 @@ // limitations under the License #include <string> +#include <tuple> #include <vector> +#include <process/collect.hpp> +#include <process/io.hpp> +#include <process/subprocess.hpp> + #include <stout/bytes.hpp> #include <stout/check.hpp> #include <stout/error.hpp> @@ -37,6 +42,58 @@ using std::string; using std::vector; +struct CommandResult +{ + Option<int> status; + string out; + string err; +}; + + +static Future<CommandResult> result(const Subprocess& s) +{ + CHECK_SOME(s.out()); + CHECK_SOME(s.err()); + + return await( + s.status(), + io::read(s.out().get()), + io::read(s.err().get())) + .then([](const std::tuple< + Future<Option<int>>, + Future<string>, + Future<string>>& t) -> Future<CommandResult> { + Future<Option<int>> status = std::get<0>(t); + if (!status.isReady()) { + return Failure( + "Failed to get the exit status of the subprocess: " + + (status.isFailed() ? status.failure() : "discarded")); + } + + Future<string> output = std::get<1>(t); + if (!output.isReady()) { + return Failure( + "Failed to read stdout from the subprocess: " + + (output.isFailed() ? output.failure() : "discarded")); + } + + Future<string> error = std::get<2>(t); + if (!error.isReady()) { + return Failure( + "Failed to read stderr from the subprocess: " + + (error.isFailed() ? error.failure() : "discarded")); + } + + CommandResult result; + result.status = status.get(); + result.out = output.get(); + result.err = error.get(); + + return result; + }); +} + + Try<Owned<HDFS>> HDFS::create(const Option<string>& _hadoop) { // Determine the hadoop client to use. If the user has specified
