Ejemplo n.º 1
0
func CreateNewMacAddress(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}
	fmt.Println(repo)

	mac := util.CreateNewMacAddress()
	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": mac})
}
Ejemplo n.º 2
0
func CreateVmDevice(ctx context.Context, w rest.ResponseWriter, r *rest.Request) {
	repo, ok := middleware.RepoFromContext(ctx)
	if !ok {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"})
		return
	}

	type DeviceParam struct {
		ID uint
		Sn string
	}

	var info struct {
		Devices        []DeviceParam
		VmNumber       int
		OsID           uint
		CpuCoresNumber uint
		MemoryCurrent  uint
		DiskSize       uint
	}

	if err := r.DecodeJSONPayload(&info); err != nil {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()})
		return
	}

	if len(info.Devices) <= 0 {
		w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "请选择要操作的物理机!"})
		return
	}

	for _, v := range info.Devices {
		if v.ID <= uint(0) && v.Sn != "" {
			count, err := repo.CountDeviceBySn(v.Sn)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
				return
			}
			if count > 0 {
				v.ID, err = repo.GetDeviceIdBySn(v.Sn)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
					return
				}
			}
		}

		if v.ID <= uint(0) {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "该物理机不存在(SN:" + v.Sn + ")!"})
			return
		}

		device, err := repo.GetDeviceById(v.ID)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
			return
		}

		if device.Status != "success" {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "SN:" + device.Sn + "不能安装虚拟机!"})
			return
		}
	}

	if info.VmNumber <= 0 || info.OsID <= uint(0) {
		w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "虚拟机个数和操作系统版本参数不能为空!", "Content": ""})
		return
	}

	if info.CpuCoresNumber <= uint(0) {
		info.CpuCoresNumber = uint(1)
	}

	if info.MemoryCurrent <= uint(0) {
		info.MemoryCurrent = uint(1024)
	}

	if info.DiskSize <= uint(0) {
		info.MemoryCurrent = uint(60)
	}

	//循环安装虚拟机
	var currentDeviceIndex int
	currentDeviceIndex = 0
	fix := time.Now().Format("20060102150405") + fmt.Sprintf("%d", rand.Intn(100))
	var result []model.VmDevice
	for i := 0; i < info.VmNumber; i++ {
		var deviceId uint
		deviceId = info.Devices[currentDeviceIndex].ID
		if deviceId <= uint(0) && info.Devices[currentDeviceIndex].Sn != "" {
			count, err := repo.CountDeviceBySn(info.Devices[currentDeviceIndex].Sn)
			if err != nil {
				w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
				return
			}
			if count > 0 {
				deviceId, err = repo.GetDeviceIdBySn(info.Devices[currentDeviceIndex].Sn)
				if err != nil {
					w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
					return
				}
			}
		}

		if deviceId <= uint(0) {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "该物理机不存在(SN:" + info.Devices[currentDeviceIndex].Sn + ")!"})
			return
		}

		//deviceId := info.Devices[currentDeviceIndex].ID

		currentDeviceIndex++
		if currentDeviceIndex >= len(info.Devices) {
			currentDeviceIndex = 0
		}

		device, err := repo.GetDeviceById(deviceId)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
			return
		}
		ip, err := repo.AssignNewIpByNetworkId(device.NetworkID)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
			return
		}

		var row model.VmDevice
		row.DeviceID = deviceId
		row.Ip = ip
		row.Hostname = fix + fmt.Sprintf("%d", i)
		row.Mac = util.CreateNewMacAddress()
		row.NetworkID = device.NetworkID
		row.OsID = info.OsID
		row.CpuCoresNumber = info.CpuCoresNumber
		row.CpuHotPlug = "No"
		row.CpuPassthrough = "No"
		row.CpuTopSockets = 0
		row.CpuTopCores = 0
		row.CpuTopThreads = 0
		row.CpuPinning = ""
		row.MemoryCurrent = info.MemoryCurrent
		row.MemoryMax = info.MemoryCurrent
		row.MemoryKsm = "No"
		row.DiskType = "raw"
		row.DiskSize = info.DiskSize
		row.DiskBusType = "virtio"
		row.DiskCacheMode = "writeback"
		row.DiskIoMode = "default"
		row.NetworkType = "bridge"
		row.NetworkDeviceType = "virtio"
		row.DisplayType = "serialPorts"
		row.DisplayPassword = ""
		row.DisplayUpdatePassword = "******"
		row.Status = "pre_install"

		resultAdd, errAdd := repo.AddVmDevice(row.DeviceID,
			row.Hostname,
			row.Mac,
			row.Ip,
			row.NetworkID,
			row.OsID,
			row.CpuCoresNumber,
			row.CpuHotPlug,
			row.CpuPassthrough,
			row.CpuTopSockets,
			row.CpuTopCores,
			row.CpuTopThreads,
			row.CpuPinning,
			row.MemoryCurrent,
			row.MemoryMax,
			row.MemoryKsm,
			row.DiskType,
			row.DiskSize,
			row.DiskBusType,
			row.DiskCacheMode,
			row.DiskIoMode,
			row.NetworkType,
			row.NetworkDeviceType,
			row.DisplayType,
			row.DisplayPassword,
			row.DisplayUpdatePassword,
			row.Status)
		if errAdd != nil {
			w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "操作失败:" + err.Error()})
			return
		}

		result = append(result, resultAdd)
	}
	/*
		count, err := repo.CountVmDeviceByMac(info.Mac)
		if err != nil {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "参数错误!"})
			return
		}

		if count > 0 {
			w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "该Mac已存在,继续填写会覆盖旧的数据!"})
		} else {
			w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "Mac填写正确!"})
		}
	*/
	w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功!", "Content": result})

}