Ejemplo n.º 1
0
func (s *Server) executeServiceHandler(w http.ResponseWriter, r *http.Request) {
	logRequest(r)
	imageID := r.FormValue("imageID")
	if imageID == "" {
		vars := mux.Vars(r)
		imageID = vars["imageID"]
	}

	imageID = strings.Replace(imageID, "sha256:", "", 1) // removing sha256 prefix
	if imageID == "" {
		Response{w}.ServerNewError("execute docker image: imageID required")
		return
	}
	logParam("imageID", imageID)

	image, err := s.docker.InspectImage(dckr.ImageID(imageID))
	if err != nil {
		Response{w}.ServerError("execute docker image: inspectImage: ", err)
	}
	binds, err := makeBinds(r, image)
	if err != nil {
		Response{w}.ServerError("execute docker image: binds: ", err)
		return
	}
	logParam("binds", strings.Join(binds, " : "))

	containerID, err := s.docker.ExecuteImage(dckr.ImageID(imageID), binds)

	if err != nil {
		Response{w}.ServerError("execute docker image: ", err)
		return
	}
	loc := apiRootPath + jobsAPIPath + "/" + string(containerID)
	Response{w}.Location(loc).Created(jmap("Location", loc, "jobID", containerID))
}
Ejemplo n.º 2
0
func (s *Server) inspectVolumeHandler(w http.ResponseWriter, r *http.Request) {
	logRequest(r)
	vars := mux.Vars(r)
	volId := string(dckr.ImageID(vars["volumeID"]))

	imageID := "eudatgef/volume-filelist"

	// Bind the container with the volume
	volumesToMount := []string{
		volId + ":/root/volume"}

	// Execute our image (it should produce a JSON file with the list of files)
	containerID, err := s.docker.ExecuteImage(dckr.ImageID(imageID), volumesToMount)
	if err != nil {
		Response{w}.ServerError("execute docker image: ", err)
		return
	}

	// Reading the JSON file
	volumeFiles, err := s.readJSON(string(containerID), "/root/_filelist.json")
	if err != nil {
		Response{w}.ServerError("reading the list of files in a volume: ", err)
		return
	}

	// Killing the container
	_, err = s.docker.WaitContainer(containerID, true)
	if err != nil {
		Response{w}.ServerError("removing the container: ", err)
		return
	}

	json.NewEncoder(w).Encode(volumeFiles)

}
Ejemplo n.º 3
0
func (s *Server) retrieveFileHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	containerID := string(dckr.ImageID(vars["containerID"]))
	filePath := string(dckr.ImageID(vars["filePath"]))

	tarStream, err := s.docker.GetTarStream(containerID, filePath)

	if err == nil {
		tarBallReader := tar.NewReader(tarStream)
		header, err := tarBallReader.Next()
		if err != nil {
			Response{w}.ServerError("cannot read the reqested file from the archive", err)
			return
		}
		filename := header.Name

		if header.Typeflag == tar.TypeReg {
			if err == nil {
				w.Header().Set("Content-Disposition", "attachment; filename=\""+filename+"\"")
				w.Header().Set("Content-Type", r.Header.Get("Content-Type"))
				io.Copy(w, tarBallReader)
			} else {
				http.Error(w, "Cannot read the content of the requested file: "+err.Error(), http.StatusBadRequest)
				return
			}
		} else {
			http.Error(w, "Bad request", http.StatusBadRequest)
			return
		}

	} else {
		http.Error(w, "Cannot access Docker remote API: "+err.Error(), http.StatusBadRequest)
		return
	}
}
Ejemplo n.º 4
0
func (s *Server) inspectServiceHandler(w http.ResponseWriter, r *http.Request) {
	logRequest(r)
	vars := mux.Vars(r)
	imageID := dckr.ImageID(vars["imageID"])
	image, err := s.docker.InspectImage(imageID)
	if err != nil {
		Response{w}.ServerError("inspect docker image: ", err)
		return
	}
	Response{w}.Ok(jmap("Image", image, "Service", extractServiceInfo(image)))
}
Ejemplo n.º 5
0
func (s *Server) buildVolumeHandler(w http.ResponseWriter, r *http.Request) {
	logRequest(r)
	vars := mux.Vars(r)
	buildID := vars["buildID"]
	buildDir := filepath.Join(s.tmpDir, buildsTmpDir, buildID)

	// STEP 1: Get a list of files from PID
	// Temporary solution for the list of files
	var pidList []string
	pidList = append(pidList, "#!/bin/ash")
	pidList = append(pidList, "wget https://b2share.eudat.eu/record/154/files/ISGC2014_022.pdf?version=1 -P /root/volume")
	pidList = append(pidList, "wget https://b2share.eudat.eu/record/157/files/TenReasonsToSwitchFromMauiToMoab2012-01-05.pdf?version=1 -P /root/volume")
	pidList = append(pidList, "ls -l /root/volume/")

	// STEP 2: create a bash script that downloads those files
	dScriptPath := filepath.Join(buildDir, "downloader.sh")
	dScriptFile, err := os.Create(dScriptPath)
	if err != nil {
		Response{w}.ServerError("create script file:", err)
		return
	}
	log.Println("Script was created")
	_, err = dScriptFile.WriteString(strings.Join(pidList, "\n"))
	if err != nil {
		Response{w}.ServerError("write data into the script file", err)
		return
	}
	dScriptFile.Sync()
	log.Println("Wrote file list")

	err = dScriptFile.Chmod(0777)
	if err != nil {
		Response{w}.ServerError("make downloading script executable:", err)
		return
	}
	log.Println("Changed permissions")

	// STEP 3: create an image that includes the script
	var dockerFileContent []string
	dockerFileContent = append(dockerFileContent, "FROM alpine:latest")
	dockerFileContent = append(dockerFileContent, "RUN apk add --update --no-cache openssl openssl-dev ca-certificates")
	dockerFileContent = append(dockerFileContent, "RUN mkdir /root/volume")
	dockerFileContent = append(dockerFileContent, "ADD downloader.sh /root")
	dockerFileContent = append(dockerFileContent, "CMD [\"/root/downloader.sh\"]")

	dockerFilePath := filepath.Join(buildDir, "Dockerfile")
	dockerFile, err := os.Create(dockerFilePath)
	if err != nil {
		Response{w}.ServerError("create script file:", err)
		return
	}
	log.Println("Dockerfile was created")
	_, err = dockerFile.WriteString(strings.Join(dockerFileContent, "\n"))
	if err != nil {
		Response{w}.ServerError("write data into the  Dockerfile", err)
		return
	}
	dockerFile.Sync()
	log.Println("Wrote Dockerfile content")

	// STEP 4: create a new empty volume
	volume, err := s.docker.BuildVolume(buildDir)
	if err != nil {
		Response{w}.ServerError("build docker volume:", err)
		return
	}
	log.Println(volume.ID)
	log.Println(buildDir)
	log.Println("Volume was created")

	image, err := s.docker.BuildImage(buildDir)
	if err != nil {
		Response{w}.ServerError("build docker image: ", err)
		return
	}
	log.Println("Docker image was created")
	log.Println(image.ID)

	imageID := string(image.ID)

	// STEP 5: run the image, as a result we get a volume with our files
	volumesToMount := []string{
		string(volume.ID) + ":/root/volume"}

	containerID, err := s.docker.ExecuteImage(dckr.ImageID(imageID), volumesToMount)
	if err != nil {
		Response{w}.ServerError("execute docker image: ", err)
		return
	}
	log.Println("Executed the image")

	log.Println(containerID)

	_, err = s.docker.WaitContainer(containerID, true)
	if err != nil {
		Response{w}.ServerError("removing the container: ", err)
		return
	}
	log.Println("Container was removed")

	err = s.docker.DeleteImage(imageID)
	if err != nil {
		Response{w}.ServerError("removing the image "+imageID+": ", err)
		return
	}
	log.Println("Image was removed")

	Response{w}.Ok(jmap("Volume", volume))
}