Example #1
0
func createDockerfile(template string, codePath string) (strcodePathPrev string, strLocalFile string, strRemoteFile string) {
	dockerfile_template, _ := common.Config().String("image", "dockerfile_template")
	dockerfile, _ := common.Config().String("image", "dockerfile")
	datetime := time.Now().Format("2006-01-02")
	strLocalFile = dockerfile + "/" + datetime + "/" + template + "/Dockerfile"
	pos := strings.LastIndex(codePath, "/")
	strcodePathPrev = common.SubstrBefore(codePath, pos)
	if "" == strcodePathPrev {
		strcodePathPrev = "/"
	}
	relativePath := "./" + common.SubstrAfter(codePath, pos)
	addContent := "\n" + "ADD  " + relativePath + "  /data/" + template + "_code" + "\n"

	//读取模版,生成目标Dockerfile文件
	templateContent := common.ReadFile(dockerfile_template + "/" + template + "/Dockerfile")
	newContent := addNewContent(templateContent, "EXPOSE,CMD", addContent)
	createFile(strLocalFile, newContent)
	strRemoteFile = strcodePathPrev + "/Dockerfile"

	return strcodePathPrev, strLocalFile, strRemoteFile
}
Example #2
0
//处理从前台传过来的函数
func dealParams(strServerIp string, params FigParams, strParam string) (code int, result map[string]interface{}) {

	//fmt.Println("传来的参数:", strParam)

	ret := map[string]interface{}{}
	figData := ""
	commands := []map[string]string{}
	temp, _ := common.Config().String("fig", "figDirectory")
	/*
		var params FigParams
		err := json.Unmarshal([]byte(strParam), &params)
		if err != nil {
			logger.Println("json data decode faild :", err)
		}

		nCount := project_count(strServerIp, params.Project_name)
		if  nCount > 0  && params.force==0 {
			return 1, nil
		}
	*/
	figDirectory := temp + "/" + params.Project_name
	//servers := params["servers"].([]map[string]interface{})
	servers := params.Servers
	//servers := params["servers"].([][]interface{})
	for _, server := range servers {
		figData += dealServer(server.Server_name)
		figData += dealOneValue("image", server.Image)
		figData += dealMoreValues("ports", server.Ports)
		figData += dealMoreValues("links", server.Links)
		figData += dealMoreValues("volumes", server.Volumes)
		figData += dealCommand(server.Server_name, server.Command, figDirectory)
		command := dealCommandContent(server.Server_name, server.Command)
		commands = append(commands, command)
	}
	ret["fig_data"] = figData
	ret["commands"] = commands
	ret["fig_directory"] = figDirectory

	db, err := sql.Open("sqlite3", dbName)
	if err != nil {
		logger.Println(err)
	}
	defer db.Close()

	tx, err := db.Begin()
	if err != nil {
		logger.Println(err)
	}

	if params.Fig_project_id != "" {
		stmt, err := tx.Prepare("update fig_project  set fig_param=?,fig_content=?,create_time=? where fig_project_id=?")
		if err != nil {
			logger.Println(err)
		}
		defer stmt.Close()

		_, err = stmt.Exec(strParam, figData, time.Now().Unix(), params.Fig_project_id)

		if err != nil {
			logger.Println("参数1", err)
		}
	} else {
		stmt, err := tx.Prepare("insert into fig_project(project_name,machine_ip,fig_directory,fig_param,fig_content,create_time) values(?, ?, ?, ?, ?, ?)")
		if err != nil {
			logger.Println(err)
		}
		defer stmt.Close()

		_, err = stmt.Exec(params.Project_name, strServerIp, figDirectory, strParam, figData, time.Now().Unix())

		if err != nil {
			logger.Println("参数1", err)
		}
	}
	tx.Commit()

	return 0, ret
}