コード例 #1
0
ファイル: action_image.go プロジェクト: masterpy/autodep
//创建文件并写入内容,如文件已存在,覆盖旧文件
func createFile(filePath, strData string) (code int, result string) {
	if strData == "" {
		return 1, "content不能为空"
	}

	pos := strings.LastIndex(filePath, "/")
	fileDirectory := common.SubstrBefore(filePath, pos)
	if !common.IsDirExists(fileDirectory) {
		err := os.MkdirAll(fileDirectory, os.ModePerm) //生成多级目录
		if err != nil {
			fmt.Println("创建目录("+fileDirectory+")失败:", err)
			return 1, "创建目录(" + fileDirectory + ")失败"
		}
	}

	if !common.SaveFile(filePath, strData) {
		return 1, "创建文件失败"
	}

	return 0, "创建文件成功"
}
コード例 #2
0
ファイル: action_fig.go プロジェクト: masterpy/autodep
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"
}