Пример #1
0
  public WebController() {

    get("/", (request, response) -> "<h1>Welcome Cloudeply!</h1>");

    /** 创建新主机 POST /host/new */
    post(
        "/host/new",
        (request, response) -> {
          InstanceForm form = new InstanceForm();
          form.setUser(request.queryParams("user"));
          form.setPlatform(request.queryParams("platform"));
          form.setRegion(request.queryParams("region"));
          form.setOsName(
              StringUtils.defaultString(request.queryParams("osName"), "CentOS").toLowerCase());
          form.setOsBit(request.queryParams("osBit"));
          form.setOsVersion(request.queryParams("osVersion"));
          form.setCpu(request.queryParams("cpu"));
          form.setMemory(request.queryParams("memory"));
          form.setPassword(request.queryParams("password"));
          form.setInternetChargeType(request.queryParams("internetChargeType"));
          form.setInternetMaxBandwidthOut(request.queryParams("internetMaxBandwidthOut"));
          logger.debug("new host param:{}", form);
          JsonObject result2json = new JsonObject();
          // validate
          if (StringUtils.isEmpty(form.getUser())) {
            result2json.add("user", "is not empty");
          }
          if (form.getOsBit() != null && !StringUtils.isNumeric(form.getOsBit())) {
            result2json.add("osBit", "is not number");
          }
          if (form.getCpu() != null && !StringUtils.isNumeric(form.getCpu())) {
            result2json.add("cpu", "is not number");
          }
          if (form.getMemory() != null && !StringUtils.isNumeric(form.getMemory())) {
            result2json.add("memory", "is not number");
          }
          if (!result2json.isEmpty()) {
            response.status(HttpStatus.BAD_REQUEST_400);
            return result2json;
          }

          InstanceProxy instanceProxy = new InstanceProxy();
          try {
            InstanceResult r = instanceProxy.newInstance(form);
            // add user record
            UserInstance u = new UserInstance();
            u.setInstanceId(r.getInstanceId());
            u.setInstanceIp(r.getIp());
            u.setUser(form.getUser());
            u.setPlatform(form.getPlatform());
            UserInstanceHelper userInstanceHelper = new UserInstanceHelper();
            userInstanceHelper.add(u);
            return result2json.add("instanceId", r.getInstanceId()).add("ip", r.getIp());
          } catch (DeployException de) {
            response.status(HttpStatus.INTERNAL_SERVER_ERROR_500);
            return result2json.add("code", de.getCode()).add("message", de.getMessage());
          }
        });

    /**
     * 查询主机状态 GET /host/status Param: instanceId
     *
     * <p>json: {result: Running, Starting, Stopped}
     */
    get(
        "/host/status",
        (request, response) -> {
          String instanceId = request.queryParams("instanceId");
          logger.debug("param instanceId:{}", instanceId);
          JsonObject result2json = new JsonObject();
          if (StringUtils.isEmpty(instanceId)) {
            response.status(HttpStatus.BAD_REQUEST_400);
            result2json.add("instanceId", "param is not empty");
            return result2json;
          }

          AliyunInstanceService instanceService = new AliyunInstanceService();
          try {
            InstanceStatus status = instanceService.describeInstanceAttribute(instanceId);

            return result2json
                .add("instanceId", status.getInstanceId())
                .add("status", status.getStatus())
                .add("publicIp", status.getPublicIp())
                .add("innerIp", status.getInnerIp());
          } catch (DeployException de) {
            response.status(HttpStatus.INTERNAL_SERVER_ERROR_500);
            return result2json.add("code", de.getCode()).add("message", de.getMessage());
          }
        });

    /**
     * 执行ansible命令 POST /ansible/send Content-Type:application/x-www-form-urlencoded Param: command
     *
     * <p>Result: responseStatus == 200:成功, 400:参数错误, 500:系统错误 responseBody
     */
    post(
        "/ansible/send",
        (request, response) -> {
          JsonObject result2json = new JsonObject();
          String command = request.queryParams("command");
          logger.debug("command param:{}", command);
          if (StringUtils.isEmpty(command)) {
            response.status(HttpStatus.BAD_REQUEST_400);
            return result2json.add("command", "param is not empty");
          }
          command =
              StringUtils.toEncodedString(Base64.decodeBase64(command), Charset.defaultCharset());
          logger.debug("command decode:{}", command);
          ProcessBuilder pb = new ProcessBuilder();
          pb.command("/bin/sh", "-c", command);
          try {
            Process p = pb.start();
            String result = IOUtils.toString(p.getErrorStream(), Charset.defaultCharset());
            logger.info("command process:\n{}", result);
            return result2json.add("result", result);
          } catch (IOException ioe) {
            logger.error("command execute error", ioe);
            response.status(HttpStatus.INTERNAL_SERVER_ERROR_500);
            return result2json.add("error", ioe.getMessage());
          }
        });
  }
Пример #2
0
  public static void main(String[] args) {
    ProcessBuilder process = new ProcessBuilder();
    Integer port;
    if (process.environment().get("PORT") != null) {
      port = Integer.parseInt(process.environment().get("PORT"));
    } else {
      port = 4567;
    }

    staticFileLocation("/public");
    String layout = "templates/layout.vtl";

    get(
        "/",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/index.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
    // get(/) type of route get this url..

    get(
        "/tasks",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("tasks", Task.all());
          model.put("template", "templates/tasks.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
    // After they submit the form, this is where they will be taken /tasks.vtl

    //  get("tasks/new", (request, response) -> {
    //    HashMap<String, Object> model = new HashMap<String, Object>();
    //    model.put("template", "templates/task-form.vtl");
    //    return new ModelAndView(model, layout);
    //  }, new VelocityTemplateEngine());
    //  //task-form is where client inputs data and hits submit

    post(
        "/tasks",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Category category = Category.find(Integer.parseInt(request.queryParams("categoryId")));
          String description = request.queryParams("description");
          Task newTask = new Task(description);
          category.addTask(newTask);
          model.put("category", category);
          model.put("template", "templates/success.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
    // grabs information and makes a new description of the information in the array
    // takes you to a new page

    get(
        "/tasks/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          Task task = Task.find(Integer.parseInt(request.params(":id")));
          model.put("task", task);
          model.put("template", "templates/task.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/categories",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("categories", Category.all());
          model.put("template", "templates/categories.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/categories/new",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("template", "templates/category-form.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    post(
        "/categories",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          String name = request.queryParams("name");
          Category newCategory = new Category(name);
          model.put("category", newCategory);
          model.put("template", "templates/success.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/categories/:id",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("category", Category.find(Integer.parseInt(request.params(":id"))));
          model.put("template", "templates/category.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());

    get(
        "/categories/:id/tasks/new",
        (request, response) -> {
          HashMap<String, Object> model = new HashMap<String, Object>();
          model.put("category", Category.find(Integer.parseInt(request.params(":id"))));
          model.put("template", "templates/category-tasks-form.vtl");
          return new ModelAndView(model, layout);
        },
        new VelocityTemplateEngine());
  } // end of main