Skip to content

Commit

Permalink
Merge pull request #19217 from wanyaoqi/automated-cherry-pick-of-#192…
Browse files Browse the repository at this point in the history
…16-upstream-master

Automated cherry pick of #19216: Automated cherry pick of #19215: fix(host-deployer): deploy params has special char
  • Loading branch information
zexi authored Jan 10, 2024
2 parents ecd9c7d + 708f7c1 commit bd670e7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
36 changes: 30 additions & 6 deletions pkg/hostman/diskutils/qemu_kvm/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"sync"
"sync/atomic"

"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/stringutils"
Expand Down Expand Up @@ -54,8 +55,9 @@ var (

DEPLOY_ISO = "/opt/cloud/host-deployer/host_deployer_v1.iso"
// DEPLOYER_BIN = "/opt/yunion/bin/host-deployer --common-config-file /opt/yunion/common.conf --config /opt/yunion/host.conf"
DEPLOYER_BIN = "/opt/yunion/bin/host-deployer --config /opt/yunion/host.conf"
YUNIONOS_PASSWD = "mosbaremetal"
DEPLOYER_BIN = "/opt/yunion/bin/host-deployer --config /opt/yunion/host.conf"
DEPLOY_PARAMS_FILE = "/deploy_params"
YUNIONOS_PASSWD = "mosbaremetal"
)

type QemuDeployManager struct {
Expand All @@ -65,6 +67,7 @@ type QemuDeployManager struct {
portsInUse *sync.Map
lastUsedSshPort int
qemuCmd string
memSizeMb int

c chan struct{}
}
Expand Down Expand Up @@ -156,6 +159,10 @@ func (m *QemuDeployManager) GetSshFreePort() int {
return port
}

func (m *QemuDeployManager) getMemSizeMb() int {
return m.memSizeMb
}

var manager *QemuDeployManager

func tryCleanGuest(hmpPath string) {
Expand Down Expand Up @@ -183,7 +190,7 @@ func tryCleanGuest(hmpPath string) {
func InitQemuDeployManager(
cpuArch, qemuVersion string,
enableRemoteExecutor, hugepage bool,
hugepageSizeKB, deployConcurrent int,
hugepageSizeKB, memSizeMb, deployConcurrent int,
) error {
if deployConcurrent <= 0 {
deployConcurrent = 10
Expand Down Expand Up @@ -220,6 +227,7 @@ func InitQemuDeployManager(
cpuArch: cpuArch,
hugepage: hugepage,
hugepageSizeKB: hugepageSizeKB,
memSizeMb: memSizeMb,
portsInUse: new(sync.Map),
c: make(chan struct{}, deployConcurrent),
qemuCmd: qemuCmd,
Expand Down Expand Up @@ -281,7 +289,7 @@ func (d *QemuKvmDriver) Connect(guestDesc *apis.GuestDesc) error {
func (d *QemuKvmDriver) connect(guestDesc *apis.GuestDesc) error {
var (
ncpu = 2
memSizeMB = 256
memSizeMB = manager.getMemSizeMb()
disks = make([]string, 0)
)

Expand Down Expand Up @@ -370,14 +378,30 @@ func (d *QemuKvmDriver) sshRun(cmd string) ([]string, error) {
return d.sshClient.Run(cmd)
}

func (d *QemuKvmDriver) sshFilePutContent(params interface{}, filePath string) error {
jcontent := jsonutils.Marshal(params)
cmd := fmt.Sprintf(`cat << EOF > %s
%s
EOF`, filePath, jcontent.String())
out, err := d.sshRun(cmd)
if err != nil {
return errors.Wrapf(err, "sshFilePutContent %s", out)
}
return nil
}

func (d *QemuKvmDriver) DeployGuestfs(req *apis.DeployParams) (*apis.DeployGuestFsResponse, error) {
defer func() {
logStr, _ := d.sshRun("test -f /log && cat /log")
log.Infof("DeployGuestfs log: %v", strings.Join(logStr, "\n"))
}()

params, _ := json.Marshal(req)
cmd := fmt.Sprintf("%s --deploy-action deploy_guest_fs --deploy-params '%s'", DEPLOYER_BIN, params)
err := d.sshFilePutContent(req, DEPLOY_PARAMS_FILE)
if err != nil {
return nil, errors.Wrap(err, "DeployGuestfs ssh copy deploy params")
}

cmd := fmt.Sprintf("%s --deploy-action deploy_guest_fs --deploy-params-file %s", DEPLOYER_BIN, DEPLOY_PARAMS_FILE)
out, err := d.sshRun(cmd)
if err != nil {
return nil, errors.Wrapf(err, "run deploy_guest_fs failed %s", out)
Expand Down
1 change: 1 addition & 0 deletions pkg/hostman/hostdeployer/deployserver/deployserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ func (s *SDeployService) PrepareEnv() error {
DeployOption.EnableRemoteExecutor,
DeployOption.HugepagesOption == "native",
DeployOption.HugepageSizeMb*1024,
DeployOption.DeployGuestMemSizeMb,
DeployOption.DeployConcurrent,
)
if err != nil {
Expand Down
26 changes: 21 additions & 5 deletions pkg/hostman/hostdeployer/deployserver/localdeploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package deployserver

import (
"encoding/json"
"os"
"strings"

"github.com/sirupsen/logrus"

"yunion.io/x/jsonutils"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/util/netutils"

Expand All @@ -29,6 +29,7 @@ import (
"yunion.io/x/onecloud/pkg/hostman/guestfs/fsdriver"
deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
"yunion.io/x/onecloud/pkg/hostman/hostdeployer/consts"
"yunion.io/x/onecloud/pkg/util/fileutils2"
"yunion.io/x/onecloud/pkg/util/qemuimg"
"yunion.io/x/onecloud/pkg/util/winutils"
)
Expand Down Expand Up @@ -121,32 +122,47 @@ func LocalInitEnv() error {
return nil
}

func unmarshalDeployParams(val interface{}) error {
if DeployOption.DeployParamsFile != "" {
deployParams, err := fileutils2.FileGetContents(DeployOption.DeployParamsFile)
if err != nil {
return errors.Wrapf(err, "failed get params from %s", DeployOption.DeployParamsFile)
}
DeployOption.DeployParams = deployParams
}
jDeployParams, err := jsonutils.Parse([]byte(DeployOption.DeployParams))
if err != nil {
return errors.Wrap(err, "failed parse deploy json")
}
return jDeployParams.Unmarshal(val)
}

func StartLocalDeploy(deployAction string) (interface{}, error) {
localDeployer := LocalDeploy{}
switch deployAction {
case "deploy_guest_fs":
params := new(deployapi.DeployParams)
if err := json.Unmarshal([]byte(DeployOption.DeployParams), params); err != nil {
if err := unmarshalDeployParams(params); err != nil {
return nil, errors.Wrap(err, "unmarshal params")
}
return localDeployer.DeployGuestFs(params)
case "resize_fs":
return localDeployer.ResizeFs(nil)
case "format_fs":
params := new(deployapi.FormatFsParams)
if err := json.Unmarshal([]byte(DeployOption.DeployParams), params); err != nil {
if err := unmarshalDeployParams(params); err != nil {
return nil, errors.Wrap(err, "unmarshal params")
}
return localDeployer.FormatFs(params)
case "save_to_glance":
params := new(deployapi.SaveToGlanceParams)
if err := json.Unmarshal([]byte(DeployOption.DeployParams), params); err != nil {
if err := unmarshalDeployParams(params); err != nil {
return nil, errors.Wrap(err, "unmarshal params")
}
return localDeployer.SaveToGlance(params)
case "probe_image_info":
params := new(deployapi.ProbeImageInfoPramas)
if err := json.Unmarshal([]byte(DeployOption.DeployParams), params); err != nil {
if err := unmarshalDeployParams(params); err != nil {
return nil, errors.Wrap(err, "unmarshal params")
}
return localDeployer.ProbeImageInfo(params)
Expand Down
12 changes: 7 additions & 5 deletions pkg/hostman/hostdeployer/deployserver/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ type SDeployOptions struct {

AllowVmSELinux bool `help:"turn off vm selinux" default:"false" json:"allow_vm_selinux"`

HugepagesOption string `help:"Hugepages option: disable|native|transparent" default:"transparent"`
HugepageSizeMb int `help:"hugepage size mb default 1G" default:"1024"`
DefaultQemuVersion string `help:"Default qemu version" default:"4.2.0"`
ListenInterface string `help:"Master address of host server"`
Networks []string `help:"Network interface information"`
HugepagesOption string `help:"Hugepages option: disable|native|transparent" default:"transparent"`
HugepageSizeMb int `help:"hugepage size mb default 1G" default:"1024"`
DefaultQemuVersion string `help:"Default qemu version" default:"4.2.0"`
DeployGuestMemSizeMb int `help:"Deploy guest mem size mb" default:"320"`
ListenInterface string `help:"Master address of host server"`
Networks []string `help:"Network interface information"`

DeployAction string `help:"local deploy action"`
DeployParams string `help:"params for deploy action"`
DeployParamsFile string `help:"file store params for deploy action"`
DeployConcurrent int `help:"qemu-kvm deploy driver concurrent" default:"5"`
}

Expand Down

0 comments on commit bd670e7

Please sign in to comment.