use of co.cask.cdap.cli.exception.CommandInputError in project cdap by caskdata.
the class AbstractCommand method getTimestamp.
/**
* Returns a timestamp in milliseconds.
*
* @param arg The string argument user provided.
* @param base The base timestamp to relative from if the time format provided is a relative time.
* @return Timestamp in milliseconds
* @throws co.cask.cdap.cli.exception.CommandInputError if failed to parse input.
*/
protected long getTimestamp(String arg, long base) {
try {
if (arg.startsWith("+") || arg.startsWith("-")) {
int dir = arg.startsWith("+") ? 1 : -1;
char type = arg.charAt(arg.length() - 1);
int offset = Integer.parseInt(arg.substring(1, arg.length() - 1));
switch(type) {
case 's':
return base + dir * TimeUnit.SECONDS.toMillis(offset);
case 'm':
return base + dir * TimeUnit.MINUTES.toMillis(offset);
case 'h':
return base + dir * TimeUnit.HOURS.toMillis(offset);
case 'd':
return base + dir * TimeUnit.DAYS.toMillis(offset);
default:
throw new CommandInputError(this, "Unsupported relative time format: " + type);
}
}
if (arg.equalsIgnoreCase("min")) {
return 0L;
}
if (arg.equalsIgnoreCase("max")) {
return Long.MAX_VALUE;
}
return Long.parseLong(arg);
} catch (NumberFormatException e) {
throw new CommandInputError(this, "Invalid number value: " + arg + ". Reason: " + e.getMessage());
}
}
use of co.cask.cdap.cli.exception.CommandInputError in project cdap by caskdata.
the class CallServiceCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String method = arguments.get(ArgumentName.HTTP_METHOD.toString());
String path = arguments.get(ArgumentName.ENDPOINT.toString());
path = path.startsWith("/") ? path.substring(1) : path;
String headers = arguments.getOptional(ArgumentName.HEADERS.toString(), "");
String bodyString = arguments.getOptional(ArgumentName.HTTP_BODY.toString(), "");
String bodyFile = arguments.getOptional(ArgumentName.LOCAL_FILE_PATH.toString(), "");
Preconditions.checkNotNull(bodyString);
Preconditions.checkNotNull(bodyFile);
if (!bodyString.isEmpty() && !bodyFile.isEmpty()) {
String message = String.format("Please provide either [body <%s>] or [body:file <%s>], " + "but not both", ArgumentName.HTTP_BODY.toString(), ArgumentName.LOCAL_FILE_PATH.toString());
throw new CommandInputError(this, message);
}
Map<String, String> headerMap = GSON.fromJson(headers, new TypeToken<Map<String, String>>() {
}.getType());
ServiceId service = new ServiceId(parseProgramId(arguments, ElementType.SERVICE));
URL url = arguments.hasArgument(ArgumentName.APP_VERSION.getName()) ? new URL(serviceClient.getVersionedServiceURL(service), path) : new URL(serviceClient.getServiceURL(service), path);
HttpMethod httpMethod = HttpMethod.valueOf(method);
HttpRequest.Builder builder = HttpRequest.builder(httpMethod, url).addHeaders(headerMap);
if (httpMethod == HttpMethod.GET && (!bodyFile.isEmpty() || !bodyString.isEmpty())) {
throw new UnsupportedOperationException("Sending body in a GET request is not supported");
}
if (!bodyFile.isEmpty()) {
builder.withBody(filePathResolver.resolvePathToFile(bodyFile));
} else if (!bodyString.isEmpty()) {
builder.withBody(bodyString);
}
HttpResponse response = restClient.execute(builder.build(), clientConfig.getAccessToken());
output.printf("< %s %s\n", response.getResponseCode(), response.getResponseMessage());
for (Map.Entry<String, String> header : response.getHeaders().entries()) {
output.printf("< %s: %s\n", header.getKey(), header.getValue());
}
output.print(response.getResponseBodyAsString());
}
use of co.cask.cdap.cli.exception.CommandInputError in project cdap by caskdata.
the class GetProgramInstancesCommand method perform.
@Override
@SuppressWarnings("deprecation")
public void perform(Arguments arguments, PrintStream output) throws Exception {
String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
ApplicationId appId = cliConfig.getCurrentNamespace().app(programIdParts[0]);
int instances;
switch(elementType) {
case FLOWLET:
if (programIdParts.length < 3) {
throw new CommandInputError(this);
}
String flowId = programIdParts[1];
String flowletName = programIdParts[2];
FlowletId flowlet = appId.flow(flowId).flowlet(flowletName);
instances = programClient.getFlowletInstances(flowlet);
break;
case WORKER:
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String workerId = programIdParts[1];
ProgramId worker = appId.worker(workerId);
instances = programClient.getWorkerInstances(worker);
break;
case SERVICE:
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String serviceName = programIdParts[1];
instances = programClient.getServiceInstances(appId.service(serviceName));
break;
default:
// TODO: remove this
throw new IllegalArgumentException("Unrecognized program element type for scaling: " + elementType);
}
output.println(instances);
}
use of co.cask.cdap.cli.exception.CommandInputError in project cdap by caskdata.
the class GetProgramLiveInfoCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String appId = programIdParts[0];
String programName = programIdParts[1];
ProgramId program = cliConfig.getCurrentNamespace().app(appId).program(elementType.getProgramType(), programName);
DistributedProgramLiveInfo liveInfo = programClient.getLiveInfo(program);
if (liveInfo == null) {
output.println("No live info found");
return;
}
Table table = Table.builder().setHeader("app", "type", "id", "runtime", "yarn app id").setRows(ImmutableList.of(liveInfo), new RowMaker<DistributedProgramLiveInfo>() {
@Override
public List<?> makeRow(DistributedProgramLiveInfo object) {
return Lists.newArrayList(object.getApp(), object.getType(), object.getName(), object.getRuntime(), object.getYarnAppId());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, table);
if (liveInfo.getContainers() != null) {
Table containersTable = Table.builder().setHeader("containers", "instance", "host", "container", "memory", "virtual cores", "debug port").setRows(liveInfo.getContainers(), new RowMaker<Containers.ContainerInfo>() {
@Override
public List<?> makeRow(Containers.ContainerInfo object) {
return Lists.newArrayList("", object.getInstance(), object.getHost(), object.getContainer(), object.getMemory(), object.getVirtualCores(), object.getDebugPort());
}
}).build();
cliConfig.getTableRenderer().render(cliConfig, output, containersTable);
}
}
use of co.cask.cdap.cli.exception.CommandInputError in project cdap by caskdata.
the class GetProgramLogsCommand method perform.
@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
String appId = programIdParts[0];
String startString = arguments.getOptional(ArgumentName.START_TIME.toString(), "0");
long start = TimeMathParser.parseTimeInSeconds(startString);
String stopString = arguments.getOptional(ArgumentName.END_TIME.toString(), Long.toString(Integer.MAX_VALUE));
long stop = TimeMathParser.parseTimeInSeconds(stopString);
String logs;
if (elementType.getProgramType() != null) {
if (programIdParts.length < 2) {
throw new CommandInputError(this);
}
String programName = programIdParts[1];
ProgramId programId = cliConfig.getCurrentNamespace().app(appId).program(elementType.getProgramType(), programName);
logs = programClient.getProgramLogs(programId, start, stop);
} else {
throw new IllegalArgumentException("Cannot get logs for " + elementType.getNamePlural());
}
output.println(logs);
}
Aggregations