Ejemplo n.º 1
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"
}
Ejemplo n.º 2
0
func CreateImage(request common.RequestData) (code int, result string) {
	var image CreateImageStru
	err := json.Unmarshal([]byte(request.Params), &image)
	if err != nil {
		logger.Println("json data decode faild :", err)
	}

	//参数校验
	if image.Template == "" {
		code = 1
		result = "template cann't be empty "
		return code, result
	}

	if image.Image_name == "" {
		code = 1
		result = "image_name cann't be empty "
		return code, result
	}

	if image.Code_path == "" {
		code = 1
		result = "code_path cann't be empty "
		return code, result
	}

	if image.Creator == "" {
		code = 1
		result = "creator cann't be empty "
		return code, result
	}

	strcodePathPrev, strLocalFile, strRemoteFile := createDockerfile(image.Template, image.Code_path)

	if "" == strcodePathPrev {
		code = 1
		result = "dockerfile_directory is empty,create_dockerfile error"
		return code, result
	}
	fmt.Println("build镜像,生成Dockerfile成功")

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

	ret1, out := buildImage(strServerIP, image.Image_name, strcodePathPrev)
	if ret1 > 0 {
		code = 1
		result = "build image err:" + out
		return code, result
	}
	fmt.Println("build镜像成功")

	code, result = saveImageToDb(image)
	if code != 0 {
		return code, result
	}
	fmt.Println("build镜像,增加数据成功")

	return 0, out
}