61 startScenario(options.
command, localScen, options.
print, armarXManager->getIceManager()->getIceGridSession()->getAdmin(), options.
parameters);
66 if (app.get() !=
nullptr)
68 startApplication(options.
command, app, options.
print, armarXManager->getIceManager()->getIceGridSession()->getAdmin(), options.
parameters);
73 optionsManager.
showHelp(options,
"Could not find application '" + options.
applicationName +
"' in scenario '" + localScen->getName() +
"'");
80 std::cout <<
"Finished" << std::endl;
85 armarXManager->shutdown();
88 armarXManager->waitForShutdown();
92 int ScenarioCli::run(
int argc,
char* argv[])
95 using namespace Parser;
96 using namespace Data_Structure;
103 std::cout <<
"Failed to parse arguments. exiting";
106 ScenarioManagerCliOptions cliOptionsManager;
107 options = cliOptionsManager.parseCmdOptions(argc, argv);
110 if (!options.parameters.empty())
112 std::cout <<
"Additional command line parameters are: " << options.parameters << std::endl;
116 if (options.showHelp || options.error)
120 cliOptionsManager.showHelp(options,
"Could not parse input parameters");
123 cliOptionsManager.showHelp(options);
128 if (options.print && options.command !=
"start")
130 cliOptionsManager.showHelp(options,
"Print is only a valid option for the start command");
133 else if (options.wait && (options.command ==
"list" || options.command ==
"status"))
135 cliOptionsManager.showHelp(options,
"Wait is not valid for the list or status command");
139 PackageBuilder builder;
142 if (options.command.compare(
"list") == 0)
144 std::cout <<
"Listing all Scenarios out of default Packages" << std::endl;
147 std::cout <<
"Scenarios in " <<
package << ":" << std::endl;
148 PackagePtr currentPackage = builder.parsePackage(package);
149 if (currentPackage.get() ==
nullptr)
151 std::cout <<
"Package " <<
package << " is listed in default packages, but could not be found" << std::endl << std::endl << std::endl;
154 for (
auto scenario : * (currentPackage->getScenarios()))
156 std::cout << scenario->getName() << std::endl;
158 std::cout << std::endl << std::endl;
163 std::string packageName = options.packageName;
166 if (options.packageName.empty())
168 if (std::filesystem::is_regular_file(std::filesystem::path(options.scxPath))
169 || std::filesystem::is_directory(std::filesystem::path(options.scxPath)))
172 std::string filePackageName;
173 if (std::filesystem::is_regular_file(std::filesystem::path(options.scxPath)))
175 filePackageName =
parser.getPackageNameFromScx(options.scxPath);
179 filePackageName =
parser.getPackageNameFromScx((std::filesystem::path(options.scxPath) / std::filesystem::path(scenarioName +
".scx")).string());
182 if (filePackageName !=
"")
184 packageName = filePackageName;
188 packageName = packageChoosingDialog(scenarioName);
189 if (packageName.empty())
191 options.error =
true;
192 cliOptionsManager.showHelp(options,
"Could not find any package for " + options.scxPath +
". Please add a PackageName or Add the PackageName to your default config.");
199 packageName = packageChoosingDialog(scenarioName);
200 if (packageName.empty())
202 options.error =
true;
203 cliOptionsManager.showHelp(options,
"Could not find any package for " + options.scxPath +
". Please add a PackageName or Add the PackageName to your default config.");
210 if (scenarioName.empty())
212 options.error =
true;
213 cliOptionsManager.showHelp(options,
"Could not find Scenario " + options.scxPath +
" in Package " + packageName);
220 CMakePackageFinder finder(packageName);
221 if (!finder.packageFound())
223 options.error =
true;
224 cliOptionsManager.showHelp(options,
"CMakePackageFinder could not find package '" + packageName +
"'");
229 std::vector<std::string> scenarios =
parser.getScenariosFromFolder(finder.getScenariosDir());
231 for (
auto scen : scenarios)
233 auto sFolderPos = scen.rfind(
"/");
234 if ((sFolderPos == std::string::npos && scen == scenarioName)
235 || (sFolderPos != std::string::npos && scen.substr(sFolderPos + 1) == scenarioName))
237 package = builder.parsePackage(packageName, StringList {scen + "::Package::" + packageName});
242 if (package.get() ==
nullptr)
244 cliOptionsManager.showHelp(options,
"Failed to parse package " + packageName);
248 scenario = package->getScenarioByName(scenarioName);
250 if (scenario.get() ==
nullptr)
252 options.
error =
true;
253 cliOptionsManager.showHelp(options,
"Could not find Scenario " + scenarioName +
" in Package " + package->getName());
257 if (options.command ==
"generate")
266 localScen = scenario;
268 const char* n_argv[] = {argv[0],
"--ArmarX.RedirectStdout=false"};
272 std::string ScenarioCli::packageChoosingDialog(std::string scenarioName)
275 using namespace Parser;
276 using namespace Data_Structure;
277 using namespace Exec;
278 StringList packages = PackageBuilder::FilterPackagesForScenario(scenarioName);
280 if (packages.size() == 0)
284 else if (packages.size() == 1)
288 else if (packages.size() > 1)
291 while (answer > packages.size())
293 std::cout <<
"Found multiple possible packages for this Scenario path, please select: " << std::endl;
295 for (
auto package : packages)
297 std::cout << number <<
") " <<
package << std::endl;
300 std::cout <<
"Please enter a number now: " << std::endl;
302 std::string answerString;
303 std::cin >> answerString;
306 answer = boost::lexical_cast<size_t>(answerString);
310 std::cout <<
"ERROR: Could not convert input to a number - please type in a number between 1 and " << packages.size() << std::endl << std::endl;
314 if (answer > packages.size() || answer == 0)
321 return packages[answer];
330 using namespace Exec;
331 StopStrategyFactory stopStrategyFactory;
332 Executor executor(stopStrategyFactory.getStopStrategy(StopperFactory::getFactory()->getPidStopper()), StarterFactory::getFactory()->getStarter());
334 std::future<void> result;
339 for (
auto app : *scenario->getApplications())
341 app->setStatus(executor.getApplicationStatus(app));
345 if (command ==
"status")
347 std::cout <<
"Status of " << scenario->getName() <<
" is: ";
370 std::cout << std::endl;
372 for (
auto app : *scenario->getApplications())
374 std::cout <<
"\t " << app->getName() <<
" : ";
392 std::cout << std::endl;
396 else if (command ==
"periodic_status")
400 std::cout <<
"Status of " << scenario->getName() <<
" is: ";
402 if (scenario->getStatus() ==
"Running")
404 coloredStdOut(
"Running", 92);
406 else if (scenario->getStatus() ==
"Stopped")
408 coloredStdOut(
"Stopped", 31);
410 else if (scenario->getStatus() ==
"Mixed")
412 coloredStdOut(
"Mixed", 93);
414 std::cout << std::endl;
415 for (
auto app : *scenario->getApplications())
417 app->setStatus(executor.getApplicationStatus(app));
422 else if (command ==
"start")
426 std::cout <<
"Starting Scenario ";
428 result = executor.startScenario(scenario, printOnly, commandLineParameters);
430 else if (command ==
"stop")
432 std::cout <<
"Stopping Scenario ";
433 result = executor.stopScenario(scenario);
435 else if (command ==
"kill")
437 std::cout <<
"Killing Scenario ";
438 executor.setStopStrategy(stopStrategyFactory.getStopAndKillStrategy(StopperFactory::getFactory()->getPidStopper(), 500), scenario);
440 result = executor.stopScenario(scenario);
442 else if (command ==
"restart")
445 executor.setStopStrategy(stopStrategyFactory.getStopAndKillStrategy(StopperFactory::getFactory()->getPidStopper(), 500), scenario);
447 result = executor.restartScenario(scenario, printOnly);
452 std::cout << scenario->getName() << std::endl;
461 using namespace Exec;
462 StopStrategyFactory stopStrategyFactory;
463 Executor executor(stopStrategyFactory.getStopStrategy(StopperFactory::getFactory()->getPidStopper()), StarterFactory::getFactory()->getStarter());
465 std::future<void> result;
470 app->setStatus(executor.getApplicationStatus(app));
472 if (command ==
"status")
474 std::cout <<
"Status of " << app->getName() <<
"is: ";
491 std::cout << std::endl;
494 else if (command ==
"start")
498 std::cout <<
"Starting Application ";
501 result = executor.startApplication(app, printOnly, commandLineParameters);
503 else if (command ==
"stop")
505 std::cout <<
"Stopping Application ";
506 result = executor.stopApplication(app);
508 else if (command ==
"kill")
510 std::cout <<
"Killing Application ";
511 executor.setStopStrategy(stopStrategyFactory.getStopAndKillStrategy(StopperFactory::getFactory()->getByNameStopper(), 0), app->getScenario());
512 result = executor.stopApplication(app);
514 else if (command ==
"restart")
516 std::cout <<
"Restarting Application ";
517 executor.setStopStrategy(stopStrategyFactory.getStopAndKillStrategy(StopperFactory::getFactory()->getByNameStopper(), 0), app->getScenario());
518 result = executor.restartApplication(app, printOnly);
523 std::cout << app->getName() << std::endl;
528 void ScenarioCli::coloredStdOut(std::string
message,
int colorCode)
530 std::cout <<
"\033[" << colorCode <<
"m" <<
message <<
"\033[0m";