Example #1
0
  @Post
  public StringRepresentation destroy(Representation entity) {
    StringRepresentation representation = null;

    JsonRepresentation represent;
    try {
      represent = new JsonRepresentation(entity);
      UserSessions sessions = UserSessions.getInstance();
      User user = sessions.getUserByRequest(getRequest());
      JSONObject obj = represent.getJsonObject();
      ClusterDao dao = new ClusterDao();

      if (user != null) {
        int clusterID = Integer.valueOf(obj.get("0") + "");
        String pwd = obj.get("1") + "";
        if (pwd.equals("")) pwd = user.getCloudSecure();

        ClusterConfiguration clusterConfig = dao.findSpecificCluster(user.getId(), clusterID);
        if (clusterConfig != null) {
          ConnectionUtil.getInputStream(clusterConfig.getCloudUsername(), pwd);

          if (clusterConfig.getState() == ClusterConfiguration.UP) {
            clusterConfig.setCloudPassword(pwd);
            clusterConfig.setActionType(ClusterConfiguration.DESTROY_CLUSTER);
            clusterConfig.setCloudgeneUser(user);
            clusterConfig.setState(ClusterConfiguration.QUEUE);

            // that it doesn't appear in queue / db at the same time
            dao.updateCluster(clusterConfig.getPk(), 1);
            // add to queue
            ClusterQueue.getInstance().submit(clusterConfig);

            // add to threadpool
            ClusterTask task = new ClusterTask(clusterConfig);
            ClusterThreadPoolDelete.getInstance().runTask(task);
          }

          getResponse().setStatus(Status.SUCCESS_OK);
        }
      }

    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      getResponse().setEntity(representation);
      return representation;
    }
    return representation;
  }
Example #2
0
  @Post
  public Representation acceptRepresentation(Representation entity) {
    Representation representation = null;
    try {
      JsonRepresentation represent = new JsonRepresentation(entity);

      UserSessions sessions = UserSessions.getInstance();
      User user = sessions.getUserByRequest(getRequest());

      JSONObject obj = represent.getJsonObject();
      if (user != null) {
        ClusterConfiguration clusterConfig = new ClusterConfiguration();
        String usr = obj.get("loginUsername").toString();
        String pwd = obj.get("loginPassword").toString();
        String program = obj.get("program").toString();
        String ssh = obj.get("key").toString();
        String name = obj.get("name").toString();
        String provider = obj.get("cluster").toString();
        String amount = obj.get("amount").toString();
        String bucket = obj.get("bucketName").toString();
        String type = obj.get("type").toString();
        String dirLog = "logs";

        /** check credentials */
        ConnectionUtil.getInputStream(usr, pwd);
        UserDao dao = new UserDao();
        if (!obj.has("saveCre")) {
          user.setCloudKey("");
          user.setCloudSecure("");
          dao.updateCredential(user);
        } else {
          user.setCloudKey(usr);
          user.setCloudSecure(pwd);
          dao.updateCredential(user);
        }
        user.setCloudKey(usr);
        user.setCloudSecure(pwd);

        /** save ssh data */
        if (ssh.equals("1")) {
          createKey(clusterConfig, user);
        } else if (ssh.equals("3")) {
          clusterConfig.setSshPrivate(user.getSshKey());
          clusterConfig.setSshPublic(user.getSshPub());
        }
        if (obj.has("saveSsh")) {
          dao.updateSSH(user);
        }
        if (obj.has("s3Export")) {
          clusterConfig.setS3Bucket(bucket);
        }

        /** CONFIGURE CLUSTER */
        Program prog = Programs.getProgramByName(program);
        clusterConfig.setProgram(prog.getCluster());
        Utils.checkDirAvailable(dirLog);
        clusterConfig.setLog(dirLog + File.separatorChar + System.currentTimeMillis() + ".txt");
        clusterConfig.setCloudID(String.valueOf(System.currentTimeMillis()));
        clusterConfig.setName(name);
        clusterConfig.getProgram().setProvider(provider);
        clusterConfig.setCloudUsername(usr);
        clusterConfig.setCloudPassword(pwd);
        clusterConfig.setAmount(Integer.valueOf(amount));
        clusterConfig.setCloudgeneUser(user);
        clusterConfig.setSSHAvailable(true);
        clusterConfig.setInstanceType(type);
        clusterConfig.setStartTime(System.currentTimeMillis());
        clusterConfig.setActionType(ClusterConfiguration.CREATE_CLUSTER);
        clusterConfig.setState(ClusterConfiguration.QUEUE);

        /** add to queue */
        ClusterQueue.getInstance().submit(clusterConfig);

        /** add to threadpool */
        ClusterTask task = new ClusterTask(clusterConfig);
        ClusterThreadPoolCreate.getInstance().runTask(task);
        getResponse().setStatus(Status.SUCCESS_OK);

      } else {
        representation = new StringRepresentation("No user");
        getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        getResponse().setEntity(representation);
        return representation;
      }
    } catch (S3ServiceException e) {
      representation = new StringRepresentation("Please check your security credentials");
      getResponse().setEntity(representation);
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      e.printStackTrace();
      return representation;
    } catch (Exception e) {
      // TODO Auto-generated catch block
      representation = new StringRepresentation("Error occured");
      getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
      getResponse().setEntity(representation);
      e.printStackTrace();
      return representation;
    }
    return representation;
  }