示例#1
0
 /** Run all paragraphs sequentially. */
 public synchronized void runAll() {
   String cronExecutingUser = (String) getConfig().get("cronExecutingUser");
   if (null == cronExecutingUser) {
     cronExecutingUser = "******";
   }
   for (Paragraph p : getParagraphs()) {
     if (!p.isEnabled()) {
       continue;
     }
     AuthenticationInfo authenticationInfo = new AuthenticationInfo();
     authenticationInfo.setUser(cronExecutingUser);
     p.setAuthenticationInfo(authenticationInfo);
     run(p.getId());
   }
 }
示例#2
0
 public void persist(AuthenticationInfo subject) throws IOException {
   Preconditions.checkNotNull(subject, "AuthenticationInfo should not be null");
   stopDelayedPersistTimer();
   snapshotAngularObjectRegistry(subject.getUser());
   index.updateIndexDoc(this);
   repo.save(this, subject);
 }
  private InterpreterContext getInterpreterContext(InterpreterOutput output) {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;

    if (!factory.getInterpreterSettings(note.getId()).isEmpty()) {
      InterpreterSetting intpGroup = factory.getInterpreterSettings(note.getId()).get(0);
      registry = intpGroup.getInterpreterGroup(getUser(), note.getId()).getAngularObjectRegistry();
      resourcePool = intpGroup.getInterpreterGroup(getUser(), note.getId()).getResourcePool();
    }

    List<InterpreterContextRunner> runners = new LinkedList<>();
    for (Paragraph p : note.getParagraphs()) {
      runners.add(new ParagraphRunner(note, note.getId(), p.getId()));
    }

    final Paragraph self = this;

    Credentials credentials = note.getCredentials();
    if (authenticationInfo != null) {
      UserCredentials userCredentials =
          credentials.getUserCredentials(authenticationInfo.getUser());
      authenticationInfo.setUserCredentials(userCredentials);
    }

    InterpreterContext interpreterContext =
        new InterpreterContext(
            note.getId(),
            getId(),
            getRequiredReplName(),
            this.getTitle(),
            this.getText(),
            this.getAuthenticationInfo(),
            this.getConfig(),
            this.settings,
            registry,
            resourcePool,
            runners,
            output);
    return interpreterContext;
  }
  private InterpreterContext getInterpreterContext() {
    AngularObjectRegistry registry = null;
    ResourcePool resourcePool = null;

    if (!getNoteReplLoader().getInterpreterSettings().isEmpty()) {
      InterpreterSetting intpGroup = getNoteReplLoader().getInterpreterSettings().get(0);
      registry = intpGroup.getInterpreterGroup(note.id()).getAngularObjectRegistry();
      resourcePool = intpGroup.getInterpreterGroup(note.id()).getResourcePool();
    }

    List<InterpreterContextRunner> runners = new LinkedList<InterpreterContextRunner>();
    for (Paragraph p : note.getParagraphs()) {
      runners.add(new ParagraphRunner(note, note.id(), p.getId()));
    }

    final Paragraph self = this;

    Credentials credentials = note.getCredentials();
    if (authenticationInfo != null) {
      UserCredentials userCredentials =
          credentials.getUserCredentials(authenticationInfo.getUser());
      authenticationInfo.setUserCredentials(userCredentials);
    }

    InterpreterContext interpreterContext =
        new InterpreterContext(
            note.id(),
            getId(),
            this.getTitle(),
            this.getText(),
            this.getAuthenticationInfo(),
            this.getConfig(),
            this.settings,
            registry,
            resourcePool,
            runners,
            new InterpreterOutput(
                new InterpreterOutputListener() {
                  @Override
                  public void onAppend(InterpreterOutput out, byte[] line) {
                    updateParagraphResult(out);
                    ((ParagraphJobListener) getListener())
                        .onOutputAppend(self, out, new String(line));
                  }

                  @Override
                  public void onUpdate(InterpreterOutput out, byte[] output) {
                    updateParagraphResult(out);
                    ((ParagraphJobListener) getListener())
                        .onOutputUpdate(self, out, new String(output));
                  }

                  private void updateParagraphResult(InterpreterOutput out) {
                    // update paragraph result
                    Throwable t = null;
                    String message = null;
                    try {
                      message = new String(out.toByteArray());
                    } catch (IOException e) {
                      logger().error(e.getMessage(), e);
                      t = e;
                    }
                    setReturn(new InterpreterResult(Code.SUCCESS, out.getType(), message), t);
                  }
                }));
    return interpreterContext;
  }
 public void setAuthenticationInfo(AuthenticationInfo authenticationInfo) {
   this.authenticationInfo = authenticationInfo;
   this.user = authenticationInfo.getUser();
 }
 private boolean isUserAuthorizedToAccessInterpreter(InterpreterOption intpOpt) {
   return intpOpt.permissionIsSet()
       && hasPermission(authenticationInfo.getUser(), intpOpt.getUsers());
 }
  @Override
  protected Object jobRun() throws Throwable {
    String replName = getRequiredReplName();
    Interpreter repl = getRepl(replName);
    logger.info("run paragraph {} using {} " + repl, getId(), replName);
    if (repl == null) {
      logger.error("Can not find interpreter name " + repl);
      throw new RuntimeException("Can not find interpreter for " + getRequiredReplName());
    }
    InterpreterSetting intp = getInterpreterSettingById(repl.getInterpreterGroup().getId());
    while (intp.getStatus()
        .equals(
            org.apache.zeppelin.interpreter.InterpreterSetting.Status.DOWNLOADING_DEPENDENCIES)) {
      Thread.sleep(200);
    }
    if (this.noteHasUser() && this.noteHasInterpreters()) {
      if (intp != null
          && interpreterHasUser(intp)
          && isUserAuthorizedToAccessInterpreter(intp.getOption()) == false) {
        logger.error("{} has no permission for {} ", authenticationInfo.getUser(), repl);
        return new InterpreterResult(
            Code.ERROR,
            authenticationInfo.getUser() + " has no permission for " + getRequiredReplName());
      }
    }

    String script = getScriptBody();
    // inject form
    if (repl.getFormType() == FormType.NATIVE) {
      settings.clear();
    } else if (repl.getFormType() == FormType.SIMPLE) {
      String scriptBody = getScriptBody();
      Map<String, Input> inputs = Input.extractSimpleQueryParam(scriptBody); // inputs will be built
      // from script body

      final AngularObjectRegistry angularRegistry =
          repl.getInterpreterGroup().getAngularObjectRegistry();

      scriptBody = extractVariablesFromAngularRegistry(scriptBody, inputs, angularRegistry);

      settings.setForms(inputs);
      script = Input.getSimpleQuery(settings.getParams(), scriptBody);
    }
    logger.debug("RUN : " + script);
    try {
      InterpreterContext context = getInterpreterContext();
      InterpreterContext.set(context);
      InterpreterResult ret = repl.interpret(script, context);

      if (Code.KEEP_PREVIOUS_RESULT == ret.code()) {
        return getReturn();
      }

      context.out.flush();
      List<InterpreterResultMessage> resultMessages = context.out.toInterpreterResultMessage();
      resultMessages.addAll(ret.message());
      return new InterpreterResult(ret.code(), resultMessages);
    } finally {
      InterpreterContext.remove();
    }
  }