Example #1
0
//Builds the image in a docker node.
func BuildImageFromDockerfile(w rest.ResponseWriter, r *rest.Request) {

	passedParams := PassedParams{}

	//If the Content-Type is multipart, parse it.  Otherwise unpack the json.
	if strings.Contains(r.Header.Get("Content-Type"), "multipart") == true {

		form, err := r.MultipartReader()
		TarPart, err := form.NextPart()
		TarData, err := ioutil.ReadAll(TarPart)
		JsonPart, err := form.NextPart()
		JsonData, _ := ioutil.ReadAll(JsonPart)

		err = json.Unmarshal(JsonData, &passedParams)
		if err != nil {
			rest.Error(w, err.Error(), 400)
			return
		}
		passedParams.TarFile = TarData

	} else {
		//Unpack the params that come in.
		err := r.DecodeJsonPayload(&passedParams)
		if err != nil {
			rest.Error(w, err.Error(), 400)
			return
		}
	}

	if Validate(passedParams) {
		//Create a uuid for the specific job.
		var jobid JobID
		jobid.JobIdentifier = JobUUIDString()

		//Open a redis connection.  c is type redis.Conn
		c := RedisConnection()

		//Set the status to building in the cache.
		n, err := c.Do("HSET", jobid.JobIdentifier, "status", "Building")
		if n == 0 {
			rest.Error(w, "Unable to access the redis cache.  Make sure the CACHE_PASSWORD is set.", 501)
			return
		}
		if err != nil {
			rest.Error(w, " err trigg Unable to access the redis cache.  Make sure the CACHE_PASSWORD is set.", 400)
			return
		}
		c.Do("HSET", jobid.JobIdentifier, "logs", "Fetching logs...")

		//Launch a goroutine to build, push, and delete.  Updates the cache as the process goes on.
		//Also pass it the file that came from the CLI?
		go BuildPushAndDeleteImage(jobid.JobIdentifier, passedParams, c)

		//Write the jobid back
		w.WriteJson(jobid)
	} else {
		//Params didn't validate.  Bad Request.
		rest.Error(w, "Insufficient Information.  Must provide at least an Image Name and a Dockerfile/Tarurl.", 400)
		return
	}

}