예제 #1
0
  //	/containers/4fa6e0f0c678/logs?stderr=1&stdout=1&timestamps=1&follow=1&tail=10
  // @GET
  // @Path("search")
  // public String searchImage(@QueryParam("imageName") String name)
  // throws HibernateException {
  // try {
  // DockerClient dockerClient = MyDockerClient.getDockerClient();
  // List<SearchItem> items= dockerClient.searchImagesCmd(name).exec();
  // return ParseToReponse.parse("1", "all search info", items, items.size());
  // } catch (Exception e) {
  // // TODO: handle exception
  // e.printStackTrace();
  // return ParseToReponse.parse("2", e.getMessage(), null, 0);
  // }
  //
  // }
  //
  //
  @DELETE
  @Path("{id}")
  public Response deleteUser(@PathParam("id") int id) {
    try {
      String queryString = "from Container where id=?";
      beginTransaction();
      Query query = session.createQuery(queryString);
      query.setParameter(0, id);
      List<model.Container> containers = query.list();
      if (containers.size() < 1) {
        return ParseToReponse.parse("3", "no such container", null, 0);
      }
      String nameString = containers.get(0).getHostName() + "/" + containers.get(0).getName();
      DockerClient dockerClient = MyDockerClient.getDockerClient();
      // dockerClient.stopContainerCmd(nameString).exec();
      // dockerClient.killContainerCmd(nameString).exec();
      dockerClient.removeContainerCmd(nameString).exec();

      beginTransaction();
      model.Container container = (model.Container) session.load(model.Container.class, id);
      session.delete(container);
      endTransaction(true);
      return ParseToReponse.parse("1", "delete container successfully", null, 0);
    } catch (Exception e) {
      e.printStackTrace();
      return ParseToReponse.parse("2", e.getMessage(), null, 0);
      // TODO: handle exception
    }
  }
예제 #2
0
  @POST
  public Response createContainer(
      @FormParam("containerName") String containerName,
      @FormParam("userId") String userId,
      @FormParam("cmd") String cmd,
      @FormParam("imageName") String imageName)
      throws HibernateException {
    DockerClient dockerClient = MyDockerClient.getDockerClient();
    try {
      String[] cmdarr = {};
      if (!("".equals(cmd) || cmd == null)) cmdarr = cmd.split(" ");
      CreateContainerResponse ccResponse =
          dockerClient
              .createContainerCmd(imageName)
              .withName(containerName)
              .withTty(true)
              .withStdinOpen(true)
              .withCmd(cmdarr)
              .exec();
      System.out.println("have created" + ccResponse.toString());
      List<Container> containers = dockerClient.listContainersCmd().withShowAll(true).exec();
      String fullNameString = null;
      for (int i = 0; i < containers.size(); i++) {
        if (containers.get(i).getId().equals(ccResponse.getId())) {
          fullNameString = containers.get(i).getNames()[0];
          System.out.println("find it" + fullNameString);
        }
      }
      String hostName = fullNameString.split("/")[1];
      // Map<String, String> mes = new HashMap<String, String>();
      // mes.put("id", ccResponse.getId());
      // mes.put("assignHost", hostName);
      model.Container con =
          new model.Container(
              containerName, Integer.valueOf(userId), imageName, ccResponse.getId(), hostName);
      beginTransaction();
      Serializable s = session.save(con);

      endTransaction(true);
      System.out.println(s);

      return ParseToReponse.parse("1", "createContainer successfully", "id:" + s, 0);
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      return ParseToReponse.parse("1", e.getMessage(), "id:", 0);
    }

    // String queryString = "from Image where name=?";
    // beginTransaction();
    // Query query = session.createQuery(queryString);
    // query.setParameter(0, imageFullNname);
    // List<Image> image1 = query.list();
    // if(image1.size()>0){
    // beginTransaction();
    // Image Image2 = (Image) session.load(Image.class,
    // image1.get(0).getId());
    // session.delete(Image2);
    // endTransaction(true);
    // }

    // Image image = new Image(imageFullNname, Integer.valueOf(isPublic),
    // Integer.valueOf(userId), imageLongId);
    // beginTransaction();
    // Serializable s = session.save(image);
    // endTransaction(true);
    // System.out.println(s);

  }