//获取cidr信息 func GetCidrInfoByNetwork(ctx context.Context, w rest.ResponseWriter, r *rest.Request) { _, ok := middleware.RepoFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } logger, ok := middleware.LoggerFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } var info struct { Network string } if err := r.DecodeJSONPayload(&info); err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误"}) return } info.Network = strings.TrimSpace(info.Network) //处理网段 network, err := util.GetCidrInfo(info.Network, logger) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": network}) }
func RunCreateVm(ctx context.Context, vmDeviceId uint) error { repo, ok := middleware.RepoFromContext(ctx) if !ok { return errors.New("内部服务器错误") } logger, ok := middleware.LoggerFromContext(ctx) if !ok { return errors.New("内部服务器错误") } vmDevice, err := repo.GetVmDeviceById(vmDeviceId) if err != nil { return err } device, err := repo.GetDeviceById(vmDevice.DeviceID) if err != nil { return err } var cmdFormat = `LANG=C virt-install --connect qemu+ssh://root@%s/system \ --name=%s \ --os-type=windows \ --vcpus=%d \ --ram=%d \ --hvm \ --boot hd,network,menu=on \ --accelerate \ --graphics vnc,listen=0.0.0.0,port=%s \ --noautoconsole \ --autostart \ --network bridge=br0,model=virtio,mac=%s \ --disk path=/dev/VolGroup0/%s,device=disk,bus=virtio,cache=none,sparse=false,format=raw` var cmd = fmt.Sprintf(cmdFormat, device.Ip, vmDevice.Hostname, vmDevice.CpuCoresNumber, vmDevice.MemoryCurrent, vmDevice.VncPort, vmDevice.Mac, vmDevice.Hostname) logger.Debugf("create vm:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("create result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("create error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return errors.New(runResult) } return nil }
func RunCreateVol(ctx context.Context, vmDeviceId uint) error { repo, ok := middleware.RepoFromContext(ctx) if !ok { return errors.New("内部服务器错误") } logger, ok := middleware.LoggerFromContext(ctx) if !ok { return errors.New("内部服务器错误") } conf, ok := middleware.ConfigFromContext(ctx) if !ok { return errors.New("内部服务器错误") } vmDevice, err := repo.GetVmDeviceById(vmDeviceId) if err != nil { return err } device, err := repo.GetDeviceById(vmDevice.DeviceID) if err != nil { return err } storage := conf.Vm.Storage if storage == "" { storage = "guest_images_lvm" } var cmdFormat = `LANG=C virsh --connect qemu+ssh://root@%s/system vol-create-as %s %s %dG` var cmd = fmt.Sprintf(cmdFormat, device.Ip, storage, vmDevice.Hostname, vmDevice.DiskSize) logger.Debugf("vm create vol:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("create result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("create error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return errors.New(runResult) } return nil }
func CollectAndUpdateVmHostResource(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 } logger, ok := middleware.LoggerFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } conf, ok := middleware.ConfigFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } UpdateVmHostResource(logger, repo, conf, 0) w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功"}) }
//restart vm func RunReStartVm(ctx context.Context, vmDeviceId uint) error { repo, ok := middleware.RepoFromContext(ctx) if !ok { return errors.New("内部服务器错误") } logger, ok := middleware.LoggerFromContext(ctx) if !ok { return errors.New("内部服务器错误") } vmDevice, err := repo.GetVmDeviceById(vmDeviceId) if err != nil { return err } device, err := repo.GetDeviceById(vmDevice.DeviceID) if err != nil { return err } var cmdFormat = `LANG=C virsh --connect qemu+ssh://root@%s/system 'destroy %s; start %s'` var cmd = fmt.Sprintf(cmdFormat, device.Ip, vmDevice.Hostname, vmDevice.Hostname) logger.Debugf("restart vm:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("restart result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("restart error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return errors.New(runResult) } return nil }
func IconvFile(filename string, fromCode string, toCode string, ctx context.Context) error { if filename == "" || fromCode == "" || toCode == "" { return errors.New("参数错误") } logger, ok := middleware.LoggerFromContext(ctx) if !ok { return errors.New("内部服务器错误") } if !FileExist(filename) { return errors.New(filename + "文件不存在") } newFilename := filename + "_" + toCode cmd := fmt.Sprintf("iconv -t %s -f %s -c %s > %s", toCode, fromCode, filename, newFilename) logger.Debugf("run script: %s", cmd) bytes, err := ExecScript(cmd) logger.Debugf("run result: %s", string(bytes)) if err != nil { logger.Errorf("run error:%s", err.Error()) return err } //delete old file errRemove := os.Remove(filename) if errRemove != nil { return errRemove } //rename errRename := os.Rename(newFilename, filename) if errRename != nil { return errRename } return nil }
func UpdateNetworkById(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 } logger, ok := middleware.LoggerFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } var info struct { ID uint Network string Netmask string Gateway string Vlan string Trunk string Bonding string AccessToken string } if err := r.DecodeJSONPayload(&info); err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } info.Network = strings.TrimSpace(info.Network) info.Netmask = strings.TrimSpace(info.Netmask) info.Gateway = strings.TrimSpace(info.Gateway) info.Vlan = strings.TrimSpace(info.Vlan) info.Trunk = strings.TrimSpace(info.Trunk) info.Bonding = strings.TrimSpace(info.Bonding) info.AccessToken = strings.TrimSpace(info.AccessToken) _, errVerify := VerifyAccessPurview(info.AccessToken, ctx, true, w, r) if errVerify != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errVerify.Error()}) return } if info.Network == "" || info.Netmask == "" || info.Gateway == "" { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "请将信息填写完整!"}) return } isValidate, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.Netmask) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } if !isValidate { w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "掩码格式不正确!", "Content": ""}) return } isValidateGageway, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.Gateway) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } if !isValidateGageway { w.WriteJSON(map[string]interface{}{"Status": "failure", "Message": "网关格式不正确!", "Content": ""}) return } count, err := repo.CountNetworkByNetworkAndId(info.Network, info.ID) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } if count > 0 { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "该网段已存在!"}) return } oldNetwork, err := repo.GetNetworkById(info.ID) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } mod, err := repo.UpdateNetworkById(info.ID, info.Network, info.Netmask, info.Gateway, info.Vlan, info.Trunk, info.Bonding) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } //网段发生更改的情况下,重新分配IP if oldNetwork.Network != info.Network { //处理网段 network, err := util.GetCidrInfo(info.Network, logger) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } ipList, err := util.GetIPListByMinAndMaxIP(network["MinIP"], network["MaxIP"]) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } _, errDelete := repo.DeleteIpByNetworkId(info.ID) if errDelete != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errDelete.Error()}) return } for _, ip := range ipList { _, err := repo.AddIp(info.ID, ip) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } } } w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功", "Content": mod}) }
func SaveDhcpSubnet(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 } var info struct { ID uint StartIp string EndIp string Gateway string AccessToken string } if err := r.DecodeJSONPayload(&info); err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误"}) return } info.StartIp = strings.TrimSpace(info.StartIp) info.EndIp = strings.TrimSpace(info.EndIp) info.Gateway = strings.TrimSpace(info.Gateway) isValidate, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.StartIp) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } if !isValidate { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "起始IP格式不正确!", "Content": ""}) return } isValidateEndIp, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.EndIp) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } if !isValidateEndIp { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "结束IP格式不正确!", "Content": ""}) return } isValidateGateway, err := regexp.MatchString("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$", info.Gateway) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "参数错误" + err.Error()}) return } if !isValidateGateway { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "网关格式不正确!", "Content": ""}) return } info.AccessToken = strings.TrimSpace(info.AccessToken) _, errVerify := VerifyAccessPurview(info.AccessToken, ctx, true, w, r) if errVerify != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errVerify.Error()}) return } if info.StartIp == "" || info.EndIp == "" || info.Gateway == "" { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "请将信息填写完整!"}) return } if info.ID > uint(0) { _, err := repo.UpdateDhcpSubnetById(info.ID, info.StartIp, info.EndIp, info.Gateway) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } } else { _, err := repo.AddDhcpSubnet(info.StartIp, info.EndIp, info.Gateway) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } } logger, ok := middleware.LoggerFromContext(ctx) if !ok { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "内部服务器错误"}) return } //save to dhcp config file file := "/etc/dhcp/dhcpd.conf" if !util.FileExist(file) { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "数据已保存,DHCP配置文件(" + file + ")不存在,请手工配置!"}) return } text, err := util.ReadFile(file) if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } reg, err := regexp.Compile("(?s)subnet(\\s+)(.*?)(\\s+)(netmask)(\\s+)(.*?)(\\s+){(.*?)}") if err != nil { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": err.Error()}) return } matchs := reg.FindAllStringSubmatch(text, -1) if len(matchs) <= 0 { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "DHCP配置文件(" + file + ")未配置subnet节点,请手工操作!"}) return } if len(matchs) > 1 { w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "DHCP配置文件(" + file + ")存在多个subnet节点,请手工操作!"}) return } strFormat := `subnet %s netmask %s { range %s %s; option routers %s; }` str := fmt.Sprintf(strFormat, matchs[0][2], matchs[0][6], info.StartIp, info.EndIp, info.Gateway) text = strings.Replace(text, matchs[0][0], str, -1) logger.Debugf("update dhcp config %s:%s", file, text) var bytes = []byte(text) errWrite := ioutil.WriteFile(file, bytes, 0666) if errWrite != nil { logger.Errorf("error:%s", errWrite.Error()) w.WriteJSON(map[string]interface{}{"Status": "error", "Message": errWrite.Error()}) return } //restart dhcp service var cmd = "service dhcpd restart" logger.Debugf("restart dhcpd:%s", cmd) restartBytes, err := util.ExecScript(cmd) logger.Debugf("result:%s", string(restartBytes)) var runResult = "<br>执行脚本:" + cmd runResult += "<br>" + "执行结果:" + string(restartBytes) if err != nil { runResult += "<br>" + "错误信息:" + err.Error() logger.Errorf("error:%s", err.Error()) w.WriteJSON(map[string]interface{}{"Status": "error", "Message": "DHCP重启失败!" + runResult}) return } w.WriteJSON(map[string]interface{}{"Status": "success", "Message": "操作成功"}) }