37 #include <Common/Version.h>
46 #include <sys/types.h>
63 struct passwd *pw = getpwuid(getuid());
64 cache_dir.append(pw->pw_dir);
65 cache_dir.append(
"/.cluster");
68 string cmd = (string)
"/bin/rm -rf " + cache_dir;
69 if (system(cmd.c_str()) != 0) {
70 cout <<
"Failed execution: " << cmd << endl;
71 quick_exit(EXIT_FAILURE);
75 const string locate_definition_file() {
81 if (getcwd(cwd, 1024) ==
nullptr) {
82 cout <<
"getcwd() - " << strerror(errno) << endl;
83 quick_exit(EXIT_FAILURE);
86 fname.append(
"/cluster.def");
97 quick_exit(EXIT_FAILURE);
101 bool is_environment_setting(
const string &arg) {
105 const char *ptr = arg.c_str();
111 void exec_command(
const string &script, vector<string> environment,
112 vector<string> arguments) {
113 char **argv =
new char *[environment.size() + arguments.size() + 3];
115 if (environment.empty()) {
116 argv[0] = (
char *)script.c_str();
118 for (
auto & arg : arguments)
119 argv[i++] = (
char *)arg.c_str();
121 if (execv(script.c_str(), argv) < 0) {
122 cout <<
"execv() failed - " << strerror(errno) << endl;
123 quick_exit(EXIT_FAILURE);
127 argv[0] = (
char *)
"env";
129 for (
auto & setting : environment)
130 argv[i++] = (
char *)setting.c_str();
131 argv[i++] = (
char *)script.c_str();
132 for (
auto & arg : arguments)
133 argv[i++] = (
char *)arg.c_str();
135 if (execvp(
"env", argv) < 0) {
136 cout <<
"execv() failed - " << strerror(errno) << endl;
137 quick_exit(EXIT_FAILURE);
143 const char *usage[] = {
145 "usage: ht_cluster [-f <fname>] [<environment>] <task> [<arg> ...]",
146 "usage: ht_cluster <options>",
150 " --clear-cache Clear command script cache directory (~/.cluster)",
151 " --display-json Display JSON representation of cluster definition",
153 " --display-script Display script compiled from cluster definition",
155 " -e, --explain <task> Display long description for <task>",
156 " -f <fname> Use <fname> for the cluster definition file",
157 " --help Display this help message",
158 " -T, --tasks List all tasks with short description of each",
159 " --version Display version string",
161 "This program is used to run tasks defined in the cluster definition file.",
162 "The default name of the cluster definition file is \"cluster.def\" and is",
163 "expected to be located in the current working directory, or if not found",
164 "there, in the directory ../conf relative to the directory containing the",
165 "ht_cluster executable file. The location of the cluster definition file",
166 "can also be specified explicitly with the -f option.",
168 "If a task name (with optional arguments) is supplied on the command line,",
169 "the corresponding task will be run. Optionally, environment variables",
170 "can be set by supplying a list of space-separated <name>=<value> variable",
171 "specifications where <name> is the name of the environment variable to set",
172 "and <value> is the value you would like to set it to prior to running the",
173 "task. The environment variables must appear before the task name on the",
176 "If ht_cluster is run with no arguments, it will enter interactive mode.",
177 "In this mode tasks or any shell command can be run interactively. Tasks",
178 "can be run on their default roles or any set of machines matching a host",
179 "specification. Shell commands can be run on any set of roles or on any",
180 "set of machines matching a host specification. Type 'help' at the",
181 "interactive command prompt for details on the format of interactive",
190 int main(
int argc,
char **argv) {
197 vector<string> environment;
198 string definition_file;
199 vector<string> arguments;
200 bool display_script {};
201 bool display_json {};
210 if (!strcmp(argv[1],
"--help") || !strcmp(argv[1],
"-h")) {
211 for (
size_t i=0; usage[i]; i++)
212 cout << usage[i] <<
"\n";
214 quick_exit(EXIT_SUCCESS);
216 else if (!strcmp(argv[1],
"--version")) {
218 quick_exit(EXIT_SUCCESS);
220 else if (!strcmp(argv[1],
"--clear-cache")) {
222 quick_exit(EXIT_SUCCESS);
224 else if (!strcmp(argv[i],
"-f")) {
225 if (!definition_file.empty()) {
226 cout <<
"error: -f option supplied multiple times" << endl;
227 quick_exit(EXIT_FAILURE);
231 cout <<
"error: missing argument to -f option" << endl;
232 quick_exit(EXIT_FAILURE);
234 definition_file.append(argv[i]);
238 else if (!strcmp(argv[i],
"--display-script")) {
239 display_script =
true;
241 else if (!strcmp(argv[i],
"--display-json")) {
244 else if (!is_environment_setting(argv[i]))
246 environment.push_back(argv[i]);
252 arguments.push_back(argv[i]);
256 if (definition_file.empty())
257 definition_file = locate_definition_file();
259 if (display_script) {
262 if (system(cmd.c_str()) != 0) {
263 cout <<
"Failed execution: " << cmd << endl;
264 quick_exit(EXIT_FAILURE);
266 quick_exit(EXIT_SUCCESS);
268 else if (display_json) {
269 cout <<
ToJson(definition_file).
str() << endl;
275 if (!arguments.empty())
276 exec_command(compiler.
output_script(), environment, arguments);
278 interp = make_shared<ClusterCommandInterpreter>(compiler.
output_script());
279 shell = make_shared<CommandShell>(
"cluster",
"Cluster", interp,
properties);
282 shell->set_line_command_mode(
true);
284 interp->set_silent(shell->silent());
285 interp->set_test_mode(shell->test_mode());
287 status = shell->run();
Retrieves system information (hardware, installation directory, etc)
string str()
Returns pathname of output script.
PropertiesPtr properties
This singleton map stores all options.
static void initialize(uint16_t reactor_count)
Initializes I/O reactors.
static void initialize(const String &install_directory=String())
Initializes the static class members; checks header version vs.
static bool exists(const String &fname)
Checks if a file or directory exists.
static int32_t get_processor_count()
The processor count.
string output_script()
Returns pathname of output script.
File system utility functions.
Compiles a cluster definition file into an executable bash script.
const char * get_text(int error)
Returns a descriptive error message.
bool status(ContextPtr &context, Timer &timer, Status &status)
Runs a status check on the master.
Compatibility Macros for C/C++.
Initialization helper for applications.
std::shared_ptr< CommandInterpreter > CommandInterpreterPtr
std::shared_ptr< CommandShell > CommandShellPtr
Declarations for Compiler.
static String install_dir
The installation directory.
Compiles a cluster definition file into an executable bash script.
int main(int argc, char **argv)
This is a generic exception class for Hypertable.
Declarations for ClusterCommandInterpreter.
Cluster definition file translation definitions.
const char * version_string()
int code() const
Returns the error code.