// NewRepo 创建mysql数据实现实例 func NewRepo(conf *config.Config, log logger.Logger) (*MySQLRepo, error) { var connection string if conf.Repo.ConnectionIsCrypted != "" { str, err := util.RSADecrypt(conf.Rsa.PrivateKey, conf.Repo.ConnectionIsCrypted) if err != nil { return nil, err } connection = str } else if conf.Repo.Connection != "" { connection = conf.Repo.Connection } if connection == "" { return nil, errors.New("请先配置好数据库连接信息!") } db, err := gorm.Open("mysql", connection) if err != nil { log.Errorf("database connection failed:%s", err.Error()) return nil, err } db.LogMode(true) repo := &MySQLRepo{ conf: conf, log: log, db: &db, } return repo, nil }
//get vm pool info func RunGetVmHostPoolInfo(repo model.Repo, logger logger.Logger, conf *config.Config, deviceId uint) (string, error) { device, err := repo.GetDeviceById(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 pool-info %s` var cmd = fmt.Sprintf(cmdFormat, device.Ip, storage) logger.Debugf("get vm host pool info:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return "", errors.New(runResult) } return string(bytes), nil }
//get vm info func RunGetVmInfo(repo model.Repo, logger logger.Logger, vmDeviceId uint) (string, error) { 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 dominfo %s` var cmd = fmt.Sprintf(cmdFormat, device.Ip, vmDevice.Hostname) logger.Debugf("get vm info:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return "", errors.New(runResult) } return string(bytes), nil }
//delete noVNC token file func RunDeleteVmNoVncTokenFile(repo model.Repo, logger logger.Logger, vmDeviceId uint) error { vmDevice, err := repo.GetVmDeviceById(vmDeviceId) if err != nil { return err } var dir = "/etc/cloudboot-server/novnc-tokens" var file = vmDevice.Hostname logger.Debugf("delete vm novnc token file:%s", dir+"/"+file) if util.FileExist(dir + "/" + file) { err := os.Remove(dir + "/" + file) if err != nil { logger.Errorf("error:%s", err.Error()) return err } } return nil }
//create noVNC token file func RunCreateVmNoVncTokenFile(repo model.Repo, logger logger.Logger, vmDeviceId uint) error { vmDevice, err := repo.GetVmDeviceById(vmDeviceId) if err != nil { return err } device, err := repo.GetDeviceById(vmDevice.DeviceID) if err != nil { return err } var newHostname = strings.Replace(vmDevice.Hostname, ":", "_", -1) var strFormat = `%s: %s:%s` var str = fmt.Sprintf(strFormat, newHostname, device.Ip, vmDevice.VncPort) var dir = "/etc/cloudboot-server/novnc-tokens" var file = vmDevice.Hostname if !util.FileExist(dir) { err := os.MkdirAll(dir, 0777) if err != nil { return err } } //文件已存在,先删除 if util.FileExist(dir + "/" + file) { err := os.Remove(dir + "/" + file) if err != nil { return err } } logger.Debugf("create vm novnc token file %s:%s", dir+"/"+file, str) bytes := []byte(str) errCreate := ioutil.WriteFile(dir+"/"+file, bytes, 0644) if errCreate != nil { logger.Errorf("error:%s", errCreate.Error()) return errCreate } return nil }
//test connect vm host func RunTestConnectVmHost(repo model.Repo, logger logger.Logger, deviceId uint) (string, error) { device, err := repo.GetDeviceById(deviceId) if err != nil { return "", err } var cmdFormat = `LANG=C ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o BatchMode=yes -o ConnectTimeout=3 root@%s 'w'` var cmd = fmt.Sprintf(cmdFormat, device.Ip) logger.Debugf("test connect vm host:%s", cmd) var runResult = "执行脚本:\n" + cmd bytes, err := util.ExecScript(cmd) logger.Debugf("result:%s", string(bytes)) runResult += "\n\n" + "执行结果:\n" + string(bytes) if err != nil { logger.Errorf("error:%s", err.Error()) runResult += "\n\n" + "错误信息:\n" + err.Error() return "", errors.New(runResult) } return string(bytes), nil }