Example #1
0
func (ss *SubmitServer) getJobResult(user *Hyades.Person, w http.ResponseWriter, req *http.Request) {
	req.ParseForm()
	id, ok := req.Form["id"]
	if !ok {
		http.Error(w, "Job id not provided", http.StatusNotFound)
		return
	}

	job, err := ss.jobs.GetJob(id[0])
	if err != nil {
		log.Println("id[0]", id[0])
		log.Println("getJobResult - ss.jobs.GetJob", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	log.Println("job.JobFolder", job.JobFolder)
	log.Println("user.Username", user.Username)

	TempJobFolder := filepath.Join(*configuration.DataPath, job.JobFolder, job.Name+fmt.Sprint(job.Id))

	zipedfilePath := filepath.Join(*configuration.DataPath, job.JobFolder, "Job"+job.Name+fmt.Sprint(job.Id)+".zip")
	log.Println("Creating zip at", zipedfilePath)
	zipedFile, err := os.OpenFile(zipedfilePath, os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		log.Println("Error creating file", zipedfilePath, err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	stat, err := zipedFile.Stat()
	if err == nil && stat.Size() == 0 {
		log.Println("About to compress", TempJobFolder)
		Hyades.ZipCompressFolderWriter(TempJobFolder, zipedFile)
		log.Println("Zipped file")
	}

	http.ServeContent(w, req, "Job"+job.Name+".zip", time.Now(), zipedFile)

}
Example #2
0
func DoWork(work *Hyades.WorkComms, resChan chan *Hyades.WorkResult) {

	res := &Hyades.WorkResult{EnvLength: 0, StdOutStream: make([]byte, 0), ErrOutStream: make([]byte, 0), Error: "", Done: 0}

	defer func() {
		if err := recover(); err != nil {
			res.Error = fmt.Sprint(err)
		}
		resChan <- res
	}()
	TempJobFolder := filepath.Join("Env", "Temp")
	log.Println("Making folder:[", TempJobFolder, "]")

	err := os.MkdirAll(TempJobFolder, os.ModeDir|os.ModePerm)
	if err != nil {
		log.Println("Error creating folder:", err)
		res.Error = err.Error()

		return
	}
	defer os.RemoveAll(TempJobFolder)

	envreader := bytes.NewReader(work.Env)
	unzipper, err := zip.NewReader(envreader, int64(len(work.Env)))

	if err != nil {
		log.Println("Error zip.NewReader:", err)
		res.Error = err.Error()
		return
	}

	for _, file := range unzipper.File {
		os.MkdirAll(filepath.Join(TempJobFolder, path.Dir(file.Name)), os.ModeDir|os.ModePerm)

		outfile, _ := os.Create(filepath.Join(TempJobFolder, file.Name))
		zf, err := file.Open()
		if err != nil {
			log.Println("Error reading zip:", err)
			res.Error = err.Error()
			return
		}
		io.Copy(outfile, zf)
		outfile.Close()
		zf.Close()
	}

	stdBuf := new(bytes.Buffer)
	errBuf := new(bytes.Buffer)

	log.Println("Chmoding ", filepath.Join(TempJobFolder, work.Parts.Command))
	err = os.Chmod(filepath.Join(TempJobFolder, work.Parts.Command), os.ModePerm)
	if err != nil {
		log.Println("Chmod", err)
	}

	var cmd *exec.Cmd
	if runtime.GOOS == "linux" {
		log.Println("Setting up the linux ccommand", work.Parts.Command, work.Parts.Parameters)
		cmd = exec.Command(work.Parts.Command, work.Parts.Parameters...)
	} else {
		log.Println("Setting up the windows ccommand")
		cmd = exec.Command("cmd", "/C", "cd "+TempJobFolder, " & "+work.Parts.Command+" "+strings.Join(work.Parts.Parameters, " "))
	}

	fullpath, _ := filepath.Abs(TempJobFolder)
	fullpath = "\"" + fullpath + "\""

	log.Println("Running command", work.Parts.Command)

	cmd.Dir = TempJobFolder
	cmd.Stdout = stdBuf
	cmd.Stderr = errBuf
	err = cmd.Run()
	log.Println("Std", len(stdBuf.Bytes()))
	log.Println("Err", len(errBuf.Bytes()))
	if err != nil {
		if _, ok := err.(*exec.ExitError); !ok {

			log.Println("Error running command:", err)
			res.Error = "Error running command:" + err.Error()
		}

		return
	}
	cmd.StdoutPipe()
	log.Println("Done command", work.Parts.Command)

	//Delete any exes in the folder; they don't need to be sent back to the server

	scan, err := os.Open(TempJobFolder)
	if err != nil {
		panic(err)
	}

	files, err := scan.Readdir(-1)
	scan.Close()

	if err != nil {
		log.Println(err)
	} else {
		for _, file := range files {
			filename := file.Name()
			if filename[len(filename)-4:] == ".exe" {
				if err := os.Remove(filepath.Join(TempJobFolder, filename)); err != nil {
					log.Println(err)
				}
			}
		}
	}
	var tempWriter io.ReadWriteCloser
	var Bufferlength func() int
	tempFile, err := os.Create("temp.zip")
	if err == nil {
		tempWriter = tempFile

		Bufferlength = func() int {
			s, err := tempFile.Stat()
			if err != nil {
				panic(err)
			}
			return int(s.Size())
		}

	} else {
		log.Println("Could not keep zip on disk, trying to use memory")
		buffer := &bytes.Buffer{}
		Bufferlength = func() int { return int(buffer.Len()) }
		tempWriter = nopCloser{buffer, buffer}
	}

	if work.ReturnEnv {
		Hyades.ZipCompressFolderWriter(TempJobFolder, tempWriter)
	}

	if tempFile != nil {
		tempFile.Seek(0, 0)
	}

	res.SetEnv(tempWriter)
	res.EnvLength = Bufferlength()
	res.StdOutStream = stdBuf.Bytes()
	res.ErrOutStream = errBuf.Bytes()
	log.Println("Did work:", res)

}