Finished log web endpoints

This commit is contained in:
Tim Shannon 2016-04-07 14:49:54 +00:00
parent b9c945ed92
commit e013f2bff4
8 changed files with 257 additions and 27 deletions

View File

@ -77,6 +77,10 @@ func (p *Project) load() {
func (p *Project) fetch() { func (p *Project) fetch() {
p.setStage(stageFetch) p.setStage(stageFetch)
if p.Fetch == "" {
return
}
tempDir := filepath.Join(p.dir(), strconv.FormatInt(time.Now().Unix(), 10)) tempDir := filepath.Join(p.dir(), strconv.FormatInt(time.Now().Unix(), 10))
if p.errHandled(os.MkdirAll(tempDir, 0777)) { if p.errHandled(os.MkdirAll(tempDir, 0777)) {
@ -137,6 +141,10 @@ func (p *Project) fetch() {
func (p *Project) build() { func (p *Project) build() {
p.setStage(stageBuild) p.setStage(stageBuild)
if p.Build == "" {
return
}
output, err := runCmd(p.Build, p.workingDir()) output, err := runCmd(p.Build, p.workingDir())
if p.errHandled(err) { if p.errHandled(err) {
@ -154,6 +162,10 @@ func (p *Project) build() {
// test runs the test scripts // test runs the test scripts
func (p *Project) test() { func (p *Project) test() {
p.setStage(stageTest) p.setStage(stageTest)
if p.Test == "" {
return
}
output, err := runCmd(p.Test, p.workingDir()) output, err := runCmd(p.Test, p.workingDir())
if p.errHandled(err) { if p.errHandled(err) {
@ -172,6 +184,10 @@ func (p *Project) test() {
func (p *Project) release() { func (p *Project) release() {
p.setStage(stageRelease) p.setStage(stageRelease)
if p.Release == "" {
return
}
output, err := runCmd(p.Release, p.workingDir()) output, err := runCmd(p.Release, p.workingDir())
if p.errHandled(err) { if p.errHandled(err) {
@ -193,7 +209,7 @@ func (p *Project) release() {
return return
} }
if p.errHandled(p.ds.AddRelease(p.version, buff)) { if p.errHandled(p.ds.AddRelease(p.version, p.ReleaseFile, buff)) {
return return
} }

View File

@ -15,8 +15,10 @@ import (
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
) )
//TODO: Move this all over to GobStore if I ever get around to finishing it
// ErrNotFound is the error returned when a value cannot be found in the store for the given key // ErrNotFound is the error returned when a value cannot be found in the store for the given key
var ErrNotFound = errors.New("Value not found") var ErrNotFound = errors.New("Value not found in datastore")
// Store is a datastore for getting and setting data for a given ironsmith project // Store is a datastore for getting and setting data for a given ironsmith project
// run on top of a Bolt DB file // run on top of a Bolt DB file

View File

@ -58,7 +58,7 @@ func (ds *Store) LastVersion(stage string) (string, error) {
} }
} }
return ErrNotFound return nil // not found return blank
}) })
if err != nil { if err != nil {
@ -102,3 +102,79 @@ func (ds *Store) Versions() ([]*Log, error) {
return vers, nil return vers, nil
} }
// VersionLog returns all the log entries for a given version
func (ds *Store) VersionLog(version string) ([]*Log, error) {
var logs []*Log
if version == "" {
return logs, nil
}
verFound := false
err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketLog)).Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
l := &Log{}
err := json.Unmarshal(v, l)
if err != nil {
return err
}
if verFound && l.Version != version {
return nil
}
if l.Version == version {
logs = append(logs, l)
verFound = true
}
}
return nil
})
if err != nil {
return nil, err
}
return logs, nil
}
// StageLog returns the log entry for a given version + stage
func (ds *Store) StageLog(version, stage string) (*Log, error) {
var entry *Log
if version == "" || stage == "" {
return nil, ErrNotFound
}
err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketLog)).Cursor()
for k, v := c.Last(); k != nil; k, v = c.Prev() {
l := &Log{}
err := json.Unmarshal(v, l)
if err != nil {
return err
}
if l.Version == version && l.Stage == stage {
entry = l
return nil
}
}
return ErrNotFound
})
if err != nil {
return nil, err
}
return entry, nil
}

View File

@ -12,9 +12,10 @@ import (
) )
type release struct { type release struct {
When time.Time `json:"when"` When time.Time `json:"when"`
Version string `json:"version"` Version string `json:"version"`
FileKey TimeKey `json:"file"` FileName string `json:"fileName"`
FileKey TimeKey `json:"fileKey"`
} }
const ( const (
@ -23,14 +24,14 @@ const (
) )
// AddRelease adds a new Release // AddRelease adds a new Release
func (ds *Store) AddRelease(version string, fileData []byte) error { func (ds *Store) AddRelease(version, fileName string, fileData []byte) error {
key := NewTimeKey()
fileKey := NewTimeKey() fileKey := NewTimeKey()
r := &release{ r := &release{
When: key.Time(), When: fileKey.Time(),
Version: version, Version: version,
FileKey: fileKey, FileName: fileName,
FileKey: fileKey,
} }
dsValue, err := json.Marshal(r) dsValue, err := json.Marshal(r)
@ -39,7 +40,7 @@ func (ds *Store) AddRelease(version string, fileData []byte) error {
} }
return ds.bolt.Update(func(tx *bolt.Tx) error { return ds.bolt.Update(func(tx *bolt.Tx) error {
err = tx.Bucket([]byte(bucketReleases)).Put(key.Bytes(), dsValue) err = tx.Bucket([]byte(bucketReleases)).Put([]byte(version), dsValue)
if err != nil { if err != nil {
return err return err
} }
@ -47,3 +48,42 @@ func (ds *Store) AddRelease(version string, fileData []byte) error {
return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData) return tx.Bucket([]byte(bucketFiles)).Put(fileKey.Bytes(), fileData)
}) })
} }
func (ds *Store) Release(version string) {
}
// Releases lists all the releases in a given project
func (ds *Store) Releases() ([]*Log, error) {
var vers []*Log
err := ds.bolt.View(func(tx *bolt.Tx) error {
c := tx.Bucket([]byte(bucketLog)).Cursor()
var current = ""
for k, v := c.Last(); k != nil; k, v = c.Prev() {
l := &Log{}
err := json.Unmarshal(v, l)
if err != nil {
return err
}
// capture the newest entry for each version
if l.Version != current {
l.Log = "" // only care about date, ver and stage
vers = append(vers, l)
current = l.Version
}
}
return nil
})
if err != nil {
return nil, err
}
return vers, nil
}

View File

@ -41,6 +41,7 @@ func errHandled(err error, w http.ResponseWriter) bool {
errMsg = fmt.Sprintf("We had trouble parsing your input, please check your input and try again: %s", err) errMsg = fmt.Sprintf("We had trouble parsing your input, please check your input and try again: %s", err)
default: default:
status = statusError status = statusError
log.Printf("An error has occurred from a web request: %s", errMsg)
errMsg = "An internal server error has occurred" errMsg = "An internal server error has occurred"
} }

View File

@ -214,6 +214,20 @@ func (p *Project) versions() ([]*datastore.Log, error) {
return p.ds.Versions() return p.ds.Versions()
} }
func (p *Project) versionLog(version string) ([]*datastore.Log, error) {
p.RLock()
defer p.RUnlock()
return p.ds.VersionLog(version)
}
func (p *Project) stageLog(version, stage string) (*datastore.Log, error) {
p.RLock()
defer p.RUnlock()
return p.ds.StageLog(version, stage)
}
func (p *Project) setData(new *Project) { func (p *Project) setData(new *Project) {
p.Lock() p.Lock()
defer p.Unlock() defer p.Unlock()

View File

@ -77,14 +77,23 @@ func (m *methodHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
/* /*
Routes log Routes
/project/<project-id>/<version>/<stage> /log/<project-id>/<version>/<stage>
/project/ - list all projects /log/ - list all projects
/project/<project-id> - list all versions in a project, triggers new builds /log/<project-id> - list all versions in a project, triggers new builds
/project/<project-id>/<version> - list combined output of all stages for a given version /log/<project-id>/<version> - list combined output of all stages for a given version
/project/<project-id>/<version>/<stage. - list output of a given stage of a given version /log/<project-id>/<version>/<stage> - list output of a given stage of a given version
release routes
/release/<project-id>/<version>
/release/<project-id> - list last release for a given project ?all returns all the releases for a project
/release/<project-id>/<version> - list release for a given project version
trigger routes
/trigger/<project-id>
Triggers a project to start a cycle
*/ */
func routes() { func routes() {
@ -94,8 +103,8 @@ func routes() {
get: rootGet, get: rootGet,
}) })
webRoot.Handle("/project/", &methodHandler{ webRoot.Handle("/log/", &methodHandler{
get: projectGet, get: logGet,
}) })
} }

View File

@ -32,14 +32,17 @@ func splitPath(path string) (project, version, stage string) {
return return
} }
// /project/* /*
func projectGet(w http.ResponseWriter, r *http.Request) { /log/ - list all projects
prj, ver, _ := splitPath(r.URL.Path) /log/<project-id> - list all versions in a project, triggers new builds
/log/<project-id>/<version> - list combined output of all stages for a given version
//values := r.URL.Query() /log/<project-id>/<version>/<stage> - list output of a given stage of a given version
*/
func logGet(w http.ResponseWriter, r *http.Request) {
prj, ver, stg := splitPath(r.URL.Path)
if prj == "" { if prj == "" {
//get all projects ///log/ - list all projects
pList, err := projects.webList() pList, err := projects.webList()
if errHandled(err, w) { if errHandled(err, w) {
return return
@ -62,7 +65,8 @@ func projectGet(w http.ResponseWriter, r *http.Request) {
//project found //project found
if ver == "" { if ver == "" {
//list versions ///log/<project-id> - list all versions in a project, triggers new builds
vers, err := project.versions() vers, err := project.versions()
if errHandled(err, w) { if errHandled(err, w) {
return return
@ -74,4 +78,72 @@ func projectGet(w http.ResponseWriter, r *http.Request) {
return return
} }
//ver found
if stg == "" {
///log/<project-id>/<version> - list combined output of all stages for a given version
logs, err := project.versionLog(ver)
if errHandled(err, w) {
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: logs,
})
return
}
//stage found
///log/<project-id>/<version>/<stage> - list output of a given stage of a given version
log, err := project.stageLog(ver, stg)
if errHandled(err, w) {
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: log,
})
return
}
/*
/release/<project-id>/<version>
/release/<project-id> - list last release for a given project ?all returns all the releases for a project
/release/<project-id>/<version> - list release for a given project version
*/
func releaseGet(w http.ResponseWriter, r *http.Request) {
prj, ver, stg := splitPath(r.URL.Path)
values := r.URL.Query()
if prj == "" {
four04(w, r)
return
}
project, ok := projects.get(prj)
if !ok {
four04(w, r)
return
}
//project found
if ver == "" {
///release/<project-id> - list last release for a given project ?all returns all the releases for a project
vers, err := project.versions()
if errHandled(err, w) {
return
}
respondJsend(w, &JSend{
Status: statusSuccess,
Data: vers,
})
return
}
//ver found
} }