Example #1
0
char		*key_to_value(t_list *env, char *key)
{
  t_list	*curr;

  curr = env;
  while (curr != NULL)
    {
      if (my_strcmp(get_env_key(curr->data), key) == 0)
	return (get_env_value(curr->data));
      curr = curr->next;
    }
  return (NULL);
}
/**
 * generate launch container PB request
     message ContainerLaunchContextProto {
      optional ContainerIdProto container_id = 1;
      optional string user = 2;
      optional ResourceProto resource = 3;
      repeated StringLocalResourceMapProto localResources = 4;
      optional bytes container_tokens = 5;
      repeated StringBytesMapProto service_data = 6;
      repeated StringStringMapProto environment = 7;
      repeated string command = 8;
      repeated ApplicationACLMapProto application_ACLs = 9;
    }

    message StartContainerRequestProto {
      optional ContainerLaunchContextProto container_launch_context = 1;
    }
 */
static int generate_launch_container_request(
        char** buffer,
        int* size,
        hadoop_rpc_proxy_t* proxy,
        int container_id,
        containers_launch_context_t* launch_context) {
    int rc;
    struct pbc_wmessage* req = pbc_wmessage_new(env, "StartContainerRequestProto");
    if (!req) {
        opal_output(0, "get StartContainerRequestProto message failed.\n");
        return -1;
    }

    struct pbc_wmessage* ctx = pbc_wmessage_message(req, "container_launch_context");
    if (!ctx) {
        opal_output(0, "get container_launch_context from StartContainerRequestProto failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }

    // set container_id
    struct pbc_wmessage* id_proto = pbc_wmessage_message(ctx, "container_id");
    if (!id_proto) {
        opal_output(0, "get ContainerIdProto from ContainerLaunchContextProto failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }
    rc = pbc_wmessage_integer(id_proto, "id", container_id, NULL);
    if (0 != rc) {
        opal_output(0, "pack container-id failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }
    rc = set_app_attempt_id(id_proto, "app_attempt_id", proxy);
    if (0 != rc) {
        opal_output(0, "pack app_attempt_id failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }
    rc = set_app_id(id_proto, "app_id", proxy);
    if (0 != rc) {
        opal_output(0, "pack app_id failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }

    // pack user
    rc = pbc_wmessage_string(ctx, "user", getlogin(), 0);
    if (rc != 0) {
        opal_output(0, "pack user name failed.\n");
        return -1;
    }

    // pack resource
    struct pbc_wmessage* res_msg = pbc_wmessage_message(ctx, "resource");
    if (!res_msg) {
        opal_output(0, "get resource_proto from context failed.\n");
        return -1;
    }
    rc = pbc_wmessage_integer(res_msg, "memory", launch_context->resource.memory_per_slot, NULL);
    if (rc != 0) {
        pbc_wmessage_delete(req);
        opal_output(0, "pack memory to resource failed.\n");
        return -1;
    }
    // TODO, in 2.0.3, need pack cpu

    // pack localResources
    rc = set_local_resources(ctx, "localResources");
    if (rc != 0) {
        pbc_wmessage_delete(req);
        opal_output(0, "pack local resources failed.\n");
        return -1;
    }

    // pack env
    int offset = 0;
    if (launch_context->env) {
        while (launch_context->env[offset]) {
            struct pbc_wmessage* env_msg = pbc_wmessage_message(ctx, "environment");
            if (!env_msg) {
                pbc_wmessage_delete(req);
                opal_output(0, "get env message from context failed.\n");
                return -1;
            }

            char* key = get_env_key(launch_context->env[offset]);
            char* val = get_env_val(launch_context->env[offset]);
            if ((!key) || (!val)) {
                if (key) {
                    free(key);
                }
                if (val) {
                    free(val);
                }
                pbc_wmessage_delete(req);
                opal_output(0, "get env key or value failed, env=%s.\n", launch_context->env[offset]);
                return -1;
            }

            // pack key
            rc = pbc_wmessage_string(env_msg, "key", key, 0);
            free(key);
            if (rc != 0) {
                free(val);
                pbc_wmessage_delete(req);
                opal_output(0, "set key to environment failed.\n");
                return -1;
            }
            // pack val
            rc = pbc_wmessage_string(env_msg, "value", val, 0);
            if (rc != 0) {
                free(val);
                pbc_wmessage_delete(req);
                opal_output(0, "set value to environment failed.\n");
                return -1;
            }
            free(val);
            offset++;
        }
    }

    // pack $PATH, $LD_LIBRARY_PATH, $DYLD_LIBRARY_PATH, $CLASSPATH to env
    if (0 != (rc = pack_env_to_launch_ctx("PATH", ctx))) {
        pbc_wmessage_delete(req);
        return -1;
    }
    if (0 != (rc = pack_env_to_launch_ctx("LD_LIBRARY_PATH", ctx))) {
        pbc_wmessage_delete(req);
        return -1;
    }
    if (0 != (rc = pack_env_to_launch_ctx("DYLD_LIBRARY_PATH", ctx))) {
        pbc_wmessage_delete(req);
        return -1;
    }
    if (0 != (rc = pack_env_to_launch_ctx("CLASSPATH", ctx))) {
        pbc_wmessage_delete(req);
        return -1;
    }

    // pack command
    char* command = concat_argv_to_cmd(launch_context->argv);
    if (!command) {
        pbc_wmessage_delete(req);
        opal_output(0, "concat argv to command to command failed. argv[0]:%s.\n", launch_context->argv[0]);
        return -1;
    }
    rc = pbc_wmessage_string(ctx, "command", command, 0);
    free(command);
    if (rc != 0) {
        opal_output(0, "pack command to context failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }

    struct pbc_slice slice;
    pbc_wmessage_buffer(req, &slice);

    /* try to create HadoopRpcRequestProto */
    rc = generate_hadoop_request((const char*)(slice.buffer),
        slice.len,
        CONTAINER_MANAGER_PROTOCOL_NAME,
        START_CONTAINER_METHOD_NAME,
        buffer,
        size);
    if (0 != rc) {
        opal_output(0, "create HadoopRpcRequestProto failed.\n");
        pbc_wmessage_delete(req);
        return -1;
    }

    pbc_wmessage_delete(req);
    return 0;
}