示例#1
0
func copy_template(strServerIP string, strOurce string, strProjectName string) (ok bool) {

	strRemoteDir := "/data/" + strProjectName
	//strRemoteDir:="/home/tomzhao/"+strProjectName;
	ret1, _ := common.ExecRemoteCMD(strServerIP, "mkdir -p", strRemoteDir)
	if ret1 > 0 {
		return false
	}

	strRemoteDir = strServerIP + ":" + strRemoteDir + "/."
	ret1, _ = common.TransferDirSSH(strOurce, strRemoteDir)
	if ret1 > 0 {
		return false
	}
	return true
}
示例#2
0
func fig_transfer(strServerIP string, type_flag string, params map[string]interface{}) (ret bool, err string) {
	var (
		strRemoteDir string
		ok           bool
	)
	//获取项目名称
	strFigDirectory, ok := params["fig_directory"].(string)
	if !ok {
		return false, "fig directory empty!!!!"
	}

	str := strings.Split(strFigDirectory, "/")
	strProjectName := str[len(str)-1]

	strFigData, ok := params["fig_data"].(string)
	if !ok {
		return false, "fig_data empty!!!!"
	}

	//生成项目fig文件(这个必须使用fig作为文件名)
	strFileName := "fig.yml"
	ok = common.SaveFile(strFileName, strFigData)
	if !ok {
		return false, "save fig file empty!!!!"
	}

	//创建远程目录
	//strRemoteDir = FIG_PATH + strProjectName
	strRemoteDir = strFigDirectory
	fmt.Println(strRemoteDir)

	//删除远程目录
	_, _ = common.ExecRemoteRM(strServerIP, strRemoteDir)
	//支持递归生成不存在目录
	ret1, _ := common.ExecRemoteCMD(strServerIP, "mkdir -p", strRemoteDir)
	if ret1 > 0 {
		return false, "Create fig Remote Path faild!!!!"
	}

	if type_flag == "0" {
		copy_template(strServerIP, "../../template/software", strProjectName)
	}
	//传输文件到远程目录
	strRemoteFile := strServerIP + ":" + strRemoteDir + "/" + strFileName
	ret1, _ = common.TransferFileSSH(strFileName, strRemoteFile)
	if ret1 > 0 {
		return false, "Transfer File faild!!!!"
	}

	// if type_flag == "1" {
	// 	return true, "ok"
	// }
	//创建启动文件
	//mapCommands, ok := params["commands"].(map[string]interface{})
	fmt.Println("commands=", params["commands"].([]map[string]string))
	commands := params["commands"].([]map[string]string)
	if len(commands) > 0 {
		//strRemoteDir = FIG_PATH + strProjectName + "/startup"
		strRemoteDir = strFigDirectory + "/startup"
		//创建远程目录
		ret1, _ = common.ExecRemoteCMD(strServerIP, "mkdir", strRemoteDir)
		if ret1 > 0 {
			return false, "Create fig Remote Path faild!!!!"
		}

		for i := 0; i < len(commands); i++ {
			for k, v := range commands[i] {
				//保存启动文件
				strStartDir := strRemoteDir + "/" + k
				ret1, _ = common.ExecRemoteCMD(strServerIP, "mkdir -p", strStartDir)
				if ret1 > 0 {
					return false, "Create fig Remote Path faild!!!!"
				}

				strStartFile := "start.sh"
				ok = common.SaveFile(strStartFile, v)
				if !ok {
					return false, "save start file empty!!!!"
				}

				//传输文件到远程目录
				strRemoteFile := strServerIP + ":" + strStartDir + "/" + strStartFile
				ret1, _ = common.TransferFileSSH(strStartFile, strRemoteFile)
				if ret1 > 0 {
					return false, "Transfer File faild!!!!"
				}

				//远程脚本设置执行权限
				strRemoteFile = strStartDir + "/" + strStartFile
				fmt.Println("strRemoteFile=", strRemoteFile)
				ret1, _ = common.ExecRemoteChmod(strServerIP, "+x", strRemoteFile)
				if ret1 > 0 {
					return false, "Exec Remote Shell faild!!!!"
				}
			}
		}
	}

	fmt.Println("strFigDirectory=", strFigDirectory)
	fmt.Println("strFile=", strFileName)
	return true, "ok"
}