DynamicPyModelDataService.java [src/csip] Revision: default  Date:
/*
 * $Id$
 *
 * This file is part of the Cloud Services Integration Platform (CSIP),
 * a Model-as-a-Service framework, API and application suite.
 *
 * 2012-2022, Olaf David and others, OMSLab, Colorado State University.
 *
 * OMSLab licenses this file to you under the MIT license.
 * See the LICENSE file in the project root for more information.
 */
package csip;

import csip.api.server.ServiceException;
import csip.api.server.Executable;
import csip.annotations.Author;
import csip.annotations.Description;
import csip.annotations.Documentation;
import csip.annotations.License;
import csip.annotations.Name;
import csip.annotations.Resource;
import csip.annotations.ResourceType;
import csip.annotations.State;
import csip.annotations.VersionInfo;
import csip.utils.Binaries;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.util.logging.Level;
import javax.ws.rs.Path;

@Name("DynPy")
@Description("Dynamic, server-side Python execution.")
@Documentation("https://alm.engr.colostate.edu/csip")
@State(State.STABLE)
@License(License.MIT)
@VersionInfo("$Id$")
@Author(name = "od", email = "<odavid@colostate.edu>", org = "CSU")
@Path("p/dynpy/1.0")
public class DynamicPyModelDataService extends ModelDataService {

  static final String PY_SCRIPT = "py_script";


  @Override
  protected void doProcess() throws Exception {

    if (!metainfo().hasName(PY_SCRIPT)) {
      throw new ServiceException("Missing Python script in metainfo.");
    }

    String pyScript = metainfo().getString(PY_SCRIPT);
    if (!attachments().hasFile(pyScript)) {
      throw new ServiceException("Missing Python attachment: " + pyScript);
    }

    Resource res = new Resource() {
      @Override
      public String file() {
        try {
          return attachments().getFile(pyScript).toString();
        } catch (ServiceException ex) {
          LOG.log(Level.SEVERE, null, ex);
        }
        return null;
      }


      @Override
      public ResourceType type() {
        return ResourceType.PYTHON3;
      }


      @Override
      public String id() {
        return "";
      }


      @Override
      public boolean wine() {
        return false;
      }


      @Override
      public String args() {
        return "";
      }


      @Override
      public String[] env() {
        return new String[]{};
      }


      @Override
      public Class<?> from() {
        return Object.class;
      }


      @Override
      public Class<? extends Annotation> annotationType() {
        return Resource.class;
      }
    };

    Executable e = Binaries.getResourcePython(res, getWorkspaceDir0(), LOG, this.getClass());
    StringWriter err = new StringWriter();
    e.redirectError(err);
    LOG.info("running : " + e.getName());
    int ret = e.exec();
    LOG.info("done with exit value: " + ret);

    // Not Successful.
    if (ret != 0)
      throw new ServiceException(err.toString());
  }
}