Ejemplo n.º 1
0
func (this *LaunchController) Post() {

	sess := this.GetSession("acme")
	if sess == nil {
		this.Redirect("/user/login/home", 302)
		return
	}
	m := sess.(map[string]interface{})
	username, _ := m["username"].(string)
	username = strings.ToLower(username)

	this.activeContent("appLaunch")
	fmt.Println("app launch started")
	numbertolaunch, _ := strconv.Atoi(this.GetString("launchNumber"))

	file := this.GetString("filelist")

	for i := 0; i < numbertolaunch; i++ {

		//Read File for launching
		podfile, err := ioutil.ReadFile("./restcalls/" + file)

		if err != nil {
			panic(err)
		}

		chararactersToGenerate := 8
		set := gostrgen.Lower | gostrgen.Digit
		include := ""   // optionally include some additional letters
		exclude := "Ol" //exclude big 'O' and small 'l' to avoid confusion with zero and one.

		randomstring, err := gostrgen.RandGen(chararactersToGenerate, set, include, exclude)
		if err != nil {
			fmt.Println(err)
		}

		//append username into JSON FILE

		var newstring string = ""
		var usernamestring = username + "-" + randomstring

		JSONSPLIT := strings.Split(string(podfile), "#USERNAME")
		newstring = newstring + JSONSPLIT[0] + usernamestring + JSONSPLIT[1] + usernamestring + JSONSPLIT[2]

		fmt.Println(newstring)

		//Create the request
		url := "http://" + os.Getenv("MARATHON_ENDPOINT") + "/v2/apps/"
		bytestring := []byte(newstring)
		req, err := http.NewRequest("POST", url, bytes.NewBuffer(bytestring))

		//Make the request
		res, err := http.DefaultClient.Do(req)

		if err != nil {
			panic(err) //Something is wrong while sending request
		}

		if res.StatusCode != 201 {
			fmt.Printf("Success expected: %d", res.StatusCode) //Uh-oh this means our test failed
		}

		this.Data["Launching"] = "Launching successful! Redirecting in 2s ..."
		time.Sleep(5 * time.Second)
		this.Redirect("/user/apps", 302)

	}
	flash := beego.ReadFromRequest(&this.Controller)
	if n, ok := flash.Data["notice"]; ok {
		this.Data["notice"] = n
	}
}
Ejemplo n.º 2
0
func (this *LaunchController) Post() {

	sess := this.GetSession("acme")
	if sess == nil {
		this.Redirect("/user/login/home", 302)
		return
	}
	m := sess.(map[string]interface{})
	username, _ := m["username"].(string)
	username = strings.ToLower(username)

	this.activeContent("appLaunch")
	fmt.Println("app launch started")
	numbertolaunch, _ := strconv.Atoi(this.GetString("launchNumber"))

	file := this.GetString("filelist")

	for i := 0; i < numbertolaunch; i++ {

		//Read File for launching
		podfile, err := ioutil.ReadFile("./restcalls/" + file)

		if err != nil {
			panic(err)
		}

		chararactersToGenerate := 8
		set := gostrgen.Lower | gostrgen.Digit
		include := ""   // optionally include some additional letters
		exclude := "Ol" //exclude big 'O' and small 'l' to avoid confusion with zero and one.

		randomstring, err := gostrgen.RandGen(chararactersToGenerate, set, include, exclude)
		if err != nil {
			fmt.Println(err)
		}

		//append username
		//Grab the x characters before hitting a '{' character
		//Create new string, removing those characters and appending 'username-' to the id field,
		//for proper routing.

		var inFileUntilBrace int = 0
		var inFile string = ""
		var newstring string = ""
		var usernamestring = username + "-"

		for i := 0; i < len(podfile); i++ {

			if string(podfile[i]) == "{" {
				inFileUntilBrace = i
				break
			}

			inFile = inFile + string(podfile[i])
		}

		fmt.Println("Where is brace: ", inFileUntilBrace)
		j, _ := strconv.Atoi(inFile)
		fmt.Println("Where does id go? : ", inFile)
		for i := inFileUntilBrace; i < len(podfile); i++ {

			if i == j {
				newstring = newstring + usernamestring + randomstring + "-"
			}

			newstring = newstring + string(podfile[i])
		}

		fmt.Println(newstring)

		//Create the request
		url := "http://107.167.184.225:8080/v2/apps"
		bytestring := []byte(newstring)
		req, err := http.NewRequest("POST", url, bytes.NewBuffer(bytestring))

		//Make the request
		res, err := http.DefaultClient.Do(req)

		if err != nil {
			panic(err) //Something is wrong while sending request
		}

		if res.StatusCode != 201 {
			fmt.Printf("Success expected: %d", res.StatusCode) //Uh-oh this means our test failed
		}

		fmt.Println("Deployment status code: ", res.StatusCode)

	}
	flash := beego.ReadFromRequest(&this.Controller)
	if n, ok := flash.Data["notice"]; ok {
		this.Data["notice"] = n
	}
}