Пример #1
0
func GetDataAlternate(b *model.Bao, path string) error {
	files := make(map[string]string)
	gistUrl := "https://gist.github.com" + path
	rawPath := "https://gist.githubusercontent.com" + path

	resp, err := http.Get(gistUrl + ".json")
	if err != nil {
		return err
	}

	contents, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != 200 {
		return fmt.Errorf("%s", string(contents))
	}
	if err != nil {
		return err
	}
	if resp.StatusCode == 404 {
		err = fmt.Errorf("Gist not found")
		return err
	}
	var gistJson gistJson
	err = json.Unmarshal(contents, &gistJson)
	if err != nil {
		return err
	}

	for _, value := range gistJson.Files {
		var resp *http.Response
		resp, err = http.Get(rawPath + "/raw/" + value)
		if err != nil {
			return err
		}
		var contents []byte
		contents, err = ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}
		files[value] = string(contents)
	}
	b.Url = gistUrl
	b.GitPullUrl = "https://gist.github.com/" + b.GistId + ".git"
	return nil
}
Пример #2
0
func GetData(b *model.Bao) error {
	client := &http.Client{}
	req, err := http.NewRequest(
		"GET",
		"https://api.github.com/gists/"+b.GistId,
		nil,
	)
	if err != nil {
		return err
	}
	req.SetBasicAuth(
		github_access_key,
		"",
	)
	resp, err := client.Do(req)
	if err != nil {
		return err
	}

	contents, err := ioutil.ReadAll(resp.Body)
	if resp.StatusCode != 200 {
		return fmt.Errorf("Error code %d: %s", resp.StatusCode, string(contents))
	}
	if err != nil {
		return err
	}
	var responseStruct githubApiGistResponse
	err = json.Unmarshal(contents, &responseStruct)
	if err != nil {
		return err
	}
	for key, value := range responseStruct.Files {
		b.Files = append(b.Files, model.File{
			Filename: key,
			Language: value.Language,
			RawUrl:   value.Raw_url,
		})
	}

	b.Url = responseStruct.Html_url
	b.GitPullUrl = responseStruct.Git_pull_url
	return nil
}
Пример #3
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
	}

}
Пример #4
0
func ReadyHandler(w http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	baoIdString := vars["bao-id"]
	baoId, err := strconv.Atoi(baoIdString)
	if baoIdString == "" || err != nil {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("Error parsing bao id"))
		return
	}
	var bao model.Bao
	model.DB.Find(&bao, int64(baoId))

	if bao.IsComplete == true {
		fmt.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte("Bao is already complete"))
		return
	}

	bao.IsReady = true
	model.DB.Save(&bao)
	return
}
Пример #5
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
}
Пример #6
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)

}