Exemplo n.º 1
0
Arquivo: run.go Projeto: ranjib/Gypsy
// REST: /pipelines/{pipeline_name}/runs/{run_id}
func (s *HttpServer) ShowRun(resp http.ResponseWriter, req *http.Request) {
	p := mux.Vars(req)["pipeline_name"]
	r := mux.Vars(req)["run_id"]
	i, err := strconv.Atoi(r)
	if err != nil {
		log.Warnf("Failed to convert run id %s for pipeline '%s'. Error: %v", r, p, err)
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}
	var run []byte
	err1 := s.db.View(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("runs"))
		runBucket := b.Bucket([]byte(p))
		run = runBucket.Get(util.Itob(uint64(i)))
		return nil
	})
	if err1 != nil {
		log.Errorf("Failed to list runs: %v", err1)
		http.Error(resp, err1.Error(), http.StatusInternalServerError)
		return
	}
	if run == nil {
		log.Warnf("No run found")
		http.Error(resp, "Not present", http.StatusNotFound)
		return
	}
	resp.Header().Set("Content-Type", "application/json")
	resp.Write(run)
}
Exemplo n.º 2
0
Arquivo: run.go Projeto: ranjib/Gypsy
// REST: /pipelines/{pipeline_name}/runs/{run_id}
func (s *HttpServer) DeleteRun(resp http.ResponseWriter, req *http.Request) {
	p := mux.Vars(req)["pipeline_name"]
	r := mux.Vars(req)["run_id"]
	i, err1 := strconv.Atoi(r)
	if err1 != nil {
		log.Warnf("Failed to convert run id %s for pipeline '%s'. Error: %v", r, p, err1)
		http.Error(resp, err1.Error(), http.StatusBadRequest)
		return
	}
	err := s.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("runs"))
		if b == nil {
			log.Errorf("Runs bucket not found")
			return fmt.Errorf("Runs bucket not found")
		}
		pipeline := b.Bucket([]byte(p))
		if pipeline == nil {
			log.Errorf("Run bucket for pipeline %s was not found", p)
			return fmt.Errorf("Run bucket for pipeline %s was not found", p)
		}
		log.Printf("Deleting run '%s' for pipeline: %s", r, p)
		return pipeline.Delete(util.Itob(uint64(i)))
	})
	if err != nil {
		log.Warnf("Failed to delete artifact '%s' for pipeline '%s'. Error: %v", r, p, err)
		http.Error(resp, err.Error(), http.StatusInternalServerError)
	}
}
Exemplo n.º 3
0
// REST: /pipelines/{pipeline_name}/runs/{run_id}/artifacts
func (s *HttpServer) ListArtifacts(resp http.ResponseWriter, req *http.Request) {
	artifacts := []string{}
	p := mux.Vars(req)["pipeline_name"]
	r := mux.Vars(req)["run_id"]
	i, e := strconv.Atoi(r)
	if e != nil {
		log.Warnf("Failed to convert run id %s for pipeline '%s'. Error: %v", r, p, e)
		http.Error(resp, e.Error(), http.StatusBadRequest)
		return
	}
	err := s.db.View(func(tx *bolt.Tx) error {
		a := tx.Bucket([]byte("artifacts"))
		if a == nil {
			log.Errorf("Artifact bucket not found")
			return fmt.Errorf("Artifact bucket not found")
		}
		b := a.Bucket([]byte(p))
		if b == nil {
			log.Errorf("Artifact bucket for pipeline %s not found", p)
			return fmt.Errorf("Artifact bucket for pipeline %s not found", p)
		}
		runBucket := b.Bucket(util.Itob(uint64(i)))
		if runBucket == nil {
			log.Errorf("Sub0bucket for pipeline %s's run id %d  not found", p, i)
			return fmt.Errorf("Sub0bucket for pipeline %s's run id %d  not found", p, i)
		}
		c := runBucket.Cursor()
		for k, _ := c.First(); k != nil; k, _ = c.Next() {
			artifacts = append(artifacts, string(k[:]))
		}
		log.Printf("List of artifacts %v", artifacts)
		return nil
	})
	if err != nil {
		log.Errorf("Failed to list pipelines: %v", err)
		http.Error(resp, err.Error(), http.StatusInternalServerError)
		return
	}
	js, err := json.Marshal(artifacts)
	if err != nil {
		log.Errorf("Failed to marshal json: %v", err)
		http.Error(resp, err.Error(), http.StatusInternalServerError)
		return
	}
	resp.Header().Set("Content-Type", "application/json")
	resp.Write(js)
}
Exemplo n.º 4
0
func (s *HttpServer) artifactBucket(tx *bolt.Tx, pipeline string, runId int) (*bolt.Bucket, error) {
	root := tx.Bucket([]byte("artifacts"))
	if root == nil {
		log.Errorf("Artifacts bucket not found")
		return nil, fmt.Errorf("Artifacts bucket not found")
	}
	pipelineBucket := root.Bucket([]byte(pipeline))
	if pipelineBucket == nil {
		log.Errorf("Bucket not presesent artifacts/%s", pipeline)
		return nil, fmt.Errorf("Bucket not presesent artifacts/%s", pipeline)
	}
	runBucket := pipelineBucket.Bucket(util.Itob(uint64(runId)))
	if runBucket == nil {
		log.Errorf("Bucket not presesent artifacts/%s/%d", pipeline, runId)
		return nil, fmt.Errorf("Bucket not presesent artifacts/%s/%d", pipeline, runId)
	}
	return runBucket, nil
}
Exemplo n.º 5
0
Arquivo: run.go Projeto: ranjib/Gypsy
// REST: /pipelines/{pipeline_name}/runs/{run_id}
func (s *HttpServer) UpdateRun(resp http.ResponseWriter, req *http.Request) {
	p := mux.Vars(req)["pipeline_name"]
	r := mux.Vars(req)["run_id"]
	i, err := strconv.Atoi(r)
	if err != nil {
		log.Warnf("Failed to convert run id %s for pipeline '%s'. Error: %v", r, p, err)
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}
	body, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Warnf("Failed to read request body : %v", err)
		http.Error(resp, err.Error(), http.StatusInternalServerError)
		return
	}
	log.Println(string(body[:]))
	var run structs.Run
	if err := json.Unmarshal(body, &run); err != nil {
		log.Warnf("Failed to unmarshal request : %v", err)
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}
	log.Infof("Run data validation succeeded, saving data")
	err1 := s.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("runs"))
		log.Infof("Bucket '%s' will be created if not exist", p)
		runBucket, e := b.CreateBucketIfNotExists([]byte(p))
		if e != nil {
			log.Errorln("Failed to create sub bucket")
			return e
		}
		return runBucket.Put(util.Itob(uint64(i)), body)
	})
	if err1 != nil {
		log.Warnf("Failed to store run details: %v", err1)
		http.Error(resp, err1.Error(), http.StatusInternalServerError)
		return
	}
}
Exemplo n.º 6
0
// REST: 	/pipelines/{pipeline_name}/runs/{run_id}/artifacts/{artifact_name}
func (s *HttpServer) UploadArtifact(resp http.ResponseWriter, req *http.Request) {
	req.ParseMultipartForm(32 << 20)
	p := mux.Vars(req)["pipeline_name"]
	r := mux.Vars(req)["run_id"]
	a := mux.Vars(req)["artifact_name"]
	i, err := strconv.Atoi(r)
	if err != nil {
		log.Warnf("Failed to convert run id %s for pipeline '%s'. Error: %v", r, p, err)
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}

	file, handler, err := req.FormFile("artifact")
	if err != nil {
		log.Warnf("Failed in form file invocation '%s'. Error: %v", err)
		http.Error(resp, err.Error(), http.StatusBadRequest)
		return
	}
	defer file.Close()
	fmt.Fprintf(resp, "%v", handler.Header)
	artifactPath := filepath.Join(s.artifactLocation, p, r, a)
	if e := os.MkdirAll(filepath.Dir(artifactPath), 0775); e != nil {
		log.Errorf("Failed to create artifact directory %s. Error: %v", filepath.Dir(artifactPath), e)
		http.Error(resp, e.Error(), http.StatusInternalServerError)
		return
	}
	fw, e1 := os.OpenFile(artifactPath, os.O_WRONLY|os.O_CREATE, 0666)
	if e1 != nil {
		log.Warnf("Failed in create file '%s'. Error: %v", artifactPath, e1)
		http.Error(resp, e1.Error(), http.StatusInternalServerError)
		return
	}
	defer fw.Close()
	_, e := io.Copy(fw, file)
	if e != nil {
		log.Warnf("Failed in copy file '%s'. Error: %v", artifactPath, e)
		http.Error(resp, e.Error(), http.StatusInternalServerError)
		return
	}

	err1 := s.db.Update(func(tx *bolt.Tx) error {
		b := tx.Bucket([]byte("artifacts"))
		if b == nil {
			log.Errorf("Artifacts bucket not found")
			return fmt.Errorf("Artifacts bucket not found")
		}
		pipeline, err := b.CreateBucketIfNotExists([]byte(p))
		if err != nil {
			log.Errorf("Failed to create sub-bucket for pipeline %s under artifacts bucket. Error:%v", p, err)
			return err
		}
		runBucket, err := pipeline.CreateBucketIfNotExists(util.Itob(uint64(i)))
		if err != nil {
			log.Errorf("Failed to create sub-bucket for pipeline %s under artifacts bucket. Error:%v", p, err)
			return err
		}
		log.Printf("Saving artifact '%s' for pipeline: %s run id %d", a, p, i)
		return runBucket.Put([]byte(a), []byte(artifactPath))
	})
	if err1 != nil {
		log.Warnf("Failed to save artifact: %v", err1)
		http.Error(resp, err.Error(), http.StatusInternalServerError)
		return
	}
}