Esempio n. 1
0
 def get(self, request):
     conn = get_conn()
     vm_install_method = [
         ["install_method_iso", INSTALL_METHOD_ISO, 1],
         ["install_method_url", INSTALL_METHOD_URL, 0],
         ["install_method_pxe", INSTALL_METHOD_PXE, 0],
         ["install_method_import", INSTALL_METHOD_IMPORT, 0],
         ["install_method_container_app", INSTALL_METHOD_CONTAINER_APP, 0],
         ["install_method_container_o", INSTALL_METHOD_CONTAINER_OS, 0]
     ]
     vm_host_space = VMCreator.host_space(conn)
     vm_default_dir = VMCreator.default_dir(conn)
     return Reply(RESPONSE_SUCCESS,
                  data={
                      "install_method":
                      vm_install_method,
                      "host_cpu_count":
                      conn.host_active_processor_count(),
                      "host_memory":
                      int(round((conn.host_memory_size() / 1024))),
                      "host_space":
                      vm_host_space,
                      "default_dir":
                      vm_default_dir
                  },
                  status=status.HTTP_200_OK,
                  CROS=True)
Esempio n. 2
0
 def post(self, request):
     """
     POST method: Remove a new vm "delete"
     """
     conn = get_conn()
     vm_list = request.data.get("vm_list", [])
     if not len(vm_list) and "getlist" in dir(request.data):
         vm_list = request.data.getlist("vm_list[]", [])
     if not len(vm_list):
         return Reply(RESPONSE_ERROR,
                      data="query_params is required",
                      status=status.HTTP_400_BAD_REQUEST,
                      CROS=True)
     vms = []
     for vm_name in vm_list:
         vm = conn.get_vm(vm_name)
         if vm is None:
             return Reply(RESPONSE_ERROR,
                          data="query_params is invalid",
                          status=status.HTTP_400_BAD_REQUEST,
                          CROS=True)
         vms.append(vm)
     for vm in vms:
         vm.delete()
     return Reply(RESPONSE_DONE, status=status.HTTP_200_OK, CROS=True)
Esempio n. 3
0
 def post(self, request):
     """
     POST method: Create a new vm
     """
     conn = get_conn()
     vm_name = request.data.get("vm_name", None)
     vm_install_method = request.data.get("vm_install_method", None)
     vm_iso_path = request.data.get("vm_iso_path", None)
     vm_os_type = request.data.get("vm_os_type", None)
     vm_os_version = request.data.get("vm_os_version", None)
     vm_cpu = request.data.get("vm_cpu", None)
     vm_mem = request.data.get("vm_mem", None)
     vm_storage_size = request.data.get("vm_storage_size", None)
     # raise no param provide
     if not vm_name or not vm_os_type or not vm_os_version or not (
             vm_iso_path or not vm_install_method) or not (
                 vm_cpu or not vm_mem or not vm_storage_size):
         return Reply(RESPONSE_ERROR,
                      data="query_params is required",
                      status=status.HTTP_400_BAD_REQUEST,
                      CROS=True)
     # raise int param format invalid
     try:
         vm_install_method = int(vm_install_method)
         vm_cpu = int(vm_cpu)
         vm_mem = int(vm_mem)
         vm_storage_size = int(vm_storage_size)
     except Exception, e:
         return Reply(RESPONSE_ERROR,
                      data="query_params is invalid",
                      status=status.HTTP_400_BAD_REQUEST,
                      CROS=True)
Esempio n. 4
0
    def get(self, request):
        """
        GET method: Get vm list
        """
        conn = get_conn()
        vms = conn.list_vms()
        vmlist = []
        for vm in vms:
            vmlist.append(_format_vm(vm))

        return Reply(RESPONSE_SUCCESS,
                     data={"vms": vmlist},
                     status=status.HTTP_200_OK,
                     CROS=True)
Esempio n. 5
0
    def get(self, request):
        """
        GET method: Get vm detail information
        """
        conn = get_conn()
        vm_name = request.GET.get("vm_name", None)
        if vm_name is None:
            return Reply(RESPONSE_ERROR,
                         data="query_params is required",
                         status=status.HTTP_400_BAD_REQUEST,
                         CROS=True)
        vm = conn.get_vm(vm_name)
        if vm is None:
            return Reply(RESPONSE_ERROR,
                         data="vm '%s' not found" % vm_name,
                         status=status.HTTP_404_NOT_FOUND,
                         CROS=True)

        return Reply(RESPONSE_SUCCESS,
                     data=_format_vm(vm, conn, v=True),
                     status=status.HTTP_200_OK,
                     CROS=True)
Esempio n. 6
0
    def post(self, request):
        """
        POST method: Change vms lifecycle
        """
        conn = get_conn()
        vm_list = request.data.get("vm_list", [])
        if not len(vm_list) and "getlist" in dir(request.data):
            vm_list = request.data.getlist("vm_list[]", [])
        vm_status = request.data.get("vm_status", None)
        if not len(vm_list):
            return Reply(RESPONSE_ERROR,
                         data="query_params is required",
                         status=status.HTTP_400_BAD_REQUEST,
                         CROS=True)

        if not vm_status or (vm_status not in [
                "shutdown", "reboot", "destroy", "reset", "startup", "suspend",
                "resume"
        ]):
            return Reply(RESPONSE_ERROR,
                         data="lifecycle status is invalid",
                         status=status.HTTP_400_BAD_REQUEST,
                         CROS=True)
        vms = []
        for vm_name in vm_list:
            vm = conn.get_vm(vm_name)
            if vm is None:
                return Reply(RESPONSE_ERROR,
                             data="query_params is invalid",
                             status=status.HTTP_400_BAD_REQUEST,
                             CROS=True)
            vms.append(vm)
        for idx in range(len(vms)):
            try:
                getattr(vms[idx], vm_status)()
                vms[idx] = True
            except Exception, e:
                vms[idx] = e