Search in sources :

Example 11 with CommandInputError

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());
    }
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError)

Example 12 with CommandInputError

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());
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) HttpRequest(co.cask.common.http.HttpRequest) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL) ServiceId(co.cask.cdap.proto.id.ServiceId) TypeToken(com.google.common.reflect.TypeToken) Map(java.util.Map) HttpMethod(co.cask.common.http.HttpMethod)

Example 13 with CommandInputError

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);
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) FlowletId(co.cask.cdap.proto.id.FlowletId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 14 with CommandInputError

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);
    }
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) Table(co.cask.cdap.cli.util.table.Table) DistributedProgramLiveInfo(co.cask.cdap.proto.DistributedProgramLiveInfo) RowMaker(co.cask.cdap.cli.util.RowMaker) Containers(co.cask.cdap.proto.Containers) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 15 with CommandInputError

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);
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) ProgramId(co.cask.cdap.proto.id.ProgramId)

Aggregations

CommandInputError (co.cask.cdap.cli.exception.CommandInputError)21 ProgramId (co.cask.cdap.proto.id.ProgramId)8 Table (co.cask.cdap.cli.util.table.Table)7 ApplicationId (co.cask.cdap.proto.id.ApplicationId)6 ScheduleId (co.cask.cdap.proto.id.ScheduleId)6 RowMaker (co.cask.cdap.cli.util.RowMaker)4 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)4 Schedule (co.cask.cdap.api.schedule.Schedule)2 Schedules (co.cask.cdap.api.schedule.Schedules)2 ScheduleInstanceConfiguration (co.cask.cdap.proto.ScheduleInstanceConfiguration)2 FlowletId (co.cask.cdap.proto.id.FlowletId)2 NamespaceId (co.cask.cdap.proto.id.NamespaceId)2 ServiceId (co.cask.cdap.proto.id.ServiceId)2 WorkflowActionNode (co.cask.cdap.api.workflow.WorkflowActionNode)1 WorkflowToken (co.cask.cdap.api.workflow.WorkflowToken)1 Containers (co.cask.cdap.proto.Containers)1 DistributedProgramLiveInfo (co.cask.cdap.proto.DistributedProgramLiveInfo)1 RunRecord (co.cask.cdap.proto.RunRecord)1 ScheduleDetail (co.cask.cdap.proto.ScheduleDetail)1 WorkflowId (co.cask.cdap.proto.id.WorkflowId)1