Exemplo n.º 1
0
// 分区
func diskpart() error {
	var cmd = `select disk 0
create partition extended
create partition logical
assign
format fs=ntfs quick`

	utils.Logger.Debug(cmd)
	diskpartFilePath := path.Join(rootPath, "disk.txt")

	if utils.CheckFileIsExist(diskpartFilePath) {
		os.Remove(diskpartFilePath)
	}
	file, err := os.Create(diskpartFilePath)
	if err != nil {
		utils.Logger.Error(err.Error())
		return err
	}
	defer file.Close()

	file.WriteString(cmd)
	file.Close()

	var diskCmd = exec.Command("diskpart", "/s", diskpartFilePath)
	if output, err := diskCmd.Output(); err != nil {
		utils.Logger.Error(err.Error())
		return err
	} else {
		utils.Logger.Debug(string(output))
		return nil
	}
}
Exemplo n.º 2
0
func run(c *cli.Context) error {
	// init log
	utils.InitFileLog()

	if utils.CheckFileIsExist(preInstallScript) {
		if _, err := utils.ExecScript(preInstallScript); err != nil {
			utils.Logger.Error("preinstall error: %s", err.Error())
		}
	}
	//init cloudboot server host
	serverIp := getDomainLookupIP(serverHost)
	if serverIp != "" {
		serverHost = serverIp
	}

	if !utils.PingLoop(serverHost, 300, 2) {
		return errors.New("ping timeout")
	}

	var sn = getSN()
	isVm := isVirtualMachine()
	if isVm {
		sn = getMacAddress()
	}

	if sn == "" {
		utils.Logger.Error("get sn failed!")
	}

	restInfo, err := getRestInfo(sn, serverHost)
	if err != nil {
		utils.Logger.Error(err.Error())
	}
	// fmt.Println(restInfo)

	var nicInterfaceIndex = getNicInterfaceIndex(restInfo.HWADDR)
	if nicInterfaceIndex == "" {
		utils.Logger.Error("get nic interface index failed")
	}

	var dns = getDNS()
	if dns == "" {
		utils.Logger.Error("get dns failed")
	}

	if err = diskpart(); err != nil {
		utils.Logger.Error(err.Error())
	}
	utils.ReportProgress(0.7, sn, "分区", "diskpart", serverHost)

	if err = changeHostname(restInfo.Hostname); err != nil {
		utils.Logger.Error(err.Error())
	}
	utils.ReportProgress(0.75, sn, "修改主机名", "change hostname", serverHost)

	if err = changeIP(nicInterfaceIndex, restInfo.Ip, restInfo.Netmask, restInfo.Gateway); err != nil {
		utils.Logger.Error(err.Error())
	}

	if err = changeDNS(nicInterfaceIndex, dns); err != nil {
		utils.Logger.Error(err.Error())
	}

	time.Sleep(30 * time.Second)
	if !utils.PingLoop(serverHost, 300, 2) {
		return errors.New("ping timeout")
	}
	utils.ReportProgress(0.8, sn, "修改网络配置", "change network", serverHost)

	if err = changeReg(); err != nil {
		utils.Logger.Error(err.Error())
	}
	utils.ReportProgress(0.9, sn, "修改注册表", "change reg", serverHost)

	utils.ReportProgress(1.0, sn, "安装完成", "finish", serverHost)

	if utils.CheckFileIsExist(postInstallScript) {
		if _, err := utils.ExecScript(postInstallScript); err != nil {
			utils.Logger.Error("postInstall error: %s", err.Error())
		}
	}

	reboot()

	return nil
}