Skip to content

Commit e685915

Browse files
committed
Merge branch 'main' of https://github.com/easysoft/zenagent
# Conflicts: # cmd/host/router/router.go
2 parents 0c9d19c + e72645f commit e685915

File tree

15 files changed

+174
-25
lines changed

15 files changed

+174
-25
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
*.log
33
/zv.db
44
/bin/
5+
/zentaoatf.code-workspace
56

67
/log/
78
/cmd/test/log/

cmd/host/router/handler/kvm.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hostHandler
22

33
import (
44
v1 "github.com/easysoft/zagent/cmd/host/router/v1"
5+
hostAgentService "github.com/easysoft/zagent/internal/host/service"
56
kvmService "github.com/easysoft/zagent/internal/host/service/kvm"
67
consts "github.com/easysoft/zagent/internal/pkg/const"
78
natHelper "github.com/easysoft/zagent/internal/pkg/utils/net"
@@ -10,8 +11,9 @@ import (
1011
)
1112

1213
type KvmCtrl struct {
13-
KvmService *kvmService.KvmService `inject:""`
14-
LibvirtService *kvmService.LibvirtService `inject:""`
14+
KvmService *kvmService.KvmService `inject:""`
15+
SnapService *hostAgentService.SnapService `inject:""`
16+
LibvirtService *kvmService.LibvirtService `inject:""`
1517
}
1618

1719
func NewKvmCtrl() *KvmCtrl {
@@ -126,7 +128,11 @@ func (c *KvmCtrl) Boot(ctx iris.Context) {
126128
return
127129
}
128130

129-
c.KvmService.StartVm(name)
131+
err := c.KvmService.StartVm(name)
132+
if err != nil {
133+
ctx.JSON(_httpUtils.RespData(consts.ResultFail, err.Error(), name))
134+
return
135+
}
130136

131137
ctx.JSON(_httpUtils.RespData(consts.ResultPass, "success to boot vm", name))
132138
return
@@ -332,7 +338,7 @@ func (c *KvmCtrl) Remove(ctx iris.Context) {
332338
_, _ = ctx.JSON(_httpUtils.RespData(consts.ResultFail, "vm name is empty", nil))
333339
return
334340
}
335-
341+
c.SnapService.RemoveSnapsByVmName(req.Name)
336342
err := c.LibvirtService.RemoveVmByName(req.Name, req.RemoveDisk)
337343
if err != nil {
338344
ctx.JSON(_httpUtils.RespData(consts.ResultFail, err.Error(), nil))

cmd/host/router/router.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
hostHandler "github.com/easysoft/zagent/cmd/host/router/handler"
55
hostService "github.com/easysoft/zagent/internal/host/service"
66
consts "github.com/easysoft/zagent/internal/pkg/const"
7-
"github.com/easysoft/zagent/internal/pkg/core"
87
_httpUtils "github.com/easysoft/zagent/pkg/lib/http"
98
"github.com/kataras/iris/v12"
109
)
@@ -52,7 +51,7 @@ func (r *Router) App() {
5251
client.Get("/getVncAddress", r.VirtualCtrl.GetAddress).Name = "获取vnc token对应虚拟机地址"
5352
})
5453

55-
v1.Use(core.Auth())
54+
// v1.Use(core.Auth())
5655

5756
v1.PartyFunc("/service", func(client iris.Party) {
5857
client.Post("/check", r.CheckCtrl.CheckService).Name = "检测宿主机服务状态"
@@ -93,7 +92,7 @@ func (r *Router) App() {
9392
client.Post("/remove", r.KvmCtrl.Remove).Name = "移除KVM虚拟机"
9493

9594
client.Get("/listSnap", r.SnapCtrl.ListSnap).Name = "列出虚拟机快照"
96-
client.Post("/addSnapTask", r.SnapCtrl.AddCreateSnap).Name = "创建虚拟机快照"
95+
client.Post("/addCreateSnap", r.SnapCtrl.AddCreateSnap).Name = "创建虚拟机快照"
9796
client.Post("/addRevertSnap", r.SnapCtrl.AddRevertSnap).Name = "回滚虚拟机快照"
9897
client.Post("/removeSnap", r.SnapCtrl.RemoveSnap).Name = "删除虚拟机快照"
9998
})

internal/host/service/download.go

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ func (s *DownloadService) StartTask(po agentModel.Task) {
6363
go func() {
6464
filePath := downloadUtils.GetPath(po)
6565

66+
if po.Md5 == "" {
67+
downloadUtils.GetMd5FromRemote(&po, consts.DownloadDir)
68+
}
6669
//query same md5 task
6770
sameMd5Task, _ := s.TaskRepo.GetCompletedTaskByMd5(po.Md5)
6871
if sameMd5Task.ID > 0 {

internal/host/service/kvm/libvirt.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package kvmService
33
import "C"
44
import (
55
"fmt"
6-
v1 "github.com/easysoft/zagent/cmd/host/router/v1"
76
"time"
87

8+
v1 "github.com/easysoft/zagent/cmd/host/router/v1"
9+
910
agentConf "github.com/easysoft/zagent/internal/pkg/conf"
1011
consts "github.com/easysoft/zagent/internal/pkg/const"
1112
"github.com/easysoft/zagent/internal/pkg/domain"
@@ -337,12 +338,12 @@ func (s *LibvirtService) ResumeVmByName(name string) (err error) {
337338
func (s *LibvirtService) RemoveVmByName(name string, removeDiskImage bool) (err error) {
338339
dom, err := s.GetVm(name)
339340
if err != nil {
340-
return
341+
return nil
341342
}
342343

343344
dickPath, err := s.QemuService.GetDisk(dom)
344345
if err != nil {
345-
return
346+
return nil
346347
}
347348

348349
dom.DestroyFlags(libvirt.DOMAIN_DESTROY_DEFAULT)

internal/host/service/kvm/qemu.go

+33
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"path/filepath"
8+
"regexp"
9+
"strconv"
810
"strings"
911

1012
agentConf "github.com/easysoft/zagent/internal/pkg/conf"
@@ -361,3 +363,34 @@ func (s *QemuService) GetBackingFileSize(diskPath string) (size int64) {
361363

362364
return size
363365
}
366+
367+
func (s *QemuService) GetBackingFileVirtualSize(diskPath string) (size int) {
368+
var cmd = fmt.Sprintf("qemu-img info %s", diskPath)
369+
370+
out, err := _shellUtils.ExeShell(cmd)
371+
if out == "" || err != nil {
372+
return 100
373+
}
374+
375+
outSlice := strings.Split(out, "\n")
376+
for _, line := range outSlice {
377+
line = strings.TrimSpace(line)
378+
if strings.Contains(line, "virtual size:") {
379+
reg := regexp.MustCompile(`\d+\s*GiB`)
380+
//根据规则提取关键信息
381+
sizeSlice := reg.FindStringSubmatch(line)
382+
if len(sizeSlice) < 1 {
383+
return 100
384+
}
385+
sizeString := sizeSlice[0]
386+
size, _ = strconv.Atoi(strings.TrimSpace(strings.Replace(sizeString, "GiB", "", -1)))
387+
break
388+
}
389+
}
390+
391+
if size == 0 {
392+
size = 100
393+
}
394+
395+
return size
396+
}

internal/host/service/kvm/vm.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ func (s *KvmService) CreateVmFromImage(req *v1.CreateVmReq, removeSameName bool)
5555
s.LibvirtService.SafeDestroyVmByName(vmName)
5656
}
5757

58+
virtualSize := s.QemuService.GetBackingFileVirtualSize(targetBacking)
59+
if diskSize > virtualSize {
60+
virtualSize = diskSize
61+
}
62+
5863
// create image
59-
cmdCreateImage := fmt.Sprintf(consts.CmdCreateImage, targetBacking, srcFile)
64+
cmdCreateImage := fmt.Sprintf(consts.CmdCreateImage, targetBacking, srcFile, virtualSize)
6065
out, err := _shellUtils.ExeShell(cmdCreateImage)
6166
if err != nil {
6267
_logUtils.Infof("exec cmd '%s' err, output %s, error %s", cmdCreateImage, out, err.Error())

internal/host/service/snap.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package hostAgentService
33
import (
44
"errors"
55
"fmt"
6+
"strings"
7+
"sync"
8+
"time"
9+
610
agentModel "github.com/easysoft/zagent/internal/host/model"
711
kvmService "github.com/easysoft/zagent/internal/host/service/kvm"
812
consts "github.com/easysoft/zagent/internal/pkg/const"
913
"github.com/easysoft/zagent/internal/pkg/job"
1014
"github.com/gofrs/uuid"
11-
"sync"
12-
"time"
1315

1416
v1 "github.com/easysoft/zagent/cmd/host/router/v1"
1517
hostRepo "github.com/easysoft/zagent/internal/host/repo"
@@ -44,6 +46,36 @@ func (s *SnapService) ListSnap(vm string) (ret []v1.SnapItemResp, err error) {
4446
return
4547
}
4648

49+
// RemoveSnapsByVmName 删除虚拟机下所有快照
50+
func (s *SnapService) RemoveSnapsByVmName(vm string) (err error) {
51+
snaps := s.GetVmSnaps(vm)
52+
53+
for _, snap := range snaps {
54+
err = s.RemoveSnap(&v1.SnapTaskReq{Vm: vm, Name: snap})
55+
}
56+
57+
return
58+
}
59+
60+
func (s *SnapService) GetVmSnaps(vm string) (snaps []string) {
61+
cmd := fmt.Sprintf("virsh snapshot-list %s | awk '{print $1}' | tail -n +3", vm)
62+
63+
out, err := _shellUtils.ExeShell(cmd)
64+
if err != nil {
65+
_logUtils.Infof("list snap '%s' err, output %s, error %s", cmd, out, err.Error())
66+
return
67+
}
68+
snapNames := strings.Split(out, "\n")
69+
for _, name := range snapNames {
70+
name = strings.TrimSpace(name)
71+
if name != "" {
72+
snaps = append(snaps, name)
73+
}
74+
}
75+
76+
return
77+
}
78+
4779
func (s *SnapService) AddTasks(req []v1.SnapTaskReq) (err error) {
4880
for _, item := range req {
4981
if item.Vm == "" || item.Name == "" {

internal/host/service/task.go

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ func (s *TaskService) ListTask() (ret v1.ListTaskResp, err error) {
111111
}
112112

113113
func (s *TaskService) SubmitResult(task agentModel.Task) (err error) {
114+
fmt.Println("=======submit task", task.Task, task.Status)
114115
// only submit vm task
115116
if task.Type == consts.DownloadImage {
116117
url := requestUtils.GenUrl(agentConf.Inst.Server, "api.php/v1/host/submitResult")

internal/pkg/const/cmd.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package consts
33
const (
44
CmdCreateImage = `qemu-img create -f qcow2 -F qcow2 \
55
-o cluster_size=2M,backing_file=%s \
6-
%s 60G
6+
%s %dG
77
`
88

99
CmdCreateVm = `virt-install \

internal/pkg/job/download.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func Start(downloadTask *agentModel.Task, filePath string, ch chan int) (status
2323

2424
targetDir := consts.DownloadDir
2525
if downloadTask.Md5 == "" {
26-
getMd5FromRemote(downloadTask, targetDir)
26+
GetMd5FromRemote(downloadTask, targetDir)
2727
}
2828

2929
existFile = findSameFile(*downloadTask, targetDir)
@@ -165,7 +165,7 @@ func CheckMd5(task agentModel.Task) bool {
165165
return pass
166166
}
167167

168-
func getMd5FromRemote(task *agentModel.Task, dir string) (err error) {
168+
func GetMd5FromRemote(task *agentModel.Task, dir string) (err error) {
169169
index := strings.LastIndex(task.Url, ".")
170170
md5FileUrl := task.Url[:index] + ".md5"
171171

@@ -208,6 +208,10 @@ func findSameFile(task agentModel.Task, dir string) (existFile string) {
208208
existFile = strings.Replace(md5FilePath, ".md5", "", -1)
209209

210210
if _fileUtils.FileExist(existFile) {
211+
actualVal, _ := _fileUtils.GetMd5(existFile)
212+
if actualVal != task.Md5 {
213+
existFile = ""
214+
}
211215
return
212216
} else {
213217
existFile = ""

internal/vm/service/tool.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ func (s *ToolService) startProcess(name, execPath, uuid, ip, server, secret stri
219219
var cmd *exec.Cmd
220220
if _commonUtils.IsWin() {
221221
if name == "ztf" {
222-
tmpl := `start cmd /c %s -uuid %s -h http://%s:%d -i %s -p %d ^1^> %snohup.%s.log ^2^>^&^1`
223-
cmdStr = fmt.Sprintf(tmpl, execPath, uuid, consts.KvmHostIpInNatNetwork, consts.AgentHostServicePort, ip, consts.ZtfServicePort, consts.WorkDir, name)
222+
tmpl := `start cmd /c %s -p %d -h http://%s:%d -i %s -uuid %s ^1^> %snohup.%s.log ^2^>^&^1`
223+
cmdStr = fmt.Sprintf(tmpl, execPath, consts.ZtfServicePort, consts.KvmHostIpInNatNetwork, consts.AgentHostServicePort, ip, uuid, consts.WorkDir, name)
224224
} else if name == "zd" { // set root for workdir
225-
tmpl := `start cmd /c %s -uuid %s -b %s -p %d ^1^> %snohup.%s.log ^2^>^&^1`
226-
cmdStr = fmt.Sprintf(tmpl, execPath, uuid, ip, consts.ZdServicePort, consts.WorkDir, name)
225+
tmpl := `start cmd /c %s -p %d -b %s -uuid %s ^1^> %snohup.%s.log ^2^>^&^1`
226+
cmdStr = fmt.Sprintf(tmpl, execPath, consts.ZdServicePort, ip, uuid, consts.WorkDir, name)
227227
}
228228

229229
cmd = exec.Command("cmd", "/C", cmdStr)

pkg/lib/http/http.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"encoding/json"
55
"errors"
66
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"strings"
10+
"time"
11+
712
consts "github.com/easysoft/zagent/internal/pkg/const"
813
authUtils "github.com/easysoft/zagent/internal/pkg/utils/auth"
914
_const "github.com/easysoft/zagent/pkg/const"
1015
_logUtils "github.com/easysoft/zagent/pkg/lib/log"
1116
"github.com/fatih/color"
12-
"io/ioutil"
13-
"net/http"
14-
"strings"
15-
"time"
1617
)
1718

1819
const ()

0 commit comments

Comments
 (0)