Пример #1
0
func DeployHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	id := vars["id"]
	intid, err := strconv.Atoi(id)
	int64id := int64(intid)

	if err != nil || id == "" {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	var bao model.Bao
	query := model.DB.Find(&bao, int64id)
	if query.Error == gorm.RecordNotFound {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("404"))
		return
	} else if query.Error != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return

	}
	var server model.Server
	query = model.DB.Find(&server, bao.ServerId)
	if query.Error != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	getUrl := fmt.Sprintf("http://%s:8002/build/%d", server.Ip, bao.Id)
	log.Println(getUrl)
	resp, err := http.Get(getUrl)
	log.Printf("%#v", resp)
	if err != nil || resp.StatusCode != 200 {
		bao.Console += "Uh oh, we've experienced an error. Please try again.\n"
		bao.IsComplete = true
		model.DB.Save(&bao)
		return
	}

}
Пример #2
0
func writeToBao(b *model.Bao, text string, isComplete bool) error {
	b.IsComplete = isComplete
	b.Console = b.Console + text + "\n"
	model.DB.Save(b)
	return nil
}
Пример #3
0
func BaoHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)

	base36Id := vars["id-base36"]
	fmt.Println(base36Id)
	baoId, err := strconv.ParseInt(base36Id, 36, 64)

	var bao model.Bao
	query := model.DB.Find(&bao, baoId)
	if query.Error == gorm.RecordNotFound {
		w.WriteHeader(http.StatusNotFound)
		w.Write([]byte("404"))
		return
	} else if query.Error != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	if bao.GitPullUrl == "" {

		// go func() {
		err = github.GetGistData(&bao)
		if err != nil {
			fmt.Printf("%#v", bao)
			w.WriteHeader(http.StatusInternalServerError)
			w.Write([]byte(err.Error()))
			return
		}

		bao.Console = "Welcome to gitbao!!\n" +
			"Getting ready to wrap up a tasty new bao.\n"

		bao.Console += "Found some files:\n"

		var isGo bool
		for _, value := range bao.Files {
			bao.Console += "    " + value.Filename + "\n"
			if value.Language == "Go" {
				isGo = true
			}
			if value.Filename == "Baofile" || value.Filename == "baofile" {
				bao.BaoFileUrl = value.RawUrl
			}
		}

		if isGo != true {
			bao.Console += "Whoops!\n" +
				"gitbao only supports Go programs at the moment.\n" +
				"Quitting...."
			bao.IsComplete = true
		} else {
			bao.Console += "Nice, looks like we can deploy your application\n" +
				"Modify your config file if needed and hit deploy!\n"
		}

		if bao.IsComplete != true {
			go func() {
				var server model.Server
				query = model.DB.Where("kind = ?", "xiaolong").Find(&server)
				if query.Error != nil {
					bao.Console += "Uh oh, we've experienced an error. Please try again.\n"
					fmt.Println(query.Error)
					model.DB.Save(&bao)
					return
				}
				bao.ServerId = server.Id
				model.DB.Save(&bao)
				getUrl := fmt.Sprintf("http://%s:8002/ready/%d", server.Ip, bao.Id)
				log.Println(getUrl)
				resp, err := http.Get(getUrl)
				log.Printf("%#v", resp)
				if err != nil || resp.StatusCode != 200 {
					bao.Console += "Uh oh, we've experienced an error. Please try again.\n"
					bao.IsComplete = true
					model.DB.Save(&bao)
					return
				}
			}()
		}
	}

	query = model.DB.Save(&bao)
	if query.Error != nil {
		fmt.Printf("%#v", bao)
		panic(query.Error)
	}
	RenderTemplate(w, "bao", bao)

}